Commit graph

1762 commits

Author SHA1 Message Date
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
John Johansen
0b8ea047e8 rc.apparmor: drop use of xargs as fallback when loading profiles
LP:1377338 <https://bugs.launchpad.net/ubuntu/+source/apparmor/+bug/1377338>

has been fixed for quite awhile and we don't need to call xargs as
a fallback when loading policy fails.

In addition we really don't want to be doing this because we want to
be moving to atomic profile loads where if one profile fails the
whole load fails. This is a step in that direction.

Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2022-02-04 02:22:39 -08:00
Dominique Martinet
76163f801d init: make xargs invocation busybox-compatible
busybox xargs does not have -d nor long --max-procs options,
instead use -0 (and separate arguments with printf "%s\0")
and -P which are more portable.
While we are here, also add -r (--no-run-if-empty, which also has
no long equivalent for busybox) as we likely don't want to run
anything if no profile were found

This is useful for alpine systems where findutils is not installed by
default, but busybox xargs is available.
2022-01-27 11:48:05 +09:00
Christian Boltz
854cbae52e
Fix comment typo in parser/lib.c 2021-12-05 18:16:53 +01:00
Alistair Young
a6360d9db2 Add explicit WSL detection to is_container_with_internal_policy 2021-11-08 21:21:09 +00:00
Jon Tourville
90620cb64f Generate af_names.h similar to cap_names.h 2021-10-18 15:55:57 -05:00
John Johansen
dc7755e544 parser: Fix unknown state condition RLIMIT_MODEINCLDE
The parser is broken on RLIMIT parsing when receiving unexpected input
because the shared state for this specifies RLIMIT_MODEINCLUDE which
is an unknown start condition resulting in the following warning

  parser_lex.l:745: undeclared start condition RLIMIT_MODEINCLUDE

and also means RLIMIT and INCLUDE are not properly handled

Signed-off-by: John Johansen <john.johansen@canonical.com>
2021-09-24 00:48:27 -07:00
Christian Boltz
1459f49b7a
Fix typo in manpage
Fixes: https://gitlab.com/apparmor/apparmor/-/issues/192
2021-08-17 19:43:53 +02:00
John Johansen
c60fc809a9 [6/7] parser: update the parser to add interface rules for change_X
For change_hat and change_profile instead of a single interface
rule we need to add some readonly interfaces for discovery and
the new and old proc interface for writing.

Consolidate into a single shared routine.

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/150
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/713
Signed-off-by: John Johansen <john.johansen@canonical.com>
2021-07-21 14:31:44 -07:00
Jon Tourville
598131696e Merge Parser check fix
This resolves an issue in the parser's job handling when running on a machine with >8 CPU cores. The test library was updated to resolve failures in the caching tests caused by the features directory entries being unsorted in the tests.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/775
Acked-by: John Johansen [john@jjmx.net](mailto:john@jjmx.net)
2021-07-19 20:01:57 +00:00
Jon Tourville
efe39e7590 parser: fix comment for jobs_max default value 2021-07-19 13:53:09 -05:00
Jon Tourville
e82a23dfe4 parser: sort feature directory entries in to match libapparmor's directory traversal 2021-07-16 08:57:57 -05:00
John Johansen
3d1232e640 parser: fix cache time stamp check to include dir time stamps
Currently for directory includes the directory timestamp is ignored.
This is wrong as operations like removing a file from the dir won't
be considered in the timestamp check.

Fix this by updating the timestamp check to include the included
directories timestamp.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/760
Signed-off-by: John Johansen <john@jjmx.net>
Acked-by: Georgia Garcia  <georgia.garcia@canonical.com>
2021-07-13 18:08:55 -07:00
Jon Tourville
5910b50926 parser: correctly calculate max number of jobs 2021-07-12 10:09:16 -05:00
Georgia Garcia
12957eba4d parser: CAP_AUDIT_READ is only available after Linux 3.16
The parser was not compiling on older versions of Ubuntu
(trusty) because the capability CAP_AUDIT_READ that was
defined on base_cap_names.h was not available until
kernel version 3.16.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/767
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
Acked-by: John Johansen <john.johansen@canonical.com>
2021-07-02 17:05:19 -03:00
John Johansen
8c8da7c67d parser: move ifdefs for capabilities to single common file
Unfortunately the parser was doing ifdef checks for capabilities
in two places. Move all the capability ifdefs into capability.h

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/768
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Georgia Garcia  <georgia.garcia@canonical.com>
2021-07-02 12:48:47 -07:00
Christian Boltz
e31015a11a
Fix comment wording in file_cache.h
This fixes https://gitlab.com/apparmor/apparmor/-/merge_requests/743#note_562522101
2021-05-02 11:29:41 +02:00
Christian Boltz
f0221f4bca
Add a test with recursive include in preamble 2021-04-28 13:32:08 +02:00
John Johansen
74bc4275a5 parser: Fix invalid reference to name in attachment warning
The name var is being improperly used in a warning. Not only is
it being used after it is freed, it also never had the correct value
as the "name" variable contained the value being used as the base
attachment.

Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: time out
2021-04-27 21:06:21 -07:00
John Johansen
be0d2fa947 parser: fix filter slashes for profile attachments
The parser is failing to properly filter the slashes in the profile
attachment after variable expansion. Causing matche failures when
multiple slashes occur.

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/154
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/727
Reported-by: Mikhail Morfikov <mmorfikov@gmail.com>
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: time out
2021-04-27 21:06:05 -07:00
Steve Beattie
c00b0d325b parser: add a simple one-level recursive include test
This adds a recursive include that otherwise parses correctly, to check
that the parser handles one-level recursion loop acceptably. When the
utils can support it, we should have tests that exercise deeper levels
of looping, e.g. include a -> include b -> include c -> include a or
deeper.

Without the fix in
https://gitlab.com/apparmor/apparmor/-/merge_requests/743, the parser
does fail due to hitting its file descriptor limit.

(The test at

  https://gitlab.com/apparmor/apparmor/-/blob/master/parser/tst/simple_tests/include_tests/recursive.sd

includes itself, which will result in a recursive profile definition
which isn't accepted by the parser.)

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/743
Signed-off-by: Steve Beattie <steve.beattie@canonical.com>
Signed-off-by: John Johansen <john.johansen@canonical.com>
2021-04-27 20:27:33 -07:00
John Johansen
7dcf013bca parser: add include dedup cache to handle include loops
Profile includes can be setup to loop and expand in a pathalogical
manner that causes build failures. Fix this by caching which includes
have already been seen in a given profile context.

In addition this can speed up some profile compiles, that end up
re-including common abstractions. By not only deduping the files
being included but skipping the need to reprocess and dedup the
rules within the include.

Fixes: https://bugzilla.suse.com/show_bug.cgi?id=1184779
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/743
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Steve Beattie <steve.beattie@canonical.com>
2021-04-27 20:26:57 -07:00
Christian Boltz
2b00d21e2e
Rewrite gen-dbus in python
The generated files are exactly the same.
2021-04-25 18:31:38 +02:00
John Johansen
cf809ea928 parser: Fix make DEBUG=1
When building the parser with DEBUG=1 enabled the build fails with
the following error and warnings

    In file included from parser_main.c:47:0:
    parser_main.c: In function ‘void auto_tune_parameters()’:
     parser_main.c:1421:35: error: ‘estimate_jobs’ was not declared in this scope
        PDEBUG("Auto tune: --jobs=%d", estimate_jobs);
                                   ^
    parser.h:201:37: note: in definition of macro ‘PDEBUG’
      fprintf(stderr, "parser: " fmt, ## args); \
                                     ^~~~
    parser_main.c:1421:35: note: suggested alternative: ‘estimated_jobs’
        PDEBUG("Auto tune: --jobs=%d", estimate_jobs);
                                   ^
    parser.h:201:37: note: in definition of macro ‘PDEBUG’
      fprintf(stderr, "parser: " fmt, ## args); \
                                     ^~~~
    parser.h:201:41: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long int’ [-Wformat=]
      fprintf(stderr, "parser: " fmt, ## args); \
                                         ^
    parser_main.c:1428:5: note: in expansion of macro ‘PDEBUG’
         PDEBUG("Auto tune: --jobs=%d", jobs);
     ^~~~~~
    Makefile:234: recipe for target 'parser_main.o' failed

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/745
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2021-04-21 12:57:05 -07:00
John Johansen
4101d74de6 man: apparmor.7 add info about complain mode and kernel parameters
Add additional info about complain mode, its behavior, how to enable
it and add warnings about its use.

In addition add info on how to set kernel parameters on boot for
the various options that are covered.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/722
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2021-03-15 15:24:43 -07:00
John Johansen
2852e1ecdf parser: fix filter slashes for link targets
The parser is failing to properly filter the slashes in the link name
after variable expansion. Causing match failures when multiple slashes
occur.

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/153
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/723
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Steve Beattie <steve.beattie@canonical.com>
2021-03-15 00:45:58 -07:00
John Johansen
855dbd4ac8 parser: fix rule downgrade for unix rules
Rule downgrades are used to provide some confinement when a feature
is only partially supported by the kernel.

  Eg. On a kernel that doesn't support fine grained af_unix mediation
      but does support network mediation.

        unix (connect, receive, send)
              type=stream
              peer=(addr="@/tmp/.ICE-unix/[0-9]*"),

      will be downgraded to

        network unix type=stream,

Which while more permissive still provides some mediation while
allowing the appication to still function. However making the rule
a deny rule result in tightening the profile.

  Eg.
        deny unix (connect, receive, send)
              type=stream
              peer=(addr="@/tmp/.ICE-unix/[0-9]*"),

      will be downgraded to

        deny network unix type=stream,

and that deny rule will take priority over any allow rule. Which means
that if the profile also had unix allow rules they will get blocked by
the downgraded deny rule, because deny rules have a higher priority,
and the application will break. Even worse there is no way to add the
functionality back to the profile without deleting the offending deny
rule.

To fix this we drop deny rules that can't be downgraded in a way that
won't break the application.

Fixes: https://bugzilla.opensuse.org/show_bug.cgi?id=1180766
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/700
Signed-off-by: John Johansen <john.johansen@canonical.com>
2021-03-14 07:16:24 -07:00
Christian Boltz
cc2d71023b
Improve AARE documentation in apparmor.d manpage
... as discussed on the mailinglist
2021-03-01 19:40:50 +01:00
John Johansen
d5de9692ab Merge parser: replace dynamic_cast with is_type method
The dynamic_cast operator is slow as it needs to look at RTTI information and even does some string comparisons, especially in deep hierarchies. Profiling with callgrind showed that dynamic_cast can eat a huge portion of the running time, as it takes most of the time that is spent in the simplify_tree() function. For some complex profiles, the number of calls to dynamic_cast can be in the range of millions.

This commit replaces the use of dynamic_cast in the Node hierarchy with a method called is_type(), which returns true if the pointer can be casted to the specified type. It works by looking at an Node object field that is an integer with bits set for each type up in the hierarchy. Therefore, dynamic_cast is replaced by a simple bits operation.

In my tests, for complex profiles the improvement in speed even made running apparmor_parser with "-O no-expr-simplify" slower that when simplifying, apparently because the smaller trees obtained after the expression simplification require less calls to DFA::update_state_transitions(), and that compensates the now significantly slower time spent in simplify_tree(). This opens the door to maybe avoid "-O no-expr-simplify" in the snapd daemon, thus allowing faster run-time checks in the kernel.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/711
Acked-by: John Johansen <john.johansen@canonical.com>
2021-02-17 03:44:23 +00:00
John Johansen
b6fbe10d11 parser: fix build issue with REALLOCARRAY check
On some systems the build of the parser is spitting out

cc: fatal error: no input files
compilation terminated.

This is being caused by the REALLOCARRAY checkfailing due to cpp trying
to check for both input and output files and not correctly falling
back to stdin/stdout if infile and outfile aren't specified.

Fix this by being explicit that infile and outfile are supposed to
use stdin and stdout.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/712
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2021-02-16 04:50:34 -08:00