Commit graph

1562 commits

Author SHA1 Message Date
Christian Boltz
a07515bdd4
Run severity tests with official severity.db
... instead of the slightly outdated copy in test/
2021-04-14 21:37:53 +02:00
Christian Boltz
d9cb8df696
severity.py: bump test coverage to 100%
... by adding some new tests, and by marking two lines as "pragma: no
branch" because I didn't find a testcase that doesn't let them continue
with the next line.

Finally, remove severity.py from the "not 100% covered" list in
test/Makefile.
2021-04-14 21:37:01 +02:00
Steve Beattie
94b7704e56
utils: ProfileStorage - add tests with invalid type
Add tests with invalid type to ensure error handling works as expected.

Merge branch 'cboltz/cboltz-profile-storage-tests'

[Fixed conflict with prior change to utils/test/test-profile-storage.py]
Acked-by: Steve Beattie <steve@nxnw.org>
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/735
2021-04-11 16:54:43 -07:00
Christian Boltz
38c63026a4
ProfileStorage: add tests with invalid type
... to ensure error handling works as expected.
2021-04-11 21:54:36 +02:00
Christian Boltz
6a170ddaa1
Move and rename write_header() to ProfileStorage.get_header()
Also adjust the calling code to use get_header() instead of
write_header().

Finally, move the tests to test-profile-storage.py and do a few
adjustments needed by the change. Part of these adjustments is to hand
over empty params with the correct type instead of just "None".
2021-04-11 15:05:12 +02:00
Christian Boltz
4ef975fb97
change_profile_flags(): use ProfileStorage
... instead of a dict faking it.
2021-04-11 15:03:41 +02:00
Christian Boltz
9494238eec
write_header(): replace regex with startswith()
... to make the code easier to read and (probably) more performant.
2021-04-11 15:03:34 +02:00
John Johansen
edf52a7531 Merge read_profile(): use actual profile name
... instead of profile_data\[profile\]\[profile\]\['name'\] which is not always correct.

Note: setting 'name' will be fixed in a later commit/MR, but don't hold your breath for it ;-)

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/733
Acked-by: John Johansen <john.johansen@canonical.com>
2021-04-08 06:53:25 +00:00
John Johansen
09dfbb97e5 Merge logparser: don't return empty AUDIT section
AUDIT events get skipped when parsing the log (they are not relevant for updating a profile), therefore stop returning an always-empty AUDIT section when reading the log.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/731
Acked-by: John Johansen <john.johansen@canonical.com>
2021-04-08 06:50:06 +00:00
Christian Boltz
f9078c4b01
read_profile(): use actual profile name
... instead of profile_data[profile][profile]['name'] which is not
always correct.

Note: setting 'name' will be fixed in a later commit, but don't hold
your breath for it ;-)
2021-04-04 14:04:48 +02:00
Christian Boltz
f931daa771
ask_conflict_mode: drop superfluous parameters
profile and hat are not needed by this function.
2021-04-02 17:58:41 +02:00
Christian Boltz
9aa70ab710
logparser: don't return empty AUDIT section
AUDIT events get skipped when parsing the log (they are not relevant for
updating a profile), therefore stop returning an always-empty AUDIT
section when reading the log.
2021-04-02 17:37:23 +02:00
Christian Boltz
aaad284d8d
Handle include rules in match_line_against_rule_classes()
This needed several changes because so far data for all includes was
stored in include[]. However, preamble data for everything else gets
stored in active_profiles (and with this commit, preamble includes also
get stored in active_profiles).

The needed changes to store preamble includes in active_profiles are:

* include_list_recursive(): add and honor in_preamble flag

* add this flag at several places calling include_list_recursive() for
  a preamble

* parse_profile_data(): call active_profiles.init_file() for all files.
  Before, empty/comment-only files weren't (indirectly) added to
  active_profiles because none of the add_$ruletype functions was
  called, which could lead to KeyErrors for comment-only preamble include
  files (prevented by the now-obsolete and removed check in
  get_all_merged_variables()).
2021-03-08 01:15:12 +01:00
Christian Boltz
3448127ca6
Also parse preamble rules with match_line_against_rule_classes()
For this, we have to hand over in_preamble, and to do a slightly
different handling for preamble and profile rules.

For adding preamble rules to ProfileList, add a add_rule() function that
accepts the rule type as parameter. This is easier than using one of the
add_$type functions depending on the rule type.

With this change, match_line_against_rule_classes() handles nearly all
rule types that have a *Rule class, with the exception of include rules
which need some additional work.
2021-03-07 21:33:36 +01:00
Christian Boltz
accc380326
Add in_preamble parameter to profile loading/parsing functions
in_preamble keeps track of the current parsing position.

It's True while parsing the preamble of a profile file, and when loading
an include file that is included in the preamble (typically tunables/*).

While inside a profile or parsing abstractions/*, it is False.

This commit only hands the information around and keeps in_preamble
updated, but it doesn't really get used yet.

Also adjust the tests to hand over the additional parameter to
parse_profile_data().
2021-03-07 21:33:36 +01:00
Christian Boltz
d442620102
Add match_line_against_rule_classes()
... to handle parsing of lines that are managed with a *Rule

This needs 'ruletypes' to also know about *Rule, not only about
*Ruleset.

Also switch over handling of most rules that live inside a profile from
parse_profile() to match_line_against_rule_classes() to make
parse_profile() more readable.

Exceptions are:
- include and abi, which can also exist in the preamble
- file rules, because they'd need to be handled later (after variable
  definitions)
2021-03-07 21:33:36 +01:00
Christian Boltz
4c77f7193b
Use parse() instead of _parse() in LogprofHeaderTest
The *LogprofHeaderTest accidently used the private _parse() insteaf of
the official parse().
2021-03-07 18:28:27 +01:00
Christian Boltz
d0693b09b5
Drop is_skippable_dir()
Since loadincludes() now only loads a specified list of subdirectories,
we no longer need a directory blacklist.

The only possibly remaining part are .git subdirectories (for example
tunables/.git or abstractions/.git). Since it's very unlikely that
someone would have only a subdirectory of /etc/apparmor.d/ in git, drop
that check.
2021-02-25 13:08:22 +01:00
Christian Boltz
bb5ca91e7e
loadincludes(): only load tunables and abstractions
Also split out parts of the function into loadincludes_dir() to keep the
code readable.

Note: This change might affect the list of includes proposed by
aa-logprof.
2021-02-25 12:59:12 +01:00
Christian Boltz
0f21ca6173 Merge branch 'cboltz-parse-profile-start' into 'master'
split off parse_profile_start_to_storage() from parse_profile_data()

See merge request apparmor/apparmor!710

Acked-by: Steve Beattie <steve.beattie@canonical.com>
2021-02-25 11:20:52 +00:00
Christian Boltz
0f1d5fdba4 Merge branch 'cboltz-preamble-ruletypes' into 'master'
profile_list: add preamble_ruletypes

See merge request apparmor/apparmor!708

Acked-by: Steve Beattie <steve.beattie@canonical.com>
2021-02-25 11:20:04 +00:00
Christian Boltz
f7a365f89f
Simplify handling of in_contained_hat
in_contained_hat is needed to know if we are already in a profile or
not. (Simply checking if we are in a hat doesn't work, because something
like "profile foo//bar" will set profile and hat at once, and later
(wrongfully) expect another "}".

However, the way how this variable was set became too complicated.

To simplify the code, set in_contained_hat directly in
parse_profile_data() RE_PROFILE_START instead of returning it via
parse_profile_start() and parse_profile_start_to_storage()

Since this change removes a return value from two functions, also adjust
the tests accordingly.
2021-02-14 23:08:38 +01:00
Christian Boltz
7cfda2772d
split off parse_profile_start_to_storage() from parse_profile_data()
parse_profile_start_to_storage() converts the result of
parse_profile_start() into a ProfileStorage object.

No functional change, but parse_profile_data() becomes more readable.

Also extend tests to cover parse_profile_start_to_storage().
2021-02-14 23:08:30 +01:00
Christian Boltz
7e5c02e3d4
Rewrite parse_profile_start() tests to use tests[] array
No functional changes, just more readable.
2021-02-14 19:26:40 +01:00
Christian Boltz
33f2b3692b
Un-hardcode rule list in ProfileList delete_preamble_duplicates()
Note that before this change, boolean variable definitions were not
checked in delete_preamble_duplicates().
2021-02-14 18:26:24 +01:00
Christian Boltz
718fa5b9da
profile_list: add preamble_ruletypes
... instead of keeping the list in ProfileList init_file()
2021-02-14 18:26:18 +01:00
Christian Boltz
69c6ac3073
Add make coverage-regression
... to find regressions or improvements in the python code coverage.

`make coverage-regression` will error out if a file looses its 100%
coverage, or if a file improved to 100% coverage.

Other coverage changes (for example 45% -> 47%) will be ignored.
2021-01-10 20:53:44 +01:00
John Johansen
d86b7acd31 Merge Enable minitools tests
To get them running in the CI,

* call them with `--configdir ./`
* skip testing `aa-unconfined` if securityfs is not available

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/696
Acked-by: John Johansen <john.johansen@canonical.com>
2021-01-10 11:43:18 +00:00
John Johansen
7e02f254a3 Merge add re_match_include_parse() test with invalid rule name
... to increase test coverity of regex.py to 100%.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/695
Acked-by: John Johansen <john.johansen@canonical.com>
2021-01-10 11:38:57 +00:00
John Johansen
cc28ebaab4 Merge Add missing test for ProfileList add_alias()
... to ensure that it errors out if a wrong parameter type is given.

This also increases the test coverage of ProfileList to 100%.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/694
Acked-by: John Johansen <john.johansen@canonical.com>
2021-01-10 11:35:03 +00:00
John Johansen
ca344a3601 Merge Add and use BooleanRule and BooleanRuleset classes to handle boolean variable definitions
Add the BooleanRule and BooleanRuleset classes, add handling of boolean variable definitions in ProfileList and adjust `parse_profile_data()` to use BooleanRule. As usual, add tests for the added code.

See the individual commits for the details.

Note that this MR is also a bugfix - the previous code in (3.0 and master) saved boolean variables at a wrong place, and they were silently lost when writing the profile.
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/693
Acked-by: John Johansen <john.johansen@canonical.com>
2021-01-10 11:33:01 +00:00
Christian Boltz
2e6bdc0b64
skip testing aa-unconfined if securityfs is not available 2020-12-25 21:24:42 +01:00
Christian Boltz
8d422ff66e
Test minitools with --configdir ./ 2020-12-25 21:07:18 +01:00
Christian Boltz
70bbc321db
Enable minitools tests in 'make check`
... by renaming them to test-*.py
2020-12-25 20:50:44 +01:00
Christian Boltz
c3d3203a60
add re_match_include_parse() test with invalid rule name
... to increase test coverity of regex.py to 100%.
2020-12-25 19:57:20 +01:00
Christian Boltz
32b11c0375
Add missing test for ProfileList add_alias()
... to ensure that it errors out if a wrong parameter type is given.

This also increases the test coverage of ProfileList to 100%.
2020-12-25 18:48:27 +01:00
Christian Boltz
f7e6f795c3
parse_profile_data(): Use BooleanRule
... and save rules at the right place (ProfileList) where they actually
get written when writing the profile.

This is also a bugfix - the previous code saved boolean variables at a
wrong place, and they were silently lost when writing the profile.

Extend cleanprof_test.{in,out} to ensure that this doesn't break again.

Also remove boolean_bad_[2-4] from the test-parser-simple-tests.py
exception_not_raised list because these test profiles now get correctly
detected as invalid.
2020-12-25 18:03:41 +01:00
Christian Boltz
a108934091
ProfileList: add handling of boolean variable definitions
This means adding the add_boolean() function and handling boolean
variables in get_clean() and get_raw().

Also add some tests to cover the added code.
2020-12-25 18:03:38 +01:00
Christian Boltz
3f11eebc17
Add BooleanRule and BooleanRuleset
These two classes are meant to handle the definition of boolean rules
like `$foo = true`.

Also extend RE_PROFILE_BOOLEAN to provide named matches.

As usual, add tests for the new classes.
2020-12-25 18:03:33 +01:00
Christian Boltz
2cbd0d94be
Fix comment in split_name() tests 2020-12-25 13:24:42 +01:00
Christian Boltz
c421fcd38a
apparmor.vim: add support for abi rules 2020-12-09 22:44:33 +01:00
Steve Beattie
05547ac8f6
treewide: spelling fix in logprof.conf and profile comments
Kept separate from other fixes because conf file changes can cause
problems for packagers.

Signed-off-by: Steve Beattie <steve.beattie@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/687
2020-12-01 12:47:23 -08:00
Steve Beattie
8b708d3b45
treewide: spelling/typo fixes in code strings
Fix spelling errors in code strings. Some strings are translatable.
This fixes are potentially user visible.

Signed-off-by: Steve Beattie <steve.beattie@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/687
2020-12-01 12:47:18 -08:00
Steve Beattie
461d9c2294
treewide: spelling/typo fixes in comments and docs
With the exception of the documentation fixes, these should all be
invisible to users.

Signed-off-by: Steve Beattie <steve.beattie@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/687
2020-12-01 12:47:11 -08:00
John Johansen
7c88f02d6a aa-notify: don't crash if the logfile is not present due to rotation
If aa-notify races file rotation it may crash with a trace back to
the log file being removed before the new one is moved into place.

    Traceback (most recent call last):
       File "/usr/sbin/aa-notify", line 570, in <module>
         main()
       File "/usr/sbin/aa-notify", line 533, in main
          for message in notify_about_new_entries(logfile, args.wait):
       File "/usr/sbin/aa-notify", line 145, in notify_about_new_entries
         for event in follow_apparmor_events(logfile, wait):
       File "/usr/sbin/aa-notify", line 236, in follow_apparmor_events
         if os.stat(logfile).st_ino != log_inode:
    FileNotFoundError: [Errno 2] No such file or directory: '/var/log/audit/audit.log'

If we hit this situation sleep and then retry opening the logfile.

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/130
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/688
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2020-11-30 05:19:28 -08:00
John Johansen
e0ef309542 Merge create_new_profile(): check if abstractions exist
... instead of blindly adding them to the profile, and later crash (and/or cause parser errors) because they don't exist.

Fixes: https://bugzilla.opensuse.org/show_bug.cgi?id=1178527
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/683
Acked-by: John Johansen <john.johansen@canonical.com>
2020-11-28 13:19:13 +00:00
John Johansen
44aa30cf2a Merge aa-autodep: load abstractions on start
So far, aa-autodep "accidently" loaded the abstractions when parsing the existing profiles. Obviously, this only worked if there is at least one profile in the active or extra profile directory.

Without any existing profiles, aa-autodep crashed with KeyError: '/tmp/apparmor.d/abstractions/base'

Prevent this crash by explicitely loading the abstractions on start.

Fixes: https://bugzilla.opensuse.org/show_bug.cgi?id=1178527
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/682
Acked-by: John Johansen <john.johansen@canonical.com>
2020-11-28 13:10:18 +00:00
John Johansen
0af37358e6 Merge Add --configdir to all aa-* utils
Since this option is mostly meant for testing, it will not show up in `--help`.

`aa-notify` was the only tool that honored the `__AA_CONFDIR` env variable. Drop it in favor of the `--configdir` option.

Note: Since we now pass `confdir=` to `init_aa()` (in most cases `None`), setting the default needs to be moved inside the function.

Also use `--configdir` in the tests.

See the individual commits for details.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/670
Acked-by: John Johansen <john.johansen@canonical.com>
2020-11-28 13:02:06 +00:00
Christian Boltz
dfd7c245cd
create_new_profile(): check if abstractions exist
... instead of blindly adding them to the profile, and later crash
(and/or cause parser errors) because they don't exist.

References: https://bugzilla.opensuse.org/show_bug.cgi?id=1178527#c1 [1]
2020-11-08 15:26:51 +01:00
Christian Boltz
f6b3de7116
aa-autodep: load abstractions on start
So far, aa-autodep "accidently" loaded the abstractions when parsing the
existing profiles. Obviously, this only worked if there is at least one
profile in the active or extra profile directory.

Without any existing profiles, aa-autodep crashed with
KeyError: '/tmp/apparmor.d/abstractions/base'

Prevent this crash by explicitely loading the abstractions on start.

References: https://bugzilla.opensuse.org/show_bug.cgi?id=1178527#c1 [1]
2020-11-08 14:41:33 +01:00