Commit graph

195 commits

Author SHA1 Message Date
John Johansen
e2d55844a2 parser: fix integer overflow bug in rule priority comparisons
There is an integer overflow when comparing priorities when cmp is
used because it uses subtraction to find lessthan, equal, and greater
than in one operation.

But INT_MAX and INT_MIN are being used by priorities and this results
in INT_MAX - INT_MIN and INT_MIN - INT_MAX which are both overflows
causing an incorrect comparison result and selection of the wrong
rule permission.

Closes: https://gitlab.com/apparmor/apparmor/-/issues/452
Fixes: e3fca60d1 ("parser: add the ability to specify a priority prefix to rules")
Signed-off-by: John Johansen <john.johansen@canonical.com>
2024-10-28 04:03:53 -07:00
Ryan Lee
776c56bd7e Fix compiler warnings about format specifiers with DEBUG set
As for %s when the pointer is null: even if gcc prints (null) this is still undefined behavior, so we should do this explicitly

Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
2024-10-16 12:26:54 -07:00
John Johansen
e3fca60d11 parser: add the ability to specify a priority prefix to rules
This enables adding a priority to a rules in policy, finishing out the
priority work done to plumb priority support through the internals in
the previous patch.

Rules have a default priority of 0. The priority prefix can be added
before the other currently support rule prefixes, ie.

  [priority prefix][audit qualifier][rule mode][owner]

If present a numerical priority can be assigned to the rule, where the
greater the number the higher the priority. Eg.

    priority=1 audit file r /etc/passwd,

    priority=-1 deny file w /etc/**,

Rule priority allows the rule with the highest priority to completely
override lower priority rules where they overlap. Within a given
priority level rules will accumulate in standard apparmor fashion.

    Eg. given
        priority=1 w   /*c,
        priority=0 r   /a*,
        priority=-1 k  /*b*,

    /abc, /bc, /ac   .. will have permissions of w
    /ab, /abb, /aaa, .. will have permissions of r
    /b, /bcb, /bab,  .. will have permissions of k

User specified rule priorities are currently capped at the arbitrary
values of 1000, and -1000.

Notes:
* not all rule types support the priority prefix. Rukes like
  - network
  - capability
  - rlimits need to be reworked
  need to be reworked to properly preserve the policy rule structure.
* this patch does not support priority on rule blocks
* this patch does not support using a variable in the priority value.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2024-08-14 17:15:24 -07:00
John Johansen
cd95d46397 parser: split parse boolean into boolean and integer
the parser front end boolean is used for both boolean and integer
values. This is confusing when integer values different than 1 or 0
are being assigned to and from boolean.

Split its uses into the correct semantic boolean and integer cases.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2024-08-14 17:15:24 -07:00
John Johansen
22e1863e20 Merge parser: add support for prompt rules
This adds support for prompt rules and the beginning of support for extended permissions. Currently extended permissions are only used if a prompt rule is used in policy.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1305
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2024-08-14 23:34:29 +00:00
John Johansen
4264338bed convert owner to an enum
provide better type checking and semantics to the owner conditional

Signed-off-by: John Johansen <john.johansen@canonical.com>
2024-08-14 15:47:13 -07:00
John Johansen
2e18cb9aed parser: rename rules.h perms_t to perm32_t
There are two distinct declarations of perms_t.
  rule.h: typedef uint32_t perms_t
  hfa.h: class perms_t

these definitions clash when the front end and backend share more info.
To avoid this rename rule.h to perm32_t, and move the definition into
perms.h and use it in struct aa_perms.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2024-08-14 14:39:18 -07:00
John Johansen
c86f8f06dd parser: add non-functional prompt parsing
Add the ability to parse the prompt qualifier but do not provide
functionality because the backend does not currently support prompt
permissions.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2024-08-14 14:39:18 -07:00
Georgia Garcia
15ee7ac92c parser: refactor conditional logic into its own class
Remove conditional logic from the parser and move it to its own class,
that way any improvements or conditional features will make cleaner
changes.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-08-14 17:22:48 -03:00
Georgia Garcia
ddefe11a40 parser: add fine grained conditionals to network rule
Options available are ip= and port= inside the peer group or outside,
representing local addresses and ports:

network peer=(ip=127.0.0.1 port=8080),
network ip=::1 port=8080 peer=(ip=::2 port=8081),

The 'ip' option supports both IPv4 and IPv6. Examples would be
ip=192.168.0.4, or ip=::578d

The 'port' option accepts a 16-bit unsigned integer. An example would
be port=1234

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-02-29 16:25:59 -03:00
Georgia Garcia
746f76d3e1 parser: add ability to specify permission in network rules
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-02-28 21:42:18 -03:00
John Johansen
197d00d21a parser: add support for a generic all rule type
Extend the policy syntax to have a rule that allows specifying all
permissions for all rule types.

  allow all,

This is useful for making blacklist based policy, but can also be
useful when combined with other rule prefixes, eg. to add audit
to all rules.

  audit access all,

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-09-07 01:30:15 -07:00
Georgia Garcia
2be9c431ca parser: add opt_cond in preparation to finer grained network mediation
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2023-09-07 00:13:15 -07:00
Georgia Garcia
820f1fb5f2 parser: refactor network to use rule class as its base.
There is one significant difference in the encoding of the network
rules. Before this change, when the parser was encoding a "network,"
rule, it would generate an entry for every family and every
type/protocol. After this patch the parser should generate an entry
for every family, but the type/protocol is changed to .. in the pcre
syntax. There should be no difference in behavior.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2023-09-07 00:12:51 -07:00
John Johansen
f10467556c parser: move flag init and merge to flagvals class
We have a basic flagvals class it makes sense to move the parser
code into it, to help make management easier going forward.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-08-25 07:19:01 -07:00
John Johansen
30707be87f parser: add interruptible flag
Allow indicating that prompt upcalls to userspace can be interrupted

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-08-25 07:18:50 -07:00
John Johansen
4a21bd104c parser: make attach_disconnected.path enable attach_disconnected
Currently you need to use attach_disconnected with
attach_disconnected.path=XXX to be able to attach to a different
location than / whic is ugly and redundant.

Make it so attach_disconnected.path implies attach_disconnected.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-08-14 13:28:31 -07:00
John Johansen
b46b2662ff parser: add support for attach_disconnected.path
Add support for specifying the path prefix used when attach disconnected
is specified. The kernel supports prepending a different value than
/ when a path is disconnected. Expose through a profile flag.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-08-14 01:42:28 -07:00
John Johansen
367babf9cb parser: add support for exposing a debug flag to policy
Allowing access to a debug flag can greatly improve policy debugging.
This is different than the debug mode of old, that was removed. It only
will trigger additional messages to the kernel ring buffer, not
the audit log, and it does not change mediation.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-02 03:02:02 -07:00
Georgia Garcia
e5e920d178 parser: add parser support for io_uring mediation
io_uring rules have the following format:

io_uring [<access_mode>] [<label>],
access_mode := 'sqpoll'|'override_creds'
label := 'label' '=' <target label>

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2023-05-03 16:03:52 +02:00
John Johansen
e6e3f44ff9 parser: cleanup: drop unused add_local_entry and associated vars
The code for add_local_entry is actually currently unused and will
have to change anyways by the time it is. Some drop it and the
associated variables.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 02:21:19 -07:00
John Johansen
68421547a1 refactor prefix and x check during parser
Reduce duplicate code and another step towards converting file rules
to rule_t

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 02:21:19 -07:00
John Johansen
9eb23475de parser: refactor rules parser for a common block
Another step towards having a block rule and retaining parsed rule
structure. Setup the parse to use a common block pattern, that when
we are ready will become an actual rule.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 02:21:19 -07:00
John Johansen
355730d8c7 parser: convert deny flag from bool to rule_mode
We need to be able to support more rule types than allow and deny so
convert to an enum.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 02:21:11 -07:00
John Johansen
f76d134b6c parser: convert subset flag to a bool
Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 02:17:28 -07:00
John Johansen
10a75c431f parser: rename post_process() method and move code around
The post_process() method is misnamed, it fires when the profile is
finished parsing but fires before variable expansion. Rename it
to better reflect what it does and move the trigger code into
profile as a start of cleaning this stage up.

Also document the order the hooks fire in

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 02:17:28 -07:00
John Johansen
28ae20983b parser: further reduce duplication of prefix rule parsing
The previous patch enable the prefix based rules all to use the
same code pattern. Group them together

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 02:17:28 -07:00
John Johansen
d371458533 parser: make base classes for rules using prefixes and perms and use them
Cleanup the parse code by making shared prefix and perms classes for
rules and convert rules to use them.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 02:17:26 -07:00
John Johansen
fdf5b062a9 parser: fixup audit struct to audit enum
This removes the struct wrapper used in the previous patch to ensure
that all uses are properly converted.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 02:12:35 -07:00
John Johansen
7a318d99f2 parser: convert audit from bool to enum
Audit control support is going to be extended to support allowing
policy to which rules should quiet auditing. Update the frontend
internals to prepare for this.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 02:08:20 -07:00
John Johansen
134e95f783 parser: fixup remove struct from the audit bool conversion
This removes the struct wrapper used in the previous patch to ensure
that all uses are properly converted.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 01:08:25 -07:00
John Johansen
44f3be091a parser: convert the stored audit from a bit mask to a bool
This delays the convertion of the audit flag until passing to the
backend. This is a step towards fix the parser front end so that it
doesn't use encoded permission mappings.

Note: the patch embedds the bool conversion into a struct to ensure
the compiler will fail to build unless every use is fixed. The
struct is removed in the following patch.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-31 01:05:29 -07:00
John Johansen
4fd1f97102 parser: rename mount->allow to mount->perms and switch to perm_t
Make mount permission set consistent with the other rule types. This
is a step towards refactoring.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-29 10:52:31 -07:00
John Johansen
fd9a6fe133 parser: int mode to perms
Move from using and int for permissions bit mask to a perms_t type.
Also move any perms mask that uses the name mode to perms to avoid
confusing it with other uses of mode.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-03-29 10:45:44 -07:00
Georgia Garcia
3c033606e4 parser: fix definitely and possibly lost memory leaks
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2023-03-16 18:03:57 -03:00
John Johansen
3cc73ffe8d parser: Add a set of debug flags that can be passed to the kernel
The kernel will allow for a couple of debug flags on a profile that
can be used to trigger debug messages for only profiles/labels that
have the flag set. Add basic support for these to the parser.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2022-11-22 19:31:15 +00:00
Georgia Garcia
d98c5c4cf9 parser: add parser support for message queue mediation
Message queue rules take the following format:

	mqueue [<access_mode>] [<type>] [<label>] [<mqueue name>],
	access_mode := 'r'|'w'|'rw'|'read'|'write'|
		       'create'|'open'|'delete'|
		       'getattr'|'setattr'
	type := 'type' '=' ('posix'|'sysv')
	label := 'label' '=' <target label>

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
Signed-off-by: John Johansen <john.johansen@canonical.com>
2022-11-22 19:31:15 +00:00
Georgia Garcia
f400b41c4a parser: add support for user namespace creation
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2022-10-27 17:54:42 +00:00
John Johansen
8c8da7c67d parser: move ifdefs for capabilities to single common file
Unfortunately the parser was doing ifdef checks for capabilities
in two places. Move all the capability ifdefs into capability.h

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/768
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Georgia Garcia  <georgia.garcia@canonical.com>
2021-07-02 12:48:47 -07:00
John Johansen
7dcf013bca parser: add include dedup cache to handle include loops
Profile includes can be setup to loop and expand in a pathalogical
manner that causes build failures. Fix this by caching which includes
have already been seen in a given profile context.

In addition this can speed up some profile compiles, that end up
re-including common abstractions. By not only deduping the files
being included but skipping the need to reprocess and dedup the
rules within the include.

Fixes: https://bugzilla.suse.com/show_bug.cgi?id=1184779
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/743
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Steve Beattie <steve.beattie@canonical.com>
2021-04-27 20:26:57 -07:00
John Johansen
761008f1b6 parser: fix yyerror message to not duplicate file name output
yyerror is outputting the file name twice when not in a profile or
the profilename global is not defined. Drop the second output of
the file name as it just clutters up the error message.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/610
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Mike Salvatore <mike.salvatore@canonical.com>
2020-09-03 07:19:21 -07:00
John Johansen
f5c4927c85 parser: convert remaining pwarn() to flag controlled warns
Make all warnings that go through pwarn() controllable by warning
flags. This adds several new warning control flags, documented in

  --help=warn

Convert --debug-cache to be unified with warning flags. So it can be
set by either
    --debug-cache
  or
    --warn=debug-cache

Also add an "all" option to be able to turn on all warnings.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/600
Signed-off-by: John Johansen <john.johansen@canonical.com>
2020-09-01 19:42:38 -07:00
John Johansen
c530c880b1 parser: Add warning flag that can toggle deprecation warnings
Add the flag
  --warn=deprecated

to be able to toggle deprecation warnings

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/600
Signed-off-by: John Johansen <john.johansen@canonical.com>
2020-09-01 19:42:38 -07:00
John Johansen
0d0686becc parser: Add warning flag for ABI messages
ABI warnings can be to much for a deployed system. Add

  --warn=abi

to control output of abi warnings

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/600
Signed-off-by: John Johansen <john.johansen@canonical.com>
2020-09-01 19:42:38 -07:00
John Johansen
dcc2918665 parser: add an option to allow overriding feature ABI rules
Add an option to allow setting/pinning the feature ABI and overriding
of ABI rules if they exist.

  --override-policy-abi

This option is primarily for profile development and testing without
allowing adjusting feature abis temporarily without modifying the
profile.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/579
Signed-off-by: John Johansen <john.johansen@canonical.com>
2020-08-20 01:22:03 -07:00
John Johansen
acb45dc4b0 parser: fix abi rule and pinned feature file interaction
In AppArmor 2 distros could pin the feature file being used by setting
the feature-file option in the config file.

With AppArmor 3 policy is now explicitly tagged with an abi rule.

The problem is the interaction on systems that have a mixture of
AppArmor 2 and AppArmor 3 policy and use feature pinning.

The feature pinning is required to make the apparmor 2 policy behave
as expected but it also overrides the abi rules that are explicitly
set as part of the policy. This means we either have the apparmor 2
pinned policy working as desired or the apparmor 3 policy, but not
both.

To fix this make setting the flag on command line or in config file
lower priority than an abi rule specified in policy. The ability
to override abi rules will be added in a separate patch.

The Priority ordering to determine the policy abi to use is
1. Use abi rules if present
2. if no abi rule use command line option
3. if no abi rule or command line option use config setting
4. if none of the above use the default abi

PR: https://gitlab.com/apparmor/apparmor/-/merge_requests/579
Signed-off-by: John Johansen <john.johansen@canonical.com>
2020-08-20 01:22:03 -07:00
John Johansen
c810c755b1 parser: allow backwards mapping of a capability
If a capability is known in policy but not by the kernel, check to see if it has
a backwards mapping to a different capability and use that instead.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2020-07-07 09:43:48 -07:00
John Johansen
3880ef5b54 parser: Use the kernel and policy abis to detect new capabilities
The kernel and policy abis can be used to detect and support new
capabilities without having to update base_cap_names.h and and
rebuilding the compiler.

This is not perfect however in that the does not provide any backwards
compatibility mappings, so we still need to keep the internal
capability table.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2020-07-07 09:43:48 -07:00
John Johansen
0c9884550c parser: support enforce, kill and unconfined profile modes
The enforce profile mode is the default but specifying it explicitly
has not been supported. Allow enforce to be specified as a mode. If
no mode is specified the default is still enforce.

The kernel has supported kill and unconfined profile modes for a
long time now. And support to the parser so that profiles can make
use of these modes.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/440
Fixes: https://gitlab.com/apparmor/apparmor/-/issues/7
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Steve Beattie <sbeattie@ubuntu.com>
2020-06-10 05:35:37 -07:00
John Johansen
162da1ba48 parser: add basic support for feature abis
Add basic support for policy to specify a feature abi. Under the
current implementation the first feature abi specified will be
used as the policy abi for the entire profile.

If no feature abi is defined before rules are processed then the
default policy abi will be used.

If multiple feature abi rules are encountered and the specified
abi is different then a warning will be issued, and the initial abi
will continue to be used. The ability to support multiple policy
feature abis during a compile will be added in a future patch.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/491
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Steve Beattie <sbeattie@ubuntu.com>
2020-05-29 00:23:37 -07:00