Commit graph

1861 commits

Author SHA1 Message Date
Christian Boltz
dd9b7b358f
Increase timeout in test-logprof.py
On (terribly, but real-world) slow buid hosts, running test-logprof.py
fails with a timeout. Increase the timeout so that even those build
hosts get enough time to finish the aa-logprof tests.
2023-08-15 20:49:08 +02:00
Christian Boltz
c175e414c8
Fix compability with Python < 3.9
str.removeprefix() was introduced in Python 3.9. Replace it with
backwards-compatible code.
2023-08-15 12:40:39 +02:00
John Johansen
8eee4de83e Merge parser: add support for attach_disconnected.path
Add support for specifying the path prefix used when attach disconnected
is specified.

TODO: add regression tests

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

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/661
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2023-08-14 14:15:25 +00: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
Christian Boltz
46debcc493
Add aa-logprof test framework
... and a simple test for a single (fake) event for ping.

Notes:
- to let aa-logprof work in the CI environment, we need to skip checking
  for the AppArmor mountpoint. Introduce --no-check-mountpoint for this.
- PYTHONPATH and LD_LIBRARY_PATH need to be explicitely forwarded when
  starting aa-logprof via subprocess.Popen()
- if the test runs with coverage enabled, it will also start aa-logprof
  with coverage (parameters copied from Makefile).

Speaking about coverage - this test adds 4% overall coverage, and 10%
more coverage for apparmor/aa.py.
2023-08-13 21:02:50 +02:00
John Johansen
93dff6a806 Merge parser: add support for prompt profile mode
Add support for the prompt profile flag. That allows policy to do an upcall to userspace if supported by the kernel and if a userspace daemon is available.

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

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1062
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2023-07-31 04:10:33 +00:00
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
Christian Boltz
7dc0254b90
Add option to log aa-logprof json input and output
Add a json_log option (default: disabled) to logprof.conf that enables
logging of all aa-logprof and aa-genprof input and output to a
/tmp/aa-jsonlog-* file.

This can be useful for debugging, and maybe also to create tests that do
a full aa-logprof run.

This patch introduces a minor behaviour change if aa-logprof errors out
on startup (for example if the config file is broken or the parser can't
be found):

Before:

```
$ aa-logprof --json
{"dialog": "apparmor-json-version","data": "2.12"}

ERROR: Can't find apparmor_parser at /sbin/apparmor_parser
```

After:

```
$ aa-logprof --json

ERROR: Can't find apparmor_parser at /sbin/apparmor_parser
```

Note that the json version line will not be printed if aa-logprof or
aa-genprof error out that early.

If there are no startup errors, the behaviour will not change.
2023-07-30 21:28:35 +02:00
Christian Boltz
f1ab009a09
ask_exec: Stop checking for CMD_nx (named exec)
... because it gets translated to CMD_px or CMD_cx earlier in the
function.

Also add a safety check for unknown answers.
2023-07-19 21:41:43 +02: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
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
cb2292922d utils: add io_uring python tool support
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2023-05-03 16:04:32 +02:00
Georgia Garcia
6c8331a7d0 libapparmor tests: add io_uring denied logs
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2023-05-03 16:04:32 +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
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
John Johansen
0e83933fa8 Merge Drop profile_ filename prefix for named profiles
Historically, if you create a `profile foo /bin/foo` with aa-logprof (by
choosing "named exec"), it will be saved as `/etc/apparmor.d/profile_foo`

This patch drops the `profile_` filename prefix so that the filename
will be `/etc/apparmor.d/foo`

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1014
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2023-04-25 02:29:44 +00:00
Christian Boltz
d4bcd50c5a
Drop profile_ filename prefix for named profiles
Historically, if you create a `profile foo /bin/foo` with aa-logprof (by
choosing "named exec"), it will be saved as `/etc/apparmor.d/profile_foo`

This patch drops the `profile_` filename prefix so that the filename
will be `/etc/apparmor.d/foo`
2023-04-23 19:27:11 +02:00
Christian Boltz
103be8e14b
Fix error when choosing named exec with plane profile names
When a user choooses to execute to a named profile (not: named child),
make sure to get the profile filename in the correct way to avoid a
crash.

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/314
2023-04-23 14:49:35 +02:00
John Johansen
90b3b5e572 Merge Format aa-notify to follow PEP-8
Update (most of the) code and inline comments/docstrings to follow
https://peps.python.org/pep-0008/ so that future maintenance is slightly
easier.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/996
Approved-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2023-04-01 03:20:07 +00:00
Otto Kekäläinen
fff72ed4c4 Format aa-notify to follow PEP-8
Update (most of the) code and inline comments/docstrings to follow
https://peps.python.org/pep-0008/ so that future maintenance is slightly
easier.

Continue to keep long lines as splitting them does not always improve
the code readability.
2023-03-30 22:30:34 -07:00
Christian Boltz
a724c79483
Ignore 'x' in mixed file mode log events
Probably thanks to O_MAYEXEC, denials for file access can now contain a
mix of x (exec) and other file permissions.

The actual exec should appear in a separate "exec" log event, therefore
ignore 'x' in file events for now if it's mixed with other permissions.

Note that file events ("open", "link" etc.) that contain denied_mask="x"
without another permission will still cause an error. (So far, this
hasn't been seen in the wild.)

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

Also add the log line from the bugreport and the (for now) expected
result as test_multi testcase.
2023-03-30 13:30:33 +02:00
Alexander Mikhalitsyn
4b7e868e54 parser: simple_tests: mount: mark ok_[16-19] tests as bad
These tests contains incompatible mount options and broken
after ("parser: add conflicting flags check for options= conditionals")

Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
Acked-by: John Johansen <john.johansen@canonical.com>
2023-03-28 20:07:05 -07:00
Mark Grassi
37ef82fc51 Defer logger string formatting 2023-03-05 20:42:28 -05:00
Mark Grassi
cc7f8fb4d6 Fix AttributeError caused by Python 3 migration 2023-02-19 17:18:08 -05:00
Mark Grassi
2742d1f1ee Replace mutable default arguments in utils 2023-02-19 17:17:35 -05:00
Mark Grassi
14e01b5d73 Replace mutable default arguments in tests 2023-02-19 17:12:30 -05:00
Mark Grassi
844a4dc393 Change string formatting method in Python tests 2023-02-19 16:54:38 -05:00
Mark Grassi
4dd69ddd5b Update utils/po/ with Launchpad translations revision #2507 2023-02-01 22:28:45 -05:00
Mark Grassi
e188195344
Improve utils hotkey handling 2023-01-29 13:33:44 +01:00
Mark Grassi
e554fe9fd4
Improve utils translation hotkey test
Ensure all languages are always tested and all subprocesses run successfully.
2023-01-29 13:28:10 +01:00
Georgia Garcia
e94cd809e4 utils: fix dbus access string formatting
The AppArmorBug exception was missing the variable from the message

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2022-12-16 10:37:45 -03:00
Georgia Garcia
7618d69eca utils: fix check for when there's no operation field in audit log
If the "operation" field is not present, we get the following error:
AttributeError: 'NoneType' object has no attribute 'startswith'

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2022-12-16 10:13:04 -03:00
Christian Boltz
6f84aa2092 Merge Simplify FileRule perms_with_a()
... by returning early if there's nothing to do.

The main improvement is more readable code, but there should also be a
minor performance improvement.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/955
Approved-by: Georgia Garcia <georgia.garcia@canonical.com>
Merged-by: Christian Boltz <apparmor@cboltz.de>
2022-12-12 17:41:28 +00:00
Christian Boltz
b9997473b3
Simplify FileRule perms_with_a()
... by returning early if there's nothing to do.

The main improvement is more readable code, but there should also be a
minor performance improvement.
2022-11-30 18:35:33 +01:00
Georgia Garcia
8e7b6fd583 utils: add logparser support for mqueue
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2022-11-22 19:31:15 +00:00
Georgia Garcia
d4cbcf2f07 utils: add message queue rules parsing in python tools
Based on what was done in the parser, replicate the logic
so it can be used in the python tools.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2022-11-22 19:31:15 +00:00
Christian Boltz
f03a3198a8 Merge Resolve string escape sequence DeprecationWarnings
This MR closes #286.

Strings with only invalid escape sequences were prefixed with an `r`. Strings containing both valid and invalid escape sequences were handled on a case-by-case basis, as simply changing to a raw string breaks valid escape sequences.

Closes #286
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/951
Approved-by: Christian Boltz <apparmor@cboltz.de>
Merged-by: Christian Boltz <apparmor@cboltz.de>
2022-11-22 12:40:55 +00:00
Mark Grassi
d94731ddf4 Resolve string escape sequence DeprecationWarnings 2022-11-21 22:08:42 -05:00
Mark Grassi
98817eecb6 Remove unnecessary regex backslashes 2022-11-21 22:07:49 -05:00
Christian Boltz
dd5a6c2e0a Merge Include profile name in error message on directory exec
... to make the error message more helpful.

Inspired by https://gitlab.com/apparmor/apparmor/-/issues/285

I propose this patch for 3.1 and master.
(3.0 uses a different variable name - if we want it there, it needs a slightly different patch.)

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/949
Approved-by: Jon Tourville <jon.tourville@canonical.com>
Merged-by: Christian Boltz <apparmor@cboltz.de>
2022-11-21 12:59:38 +00:00
Mark Grassi
db99138f56 Simplify rule._logprof_header_localvars() implementations 2022-11-20 10:38:42 -05:00
Mark Grassi
b150c40ed1 Unify rule._match() implementations 2022-11-20 10:38:42 -05:00
Christian Boltz
ef5c4b5085
Include profile name in error message on directory exec
... to make the error message more helpful.

Inspired by https://gitlab.com/apparmor/apparmor/-/issues/285
2022-11-20 15:09:33 +01:00
Mark Grassi
b62efce6ba Consolidate BaseRule.match() 2022-11-19 21:47:43 -05:00
Mark Grassi
d5c47ec900 Unify rule._match() calls 2022-11-19 21:47:43 -05:00
Mark Grassi
0fb7aed741 Unify rule.is_equal() type comparisons 2022-11-19 21:47:43 -05:00
Mark Grassi
e3eb5288cc Make rule.*_localvars methods private 2022-11-19 21:46:44 -05:00
Christian Boltz
b904fe78a7 Merge Make BaseRule a proper abstract base class
This closes #277 by introducing the following changes:
1. `BaseRule` was made a "proper" abstract base class in that it now enforces that subclasses implement all abstract methods.
2. As a consequence of this change, `BaseRule` can no longer be directly instantiated; tests were changed accordingly.
3. These test changes led to test failures, the reason being that many rule methods hardcoded class names, breaking inheritance. I made a commit to address this.
4. Once tests were passing, code coverage regressed due to the fact that there was now effectively-unreachable code in abstract instance methods. This code, which was only raising `NotImplementedError`s, was removed. I say "effectively-unreachable" here because it could be reached by calling `super()` via a subclass.
5. There are three "bonus" commits as well; one bug fix, one typo fix, and one cleanup.

Closes #277
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/930
Approved-by: Christian Boltz <apparmor@cboltz.de>
Merged-by: Christian Boltz <apparmor@cboltz.de>
2022-11-15 21:14:19 +00:00
Christian Boltz
d0ec2acaf2
Catch PermissionError when trying to write a profile
... and re-raise it as AppArmorException so that only the actual error
(without a backtrace) gets displayed.

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/282
2022-11-14 22:50:27 +01:00