Commit graph

1541 commits

Author SHA1 Message Date
John Johansen
e5dace9ffd parser: add support for prompt profile mode
Add support for the prompt profile mode.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-30 20:50:03 -07:00
Georgia Garcia
3cd2baccdd Merge parser: rework perms rule merging
Instead of pushing the cmp logic for rule merging into each rule
class make it the default behavior for the perms_rule_t parent class.
Also save off the original perms for the merged rule.

For classes that don't want perms merging add an alternate
dedup_perms_rule_t clase.

Signed-off-by: John Johansen <john.johansen@canonical.com>

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1069
Approved-by: Georgia Garcia <georgia.garcia@canonical.com>
Merged-by: Georgia Garcia <georgia.garcia@canonical.com>
2023-07-11 12:10:23 +00:00
John Johansen
5e8567c9e9 parser: rework perms rule merging
Instead of pushing the cmp logic for rule merging into each rule
class make it the default behavior for the perms_rule_t parent class.
Also save off the original perms for the merged rule.

For classes that don't want perms merging add an alternate
dedup_perms_rule_t clase.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-10 20:04:53 -07:00
John Johansen
74b101faa8 Merge parser: improve dfa generation
speedup and reduce memory usage of dfa generation

A variety of changes to improve dfa generation
- By switching to Nodevec instead of Node sets we can reduce memory usage slightly and reduce code
- By using charsets for chars we reduce code and increase chances of node merging/reduction which reduces memory usage slightly
- By merging charsets we reduce the number of nodes

Signed-off-by: John Johansen <john.johansen@canonical.com>

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1066
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2023-07-11 02:26:10 +00:00
Georgia Garcia
cdb5e501d6 parser: add permission merging
By changing the compare function from each rule to use class_rule_t,
instead of perms_rule_t, we temporarily ignore if permissions are
different. If every rule attribute is the same, then the permissions
can be merged. This is done at the perms_rule_t's level.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2023-07-10 18:01:32 -03:00
John Johansen
1279f85e4a Merge parser: Improve rule merging/dedup
Currently File rules are the only rules that have rule dedup/merging performed. Extend support for rule merging to all other rule types.

This can result in a small performance regression when rules can not be merged/deduped but can result in a large performance increase when lots of rules can be eliminated.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1065
Approved-by: Georgia Garcia <georgia.garcia@canonical.com>
Merged-by: John Johansen <john@jjmx.net>
2023-07-10 19:13:25 +00:00
John Johansen
501e87a3f2 parser: Cleanup parser control flags, so they display as expected to user
Instead of having multiple tables, since we have room post split
of optimization and dump flags just move all the optimization and
dump flags into a common table.

We can if needed switch the flag entry size to a long in the future.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-08 19:58:59 -07:00
John Johansen
1754b4da69 parser: add flags to control rule merging
Add the ability to control whether rule merging is done.

TODO: in the furture cleanup display of flags split accross two tables

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-08 01:06:50 -07:00
John Johansen
e84e481263 parser: cleanup and rework optimization and dump flag handling
In preparation for more flags (not all of the backend dfa based),
rework the optimization and dump flag handling which has been exclusively
around the dfa up to this point.

- split dfa control and dump flags into separate fields. This gives more
  room for new flags in the existing DFA set
- rename DFA_DUMP, and DFA_CONTROL to CONTROL_DFA and DUMP_DFA as
  this will provide more uniform naming for none dfa flags
- group dump and control flags into a structure so they can be passed
  together.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-07 17:47:41 -07:00
John Johansen
c5f2fcbb95 parser: add rule merging for dbus rules
Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-07 17:47:36 -07:00
John Johansen
5e713276ca parser: add rule merging for af_unix rules
this is reuired because af_rule merging does not take into account
the potential af_unix addresses and could incorrectly merge af_unix
rules.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-07 17:46:54 -07:00
John Johansen
00553a6dd5 parser: add rule mergeing for af_rules
Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-07 17:46:19 -07:00
John Johansen
08a0970d1f parser: add io_uring rule merging
Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-07 17:45:38 -07:00
John Johansen
ca976bf5cb parser: add rule merging for mqueue rules
Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-07 17:41:10 -07:00
John Johansen
0f660828e1 parser: add rule merging for ptrace rules
Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-07 17:40:20 -07:00
John Johansen
fd20c226e0 parser: add rule merging for userns rules
Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-07 17:39:50 -07:00
John Johansen
53b99a82f6 parser: add rule merging for signals
Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-07 17:39:33 -07:00
John Johansen
3ede2c46cf parser: add rule dedup of mount rules
Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-07 17:38:47 -07:00
John Johansen
7d9958890f parser: finish basic infrastructure for rule merging
Currently only file rules get merged. Finish adding basic support
for rule merging and make the default the behavior to dedup
merge rules that are exact matches.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-07 17:35:37 -07:00
John Johansen
dbca8ebb89 parser: Improve the rule skip test.
Rules can be marked as being deleted/merged, and should be skipped on
further processing.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-05 04:40:27 -07:00
John Johansen
f6b179010e Merge 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>

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1060
Approved-by: Georgia Garcia <georgia.garcia@canonical.com>
Merged-by: John Johansen <john@jjmx.net>
2023-07-04 06:58:16 +00:00
John Johansen
24f834b128 parser: allow character sets to be used as exact match permissions
character sets are just a way of enumerating to exact match rules
more succinctly, so loosen the exact match check to allow them.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-03 00:53:26 -07:00
John Johansen
9e9ca7e55d Drop code that is now unused because of the conversion to NodeVec
Drop NodeCache and hashedNodes as they are no longer used.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-03 00:53:26 -07:00
John Johansen
61c20a0ae8 convert anodes to using NodeVecs instead of NodeSets
Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-03 00:53:26 -07:00
John Johansen
53d00b4d2b rename hashedNodeVec to NodeVec
Shorten the name length by dropping the leading "hashed".

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-07-03 00:49:56 -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
John Johansen
460c3d5b59 policy: pin policy to 4.0 abi for dev
TO BE REVERTED: this is a dev patch to help make sure policy is getting
updated.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-06-30 23:43:40 -07:00
John Johansen
ab218526bf Merge parser: Remove check for root to run parser
The check isn't correct, it should be checking for capability
MAC_ADMIN, but in the future that won't be correct either. Instead
rely on the kernel to check permission to load policy, which it alread
does as it is possible to by-pass the parser to load policy.

Also improve the error message when the kernel does deny
loading policy due to failed permission checks.

Signed-off-by: John Johansen <john.johansen@canonical.com>

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1044
Approved-by: Seth Arnold <seth.arnold@gmail.com>
Merged-by: John Johansen <john@jjmx.net>
2023-06-30 07:02:34 +00:00
John Johansen
e7844e723e parser: Remove check for root to run parser
The check isn't correct, it should be checking for capability
MAC_ADMIN, but in the future that won't be correct either. Instead
rely on the kernel to check permission to load policy, which it alread
does as it is possible to by-pass the parser to load policy.

Also improve the error message when the kernel does deny
loading policy due to failed permission checks.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-06-29 14:42:02 -07:00
John Johansen
fef3eb3693 Merge add userspace 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>
```

You can use the following kernel tree with the io_uring mediation patch to test this feature https://gitlab.com/georgiag/apparmor-kernel/-/commits/io_uring

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/993
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2023-06-29 21:38:02 +00:00
John Johansen
d4b0fef10a parser: fix rule flag generation change_mount type rules
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1048
made it so rules like

  mount slave /snap/bin/** -> /**,

  mount /snap/bin/** -> /**,

would get passed into change_mount_type rule generation when they
shouldn't have been. This would result in two different errors.

1. If kernel mount flags were present on the rule. The error would
   be caught causing an error to be returned, causing profile compilation
   to fail.

2. If the rule did not contain explicit flags then rule would generate
   change_mount_type permissions based on souly the mount point. And
   the implied set of flags. However this is incorrect as it should
   not generate change_mount permissions for this type of rule. Not
   only does it ignore the source/device type condition but it
   generates permissions that were never intended.

   When used in combination with a deny prefix this overly broad
   rule can result in almost all mount rules being denied, as the
   denial takes priority over the allow mount rules.

Fixes: https://bugs.launchpad.net/apparmor/+bug/2023814
Fixes: https://bugzilla.opensuse.org/show_bug.cgi?id=1211989
Fixes: 9d3f8c6cc ("parser: fix parsing of source as mount point for propagation type flags")
Fixes: MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1048
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1054

Signed-off-by: John Johansen <john.johansen@canonical.com>
(cherry picked from commit 86d193e183)
Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-06-21 01:18:14 -07:00
Georgia Garcia
9d3f8c6cc0 parser: fix parsing of source as mount point for propagation type flags
Before 300889c3a, mount rules would compile policy when using source
as mount point for rules that contain propagation type flags, such as
unbindable, runbindable, private, rprivate, slave, rslave, shared, and
rshared. Even though it compiled, the rule generated would not work as
expected.

This commit fixes both issues. It allows the usage of source as mount
point for the specified flags, albeit with a deprecation warning, and
it correctly generates the mount rule.

The policy fails to load when both source and mount point are
specified, keeping the original behavior (reference
parser/tst/simple_tests/mount/bad_opt_10.sd for example).

Fixes: https://bugs.launchpad.net/bugs/1648245
Fixes: https://bugs.launchpad.net/bugs/2023025

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2023-06-07 20:16:50 -03:00
John Johansen
8d6358fa6d Merge Fix use-after-free of 'name' in parser_regex.c
'name' gets used in the error message. Make sure it only gets freed
afterwards.

This bug was introduced in be0d2fa947 /
https://gitlab.com/apparmor/apparmor/-/merge_requests/727

Fixes coverity CID 254465:  Memory - illegal accesses  (USE_AFTER_FREE)

I propose this fix for 3.0..master.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1040
Approved-by: Georgia Garcia <georgia.garcia@canonical.com>
Merged-by: John Johansen <john@jjmx.net>
2023-05-29 22:38:17 +00:00
John Johansen
dc8cbebdef Merge Fix order of if conditions to avoid unreachable code
If `else if (preprocess_only)` is true, the more strict condition
`else if (!include_file && preprocess_only)` won't be reached if it gets
checked after the shorter condition.

Exchange the two sections so that both code paths can be reached.

Fixes coverity CID 312499:  Control flow issues  (DEADCODE)

This was probably introduced in 7dcf013bca / https://gitlab.com/apparmor/apparmor/-/merge_requests/743 which means we'll need to backport this fix to 3.0 and 3.1.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1039
Approved-by: Georgia Garcia <georgia.garcia@canonical.com>
Merged-by: John Johansen <john@jjmx.net>
2023-05-29 22:29:06 +00:00
Christian Boltz
e408d03a5b
Fix use-after-free of 'name' in parser_regex.c
'name' gets used in the error message. Make sure it only gets freed
afterwards.

This bug was introduced in be0d2fa947 /
https://gitlab.com/apparmor/apparmor/-/merge_requests/727

Fixes coverity CID 254465:  Memory - illegal accesses  (USE_AFTER_FREE)
2023-05-29 22:16:09 +02:00
Christian Boltz
eabbc61509
Fix order of if conditions to avoid unreachable code
If `else if (preprocess_only)` is true, the more strict condition
`else if (!include_file && preprocess_only)` won't be reached if it gets
checked after the shorter condition.

Exchange the two sections so that both code paths can be reached.

Fixes coverity CID 312499:  Control flow issues  (DEADCODE)
2023-05-29 21:33:54 +02:00
Christian Boltz
c5c0ecb508
Fix missing uint32_t type declaration in rule.h
... by including cstdint.

Credits go to the new gcc in Tumbleweed for proposing this patch.
2023-05-29 21:24:31 +02:00
Christian Boltz
9a9af71d83
extend test profiles for mount
- in bad_?.sd, explain why the profile is bad (conflicting options)
- add a good profile with two space-separated options

This is a follow-up for https://gitlab.com/apparmor/apparmor/-/merge_requests/1029
2023-05-18 20:05:42 +02:00
Georgia Garcia
50dd41f920 parser: add io_uring simple tests
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2023-05-03 16:03:52 +02: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
89bc617d0d parser: fix conflicting mnt flag values message to have a space
The conflicting flags value message was hard to read
  conflicting flag value = lazytimenolazytime

change it to
  conflicting flag values = lazytime, nolazytime

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-04-27 04:05:44 -07:00
John Johansen
b51602233d docs apparmor.d: add missing mount options to man page
Add the missing options nostrictatime, lazytime, and nolazytime to the
apparmor.d manpage.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-04-27 04:05:44 -07:00
John Johansen
cfb77309d6 parse tests: add parse tests for missing mount options
add simple parsing tests for nostrictatime, lazytime, nolazytime

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-04-27 04:05:44 -07:00
Oliver Calder
231c469d86 parser: added nosymfollow mount option
Adds the corresponding `MS_NOSYMFOLLOW` flag to parser/mount.h as well,
defined as (1 << 8) just as in the util-linux and the kernel.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>
Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-04-27 03:21:26 -07:00
Oliver Calder
257b3cfbf6 Added MS_LAZYTIME to MS_ALL_FLAGS
Signed-off-by: Oliver Calder <oliver.calder@canonical.com>
Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-04-27 03:20:34 -07:00
Oliver Calder
bc64b824fa Issue 312: added missing kernel mount options
This patch adds the following mount options: 'nostrictatime',
'lazytime', and 'nolazytime'.

The MS_STRICTATIME mount flag already existed, and 'nostrictatime' was
listed along with 'strictatime' in the comments of parser/mount.cc, so
this patch adds a mapping for 'nostrictatime' to clear MS_STRICTATIME.

Additionally, the Linux kernel includes the 'lazytime' option with
MS_LAZYTIME mapping to (1<<25), so this patch adds MS_LAZYTIME to
parser/mount.h and the corresponding mappings in parser/mount.cc for
'lazytime' and 'nolazytime'.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>
Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-04-27 03:20:34 -07:00
John Johansen
f8117a384f parser: fix chfa quivalence class handling
The chfa equivalence class shouldn't be a reference. Its needs to
actually exist and be part of the class during later method calls.
As a reference it leads to bad references when used later.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-04-25 05:55:34 -07: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