2
0
mirror of https://gitlab.com/apparmor/apparmor synced 2025-08-29 05:17:59 +00:00

673 Commits

Author SHA1 Message Date
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
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
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
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
Georgia Garcia
c9d41a3ebb utils: fix profile and hat header generation
The header was being generated incorrectly in 2 cases:
When the profile/hat contained the parent profile in its name, as in

profile firefox//dash {
hat ^firefox//dash {

and in the unit tests, the child profile or hat was being named as the
parent profile. This was not caught by the general case because the
code has not yet been fully adapted to handle multiple nested child
profiles.

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/493

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-03-26 09:53:08 -03:00
John Johansen
e030ff7ea9 Merge utils/test/test-aa-notify.py: update last cmd for lxd VMs
When doing testing via LXD VMs and in particular when using "lxc exec" to run
commands in the VM, there is no controlling tty and so the output of last is
missing this column of data. Instead try even harder to parse the timestamp from
the output of "last".

Signed-off-by: Alex Murray <alex.murray@canonical.com>

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1582
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2025-03-19 06:17:34 +00:00
Alex Murray
b6654416b0
utils/test/test-aa-notify.py: update last cmd for lxd VMs
When doing testing via LXD VMs and in particular when using "lxc exec" to run
commands in the VM, there is no controlling tty and so the output of last is
missing this column of data. Instead try even harder to parse the timestamp from
the output of "last".

Signed-off-by: Alex Murray <alex.murray@canonical.com>
2025-03-18 17:45:00 +10:30
John Johansen
2fb0fa9964 Merge add support for writing quoted mount source and mountpoints
Add quotes if a mount source or mountpoint includes whitespace.
Also explicitely handle empty mount source (known from
1f33fc9b29c174698fdf0116a4a9f50680ec4fdb)

As usual, some tests can't hurt ;-)

I propose this fix for 4.0..master

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1573
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2025-03-18 04:56:22 +00:00
John Johansen
e38db72fea Merge parser: add support for attach_disconnected.ipc flag
The attach_disconnected.ipc flag allows the use of disconnected paths
on posix mqueues. This flag is a subset of attach_disconnected, and it
does not allow disconnected paths for all files.

Corresponding kernel patch needed to test in https://gitlab.com/georgiag/apparmor-kernel/-/tree/mqueue-ext
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1577
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2025-03-13 18:56:30 +00:00
Georgia Garcia
541d3f4489 parser: add attach_disconnected.ipc parser tests
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-03-13 14:51:18 -03:00
Christian Boltz
1ff9306c93
UnixRule: allow comma as separator in peer=
... and add some tests for it
2025-03-13 17:22:24 +01:00
Christian Boltz
d06260859b
add support for writing quoted mount source and mountpoints
Add quotes if a mount source or mountpoint includes whitespace.
Also explicitely handle empty mount source (known from
1f33fc9b29c174698fdf0116a4a9f50680ec4fdb)

As usual, some tests can't hurt ;-)
2025-03-11 21:07:14 +01:00
Christian Boltz
2afdf1b214 Merge utils: Fix mount rule handling for revokefs-fuse
First expand nested `(...)` in glob_pattern. This duplicates a few bytes, but makes the regex easier to read.

With that done, allow `-` in glob_pattern.

One of the possible matches in glob_pattern was `\w+` which matched for example `none`.

However, it doesn't match `revokefs-fuse` because of the `-`. Therefore change `\w+` to [\w-]+.

While on it, add two more tests - one for `none` with some options, and one with `revokefs-fuse`.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1565
Approved-by: Maxime Bélair <maxime.belair@canonical.com>
Approved-by: Ryan Lee <rlee287@yahoo.com>
Merged-by: Christian Boltz <apparmor@cboltz.de>
2025-03-10 20:35:57 +00:00
Bo YU
ca0afa1afb Increase timeout for test_allow_all 2025-03-04 08:36:54 +00:00
Christian Boltz
ce87b44bae
glob_pattern: allow -
One of the possible matches in glob_pattern was `\w+` which matched for
example `none`.

However, it doesn't match `revokefs-fuse` because of the `-`. Therefore
change `\w+` to [\w-]+.

While on it, add two more tests - one for `none` with some options, and
one with `revokefs-fuse`.
2025-02-28 23:47:05 +01:00
Ryan Lee
930218193b utils: remove the skip_*_profiles testing bypass
The utils should be able to skip profiles that it can't parse now,
so this test suite bypass mechanism should no longer be necessary.

Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
2025-02-27 16:48:46 -08:00
Ryan Lee
e80d5bd3d4 utils: test in test-minitools for resiliency against unparseable profiles
Since all the tools that load profiles go through the same module, this should
be sufficient as a first pass.

Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
2025-02-27 16:48:41 -08:00
Ryan Lee
e71e27be70 utils: make read_profiles robust against profiles it doesn't understand
This will allow the other tools to continue working on other profiles, even
if some of them use syntax that the utils currently can't handle.

Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
2025-02-27 16:46:22 -08:00
Ryan Lee
df0358062d utils: test: expand mechanism for ignoring profiles in tests
The utils cannot parse some of the newer profile constructs yet, so
generalize a pre-existing mechanism for skipping profiles to use that mechanism in the other tests that need it

Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
2025-02-27 13:05:00 -08:00
Christian Boltz
db7e3109cc
test-translations: include "ignore" in exec prompts
This is a follow-up of adding the "ignore" option to exec prompts in
https://gitlab.com/apparmor/apparmor/-/merge_requests/1543
2025-02-24 13:35:08 +01:00
Christian Boltz
3e7e9bf01f
test-translations: display all hotkey conflicts at once
... instead of erroring out at the first conflict.

Also display all options involved in a hotkey conflict to make fixing it
easier.
2025-02-24 13:35:05 +01:00
Zygmunt Krynicki
0acc138712 utils: abbreviate delta for Python 3.12 argparse
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2025-01-28 23:01:42 +01:00
Zygmunt Krynicki
6336465edf utils: adjusts aa-notify tests to handle Python 3.13+
Python 3.13 changes the formatting of long-short option pairs that use a
meta-variable. Up until 3.13 the meta-variable was repeated. Since
Python change [1] the meta-var is only printed once.

[1] https://github.com/python/cpython/pull/103372

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2025-01-28 20:49:16 +01:00
Ryan Lee
afd6aa0581 utils: test: account for last cmd format change in test-aa-notify
The "last" command, which was supplied by util-linux in older Ubuntu
versions, is now supplied by wtmpdb in Oracular and Plucky. Unfortunately,
this changed the output format and broke our column based parsing.

While the wtmpdb upstream has added json support at
https://github.com/thkukuk/wtmpdb/issues/20, we cannot use it because
we need to support systems that do not have this new feature added.

Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
2025-01-27 10:15:45 -08:00
Georgia Garcia
17a09d2987 Merge Allow overrides and preservation of some environment variables in utils make check
Our ubuntu packaging builds Python-enabled libapparmor's in the directories `libapparmor/libapparmor.python[version_identifier]`. In order for the util's `make check` to pick up on the correct libapparmor during the Ubuntu build process, we need the ability to override its search path. This patch introduces a `LIBAPPARMOR_BASEDIR` variable to allow for that.

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

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1497
Approved-by: Georgia Garcia <georgia.garcia@canonical.com>
Merged-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-01-23 19:11:07 +00:00
Ryan Lee
e32c267332 utils: test: use sys.executable when launching minitools in tests
This is analogous to the previous patch's change to the aa-notify tests.

Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
2025-01-23 10:37:57 -08:00
Ryan Lee
77cabf7dba utils: test: use sys.executable when launching aa-notify in tests
If the tests are running under a different Python, then the aa-notify bin should use the same Python

Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
2025-01-22 12:04:35 -08:00
Ryan Lee
3365e492a7 utils: test: test-aa-notify: Ensure aanotify_bin is always a list
os.environ returns a string, but the default value is a list, and the concatenation of __AA_CONFDIR assumes a list.
Thus, if APPARMOR_NOTIFY and __AA_CONFDIR were both specified, this would error out.

Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
2025-01-22 11:52:38 -08:00
Ryan Lee
90143494fc Allow overrides and preservation of some environment variables in utils make check
Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
2025-01-22 11:10:41 -08:00
Christian Boltz
45e4c27cf0
Add support for lastlog2 to get last login
lastlog2 is the 2038-safe replacement for wtmp, and in the meantime
became part of util-linux.

This commit switches from trying to parse the lastlog2 output to
directly reading lastlog2.db with sqlite3.

Adjust get_last_login_timestamp() to use the lastlog2 database
(/var/lib/lastlog/lastlog2.db) if it exists, and adjust
get_last_login_timestamp_lastlog2() to actually do that.

(If lastlog2.db doesn't exist, aa-notify will read wtmp as usual.)

Unfortunately lastlog2 doesn't have a way to get machine-readable output
(for example json), therefore - after trying and failing to parse the
lastlog2 output - directly read from lastlog2.db. Let's hope the format
never changes ;-)

Fixes: https://bugzilla.opensuse.org/show_bug.cgi?id=1228378

Fixes: https://bugzilla.opensuse.org/show_bug.cgi?id=1216660

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/372
2025-01-14 19:36:43 +01:00
Christian Boltz
371a9ff9ec
Add support for lastlog2 to get last login
lastlog2 is the 2038-safe replacement for wtmp, and in the meantime
became part of util-linux.

Adjust get_last_login_timestamp() to use lastlog2 if it exists, and add
get_last_login_timestamp_lastlog2() to actually do that.

(If lastlog2 doesn't exist, aa-notify will read wtmp as usual.)

Unfortunately lastlog2 doesn't have a way to get machine-readable output
(for example json), therefore we have to parse the output that is meant
for humans. Let's hope the format never changes ;-)

(The alternative would have been to use squlite3 to once more read the
data behind the official program's back, but that was already a bad idea
for wtmp, therefore I decided against it.)

Fixes: https://bugzilla.opensuse.org/show_bug.cgi?id=1228378

Fixes: https://bugzilla.opensuse.org/show_bug.cgi?id=1216660

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/372
2025-01-14 19:36:42 +01:00
Christian Boltz
7d537efcb0
Rename get_last_login_timestamp to get_last_login_timestamp_wtmp
... and add a wrapper function with the old name

Also rename the tests to the new name, and create a copy with the
original name. The copy will be adjusted to also check/expect lastlog2
results in a later commit.
2025-01-14 19:36:40 +01:00
Christian Boltz
f66ada256a
Drop now-unused split_to_merged() and its tests 2024-12-17 22:51:12 +01:00
Christian Boltz
531f47676d
ProfileList: add get_all_profiles()
... and a test for it
2024-12-17 22:51:12 +01:00
Christian Boltz
c93d560f89
Switch test-libapparmor-test_multi.py from aa to active_profiles
Note that the old code assigned dummy_prof to aa[profile][hat] and
active_profiles[profile] (= the main/parent profile) - which is
diffferent when testing a log for a child profile.

aa[profile][hat] was the wrong place - but since we used exactly that
again when checking for added exec rules, this error was hidden.

Now that the test is switched to using active_profiles, only check the
main profile for exec rules added by ask_exec(). (This will need to be
adjusted when we add a test for exec rules/events in nested childs, but
not earlier ;-)
2024-12-17 22:51:12 +01:00
Christian Boltz
c5e495c56d
ProfileList: add replace_profile()
... and some tests for it.
2024-12-17 22:51:12 +01:00
Christian Boltz
a37c65957f
ProfileList: add __getitem__()
... and add some tests for it.
2024-12-17 22:51:12 +01:00
Christian Boltz
fe9b2542ca
ProfileList: add profile_exists()
... and extend the existing tests for add_profile to also check
profile_exists().
2024-12-17 22:51:11 +01:00