Commit graph

1441 commits

Author SHA1 Message Date
Mark Grassi
854602c0d9 Use the fact that empty sequences are false. 2022-08-21 11:15:07 -04:00
Mark Grassi
68e3f12c2c Avoid escaping quotation marks where possible. 2022-08-21 11:15:07 -04:00
Mark Grassi
c57138f255 Order imports and module-level dunder name assignments. 2022-08-21 11:15:07 -04:00
Mark Grassi
dc384c48a8 Use triple double-quoted strings for docstrings. 2022-08-21 11:15:07 -04:00
Mark Grassi
f590a66e50 Remove redundant backslashes, and unnecessary semicolons and pass statements. 2022-08-21 11:15:07 -04:00
Mark Grassi
96f7121944 Fix most PEP 8 whitespace, indentation, and major line length violations. 2022-08-21 11:15:07 -04:00
Mark Grassi
e4f88cc3a8 Indent line continuations per PEP 8. 2022-08-21 11:15:07 -04:00
Mark Grassi
aff9bb8f81 Ensure no bool comparisons use equality comparisons. 2022-08-21 11:15:07 -04:00
Mark Grassi
0375ea1257 Change tabs to spaces in Python files. 2022-08-21 11:15:07 -04:00
Georgia Garcia
0afc2cbb84 parser: fix LTO build
The previous fix for LTO builds in b6d3daa7 did not take into
consideration that LTO support could be added through CFLAGS,in which
case the fix would not be applied.

This patch applied the fix -flto-partition=none even if CFLAGS is
already defined.

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
Closes #214
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/901
Acked-by: John Johansen <john@jjmx.net>
Merged-by: Georgia Garcia <georgia.garcia@canonical.com>
2022-07-25 17:34:27 -03:00
intrigeri
c0815d0e0f dirtest.sh: don't rely on apparmor_parser -N's output sort order to be deterministic
I've seen this test fail because "apparmor_parser -N" returned the expected
lines, but in a different order than what's expected (dirtest.out).

To fix this, sort both the expected and actual output.
2022-07-25 10:14:31 +00:00
Mark Grassi
cf6606d380 Ensure opened temporary files are closed. 2022-07-17 21:52:55 -04:00
Christian Boltz
97bd86c7c6 Merge Remove Python 2 support.
Per the discussion in #243, this MR removes Python 2 compatibility. Namely, this merge request:
- removes code behind `sys` and `platform` interpreter version checks
- removes `unicode` vs. `str` handling
- removes unnecessary `__future__` imports
- removes unnecessary `object` inheritance
- removes unnecessary `super()` arguments
- uncomments commented-out code with "uncomment when python3 only" notes or some variant of that message

Regarding the `unicode` vs. `str` handling, it's arguably more Pythonic to check `isinstance(x, str)` as opposed to `type(x) is str`, but I didn't want to alter code behavior.

A change needs to be made to the `INCOMPLETE_COVERAGE` setting in `utils/test/Makefile` to get the pipeline to pass. I didn't get anywhere tweaking the setting myself, so someone else with more AppArmor experience will have to make that change.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/894
Approved-by: Christian Boltz <apparmor@cboltz.de>
Merged-by: Christian Boltz <apparmor@cboltz.de>
2022-07-12 18:26:29 +00:00
intrigeri
06f86e2a02 rc.apparmor.functions: only use systemd-detect-virt if it's present
This is a follow-up on !812, which added a call to systemd-detect-virt.
Everywhere else we don't assume that program is present,
and first check if it's there before we run it.
Let's do the same here.
2022-07-06 06:41:35 +00:00
Mark Grassi
df97cf89bd Remove Python 2 support. 2022-06-29 20:41:38 -04:00
Mark Grassi
728dbde5e4 Fix inconsistent return length. 2022-06-28 08:30:31 -04:00
Mark Grassi
7581c9e113 Speed up list creations, and change lists to tuples where appropriate.. 2022-06-26 22:18:56 -04:00
Mark Grassi
ca9920cf92 Avoid unnecessary memory copies when enlarging lists. 2022-06-26 12:06:22 +00:00
Christian Boltz
50a45c6a39 Merge Ensure opened files are closed.
This partially addresses issue #239. There are still some remaining instances where opened files are not properly closed, e.g. the `NamedTemporaryFile` in `utils/apparmor/config.py`.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/885
Approved-by: Christian Boltz <apparmor@cboltz.de>
Merged-by: Christian Boltz <apparmor@cboltz.de>
2022-06-22 17:20:49 +00:00
Mark Grassi
c255968e63 Fix spelling and grammar errors. 2022-06-18 16:14:45 -04:00
Mark Grassi
bf819bc8e6 Ensure opened files are closed. 2022-06-18 14:40:58 -04:00
John Johansen
2a3140cb93 parser: fix min length calculation for inverse character sets
The inverse character set lists the characters it doesn't match. If
the inverse character set contains an oob then that is NOT considered
a match. So length should be one.

However because of oobs are handle not containing an oob doesn't mean
there is a match either. Currently the only way to match an oob is
via a positive express (no inverse matches are possible).

Signed-off-by: John Johansen <john.johansen@canonical.com>
2022-05-23 15:39:14 -07:00
Sergei Trofimovich
64a64be7ff parser/capability.h: add missing <cstdint> include
Without the change apparmor build fails on this week's gcc-13 snapshot as:

    capability.h:66:6: error: variable or field '__debug_capabilities' declared void
       66 | void __debug_capabilities(uint64_t capset, const char *name);
          |      ^~~~~~~~~~~~~~~~~~~~
    capability.h:66:27: error: 'uint64_t' was not declared in this scope
       66 | void __debug_capabilities(uint64_t capset, const char *name);
          |                           ^~~~~~~~
    capability.h:23:1: note: 'uint64_t' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?
       22 | #include <linux/capability.h>
      +++ |+#include <cstdint>
       23 |
2022-05-23 23:13:14 +01:00
Christian Boltz
8cb81ec25f
Add some empty and cut-off bad abi rules
Credits go to Seth who proposed these tests in
https://gitlab.com/apparmor/apparmor/merge_requests/196#note_108500403
2022-04-17 00:38:18 +02:00
Christian Boltz
2036f6fa9d
dirtest.sh: error out on unexpected success
... if a test is expected to fail, but succeeds.

Also fix the copyright year - the test was created in 2022, not in 2013.

This fixes my comments on
bd78b6b292
2022-04-03 17:45:24 +02:00
John Johansen
bd78b6b292 parser: dirtest that was supposed to be part of the previous commit
libapparmor: fix handling of failed symlink traversal, fixed a couple
of directory walk issues that could cause failures. The test included
in this commit was supposed to be included in the previous commit,
but was accidentally dropped. Even worse the make file changes did
make it causing the previous commit to break the CI.

Fixes: MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/85
Signed-off-by: John Johansen <john.johansen@canonical.com>
2022-02-27 01:03:22 -08:00
John Johansen
acc6ba1cb7 libapparmor: fix handling of failed symlink traversal
Ideally we would have a flag or something so the caller could choose
to handle symlinks, or traverse them. But since all callers currently
don't handle symlinks just handle them in the iterator.

Beyond fixing the early termination due to a failed symlink this also
fixes another case of failure in one job cause dir based loads to
terminate early. Which can result in partial loads.

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/215
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/850
Signed-off-by: John Johansen <john.johansen@canonical.com>
Approved-by: Georgia Garcia <georgia.garcia@canonical.com>
2022-02-27 00:55:41 -08:00
Georgia Garcia
b6d3daa715 parser: fix building with link time optimization (lto)
Libapparmor was fixed for lto builds on commit 7cde91f5 but
the parser was also failing due to the same reasons when lto
was enabled.

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/214
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2022-02-16 20:49:42 -03:00
intrigeri
322b3f4d3e profile-load: use less ambiguous if/then construct
As shellcheck taught me
today (https://github.com/koalaman/shellcheck/wiki/SC2015),
"A && B || C is not if-then-else. C may run when A is true".

It does not matter here in practice, because worst case we would run "true" once
too many, but still.
2022-02-15 07:34:17 +00:00
intrigeri
35f23a6da1 profile-load: use safer "read" construct
In this case it does not matter, we're merely testing if we can actually
read from that file, but let's make this robust (and shellcheck happy)
for future's sake.

Reference: https://www.shellcheck.net/wiki/SC2162
2022-02-15 07:28:27 +00:00
John Johansen
5a41024bbe Merge Make the systemd unit a no-op in containers with no internal policy
In 73e124d4fb I've upstreamed the `is_container_with_internal_policy()` function, but so far it was not used anywhere upstream. This is the missing bit.

I could trace the history of that patch back to 2012 (2.7.102-0ubuntu3):

    * debian/apparmor.init: do nothing in a container.  This can be
      removed once stacked profiles are supported and used by lxc.
      (LP: #978297)

Context: I lack both knowledge and motivation to keep maintaining this as part of the Debian delta. I'd rather see upstream, and in particular folks more knowledgeable than me about LXC/LXD, or with external motivation factors to work on this part of the stack, take care of it.

Note: Debian has similar code in its [sysvinit script](https://salsa.debian.org/apparmor-team/apparmor/-/blob/debian/master/debian/apparmor.init). I'm not touching that one.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/840
Acked-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2022-02-14 22:11:28 +00:00
John Johansen
6a54d59172 Merge Import profile-load script from Debian
This script is used at least by LXC upstream and MySQL in Debian:
https://codesearch.debian.net/search?q=%2Flib%2Fapparmor%2Fprofile-load

Presumably it could be useful elsewhere if it was more readily available.

Similarly to !840, this is another user of the `is_container_with_internal_policy()` function. I'd like all the callers of this function to live in harmony under the same roof, upstream.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/841
Acked-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2022-02-14 06:23:51 +00:00
intrigeri
56dd267a24 Mark variables as dummy in a way that shellcheck 0.7.0 supports
The "_" prefix is only supported in shellcheck > 0.7.2.
2022-02-13 19:43:48 +00:00
intrigeri
fb94661937 rc.apparmor.functions: mark intentionally unused variable as dummy
Thanks, shellcheck!
2022-02-13 19:43:48 +00:00
intrigeri
2e0fb86a94 rc.apparmor.functions: check command exit status directly
Thanks, shellcheck!
2022-02-13 19:43:48 +00:00
intrigeri
0bae8bf82a rc.apparmor.functions: disable shellcheck false positive 2022-02-13 19:43:48 +00:00
intrigeri
8eeb8c48c5 rc.apparmor.functions: quote to prevent word splitting and syntax error on empty output
Thanks, shellcheck!
2022-02-13 19:43:48 +00:00
intrigeri
2019519e34 Delete Red Hat initscript
Even if there are Red Hat / Fedora systems that use AppArmor, chances are that
they use systemd, and not an initscript. And even if somehow they do use an
initscript, chances are that it's not this one, as last time it has seen
a non-cosmetic change was in 2007.
2022-02-13 16:53:44 +00:00
Chris Novakovic
7f64db1f19 Display improvements in rc.apparmor.functions
* Don't call aa_log_action_end after calling aa_log_failure_msg, because
  a generic "failure" message will be outputted twice by the Red Hat and
  Slackware init scripts.
* Don't append a space to the initial output from apparmor_stop, in line
  with other usages of aa_log_daemon_msg.
2022-02-13 16:53:27 +00:00
Chris Novakovic
2f2eab3b21 Remove Debian init script
Debian doesn't use the init script provided in parser/rc.apparmor.debian,
instead preferring to patch parser/rc.apparmor.functions and call its
functions directly in an init script they maintain themselves (something
they have done since 2006). Since this script is no longer used, and
currently doesn't work correctly anyway because it lacks definitions for
several functions that are relied upon in parser/rc.apparmor.functions,
it can be removed.
2022-02-13 16:53:22 +00:00
intrigeri
c7e68d7009 Import profile-load script from Debian
This script is used at least by LXC upstream and MySQL in Debian:
https://codesearch.debian.net/search?q=%2Flib%2Fapparmor%2Fprofile-load

Presumably it could be useful elsewhere if it was more readily available.
2022-02-12 10:34:38 +00:00
intrigeri
c44e93d856 Make the systemd unit a no-op in containers with no internal policy 2022-02-12 10:23:39 +00:00
Christian Boltz
db0a8aacd5
Drop now-unused profiles_names_list() from rc.apparmor.functions
The only user was aa-remove-unknown - until the previous commit.
2022-02-11 22:59:01 +01:00
John Johansen
e726ebe376 Merge Drop unused failstop_system() from rc.apparmor.functions
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/835
Acked-by: John Johansen <john@jjmx.net>
Merged-by: John Johansen <john@jjmx.net>
2022-02-11 08:28:07 +00:00
Christian Boltz
8e1a4924d5
Drop unused failstop_system() from rc.apparmor.functions 2022-02-10 23:45:20 +01:00
Christian Boltz
8476786252
Drop skip_profile()
Its last user was profiles_names_list(), but since the previous commit
it lets apparmor_parser do this job.

Related to https://gitlab.com/apparmor/apparmor/-/issues/148
2022-02-10 22:56:16 +01:00
Christian Boltz
d38376c9c4
Simplify profiles_names_list()
... which is used by aa-remove-unknown.

apparmor_parser can read a whole directory, therefore we don't need to
do the directory listing, excluding *.rpmnew etc. ourself.

Related to https://gitlab.com/apparmor/apparmor/-/issues/148
2022-02-10 22:53:48 +01:00
Christian Boltz
5527c20833
Drop now-obsolete comment about skip_profile()
... which became superfluous after dropping use of xargs as fallback
when loading profiles in 0b8ea047e8
2022-02-09 18:32:51 +01:00
Christian Boltz
977af8c5ac
Add 'mctp' network domain keyword
Reported as comment on https://build.opensuse.org/request/show/951354
(update to glibc 2.35)
2022-02-08 19:09:24 +01:00
John Johansen
72c06c4d35 Merge Enable AppArmor to run properly under WSL/systemd
The AppArmor systemd scripts correctly detect the Windows Subsystem for Linux as a container, since all Linux distros executing under WSL 2 are containerized; however, unlike the majority of containers (since there is no accessible host distribution above them trying to set AppArmor policies that might interfere), AppArmor itself functions without problems in the WSL environment.

This patch adds WSL detection to the is_container_with_internal_policy function, allowing AppArmor to be started and operate normally in a WSL-plus-systemd environment.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/812
Acked-by: John Johansen <john@jjmx.net>
2022-02-04 10:25:14 +00:00