2
0
mirror of https://gitlab.com/apparmor/apparmor synced 2025-08-28 21:07:56 +00:00

2063 Commits

Author SHA1 Message Date
John Johansen
520227e4ea Merge Remove global declarations when variables are only read
... but don't get a new value assigned.

Found by pyflakes 3.3.2 / python 3.13.3

While on it, remove some obsolete, commented out debugging code.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1708
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2025-06-05 23:17:20 +00:00
Christian Boltz
ba5e007287
Fix parsing of mount options to honor full words
Parsing mount options (also) accepted partial matches as long as the
option started with the right characters. For example, 'options=syncfoo'
was parsed as 'sync'. This is also the reason why the list of mount
options was re-ordered so that 'r' and 'w' came last to give longer
options a chance to match (otherwise, 'rw' would be interpreted as 'r').

Fix parsing by adding a lookahead match so that the regex enforces that
the mount option is followed by whitespace, or is at the end of
rule_details.

Note that this issue only affected the options=foo syntax.
options=(foo) worked correctly even without this fix.

Now that this is fixed, move 'r' and 'w' back to their original position
in the list of mount options.

Also add a test where a mount rule ends with 'options=rw,' to ensure
that the '$' lookahead works.
2025-06-04 22:42:34 +02:00
Georgia Garcia
1009a66e0c Merge Improve error message for unknown mount options
Parsing `mount options=x` results in "Passed unknown options keyword to
MountRule: x", while parsing `mount options=xy` results in "Can't parse mount rule".

This difference happens because the code checks (besides the list of
known options) for a regex `([A-Za-z0-9])` which only matched a
single-character unknown option.

Change that regex to also match multiple characters, and also allow to
match `-` (used in some known mount options, so it's likely that it also
gets used in so far unknown mount options)

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1710
Approved-by: Georgia Garcia <georgia.garcia@canonical.com>
Merged-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-06-04 17:28:14 -03:00
Christian Boltz
9a035cb0ee
Improve error message for unknown mount options
Parsing `mount options=x` results in "Passed unknown options keyword to
MountRule: x", while parsing `mount options=xy` results in "Can't parse mount rule".

This difference happens because the code checks (besides the list of
known options) for a regex `([A-Za-z0-9])` which only matched a
single-character unknown option.

Change that regex to also match multiple characters, and also allow to
match `-` (used in some known mount options, so it's likely that it also
gets used in so far unknown mount options)
2025-06-04 20:47:14 +02:00
Christian Boltz
d223751de0
simplify mount_condition_pattern
The whole pattern already has `(...)*`, therefore there's no need to
make option_pattern optional.

Before this change, mount_condition_pattern could have matched
- on empty strings (it still can, thanks to the trailing `*` which can
  also mean "zero matches") or
- whitespace-only strings (which is covered by the two regexes using
  mount_condition_pattern - they both have `\s*` and/or `\s+` around it)
2025-06-04 19:38:56 +02:00
Christian Boltz
2bfdcb780f
Remove global declarations when variables are only read
... but don't get a new value assigned.

Found by pyflakes 3.3.2 / python 3.13.3

While on it, remove some obsolete, commented out debugging code.
2025-06-04 19:29:16 +02:00
John Johansen
23deb55149 Merge utils: add support for multiple options and fstypes in mount rules
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1693
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2025-06-03 23:43:40 +00:00
Maxime Bélair
e637884554 logprof: improve LOGPROF-SUGGEST metadata for abstractions
Improve #LOGPROF-SUGGEST metadata to support a set of space-separated
regexes. If this tag is present, the abstraction is only proposed to
aa-logprof if one of the regexes is matched.

If this abstraction should not be proposed to any profile, it is
possible to tell #LOGPROF-SUGGEST: no

Signed-off-by: Maxime Bélair <maxime.belair@canonical.com>
2025-05-28 15:47:03 +02:00
Maxime Bélair
b46f7a426c Add support for --show-matching-path and xattrs
The new option --show-matching-path shows a path that matches in the host
filesystem, to prove that the profile is indeed used.

Also, profiles' xattrs are now parsed into a dict and are taken in
consideration when looking for matching profiles.

Signed-off-by: Maxime Bélair <maxime.belair@canonical.com>
2025-05-26 20:08:38 +00:00
Maxime Bélair
db376c0458 Refactor to use cmd everywhere
Many test provide their own implementation of cmd(). This commit makes
all of them rely on common.py implementation of cmd()

Signed-off-by: Maxime Bélair <maxime.belair@canonical.com>
2025-05-26 20:08:38 +00:00
Maxime Bélair
29b4716377 Add a manual entry for aa-show-usage
Signed-off-by: Maxime Bélair <maxime.belair@canonical.com>
2025-05-26 20:08:38 +00:00
Maxime Bélair
229811de9a Add tests for aa-show-usage
Add new tests for aa-show-usage and regex.py, that is internally used by
aa-show-usage

Signed-off-by: Maxime Bélair <maxime.belair@canonical.com>
2025-05-26 20:08:38 +00:00
Maxime Bélair
b850f19622 utils: add aa-show-usage for profile usage analysis
Introduce aa-show-usage, a new helper allowing to determine which
profiles on the system are used and which are not. A profile is marked as
used when at least one file installed in the machine matches the attach point
specified in the profile.

This tool supports filtering options, allowing users to, for example,
display only unconfined profiles that are currently in use. This can
notably help sysadmins to evaluate the security of their systems.

Signed-off-by: Maxime Bélair <maxime.belair@canonical.com>
2025-05-26 20:08:38 +00:00
Georgia Garcia
2e7da63183 utils: add support for multiple fstypes in mount rules
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-05-22 15:51:45 -03:00
Georgia Garcia
79f2ea72b0 utils: add support for multiple options in mount rules
The tools don't support having multiple options specified in mount
rules as it is allowed in the parser.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-05-21 17:55:54 -03:00
John Johansen
527054e0ab Merge utils aa-notify: small tweaks to the merged rule interface
This is a set of small tweaks to the merged rule interface window

- don't specify, font or size when setting bold
- improve message around unknown profiles
- add a custom message for snap profile
- output tktheme missing message, to help users identify they can improve the interface when started manually

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1529
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2025-05-13 04:11:06 +00:00
John Johansen
a0af1797de utils aa-notify: add missing translation to interface
Add gettext translation call to a couple buttons text that were
missing the it.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2025-05-12 16:13:42 -07:00
John Johansen
9b4d747f5e utils aa-notify: If tkthemes are missing output a message
Let the user know that tkthemes is missing.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2025-05-12 16:13:35 -07:00
John Johansen
62123c6feb utils aa-notify: Add a special case for snap policy
Snap policy is a special case of the unknown profile. Give the user
a slightly better message for these messages.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2025-05-12 16:09:15 -07:00
John Johansen
9cb72734eb utils aa-notify: Update message to improve merged rules message
The current notification can be confusing, in that it can present a
profile followed by a list of rules that can't be selected.

Explictly state that the Unknown profile can't be modified so the user
has some indication that not being able to select the shown rules is
expected.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2025-05-12 16:07:38 -07:00
John Johansen
e0f8bd4400 utils aa-notify: Use default bold font
Instead of specifying the font type and size, which will not work for
all display configuration, use the the default BOLD font that tkinter
supplies.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2025-05-12 16:07:16 -07:00
John Johansen
f3178d79b8 parser: make auto vars @{exec_path} and @{attach_path} local vars
Make it so the @{exec_path} and @{attach_path} variables behavior
completely as local variables, overriding global variables of the
same name, instead of conflicting with them.

The exec var is only validate for the profile block after the attachment
is defined so the pattern

@{exec_path}=/path
profile test @{exec_path} {

   @{exec_path} rw,
}

is valid with the global var defining the attachent which then sets
the local auto @{exec_path} and @{attach_path} variables.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2025-05-10 09:55:14 -07:00
John Johansen
ee3b5d746f utils: tests: Update the utils tests for the two cases they don't handle
- the autovars not being defined because the profile doesn't have an
  attachment

- the autovar conflicting with a user defined var of the same name

Signed-off-by: John Johansen <john.johansen@canonical.com>
2025-05-09 04:53:35 -07:00
Georgia Garcia
d6951bab60 spread: add modifiers to list of spread utils tests
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-05-08 10:53:11 -03:00
Christian Boltz
94c5cf047f
Split priority rules with unsafe keyword to separate tests
This helps to limit the amount of rules skipped in the utils tests
(because the utils don't support the `unsafe` keyword)
2025-05-05 20:53:53 +02:00
John Johansen
c0fcd1698b utils: add support for priority rule prefix
Add basic support for the priority rules prefix. This patch does not
allow the utils to set or suggest priorities. It allows parsing and
retaining of the priority prefix if it already exists on rules and
checking if it's in the supported range.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
Signed-off-by: John Johansen <john.johansen@canonical.com>
2025-05-05 14:54:22 -03:00
Georgia Garcia
3389230437 utils: add allow keyword to list of unsupported modifiers
Some classes don't support modifiers like audit and deny. Only rlimit
has been checking for the allow keyword, but the others shouldn't
support it as well. Since they all do the same check, refactor them
into a method from BaseRule in case more modifiers are added.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-05-05 14:54:22 -03:00
Eisuke Kawashima
b50f516a78
chore: fix vim modelines
- set filetype, instead of syntax, in vim modelines
- replace filetype of subdomain with apparmor
- move modelines in the first or last five lines of each file so that
  vim can recognize them
2025-05-04 23:01:43 +09:00
John Johansen
d4a76c456d Merge profiles: force read permission to their attachment path
Unconfined delegates access to open file descriptors. Therefore when running a confined binary from unconfined, it will work even when the attachment path is not read-allowed.

However, as soon as these confined binaries are run from another confined process, this delegation is not permitted anymore and the program breaks.

This has been the cause of several bugs such as https://bugs.launchpad.net/ubuntu/+source/apparmor/+bug/2107455 or https://github.com/canonical/snapd/pull/15181 .

This MR makes sure every confining AppArmor profiles explicitly allow (at least) read access to their attachment path.

This Merge request:
 - Introduce `test_profile.sh`, a helper script that ensures confining AppArmor profiles explicitly allow (at least) read access to their attachment path.
 - Modifies a lot of profiles so that all profiles have r/mr access to their attachment path
 - Extends `make check` to automatically ensure all AppArmor profile grant explicit read access to their attachment path, preventing future omissions.
 - Modifies apparmor_parser to show attachment in --debug output

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1637
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2025-04-28 12:02:18 +00:00
Maxime Bélair
d4e795fe6d utils: Fix test-logprof.py for bin.ping profile
Update expected output to match the modifications on bin.ping profile.

Signed-off-by: Maxime Bélair <maxime.belair@canonical.com>
2025-04-18 12:41:56 +02:00
Georgia Garcia
8c84b36b32 utils: fix unix qualifier clean rule generation
The wrong clean rule is generated when unix rules contain qualifiers,
with the order inverted with the rule name.

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/511
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-04-17 20:39:58 -03:00
Ryan Lee
fe9c305ccd Move the "unsafe" rules of front_perms_ok simple tests to separate test file
This enables us to exercise the front perms parse logic in the utils rule parsing through the simple tests as well

Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
2025-04-10 11:19:05 -07:00
Christian Boltz
b5894687ed
Check for mount rules with multiple 'fstype'
... and adjust the tools to raise an exception if such a rule is found.
While this is not nice, it's better than the previous behaviour where
only the last 'fstype' was kept, and the others were lost when writing
the rule.
2025-04-06 15:19:30 +02:00
Christian Boltz
171e0b1fa9
Check for mount rules with multiple 'options'
... and adjust the tools to raise an exception if such a rule is found.
While this is not nice, it's better than the previous behaviour where
only the last 'options' was kept, and the others were lost when writing
the rule.
2025-04-06 15:19:01 +02:00
Christian Boltz
7726c86b79
mount: accept fstype and options in any order
Note: If multiple fstype= or options= are given, this is not detected as
an error (to keep the regex simpler). When writing back such a rule,
only one fstype and options will "survive".

Adjust the exclude list in test-parser-simple-tests.py accordingly:
- several valid mount rules no longer fail
- two invalid mount rules which so far accidentally raised an exception
  because of the fstype/options order no longer raise this exception
  (conflicting mount options, which are the real reason why these rules
  are invalid, are not detected in the tools)

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/501
2025-04-05 19:18:09 +02:00
John Johansen
2b9f2d2cb7 utils: tests: mark detached mount as tools wrong
The tools are wrong in parsing the detached mount test.

Until that can be fixed, mark the tools as wrong.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2025-04-05 00:19:09 -07:00
John Johansen
abe588cea4 Merge utils: skip user config reading in aa-notify when --configdir is given
--configdir is meant for testing and should override all other configs,
instead of being combined with them. Config combination causes aa-notify
test failures if e.g. the user-local config sets filtering options.

Also supply a notify.conf file for exclusive use during testing.

Signed-off-by: Ryan Lee <ryan.lee@canonical.com>

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1610
Approved-by: Georgia Garcia <georgia.garcia@canonical.com>
Merged-by: John Johansen <john@jjmx.net>
2025-04-03 21:36:48 +00:00
Ryan Lee
e949653b1a utils: supply a notify.conf for aa-notify tests
Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
2025-04-03 12:20:10 -07:00
Ryan Lee
096c8df52b utils: skip user config reading in aa-notify when --configdir is given
--configdir is meant for testing and should override all other configs,
instead of being combined with them. Config combination causes aa-notify
test failures if e.g. the user-local config sets filtering options.

Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
2025-04-03 12:17:25 -07:00
Ryan Lee
4623da695e utils: add unprivileged_userns to aa-notify list of special profiles
Both the unconfined profile and unprivileged_userns are part of the
default notify.conf, so the default fallback when no configurations are
present should also match this default.

Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
2025-04-02 16:48:01 -07:00
John Johansen
3b3dada5d9 Merge util: enhance AARE file path validation
Fixes https://bugs.launchpad.net/apparmor/+bug/2106033

Improve the validation of AARE file paths by introducing a new regex
that supports paths starting with '{' (e.g. '{/,/org/freedesktop/DBus}').
These paths are notably used in snap.lxd.* profiles.

Signed-off-by: Maxime Bélair <maxime.belair@canonical.com>

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1607
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2025-04-02 18:03:09 +00:00
John Johansen
0d1c694a66 Merge profiles: allow fusermount3 to mount in directories used by flatpak (LP: 2100295)
These are the default directory mounts used by Flatpak's system cache for mounting revokefs-fuse. Unfortunately, the new rules are quite broad, but we might not be able to do much better than that.

Signed-off-by: Ryan Lee <ryan.lee@canonical.com>

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1562
Approved-by: Georgia Garcia <georgia.garcia@canonical.com>
Merged-by: John Johansen <john@jjmx.net>
2025-04-02 17:54:13 +00:00
Maxime Bélair
dbf4c27154 util: enhance AARE file path validation
Fixes https://bugs.launchpad.net/apparmor/+bug/2106033

Improve the validation of AARE file paths by introducing a new regex
that supports paths starting with '{' (e.g. '{/,/org/freedesktop/DBus}').
These paths are notably used in snap.lxd.* profiles.

Signed-off-by: Maxime Bélair <maxime.belair@canonical.com>
2025-04-02 18:32:03 +02:00
John Johansen
305ef867fb Merge utils: fix cleanprof recursion error when child is defined out of parent
If the following scenario was present in a profile, cleanprof
would fail with a RecursionError exception (maximum recursion
depth exceeded)

/parent { }
/parent///child { }

This occured because in aa.py, in the write_piece function, the
wrong depth was being passed, along with a wrong hat. The
formatting of the spaces was also incorrect.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1605
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2025-04-01 21:03:56 +00:00
John Johansen
60218409cc Merge logprof: add /usr prefixed kernel module path to config for /usr merge
Signed-off-by: Ryan Lee <ryan.lee@canonical.com>

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1598
Approved-by: Christian Boltz <apparmor@cboltz.de>
Merged-by: John Johansen <john@jjmx.net>
2025-04-01 20:51:53 +00:00
Georgia Garcia
f0c96c0d68 utils: fix cleanprof recursion error when child is defined out of parent
If the following scenario was present in a profile, cleanprof
would fail with a RecursionError exception (maximum recursion
depth exceeded)

/parent { }
/parent///child { }

This occured because in aa.py, in the write_piece function, the
wrong depth was being passed, along with a wrong hat. The
formatting of the spaces was also incorrect.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-04-01 16:48:24 -03:00
Georgia Garcia
f490480558 utils: add parent to external child profiles
Profiles that are defined like below did not have the parent attribute
set in profile storage:

/parent///child {}

The condition on which child profiles were written was also changed so
they are not removed from the profile if /parent does not exist.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-04-01 16:46:16 -03:00
Christian Boltz
4c3954f13d test-cleanprof: add tests for external child profiles 2025-03-31 15:50:15 -03:00
Georgia Garcia
699d7b5c83 utils: fix cleanprof regression on header generation
Commit c9d41a3ebb introduced a regression on profile header
generation.

This commit removes the name parameter from the get_header function
since the ProfileStorage should already contain all the information
required to generate the header for profiles and hats. The tests
needed to be updated as well to make sure the ProfileStorage object
contained the information needed by the get_header method.

Fixes: c9d41a3ebb ("utils: fix profile and hat header generation")
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-03-31 15:50:15 -03:00
Ryan Lee
372dcc8250 logprof: add /usr prefixed kernel module path to config for /usr merge
Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
2025-03-27 09:50:11 -07:00