Commit graph

1898 commits

Author SHA1 Message Date
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
Christian Boltz
7cf54f2cd8
Fix hotkey conflict in utils de.po, id.po and sv.po 2020-10-31 21:59:32 +01:00
Christian Boltz
07bd11390e
Check hotkey conflicts case-insensitive
This is needed to catch conflicts between uppercase and lowercase
hotkeys of the same letter, as seen with `(B)enannt` and `A(b)lehnen` in
the german utils translations.
2020-10-31 20:21:29 +01:00
Christian Boltz
f3a816d6a5
use aa-notify --configdir in test-aa-notify.py 2020-10-29 21:24:15 +01:00
Christian Boltz
87eec0d62d
use aa-logprof --configdir ../utils in profile testsuite
(except if USE_SYSTEM is given)

This also needs an additional parser path in utils/test/logprof.conf,
which then needs an update in test-config.py.
2020-10-29 21:24:15 +01:00
Christian Boltz
ab6e9b2de2
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.
It still does if --configdir is not given.

Note: Since we now pass confdir= to init_aa() (in most cases None),
setting the default needs to be moved inside the function.
2020-10-29 21:24:15 +01:00
John Johansen
15dc06248c utils: fix make -C profiles check-logprof fails
On arch
  make -C profiles check-logprof

fails with
  *** Checking profiles from ./apparmor.d against logprof

  ERROR: Can't find AppArmor profiles in /etc/apparmor.d
  make: *** [Makefile:113: check-logprof] Error 1
  make: Leaving directory '/build/apparmor/src/apparmor-2.13.3/profiles'

because /etc/apparmor.d/ is not available in the build environment
and aa-logprofs --dir argument, is not being passed to init_aa()
but used to update profiles_dir after the fact.

Fix this by passing profiledir as an argument to init_aa()

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/36
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/663
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2020-10-22 14:56:09 -07:00
John Johansen
8ea7630b6d aa-notify: Stop aa-notify from exit after 100s of polling
When run with the -p flag, aa-notify works fine for 100 seconds and then it exits.
I suspect that the issue arises from the following check on line 259 in utils/aa-notify
if debug_logger.debug_level <= 10 and int(time.time()) - start_time > 100:
    debug_logger.debug('Debug mode detected: aborting notification emitter after 100 seconds.')
    sys.exit(0)
together with line 301 in utils/apparmor/common.py which initializes debug_logger.debug_level to logging.DEBUG which has the numerical value 10.
A simple solution might be to just remove the check as I'm not quit sure why one would want aa-notify to exit when run in debug mode in the first place.
Alternatively, one could check against debug_logger.debugging (initialized to False) or change the initialization of debug_logger.debug_level to something else, but I don't know how that would affect other consumers of utils/apparmor/common.py.

For now just add dbugger_logger.debugging as an additional check as the
reason for timing out after 100s during debugging are unclear.

Suggested-by: vicvbcun
Fixes: https://gitlab.com/apparmor/apparmor/-/issues/126
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/660
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Otto Kekäläinen <otto@kekalainen.net>
2020-10-21 16:59:28 -07:00
John Johansen
43eb54d13c utils: split linting with PYFLAKES into a separate target.
This a step towards addressing the linting of the utils causing
problems in a build vs dev environment. See
  https://gitlab.com/apparmor/apparmor/-/issues/121

Split off linting with PYFLAKES into its own target as a step towards
making the running of the lint checks as a configuration option.

https://gitlab.com/apparmor/apparmor/-/merge_requests/662
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2020-10-21 16:55:41 -07:00
Christian Boltz
2c2dbdc3a3
Add CAP_CHECKPOINT_RESTORE to severity.db 2020-10-14 14:01:55 +02:00
David Runge
e0200b1b16
Skip test if it can not access /var/log/wtmp
utils/test/test-aa-notify.py:
Change `AANotifyTest.test_entries_since_login()` to be decorated by a
`skipUnless()` checking for existence of **/var/log/wtmp** (similar to
`AANotifyTest.test_entries_since_login_verbose()`).
The test otherwise fails trying to access /var/log/wtmp in environments
where the file is not available.

Fixes #120
2020-10-02 23:58:53 +02:00
Steve Beattie
74df38e284 parser sanity tests: add more mount options tests
Attempt to get clarity on what is valid syntax for mount options and
fstype options.

Note that simple_tests/mount/bad_opt_27.sd is marked TODO, as the
parser accepts it but should not.

Also mark the tests as expecting to fail to raise an exception by the
python utils.

Signed-off-by: Steve Beattie <steve.beattie@canonical.com>
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/607
Signed-off-by: John Johansen <john.johansen@canonical.com>
2020-09-29 04:39:16 -07:00
John Johansen
0a52cf81e3 parser: add support for autobind sockets
af_unix allows for sockets to be bound to a name that is autogenerated.
Currently this type of binding is only supported by a very generic
rule.

  unix (bind) type=dgram,

but this allows both sockets with specified names and anonymous
sockets. Extend unix rule syntax to support specifying just an
auto bind socket by specifying addr=auto

eg.

  unix (bind) addr=auto,

It is important to note that addr=auto only works for the bind
permission as once the socket is bound to an autogenerated address,
the addr with have a valid unique value that can be matched against
with a regular

  addr=@name

expression

Fixes: https://bugs.launchpad.net/apparmor/+bug/1867216
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/521
Signed-off-by: John Johansen <john.johansen@canonical.com>
2020-09-29 03:34:56 -07:00
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