Commit graph

1635 commits

Author SHA1 Message Date
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
Christian Boltz
39eb1939ba
Split variables
... and enable tests related to this
2020-05-27 13:32:43 +02:00
Christian Boltz
1eb9791ed7
move separate_vars() from aa.py to VariableRule
... and also move its tests to test-variable.py
2020-05-27 13:32:43 +02:00
Christian Boltz
215ec38ae3
Add VariableRule and VariableRuleset
... and a set of tests for them.

Note that the tests include some TODOs, these will be handled in the
following commits.
2020-05-27 13:32:43 +02:00
Steve Beattie
c5a7bcd50e
utils/tests: test-include.py sometimes fails due to ordering
With the includes rule class landing, this particular test failed
in a test vm due to the sort ordering being different. It's not
clear that there should be an expectation of ordering returned from
get_full_paths(), so sort the result.

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/548
2020-05-27 01:03:56 -07:00
Christian Boltz
373e8e23b1
Fix strip_quotes() to handle empty strings
strip_quotes() assumed its parameter is at least one character long, and
errored out on an empty string.

It also converted a string consisting of a single quote to an empty
string because that single quote had a quote as first and last char.

This commit fixes these two bugs.

Also rewrite TestStripQuotes to use tests[], and add some test for an empty
string, a one-char path (just a slash) and a single quote.
2020-05-24 13:41:13 +02:00
Christian Boltz
7629d18e04
convert RE_PROFILE_VARIABLE to named match groups
(no behaviour change, just easier to use)
2020-05-22 00:51:18 +02:00
John Johansen
4b79a58c25 Merge Switch handling of include rules from filelist to IncludeRule
This patch series moves include rule handling away from the `filelist` hasher to using IncludeRule and IncludeRuleset. This means that only variable handling is left in `filelist`.

As usual, check the individual commits for details.

PR: https://gitlab.com/apparmor/apparmor/-/merge_requests/537
Acked-by: John Johansen <john.johansen@canonical.com>
2020-05-21 08:52:49 +00:00
Christian Boltz
1d2e710b26
parse_profile_data(): drop old code for parsing include rules
With this, all include rules (with and without 'if exists') get handled
by the code that handles IncludeRule.
2020-05-20 18:57:54 +02:00
Christian Boltz
cefc64522d
Drop now unused 'include' from ProfileStorage and ProfileList 2020-05-20 18:57:54 +02:00
Christian Boltz
60e8c1ff41
No longer write to filelist['include] or profile['include']
All code is migrated to IncludeRuleset / 'inc_ie'], so there's no point
in filling 'include' anymore.
2020-05-20 18:57:54 +02:00
Christian Boltz
5cfab9b81b
serialize_profile(): write preamble without checking filelist
filelist is getting emptier and emptier, and checking against a
nearly-empty filelist would result in not writing the preamble.
2020-05-20 18:57:54 +02:00
Christian Boltz
f0fa69a010
Convert compare_profiles() to use inc_ie / IncludeRule 2020-05-20 18:57:54 +02:00
Christian Boltz
706138ed67
Convert remove_duplicate_rules() to use inc_ie / IncludeRule
Besides switching to 'inc_ie', also drop the old code that removed
duplicate 'include' rules.
2020-05-20 18:57:54 +02:00
Christian Boltz
c8fa3dec0a
aa-cleanprof: don't check profile includes against preamble includes
remove_duplicate_rules() composed the 'includes' list from a) the
include rules in the profile and b) the file_includes (preamble
includes), and then checked the profile includes against this
combination of profile and preamble includes.

Checking profile includes against preamble includes is wrong, and the
only reason why this went unnoticed for years is that preamble include
files (like tunables) won't work inside a profile and therefore never
appear there.

In theory this might be a backport candidate, even if this shouldn't
cause a real-world bug.
2020-05-20 18:57:54 +02:00
Christian Boltz
6161641bcf
Convert match_includes() to use inc_ie / IncludeRule 2020-05-20 18:57:54 +02:00