Commit graph

1213 commits

Author SHA1 Message Date
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
Christian Boltz
ec741424f8
split get_profile_filename into .._from_profile_name and .._from_attachment
Split get_profile_filename() into
- get_profile_filename_from_profile_name() (parameter: a profile name)
- get_profile_filename_from_attachment() (parameter: an attachment)

Currently both functions call get_profile_filename_orig() (formerly
get_profile_filename()) so the behaviour doesn't change yet.

The most important part of this commit is changing all
get_profile_filename() calls to use one of the new functions to make
clear if they specify a profile or an attachment/executable as
parameter.

As promised, the is_attachment parameter starts to get used in this
patch ;-)

Note: The get_new parameter (which I'll explain in the patch actually
using it) is set to True in all calls to the new functions.
The long term plan is to get rid of it in most cases (hence defaulting
to False), but that will need more testing.
2018-10-23 00:28:37 +02:00
Christian Boltz
bc783372b8
Add is_attachment parameter to write_profile
The minitools call write_profile(), write_profile_feedback_ui() and
serialize_profile() with the _attachment_ as parameter.

However, aa-logprof etc. call them with the _profile name_ as parameter.

This patch adds an is_attachment parameter to write_profile() and
write_profile_feedback_ui(). It also passes it through to
serialize_profile() via the options parameter.

If is_attachment is True, the parameter will be handled as attachment,
otherwise it is expected to be a profile name.

tools.py gets changed to set is_attachment to True when calling the
functions listed above to make clear that the parameter is an attachment.

Note: This patch only adds the is_attachment parameter/option, but
doesn't change any behaviour. That will happen in the next patch.
2018-10-23 00:18:04 +02:00
nl6720
2209e09aef aa-notify man page: update user's configuration file path
Signed-off-by: nl6720 <nl6720@gmail.com>
2018-10-15 16:44:00 +03:00
Christian Boltz
5c54f66279
Add most abi/bad_*.sd tests to "exception not raised" list
Interestingly, abi/bad_6.sd is detected as invalid, and therefore not
added to the list.
2018-10-13 20:23:57 +02:00
Christian Boltz
bc492533cc
Fix aa-mergeprof crash caused by accidentially initialzed hat
Hasher causes some fun in aa-mergeprof: If the profile in
/etc/apparmor.d/ has a hat or subprofile that doesn't exist in the
to-be-merged profile, aa-mergeprof crashes. This is caused by reading
self.other.aa[program][hat]['include'] which accidently "creates" that
profile inside the aa hasher as empty hasher (instead of ProfileStorage).

Later, the code loops over self.other.aa[profile].keys(), expects
everything to be ProfileStorage, and explodes [1] when for example
trying to run .delete_duplicates on the hasher (which obviously doesn't
provide this method).

This patch adds checks to all self.other.aa accesses in
CleanProf.remove_duplicate_rules() to avoid accidently creating new keys
in the hasher.

Interestingly this bug survived unnoticed for years (at least since
2.11).

[1] last lines of the backtrace:
  File ".../utils/apparmor/cleanprofile.py", line 42, in compare_profiles
    deleted += self.remove_duplicate_rules(profile)
  File ".../utils/apparmor/cleanprofile.py", line 65, in remove_duplicate_rules
    deleted += apparmor.delete_duplicates(self.other.aa[program][hat], inc)
  File ".../utils/apparmor/aa.py", line 1680, in delete_duplicates
    deleted += profile[rule_type].delete_duplicates(include[incname][incname][rule_type])
AttributeError: 'collections.defaultdict' object has no attribute 'delete_duplicates'
2018-10-11 19:49:26 +02:00
nl6720
1fb9acc59e aa-notify: Read user's configuration file from XDG_CONFIG_HOME
Legacy path ~/.apparmor/notify.conf is preferred if it exists, otherwise
$XDG_CONFIG_HOME/apparmor/notify.conf, with fallback to
~/.config/apparmor/notify.conf, is used.

Signed-off-by: nl6720 <nl6720@gmail.com>
2018-10-03 12:38:28 +03:00
John Johansen
22fca3c217 Merge branch 'cboltz-log-to-profile-empty' into 'master'
test-libapparmor-test_multi.py: test for known-empty log

Add a check to logfile_to_profile() that checks the parsed log against a
list of input logs (log_to_profile_known_empty_log) that produce an
empty output.

PR: https://gitlab.com/apparmor/apparmor/merge_requests/214

Acked-by: John Johansen <john.johansen@canonical.com>
2018-10-03 06:47:21 +00:00
Christian Boltz
cf33ec99fc
test-libapparmor-test_multi.py: test for known-empty log
Add a check to logfile_to_profile() that checks the parsed log against a
list of input logs (log_to_profile_known_empty_log) that produce an
empty output.
2018-10-01 20:55:03 +02:00
Christian Boltz
072d3e0451
Add basic support for abi rules to the tools
Add basic "understand and keep" support for abi rules, where
"understand" means to not error out when seeing an abi rule, and "keep"
simply means to keep the original abi rule when serializing a profile.

On the long term, abi rules should be parsed (similar to include rules),
but for now, this patch is the smallest possible changeset and easy to
backport.

Note that the only added test is via cleanprof_test.* which is used by
minitools_test.py - and does _not_ run if you do a 'make check'.
Oh, and of course the simple_tests/abi/ files also get parsed by
test-parser-simple-tests.py.

Also note that serialize_profile_from_old_profile() (which no longer
exists in master, "only" in <= 2.13) would in theory also need support
for abi rules. In practise, making this another case of
"serialize_profile_from_old_profile() has known issues" is probably
fine, but we should at least test that "(V)iew changes" doesn't break if
an abi rule is present.
2018-09-26 22:09:17 +02:00
Emerson Bernier
00871696f1 add zsh to logprof.conf 2018-09-24 16:51:11 +00:00
Christian Boltz
db096135eb
add python3.7 to logprof.conf 2018-09-15 00:32:48 +02:00
Tyler Hicks
2f0a773506 utils: Point to the correct Profiles wiki page
The URL redirect ends up at a page in the new wiki that doesn't exist.
We have to link directly to the gitlab URL here since the current URL
redirect doesn't let us use a wiki.apparmor.net URL and still reach the
expected Profiles page.

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
2018-09-13 16:46:10 +00:00
Tyler Hicks
eb8975e0cc all: Use HTTPS links for apparmor.net
Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
2018-09-13 16:41:32 +00:00
Christian Boltz
51482c33f5
remove unused exception binding in sandbox.py
pyflakes 2.0 is more strict and found that 'e' is never used.

References: https://build.opensuse.org/request/show/629206 (comment
section)
2018-08-22 21:23:25 +02:00
John Johansen
86053c9e38 Merge branch 'cboltz-genprof-readonly-ratelimit' into 'master'
aa-genprof: don't crash if setting printk_ratelimit fails

See merge request apparmor/apparmor!157

Acked-by: John Johansen <john.johansen@canonical.com>
2018-08-07 10:02:25 +00:00
John Johansen
c66a1a972c Merge branch 'cboltz-fix-complain-named-profiles' into 'master'
set_profile_flags(): allow named profiles without attachment

See merge request apparmor/apparmor!142

Acked-by: John Johansen <john.johansen@canonical.com>
2018-08-07 10:00:49 +00:00
John Johansen
9dab5f07f4 Merge branch 'cboltz-change-flags' into 'master'
prevent that aa-complain etc. overwrites flags in child profiles if they differ from the main profile

See merge request apparmor/apparmor!150

Acked-by: John Johansen <john.johansen@canonical.com>
2018-08-07 09:59:39 +00:00
John Johansen
e4e73e3dc0 Merge branch 'cboltz-notify-pkg' into 'master'
make message about notify-send package cross-distro compatible

See merge request apparmor/apparmor!144

Acked-by: John Johansen <john.johansen@canonical.com>
2018-08-07 09:24:58 +00:00
Christian Boltz
961e69afe5
aa-genprof: don't crash if setting printk_ratelimit fails
When running aa-genprof in a lxd instance, printk_ratelimit is readonly
and writing to it fails. Instead of crashing with a backtrace, only
print a warning.

References: https://bugs.launchpad.net/apparmor/+bug/1785391
2018-08-05 14:46:13 +02:00
Christian Boltz
b00aab0843
let change_profile_flags() change flags in child profiles
... instead of overwriting them with the flags of the main profile.

This fixes a longstanding issue with aa-complain, aa-enforce and
aa-audit which broke the flags of child profiles and hats if they
differed from the main profile.

It also fixes several issues documented in the tests (which obviously
need adjustment to match the fixed behaviour).

Also change the "no profile found" cases to AppArmorException - errors
in a profile are not worth triggering AppArmorBug ;-)
2018-07-25 23:22:33 +02:00
Christian Boltz
d26ffbdd29
change_profile_flags: raise AppArmorBug on empty new flag 2018-07-25 23:21:28 +02:00