Commit graph

1826 commits

Author SHA1 Message Date
Maxime Bélair
6632fa6c43 aa-logprof/aa-genprof: Adding support for --allow-all, --output-dir and --no-abstraction 2024-03-28 19:29:08 +00:00
Georgia Garcia
ac02295de7 utils: support more fs types in mount rules
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-26 18:40:40 -03:00
Georgia Garcia
f4706bfdf6 utils: allow mount destination globbing
The abstraction lxc/start-container shipped by the liblxc-common
package uses the following mount rule which was not allowed by our
regexes:

  mount options=(rw, make-slave) -> **,
  mount options=(rw, make-rslave) -> **,

Since in AppArmor regex ** includes '/' but * by itself doesn't, I'm
adding explicit support for **.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-26 18:40:40 -03:00
Maxime Bélair
dac9d08764 MountRule: Fix _is_covered_localvars 2024-03-14 12:46:01 +00:00
Christian Boltz
d19db55a37
Fix test-aa-notify on openSUSE Tumbleweed (new 'last')
The new 2037-proof `last` on openSUSE Tumbleweed doesn't support the
`-1` option.

Remove it, and cut off the output manually.
2024-03-12 19:37:29 +01:00
Maxime Bélair
021c3248f9 Minor improvements for MountRule 2024-03-07 19:27:03 +00:00
Georgia Garcia
01090dcf1b utils: fix coding style in mount
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-04 09:24:58 -03:00
Georgia Garcia
3ea2bfec56 Merge Small fixes in MountRule
- Removed unnecessary variable source_is_path in mount rules
- Changed a string to a r-string to avoid an 'invalid escape sequence \s' warning

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1172
Approved-by: Georgia Garcia <georgia.garcia@canonical.com>
Merged-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-04 11:53:51 +00:00
Georgia Garcia
dfb02cbd93 Merge MountRule: check for unknown fstype and options keywords, and fix issues uncovered by that
* **MountRule: sync flags_keywords with parser code**

    ... based on /mount.cc mnt_opts_table

    Several keywords and aliases were missing in flags_keywords:
    - B
    - M
    - make-private
    - make-rprivate
    - make-rshared
    - make-rslave
    - make-runbindable
    - make-shared
    - make-slave
    - make-unbindable
    - r
    - R
    - read-only
    - w

    Also sort the keywords in the same order as in mount.cc.

    Note: AARE handling is still a TODO.

    After that, update the list of known parsing failures:
    - several valid profiles are now correctly parsed
    - some `"make-*" mount opt and an invalid src` bad profiles are no
      longer detected as being invalid

* **test-mount.py: fix MountRule instance creation**

    If fstype or options is a str, it has to be exactly one keyword, because
    \__init__() / check_and_split_list() won't parse a str.

    Our "normal" code already honors this, and only hands over fstype and
    options as sets or a single-keyword str.

    However, a few tests (wrongly) handed over a str that would need further
    parsing. Adjust the tests to no longer do this.

* **MountRule: check for unknown fstype and options**

    ... now that the previous commits fixed issues that ended up as unknown
    keywords.

    Also add mount/ok_12.sd as known-failing test. It uses fstype=AARE which
    MountRule doesn't support (yet?).

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1169
Approved-by: Georgia Garcia <georgia.garcia@canonical.com>
Merged-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-03-04 11:52:53 +00:00
Maxime Bélair
0daf3e8c9b Changing string to r-string to avoid warning 2024-03-04 09:02:24 +01:00
Maxime Bélair
a86c1bd45a Remove unnecessary variable source_is_path in mount rules 2024-03-04 09:00:58 +01:00
Christian Boltz
8f4073ecd9
MountRule: check for unknown fstype and options
... now that the previous commits fixed issues that ended up as unknown
keywords.

Also add mount/ok_12.sd as known-failing test. It uses fstype=AARE which
MountRule doesn't support (yet?).
2024-03-03 21:30:49 +01:00
Christian Boltz
8d21f01924
test-mount.py: fix MountRule instance creation
If fstype or options is a str, it has to be exactly one keyword, because
__init__() / check_and_split_list() won't parse a str.

Our "normal" code already honors this, and only hands over fstype and
options as sets or a single-keyword str.

However, a few tests (wrongly) handed over a str that would need further
parsing. Adjust the tests to no longer do this.
2024-03-03 15:52:14 +01:00
Christian Boltz
4e546291a5
MountRule: sync flags_keywords with parser code
... based on /mount.cc mnt_opts_table

Several keywords and aliases were missing in flags_keywords:
- B
- M
- make-private
- make-rprivate
- make-rshared
- make-rslave
- make-runbindable
- make-shared
- make-slave
- make-unbindable
- r
- R
- read-only
- w

Also sort the keywords in the same order as in mount.cc.

Note: AARE handling is still a TODO.

After that, update the list of known parsing failures:
- several valid profiles are now correctly parsed
- some `"make-*" mount opt and an invalid src` bad profiles are no
  longer detected as being invalid
2024-03-03 15:37:59 +01:00
Christian Boltz
8c026077d6
MountRule: make get_clean() more readable
... by getting rid of two mostly-identical, big return statements.

Also add tests for bare umound and remount rules to ensure full test
coverage.
2024-03-03 13:09:43 +01:00
Christian Boltz
5e4c4a0cb3
Make error check more readable 2024-03-03 12:53:49 +01:00
Christian Boltz
9c27a7c435
Fix writing 'mount {options,fstype} in ...' rules
We need spaces around the 'in' keyword.

Also add some tests for this.
2024-03-03 12:49:57 +01:00
Christian Boltz
a367c07437 Merge Add useful error message in test-mount.py
If /proc/filesystems contains a filesystem that is not listed in
MountRule valid_fs, print a useful error message that says what exactly
is going on, instead of only saying "False is not True".

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1166
Approved-by: Georgia Garcia <georgia.garcia@canonical.com>
Merged-by: Christian Boltz <apparmor@cboltz.de>
2024-03-01 21:30:33 +00:00
Christian Boltz
2200013088 Merge Cleanup old handling of mount rules
Now that we have MountRule and MountRuleset, drop the old "just store
the whole rule" code for mount rules.

Also drop some old tests that used that "store the whole mount rule"
code, and adjust the regex_matches tests to import the regex directly
from apparmor.regex.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1165
Approved-by: Georgia Garcia <georgia.garcia@canonical.com>
Merged-by: Christian Boltz <apparmor@cboltz.de>
2024-03-01 21:29:48 +00:00
Christian Boltz
517e7c96c8
MountRule: add '9p' filesystem
This filesystem is available during the build in build.opensuse.org
2024-03-01 21:36:26 +01:00
Christian Boltz
a7cd59819e
Add useful error message in test-mount.py
If /proc/filesystems contains a filesystem that is not listed in
MountRule valid_fs, print a useful error message that says what exactly
is going on, instead of only saying "False is not True".
2024-03-01 20:34:11 +01:00
Christian Boltz
e7f5ee3271
MountRule: Fix typo in 'btrfs' 2024-03-01 19:49:21 +01:00
Christian Boltz
da75b1c8d8
Cleanup old handling of mount rules
Now that we have MountRule and MountRuleset, drop the old "just store
the whole rule" code for mount rules.

Also drop some old tests that used that "store the whole mount rule"
code, and adjust the regex_matches tests to import the regex directly
from apparmor.regex.
2024-03-01 19:46:02 +01:00
John Johansen
35287c8e1c Merge add fine grained network mediation support
Similar to https://gitlab.com/apparmor/apparmor/-/merge_requests/1095, but this time simplified.
This version removes support for ip and port ranges and subnets. This can be added later.

It also contains an updated version of the network layout required by the kernel side of AppArmor.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1160
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2024-02-29 21:58:24 +00:00
Georgia Garcia
8a5e7227db parser: add parser tests for specified perms
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-02-29 16:25:59 -03:00
Georgia Garcia
052dd987b3 parser: add network conditional parser tests 2024-02-29 16:25:59 -03:00
Maxime Bélair
924b335dfc Adding userspace support for mount rules in aa-genprof/aa-logprof 2024-02-29 17:59:50 +00:00
Georgia Garcia
88907253e4 aa-notify: precompile filter regexes
Precompile each filter regex with re.compile so they don't need to be
recompiled for each log message when using re.match directly.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-02-26 10:24:46 -03:00
John Johansen
4b1bc85022 Merge aa-unconfined: Fix race when reading proc/*/attr/current
aa-unconfined can fault if it looses the race between checkking if
proc/*/attr/{apparmor/,}current exists, and actually opening the file.
Catch open/file errors and ignore them like the file doesn't exist.

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/355
Signed-off-by: John Johansen <john.johansen@canonical.com>

Closes #355
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1157
Acked-by: seth.arnold@gmail.com
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2024-02-25 07:36:16 +00:00
John Johansen
c4f649da92 aa-unconfined: Fix race when reading proc/*/attr/current
aa-unconfined can fault if it looses the race between checkking if
proc/*/attr/{apparmor/,}current exists, and actually opening the file.
Catch open/file errors and ignore them like the file doesn't exist.

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/355
Signed-off-by: John Johansen <john.johansen@canonical.com>
2024-02-24 20:33:35 -08:00
Georgia Garcia
4608d32628 aa-notify: add notification filtering
Allow notification filtering of the fields profile, operation, name,
denied_mask, net_family and net_socket using regex. Both command line
and config options in notify.conf are available.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-02-23 17:20:18 -03:00
Georgia Garcia
105b50502b utils: fix aa-notify last login test
The tests for aa-notify that were related to the last login were
assuming that the machine had been logged in at least once in the last
30 days, but that might not be the case.

Update the test to check for the last login date and update the test
logs considering that value.

Fixes: https://bugs.launchpad.net/bugs/1939022

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-02-21 18:18:26 -03:00
Christian Boltz
6cc3a3642d
Prevent ANSI terminal injection in aa-unconfined
/proc/$pid/cmdline can be changed by an application, therefore escape it
before printing.

The program name in /proc/$pid/exe can also contain any characters
(except \0 and shashes) and needs escaping.

Note: repr() wraps the string into single quotes, which we have to
remove to avoid changing the output format.

The test program from issue 364 now gets displayed as

    28443 /path/to/issue364 (/\x1b]0;X\x07) not confined

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/364
2024-01-20 23:42:30 +01:00
Christian Boltz
dfb6f90aee
ask_exec(): no longer skip exec events in hats
Instead of ignoring all exec events that happen in a hat/child profile,
only disallow child exec. ix and px are valid options inside a hat and
are now offered to the user.

(When the tools support nested child profiles one day, we can even allow
child exec again.)
2023-12-28 23:12:10 +01:00
John Johansen
f10e106a08 Merge parser: Add support for a default_allow mode
Add support for a default_allow mode that facillitates writing profiles
in that allow everything by default. This is not normally recomended
but fascilitates creating basic profiles while working to transition
policy away from unconfined.

This mode is being added specifically to replace the use of the
unconfined flag in these transitional profiles as the use of unconfined
in policy is confusing and does not reflect the semantics of what is
being done.

Generally the goal for policy should be to remove all default_allow
profiles once the policy is fully developed.

Note: this patch only adds parsing of default_allow mode. Currently
it sets the unconfined flag to achieve default allow but this
prevents deny rules from being applied. Once dominance is fixed a
subsequent patch will transition default_allow away from using
the unconfined flag.

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

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1109
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2023-12-23 08:38:18 +00:00
John Johansen
832bb8f417 parser: Add support for a default_allow mode
Add support for a default_allow mode that facillitates writing profiles
in that allow everything by default. This is not normally recomended
but fascilitates creating basic profiles while working to transition
policy away from unconfined.

This mode is being added specifically to replace the use of the
unconfined flag in these transitional profiles as the use of unconfined
in policy is confusing and does not reflect the semantics of what is
being done.

Generally the goal for policy should be to remove all default_allow
profiles once the policy is fully developed.

Note: this patch only adds parsing of default_allow mode. Currently
it sets the unconfined flag to achieve default allow but this
prevents deny rules from being applied. Once dominance is fixed a
subsequent patch will transition default_allow away from using
the unconfined flag.

Signed-off-by: John Johansen <john.johansen@canonical.com>
2023-11-24 15:38:19 -08:00
Christian Boltz
86c05357cf
Add test for unknown capability
Even if this is very unlikely to happen (because of the previously added
test, and because CapabilityRule only allows to specify known severity
keywords), ensure proper behaviour if an unknown severity gets rated.
2023-10-29 22:10:07 +01:00
Christian Boltz
f17bd59904
Test severity of all capabilities
... to ensure that they are all listed in severity.db
2023-10-29 22:10:07 +01:00
Christian Boltz
d38c7b22ce
CapabilityRule: simplify and improve __init__()
- convert a string parameter to a list to avoid duplication of the
  validation logic
- add separate check for empty cap_list
- remove check for empty strings - the previous commit already added
  such a check to the for loop. Also, move the comment to that check.
2023-10-29 21:42:57 +01:00
Christian Boltz
942202da17
CapabilityRule: Validate given caps against cap list
... and error out if an unknown capability is given.

This also means recognizing bad capabilities in the parser simple_tests
now works (so remove these from the exception_not_raised list), and that
we can no longer hand over an unknown capability in test-capability.py
to test their severity.
2023-10-29 21:31:43 +01:00
Christian Boltz
5c34655f4a
CapabilityRule: Add list of known capabilities
... and add a test to ensure that the list is/stays complete.
2023-10-29 21:28:13 +01:00
Christian Boltz
57ba373213
test-capability: use valid capability names
... instead of non-existing ones.

This is a search-and-replace commit:

ptrace -> sys_ptrace

chgrp -> fowner (because fowner wasn't used in the test before)
2023-10-29 21:23:00 +01:00
Christian Boltz
f2f24884c3
get_next_to_profile(): ensure all branches set all variables
This also means we can get rid of most cleanprof-specific conditions
without changing the behaviour (because the other functions don't use
'profile' yet).

Also hand over prof_filename to clean_profile() so that it doesn't need
to find it out itsself.
2023-10-12 13:44:04 +02:00
Christian Boltz
5d8347bc26
clean_profile(): re-order code
Error out early (avoids a tab level), and handle the short branch first
in the if condition.
2023-10-12 13:11:30 +02:00
Christian Boltz
0c595ac801
clean_profile(): rename filename to prof_filename
... for consistency with the variable name in all the other functions.
2023-10-12 13:08:13 +02:00
Christian Boltz
1d5f90efcd
Rename profile variable to prof_filename
... if it contains the profile filename. This avoids confusion with the
"real" 'profile' variable that contains a profile name.
2023-10-12 13:04:29 +02:00
Christian Boltz
4f51c93f9d
get_next_to_profile(): return profile and prof_filename
Before, the 'profile' return value was either a profile name or a
profile filename, depending on the active module (cleanprof vs.
everything else).

Separate the return values so that it's clear what we get.

Notes:
- This commit doesn't change functionality, only the number of return
  values and some variable names.
- There's no guarantee that all return values are set. They can also be
  None. (This might change in the future.)

Also adjust the callers of get_next_to_profile(), and rename 'profile'
to 'prof_filename' in calling functions that actually use the profile
filename.
2023-10-12 12:36:09 +02:00
Christian Boltz
4d1c17b426
Drop enable_profile() and disable_profile()
enable_profile() was unused.

disable_profile() was only used once, inline it into cmd_disable()
2023-10-12 12:36:09 +02:00
Christian Boltz
918a15e244
Merge common parts of mode changes into get_next_for_modechange() 2023-10-12 12:36:09 +02:00
Christian Boltz
fc8c7722a1
tools.py: call apparmor.read_profiles() in __init__()
... instead of calling it in every cmd_* function.
2023-10-12 12:36:05 +02:00