Commit graph

1384 commits

Author SHA1 Message Date
Christian Boltz
aa1e315e77
Add tests for link rules to test-file.py 2019-04-22 23:39:47 +02:00
Christian Boltz
f176baf904
Add support for link rules to FileRule 2019-04-22 23:39:47 +02:00
Christian Boltz
a477a06847
Extend RE_PROFILE_FILE_ENTRY to cover link rules 2019-04-22 23:39:47 +02:00
Christian Boltz
ee2185f4b6 Merge branch 'cboltz-link-owner' into 'master'
Add testcases for 'owner link' rules

See merge request apparmor/apparmor!369

Acked-by: Eric Chiang <ericchiang@google.com>
2019-04-22 17:59:06 +00:00
Christian Boltz
7d95e2658e Merge branch 'cboltz-typo' into 'master'
Fix typo in set_json_mode() comment

See merge request apparmor/apparmor!364

Acked-by: Eric Chiang <ericchiang@google.com>
2019-04-22 17:58:15 +00:00
Christian Boltz
0facb1598c
Add testcases for 'owner link' rules
.. and document that the tools don't support them yet
2019-04-22 13:36:33 +02:00
Christian Boltz
1d19bb7110
parse_profile_start: test with un-named profile
Also update the comment in _parse to match the updated return values.
2019-04-22 12:38:38 +02:00
Otto Kekäläinen
d4cab56ac7 aa-notify: Use fixed output width in tests so results always look same 2019-04-21 18:37:10 +03:00
Otto Kekäläinen
d5990da72a aa-notify: Use AATest class in tests since possible now with Python 2019-04-21 18:37:10 +03:00
Otto Kekäläinen
a74d7cf51c Re-implement aa-notify in Python (Closes: #16)
- Code layout based on aa-genprof example
- Extend Python dependencies to cover new need by aa-notify
- Update documentation after aa-notify is no longer in Perl
2019-04-21 18:37:10 +03:00
Otto Kekäläinen
3a1eec49d4 Add tests for shared aa library functions used in command line scripts 2019-04-21 18:37:10 +03:00
Otto Kekäläinen
3c7e1668bd aa.py: Indicate permission error if log file is found but cannot be opened 2019-04-21 18:35:33 +03:00
Otto Kekäläinen
455c441357 aa.py: Ensure there is always a fallback falue for the logfile location
Related to #22.

Fixes error message:

  Traceback (most recent call last):
    File "./aa-notify", line 523, in <module>
      main()
    File "./aa-notify", line 399, in main
      aa.set_logfile(args.file)
    File "/home/otto/koodia/apparmor/utils/apparmor/aa.py", line 1762,
    in set_logfile
      print(conf.find_first_file(cfg['settings']['logfiles']))
    File "/usr/lib/python3.6/configparser.py", line 1233, in __getitem__
      raise KeyError(key)
  KeyError: 'logfiles'
2019-04-20 23:34:04 +03:00
Christian Boltz
7153eb09d9
Fix typo in set_json_mode() comment 2019-04-19 22:25:30 +02:00
Otto Kekäläinen
fdd13db13b Add tests for shared aa library functions used in command line scripts 2019-04-19 17:29:26 +03:00
Christian Boltz
9feebc4363
Add some tests for complex profile names
Add some tests with the complex profile name (including alternations and
wildcards) to ensure we don't break such cases in the future.

These tests are based on the log from the (invalid) bugreport
https://gitlab.com/apparmor/apparmor/issues/26
2019-03-31 17:34:55 +02:00
John Johansen
52b3fc220e Adjust tests to match base abstraction update.
Since !345 the set of permissions that are granted (get_file_perms_2)
or suggested (propose_file_rules) has changed. These new sets are
expected due to the changes brought by this MR, so let's adjust
the test suite accordingly.

PR: https://gitlab.com/apparmor/apparmor/merge_requests/358
Acked-by: Christian Boltz <apparmor@cboltz.de>
Signed-off-by: John Johansen <john.johansen@canonical.com>
2019-03-29 07:56:23 +00:00
intrigeri
0170e98f9c Adjust tests to match base abstraction update.
Since !345 the set of permissions that are granted (get_file_perms_2)
or suggested (propose_file_rules) has changed. These new sets are
expected due to the changes brought by this MR, so let's adjust
the test suite accordingly.
2019-03-24 14:45:03 +00:00
John Johansen
cfe20d2b63 Add support for profiles with xattrs matching
Add userland support for matching based on extended file attributes. This
leverages DFA based matching already in the kernel:

https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=8e51f908
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=73f488cd

Matching is exposed via flags on the profile:

    /usr/bin/* xattrs=(user.foo=bar user.bar=foo) {
        # ...
    }

xattr values are appended to the existing xmatch via a null transition.

    $ echo '/usr/bin/* xattrs=(user.foo=foo user.bar=bar) {}' | \
        ./parser/apparmor_parser -QT -D expr-tree
    DFA: Expression Tree
    /usr/bin/[^\0000/]([^\0000/])*(\0000bar)?(\0000foo)?< 0x1>
    DFA: Expression Tree
    (\a|(\n|(\0002|\t)))< 0x4>

Tested manually on a 4.19 kernel via QEMU+KVM.

TODO:

  * ~~Add regression tests~~ (EDIT: done)
  * ~~EDIT: add support in the tools~~ (EDIT: done)

Questions for reviewers:

  * ~~parser/libapparmor: regex construction probably needs cleaning up~~ (EDIT: done)
  * ~~parser/parser_regex.c: confused what xmatch length is for~~ (EDIT: done)

/cc @mjg59

PR: https://gitlab.com/apparmor/apparmor/merge_requests/270
Signed-off-by: John Johansen <john.johansen@canonical.com>
2019-03-21 08:12:07 +00:00
Christian Boltz
49849ed7a3
update network keyword list in utils and add test
The tools also have a list of network keywords, update it:
- add xdp and qipcrtr
- move ib and mpls to match the kernel order

Also add a test to ensure that (at least) the keywords provided by the
running kernel are listed in network_domain_keywords.
2019-03-16 12:52:37 +01:00
Eric Chiang
a42fd8c6f4 parser: add support for matching based on extended file attributes
Add userland support for matching based on extended file attributes.
This leverages DFA based matching already in the kernel:

https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=8e51f908
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=73f488cd

Matching is exposed via flags on the profile:

  /usr/bin/* xattrs=(user.foo=bar user.bar=**) {
      # ...
  }

Profiles list the set of extended attributes that a file MUST have, and
a regex to match the value of that extended attributes. Additional
extended attributes on the file don't effect the match.

Signed-off-by: Eric Chiang <ericchiang@google.com>
2019-03-14 10:47:54 -07:00
Christian Boltz
62abfe38e8
Replace deprecated assertEquals with assertEqual
assertEquals is deprecated since Python 2.7 and 3.2.
2019-02-26 21:27:00 +01:00
Christian Boltz
f729391deb Merge branch 'cboltz-test-setup_aa' into 'master'
add an example for setup_aa() to test-example.py

See merge request apparmor/apparmor!330

Acked-by: Steve Beattie <steve@nxnw.org>
2019-02-13 22:42:19 +00:00
Steve Beattie
45c26214cc
utils: fixup test-aa.py tests that fail due to usr-merge
Signed-off-by: Steve Beattie <steve.beattie@canonical.com>
2019-02-12 23:17:59 -08:00
Christian Boltz
d6bc00b35b
add an example for setup_aa() to test-example.py
(commented out, but having it documented is helpful nevertheless)
2019-02-10 23:24:29 +01:00
Otto Kekäläinen
aaf7d0a27a Skip aa-notify tests if their requirements for running are missing
When run locally on a development machine or in production, the full test
is likely to run. However inside a CI system container 'last' might fail
to show last login or there might not be access to kern.log and the test
will automatically skip those without failing the whole test suite.
2019-02-09 13:50:02 +02:00
Otto Kekäläinen
f05a464369 Add tests for aa-notify
This will help ensure the future rewrite of aa-notify from Perl to Python
is less likely to introduce regressions. Tests run the command line utility
via a subprocess so it does not matter that the tests are in Python but
the aa-notify utility is in Perl (for now).
2019-02-09 13:12:01 +02:00
Otto Kekäläinen
c04a0cadef Extend common DebugLogger with option to log to stderr
This makes it possible for e.g. command line tools to have the --debug
option and when invoked print the existing debug messages directly to
stderr so the user running the command can see them.
2019-02-07 01:05:32 +02:00
Otto Kekäläinen
707ef17923 Rewrite aa-decode to use inline Python instead of to-be-deprecated Perl 2019-02-05 00:20:47 +02:00
Otto Kekäläinen
a3ae271d1e Bugfix aa-decode test: Get stderr correctly, don't ignore stderr contents
Defining 'stderr = subprocess.STDOUT' as a default value for function
did not work and the 'stderr' was always empty, thus also 'outerr' was
always empty and not standard error contents was ever considered in any
way.

Best in fact was to remove excess function arguments as they were not even
used and replace it with a simpler and less error prone structure.

Even after reading 'stderr' correctly it did not help much as all tests
used 'assertIn' which ignored excess output. Better replace the normal
output with the error output if there ever was something, since stderr
is most likely a serious thing and tests should stop on it.
2019-02-05 00:15:13 +02:00
Otto Kekäläinen
eb378f46d5 Clean up and document AppArmor UI module
- Add documentation stub for what the UI module is and how JSON works
- Make indentation consistent
- Use pythonic 'if' clauses
- Add two spaces after function definitions (Python style)
2019-02-04 20:14:58 +02:00
John Johansen
803cb5558f drop failing corner-case check in logparser.py
aa-logprof errors out if it hits a log event for a non-existing profile
while a profile file with the default filename for that non-existing
profile exists. This can for example happen after adding a profile name
to a profile if audit.log still contains events for the attachment-based
profile name.

Since we ignore log events for non-existing profiles in general, drop
the code for the special case "but a file matching the default filename
for that non-existing profile exists" and also silently ignore events
for this very special non-existing profile.

Also drop the now unused function get_profile_filename()

References: https://bugzilla.opensuse.org/show_bug.cgi?id=1120472

PR: https://gitlab.com/apparmor/apparmor/merge_requests/296
Signed-off-by: John Johansen <john.johansen@canonical.com>
2019-01-24 10:26:05 +00:00
Eric Chiang
cb8c3377ba *: ensure make apparmor_parser is cached
This change updates parser/Makefile to respect target dependencies and
not rebuild apparmor_parser if nothing's changed. The goal is to allow
cross-compiled tests #17 to run on a target system without the tests
attempting to rebuild the parser.

Two changes were made:

* Generate af_names.h in a script so the script timestamp is compared.
* Use FORCE instead of PHONY for libapparmor_re/libapparmor_re.a

Changes to list_af_names are intended to exactly replicate the old
behavior.

Signed-off-by: Eric Chiang <ericchiang@google.com>
2019-01-22 15:30:51 -08:00
Christian Boltz
ed692337a5
Drop get_profile_filename() from logparser.py
This function is unused since the last commit.
2019-01-02 20:42:57 +01:00
Christian Boltz
03ea5b82b7
drop failing corner-case check in logparser.py
aa-logprof errors out if it hits a log event for a non-existing profile
while a profile file with the default filename for that non-existing
profile exists. This can for example happen after adding a profile name
to a profile if audit.log still contains events for the attachment-based
profile name.

Since we ignore log events for non-existing profiles in general, drop
the code for the special case "but a file matching the default filename
for that non-existing profile exists" and also silently ignore events
for this very special non-existing profile.

References: https://bugzilla.opensuse.org/show_bug.cgi?id=1120472
2019-01-02 20:30:54 +01:00
Christian Boltz
274132ee6c
Add a test profile with xattrs=... to cleanprof tests 2018-12-09 20:54:41 +01:00
John Johansen
718744b122 Merge branch 'cboltz-re_flags' into 'master'
Split out RE_FLAGS

... instead of having it duplicated in RE_PROFILE_HAT_DEF and RE_PROFILE_START.

Note that the flags=... handling in RE_PROFILE_HAT_DEF was more/too
strict (for example it didn't allow whitespace around the "="), so this
change also qualifies as a little bugfix.

PR: https://gitlab.com/apparmor/apparmor/merge_requests/287
Acked-by: John Johansen <john.johansen@canonical.com>
2018-12-08 08:37:18 +00:00
John Johansen
8441a42c51 Merge branch 'cboltz-drop-activate_repo_profiles' into 'master'
Drop unused activate_repo_profiles()

PR: https://gitlab.com/apparmor/apparmor/merge_requests/274
Acked-by: John Johansen <john.johansen@canonical.com>
2018-12-08 06:55:49 +00:00
Christian Boltz
0d830fae31
Split out RE_FLAGS
... instead of having it duplicated in RE_PROFILE_HAT_DEF and
RE_PROFILE_START.

Note that the flags=... handling in RE_PROFILE_HAT_DEF was more/too
strict (for example it didn't allow whitespace around the "="), so this
change also qualifies as a little bugfix.
2018-12-06 22:02:01 +01:00
Christian Boltz
aa1e8bd55d Merge branch 'utils-xattrs' into 'master'
utils: add support to tools for profiles with xattrs

See merge request apparmor/apparmor!285

Acked-by: Christian Boltz <apparmor@cboltz.de>
2018-12-06 13:15:49 +00:00
Eric Chiang
fde015435a utils: add support to tools for profiles with xattrs
Signed-off-by: Eric Chiang <ericchiang@google.com>
2018-12-05 19:22:32 -08:00
Christian Boltz
8b4e76a7d5
Fix viewing a local inactive profile in aa-genprof
aa-genprof checks if one of the profiles in the extra profile dir
matches the binary, and proposes to use that profile as a starting
point.

Since 4d722f1839 the "(V)iew profile"
option to display the proposed profile was broken.

The easiest fix is to remember the filename in the extras directory, and
display the file from there.

Sidenote: when choosing to use the extra profile, it gets written to
disk without any problems, so this bug really only affected "(V)iew
profile" to preview the proposed extra profile.
2018-11-18 21:35:07 +01:00
Christian Boltz
04c91d5256
Drop unused activate_repo_profiles() 2018-11-18 17:20:30 +01:00
Christian Boltz
4efff35bf8
parse_profile_data(): Ensure last line in a profile is valid
'lastline' gets merged into 'line' (and reset to None) when reading the
next line. If 'lastline' isn't empty after reading the whole profile,
this means there's something unparseable at the end of the profile,
therefore parse_profile_data() should error out.

Also remove some simple_tests testcases from the 'exception_not_raised'
list - they only didn't raise the exception because the invalid rule was
the last line in the affected profile.

Thanks to Eric Chiang for accidently (and maybe even unnoticedly ;-)
discovering this bug while adding some xattr testcases that surprisingly
didn't fail in the tools.
2018-11-13 17:59:40 +01:00
Christian Boltz
273e45a4af Merge branch 'cboltz-profile-list' into 'master'
Replace "existing_profiles" & fix minitools for named profiles

See merge request apparmor/apparmor!249

Acked-by: John Johansen <john.johansen@canonical.com>
2018-11-11 13:17:00 +00:00
John Johansen
94ff870f78 remove subdomainfs support
It has been over 10 years since transition from subdomainfs to
using securityfs. Lets drop this deprecated code.

PR: https://gitlab.com/apparmor/apparmor/merge_requests/258
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: seth.arnold@canonical.com
2018-11-08 18:23:21 -08:00
Christian Boltz
4d722f1839
Replace existing_profiles & fix minitools for named profiles
Technical stuff first:

Replace existing_profiles (a dict with the filenames for both active and
inactive profiles) with active_profiles and extra_profiles which are
ProfileList()s and store the active profiles and those in the extra
directory separately. Thanks to ProfileList, now also the relation
between attachments and filenames is easily available.

Also replace all usage of existing_profiles with active_profiles and
extra_profiles, and adjust it to the ProfileList syntax everywhere.

With this change, several bugs in aa-complain and the other minitools
get fixed:
- aa-complain etc. never found profiles that have a profile name
  (the attachment wasn't checked)
- even if the profile name was given as parameter to aa-complain, it
  first did "which $parameter" so it never matched on named profiles
- profile names with alternations (without attachment specification)
  also never matched because the old code didn't use AARE.

References: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=882047#92
(search for "As usual" ;-)

Just for completeness - the matching still doesn't honor/expand
variables in the profile name.
2018-10-23 00:28:37 +02:00
Christian Boltz
789c4658e2
add ProfileList class to store list of profiles
ProfileList is meant to store the list of profiles (both name and
attachment) and in which files they live.

Also add unittests to make sure everything works as expected.
2018-10-23 00:28:37 +02:00
Christian Boltz
8809218ac8
Move updating existing_profiles out of parse_profile_data()
parse_profile_data() returns the parsed profiles, but writes to
existing_profiles directly.

read_profiles() calls parse_profile_data() and already handles adding
the parsed profiles to aa, original_aa or extras, which means updating
existing_profiles there is a much better place.

This commit also includes a hidden change: Previously, when parsing
include files, they were also added to existing_profiles. This is
superfluous, only real profiles need to be stored there.
2018-10-23 00:28:37 +02:00
Christian Boltz
a6b8d14908
split off get_new_profile_filename()
... and call it from get_profile_filename_* if get_new is True
(= always with the current code)
2018-10-23 00:28:37 +02:00