Commit graph

1500 commits

Author SHA1 Message Date
Christian Boltz
68396e9ae5
Avoid crash on exec without log events for target profile
If an exec gets denied in enforce mode, there are no log events for the
target binary/profile. Therefore, trying to set the final_name for the
target will crash with a KeyError.

Check for the existence of hashlog[aamode][target_profile] in all exec
options to prevent this crash.
2020-09-27 15:05:12 +02:00
John Johansen
4a5db481f1 Merge Add change_profile support to aa-logprof
Note that the log doesn't include enough information for EXEC MODE and EXEC COND, therefore aa-logprof will always propose ALL as EXEC COND (comm= might give a hint about EXEC COND, but isn't good enough).

With the added support in aa-logprof, remove the changeprofile tests from the known-failing list in test-libapparmor-test_multi.py.

Also add another test log (from darix) / expected profile to the libapparmor testsuite.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/631
Acked-by: John Johansen <john.johansen@canonical.com>
2020-09-25 10:15:07 +00:00
John Johansen
0dcac24510 Merge aa-logprof: propose 'include' instead of '#include' rules
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/630
Acked-by: John Johansen <john.johansen@canonical.com>
2020-09-25 10:12:20 +00:00
Christian Boltz
de0d4f688e
Remove all conflicting modes when switching profile mode
When switching to complain or enforce mode (for example with aa-complain
and aa-enforce), remove conflicting flags like kill and unconfined.
2020-09-24 23:21:54 +02:00
Christian Boltz
a2d3a382a8
add_or_remove_flag(): allow to add or remove multiple flags
Multiple flags can be given as string (will be split) or as array.

Also add some tests confirming that everything works as expected.
2020-09-24 23:21:54 +02:00
Christian Boltz
7918d8980b
Add change_profile support to aa-logprof
Note that the log doesn't include enough information for EXEC MODE and
EXEC COND, therefore aa-logprof will always propose ALL as EXEC COND
(comm= might give a hint about EXEC COND, but isn't good enough).

With the added support in aa-logprof, remove the changeprofile tests
from the known-failing list in test-libapparmor-test_multi.py.

Also add another test log (from darix) / expected profile to the
libapparmor testsuite.
2020-09-20 17:07:18 +02:00
Christian Boltz
eada7a8d44
aa-logprof: propose 'include' instead of '#include' rules 2020-09-20 14:42:25 +02:00
Christian Boltz
f0c27b57b0
aa-unconfined: add support for 'kill' profile mode
... when reading /proc/$pid/attr/{apparmor/,}current

Also add a comment about _not_ adding support for the 'unconfined'
profile mode, because that would give a quite confusing output.
2020-09-18 13:38:50 +02:00
Christian Boltz
5a31e94394
aa-unconfined: also read /proc/$pid/attr/apparmor/current
This means moving the code that reads the 'current' file into a new
function read_proc_current()Then call that function for both
/proc/$pid/attr/apparmor/current (preferred) and /proc/$pid/attr/current
(fallback).
2020-09-18 13:38:43 +02:00
Christian Boltz
b3f79f4efb
Fix aa-genprof crash if extra profile exists
serialize_profile() assumes that active_profiles has the
/etc/apparmor.d/ filename of a profile initialized.

This patch makes sure this is true even when using an extra profile by
initializing it in get_profile().

Ideally serialize_profile() shouldn't always use active_profiles, but
that will be part of a bigger change.

Reported by zt1024 including a proposed patch on
https://gitlab.com/apparmor/apparmor/-/merge_requests/604
but of course ;-) this patch is better because it selectively does the
initialization only in the case that needs it.
2020-09-12 20:25:48 +02:00
zt1024
85b5ead6aa fix bug that some rules will be added to the profile repeatedly 2020-08-20 23:46:48 +08:00
Christian Boltz
46920dd3ef
utils make check_severity_db: say ERROR for failing the build
utils `make check_severity_db` will fail the build if a (probably new)
capability in not listed in severity.db. This also means it should print
out an ERROR, not a warning.

This is a follow-up of lp#1890547 and
https://gitlab.com/apparmor/apparmor/-/merge_requests/589
2020-08-14 20:16:49 +02:00
Christian Boltz
ae01250209
Add CAP_BPF and CAP_PERFMON to severity.db
These capabilities were introduced in Linux 5.8

References: https://bugs.launchpad.net/bugs/1890547
2020-08-07 22:37:53 +02:00
Christian Boltz
98bf187323
Handle symlink log events in aa-logprof
Nobody told the tools that log events with operation="symlink" exist.
Add this keyword to the list of file or network operations (I don't
expect network symlinks ;-) but keeping everything in that list makes
things easier than special-casing it.)

Also add the log sample and expected result to the libapparmor tests.

Fixes https://gitlab.com/apparmor/apparmor/-/issues/107
2020-08-02 19:49:18 +02:00
Steve Beattie
0f3c860768
utils/tests: add libaparmor swig library path
Similar to the profiles/ check using the python utilities, the
tests for the python utilities were not including the path for the
swig libapparmor library in the LD_LIBRARY_PATH variable, only in
PYTHONPATH. This commit fixes that, renaming the variable used for
the built libapparmor check.

v2:
 - actually use the LIBAPPARMOR_PATH variable when defining
   LD_LIBRARY_PATH

Bug: https://gitlab.com/apparmor/apparmor/-/issues/98
Signed-off-by: Steve Beattie <steve.beattie@canonical.com>
Acked-by: John Johansen <john.johansen@canonical.com>
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/586
2020-07-24 23:52:45 -07:00
Christian Boltz
9e37e8830b
Use os.path.join() everywhere
... instead of merging paths with string operations
2020-06-28 13:00:32 +02:00
Christian Boltz
429bfa0a07 Merge branch 'cboltz-abs-include' into 'master'
Change internal include file storage to absolute paths

See merge request apparmor/apparmor!562

Acked-by: Steve Beattie <sbeattie@ubuntu.com>
2020-06-17 11:12:25 +00:00
Christian Boltz
4f50f16c50
Add abi rule when creating a new profile
When aa-autodep or aa-genprof create a new profile, always add an abi
rule. (Hardcoded to <abi/3.0> for now to keep things simple.)
2020-06-13 22:48:15 +02:00
John Johansen
0c9884550c parser: support enforce, kill and unconfined profile modes
The enforce profile mode is the default but specifying it explicitly
has not been supported. Allow enforce to be specified as a mode. If
no mode is specified the default is still enforce.

The kernel has supported kill and unconfined profile modes for a
long time now. And support to the parser so that profiles can make
use of these modes.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/440
Fixes: https://gitlab.com/apparmor/apparmor/-/issues/7
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Steve Beattie <sbeattie@ubuntu.com>
2020-06-10 05:35:37 -07:00
Christian Boltz
8296c107cf
Drop profile_dir parameter from ProfileList get_all_merged_variables()
This parameter is superfluous and unused since some commits.

Also adjust all callers.
2020-06-05 20:02:33 +02:00
Christian Boltz
2a742b3e6b
drop now unused get_include_path()
... and a comment mentioning it
2020-06-05 20:02:33 +02:00
Christian Boltz
0aa58536f0
include_dir_filelist(): simplify to only handle absolute paths
This is not a real change - since some commits, include_dir_filelist()
gets only called with absolute paths.

Add a check to ensure this, drop the now superfluous get_include_path()
call, and replace usage of include_name_abs with include_name (which are
the same now).

Also drop the superfluous profile_dir parameter, and adjust the only
caller accordingly.
2020-06-05 20:02:33 +02:00
Christian Boltz
7e8430575c
load_include(): get rid of incfile_abs
With the check that incfile starts with a '/', incfile and incfile_abs
(as returned by get_include_path()) are always the same.

Drop the get_include_path() call and setting incfile_abs, and replace
usage of incfile_abs with incfile.

This is just a cleanup, no behaviour change.
2020-06-05 20:02:33 +02:00
Christian Boltz
4a265e4121
Add checks to load_include() to ensure absolute paths
Also update the tests to the new behaviour.
2020-06-05 20:02:33 +02:00
Christian Boltz
9eb7a7581f
match_includes(): don't propose local/ files
This fixes the behaviour change inctroduced two commits ago.
2020-06-05 20:02:33 +02:00
Christian Boltz
221725c7d4
match_includes(): return proposals without profile_dir prefix
... so that the include rules proposed by aa-logprof continue to be
relative to the profile directory.

This fixes the behaviour change introduced in the previous commit.
2020-06-05 20:02:33 +02:00
Christian Boltz
2f522fe45b
Change internal include file storage to absolute paths
This removes the need to remove profile_dir from include paths at
various places.

A side effect is that aa-logprof / match_includes() now propose more
include rules, for example matching local/ files.

Another side effect is that proposals for include rules
(match_includes() again) now come with the full path.

Both side effects will be fixed in the next commits.
2020-06-05 19:58:09 +02:00
Christian Boltz
de8fa87051
convert profile_dir to abspath in init_aa()
This is needed for running the tests, because test/logprof.conf contains
a relative path, and tests only "manually" set the profile_dir if they
need/have a modified copy of the profiles.
2020-06-05 19:58:09 +02:00
Christian Boltz
6fe4b5e59a
fix setting apparmor.aa.profile_dir in some tests 2020-06-05 19:58:06 +02:00
Christian Boltz
fd66451054 Merge branch 'cboltz-include-sort' into 'master'
IncludeRule: sort files in included directory

... instead of relying on the filesystem(!) ordering, which will look
random to both users and unittests.

Also partially revert the test changes from
c5a7bcd50e /
https://gitlab.com/apparmor/apparmor/-/merge_requests/548 -
sorting the result only in the tests is a bad idea.

See merge request apparmor/apparmor!552

Acked-by: Steve Beattie <sbeattie@ubuntu.com>
2020-06-03 11:31:10 +00:00
John Johansen
730db17607 policy: tag policy with the AppArmor 3.0 abi
Tag profiles and abstractions with abi information.

Tagging abstractions is not strictly necessary but allows the parser
to detect when their is a mismatch and that policy will need an
update for abi.

We do not currently tag the tunables because variable declarations
are not currently affected by abi.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/491
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Steve Beattie <sbeattie@ubuntu.com>
2020-05-29 00:23:17 -07:00
Christian Boltz
c4db85c66a
IncludeRule: sort files in included directory
... instead of relying on the filesystem(!) ordering, which will look
random to both users and unittests.

Also partially revert the test changes from
c5a7bcd50e /
https://gitlab.com/apparmor/apparmor/-/merge_requests/548 -
sorting the result only in the tests is a bad idea.
2020-05-28 20:17:29 +02:00
Christian Boltz
e3af898ca4
Switch alias handling to AliasRule and AliasRuleset
This fixes cases when two aliases with the same left side were
configured - instead of "last one wins" in the dict, AliasRuleset now
keeps both.

ProfileList add_alias() changes its parameters and now expects an
AliasRule object. Adjust all callers to that.

Drop the no longer needed write_alias().

Also adjust the tests to use AliasRule and add a dedup test promised in
an earlier patch series.
2020-05-28 20:08:57 +02:00
Christian Boltz
5bf9b51d4d
Add AliasRule and AliasRuleset classes
Also add some tests for them.
2020-05-27 14:05:38 +02:00
Christian Boltz
f0fd410fd1
Extend RE_PROFILE_ALIAS to provide named matches 2020-05-27 14:05:35 +02:00
Christian Boltz
09896bd5f1 Merge branch 'cboltz-variable' into 'master'
Add VariableRule and VariableRuleset and use it for variable handling

Besides the usual advantages of switching to classes, we finally get rid of the `filelist` hasher.

While on it, also fix some bugs around variable handling, including https://bugs.launchpad.net/apparmor/+bug/1331856 and some that maybe nobody noticed before.

As usual, see the individual commits for details.

See merge request apparmor/apparmor!544

Acked-by: Steve Beattie <steve.beattie@canonical.com>
2020-05-27 11:43:29 +00:00
Christian Boltz
d19735340a
Detect invalid trailing commas in variable definitions
Trailing commas in variable values are not allowed (unless they are
quoted). Fix the regex to avoid "eating" the comma, and add a check to
detect invalid commas.

As usual, add some tests, and remove some testcases from the
exception_not_raised list.
2020-05-27 13:32:44 +02:00
Christian Boltz
0629215f2a
Check for variable names not ending with }
... and add a test to ensure that everything works as expected.

Note that broken variable names like '@{foo' match the (quite
permissive) regex, but are invalid nevertheless.
2020-05-27 13:32:44 +02:00
Christian Boltz
e9b8139cee
Check variable errors when parsing simple_tests
... by calling active_profiles.get_all_merged_variables()

Also remove vars/vars_bad_add_assignment_1.sd from the
exception_not_raised list again - now it raises an exception as
expected.
2020-05-27 13:32:44 +02:00
Christian Boltz
7d86bf9fe2
severity: replace load_variables() with set_variables()
Add set_variables() to severity.py to set the variables for severity
rating. It typically gets the data from the get_all_merged_variables()
result.

This replaces the slightly broken load_variables() that parsed profile
files for variables. (For example, parsing "@{foo} = /bar" resulted
in a variable name "@{foo} " with trailing space.)

Also adjust aa.py and the severity tests to use set_variables() (with
get_all_merged_variables()) instead of load_variables().

This also re-adds the checks that were removed in the "Store variables
in active_profiles (ProfileList)" commit earlier, while still fixing
lp:1331856.

With this change, unload_variables() becomes useless (the variables get
overwritten in set_variables() anyway), drop it and its calls.

Note that load_variables() silently ignored non-existing files while the
get_all_merged_variables() call only works for existing files that are
known to active_profiles. Since the input of ask_the_questions() and
ask_exec() comes from log_dict (= audit.log or a profile to merge), add
a check if that profile actually exists in the set of active profiles.

Also adjust the severity tests to use set_variables().

Finally, drop the tests that check for handling non-existing include
files, redefining and adding to non-existing variables - all these
things get now handled in include_list_recursive() and
get_all_merged_variables() and their tests.

Fixes: https://bugs.launchpad.net/apparmor/+bug/1331856
2020-05-27 13:32:44 +02:00
Christian Boltz
7451c341a6
add get_all_merged_variables()
This function returns a dict with all variables and their values for a
given profile file. It also checks for redefined variables, and adding
to non-existing variables, and errors out in both cases. Note that it
does not check the include order and is therefore more forgiving than
apparmor_parser.

Also add some tests for get_all_merged_variables().

Note: the tests are based on reading "real" profiles, therefore we need
to initialize apparmor.aa and call some of its functions.

The alternative would be to construct ProfileList objects for some
profiles and includes manually, but doing that in a way that can replace
the tests with "real" profiles would be quite some work, and I'm not
bored enough for that ;-)
2020-05-27 13:32:44 +02:00
Christian Boltz
b13037e36d
Move reset_aa() from aa-mergeprof to aa.py
Some tests in the next commit also need it.
2020-05-27 13:32:44 +02:00
Christian Boltz
6155b356b3
VariableRuleset: add get_merged_variables()
This function returns a dict with variables, both definitions (=) and
value additions (+=).

Also add some tests.
2020-05-27 13:32:44 +02:00
Christian Boltz
9d2a10dec7
ProfileList: merge get_clean_first() into get_clean()
Now that ProfileList handles the whole preamble, there's no need to keep
two half functions.
2020-05-27 13:32:44 +02:00
Christian Boltz
fe3c0b0ef8
Get rid of the 'filelist' hasher()
Everything handled in 'filelist' gets handled in active_profiles now.

Note: the 'elif' branch in delete_all_duplicates() was probably never
hit because `if include.get(...)` always matched. The only possible
exception might be non-existing include files, but those cause a 'file
not found' error anyway.
2020-05-27 13:32:44 +02:00
Christian Boltz
430120879c
Drop now unused store_list_var() and its tests 2020-05-27 13:32:44 +02:00
Christian Boltz
abe0e2b246
ProfileStorage: Drop 'lvar' and write_list_vars()
Since the previous commit, variables never get stored in ProfileStorage,
and write_list_vars() is unused.
2020-05-27 13:32:44 +02:00
Christian Boltz
e5d38807df
Store variables in active_profiles (ProfileList)
... instead of filelist[file]['lvar'], and also write them from there.

Also fix detection of variable definitions inside a profile, which is
not allowed.

Note that ProfileList has a different write order than the old code -
first includes, then variable definitions. This makes more sense because
typical profiles first include tunables/global, and then define
additonal variables (that might use variables from tunables/global) or
extend variables defined in tunables/global.

This change also fixes some problems with the simple_test test profiles.
The "adding to non-existing variable" check currently doesn't exist,
which "fixes" lp:1331856.

OTOH this also means that such cases are not detected, therefore add
vars_bad_add_assignment_1.sd to the exception_not_raised list.

The check will be re-added in a later commit
in get_all_merged_variables().
2020-05-27 13:32:44 +02:00
Christian Boltz
2a58e0ada2
Extend ProfileList to handle (store/write) variables
... and also add some tests.
2020-05-27 13:32:43 +02:00
Christian Boltz
61db5595aa
VariableRuleset: Prevent re-defining variables
When adding a variable with a name that is already known to the
VariableRuleset, raise an exception.

Also add a test for this.
2020-05-27 13:32:43 +02:00