Commit graph

5837 commits

Author SHA1 Message Date
Christian Boltz
e31015a11a
Fix comment wording in file_cache.h
This fixes https://gitlab.com/apparmor/apparmor/-/merge_requests/743#note_562522101
2021-05-02 11:29:41 +02:00
Steve Beattie
92c76e04fe tests: add a test with recursive include in preamble
... just to be sure that the recursion check works everywhere ;-)


MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/750
Acked-By: Steve Beattie <steve@nxnw.org>
See merge request apparmor/apparmor!750
2021-04-28 14:23:53 +00:00
Christian Boltz
f0221f4bca
Add a test with recursive include in preamble 2021-04-28 13:32:08 +02:00
John Johansen
74bc4275a5 parser: Fix invalid reference to name in attachment warning
The name var is being improperly used in a warning. Not only is
it being used after it is freed, it also never had the correct value
as the "name" variable contained the value being used as the base
attachment.

Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: time out
2021-04-27 21:06:21 -07:00
John Johansen
be0d2fa947 parser: fix filter slashes for profile attachments
The parser is failing to properly filter the slashes in the profile
attachment after variable expansion. Causing matche failures when
multiple slashes occur.

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/154
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/727
Reported-by: Mikhail Morfikov <mmorfikov@gmail.com>
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: time out
2021-04-27 21:06:05 -07:00
John Johansen
71bf9e132f Merge parser: add include dedup cache to handle include loops
Profile includes can be setup to loop and expand in a pathalogical manner that causes build failures. Fix this by caching which includes have already been seen in a given profile context.

In addition this can speed up some profile compiles, that end up re-including common abstractions. By not only deduping the files being included but skipping the need to reprocess and dedup the rules within the include.

Fixes: https://bugzilla.suse.com/show_bug.cgi?id=1184779
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/743
Acked-by: Steve Beattie <steve.beattie@canonical.com>
2021-04-28 04:00:54 +00:00
Steve Beattie
c00b0d325b parser: add a simple one-level recursive include test
This adds a recursive include that otherwise parses correctly, to check
that the parser handles one-level recursion loop acceptably. When the
utils can support it, we should have tests that exercise deeper levels
of looping, e.g. include a -> include b -> include c -> include a or
deeper.

Without the fix in
https://gitlab.com/apparmor/apparmor/-/merge_requests/743, the parser
does fail due to hitting its file descriptor limit.

(The test at

  https://gitlab.com/apparmor/apparmor/-/blob/master/parser/tst/simple_tests/include_tests/recursive.sd

includes itself, which will result in a recursive profile definition
which isn't accepted by the parser.)

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/743
Signed-off-by: Steve Beattie <steve.beattie@canonical.com>
Signed-off-by: John Johansen <john.johansen@canonical.com>
2021-04-27 20:27:33 -07:00
John Johansen
7dcf013bca parser: add include dedup cache to handle include loops
Profile includes can be setup to loop and expand in a pathalogical
manner that causes build failures. Fix this by caching which includes
have already been seen in a given profile context.

In addition this can speed up some profile compiles, that end up
re-including common abstractions. By not only deduping the files
being included but skipping the need to reprocess and dedup the
rules within the include.

Fixes: https://bugzilla.suse.com/show_bug.cgi?id=1184779
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/743
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Steve Beattie <steve.beattie@canonical.com>
2021-04-27 20:26:57 -07:00
John Johansen
a7816e1a8f Merge Rework internal profile storage and handling in the aa-* tools to use merged profile names instead of [profile][hat]
Change the tools to use merged profile names (`var['foo//bar']`) instead of the profile/hat layout (`var[profile][hat]`) in many places. Also storage gets moved to ProfileList instead of using a hasher.

Already changed places (in this MR) are parsing profiles, writing profiles, handling and storing of extra profiles, log handling and asking the user about profile additions.

Remaining usage of the `var[profile][hat]` layout are the `aa` and `original_aa` hashers, they'll be replaced in a separate MR.

See the individual commits for details. I'd also recommend to do the review on the individual commits, because the big diff is probably unreadable ;-)

While this is a big chain of changes, each commit contains working code, converting between the two storage layouts with `split_to_merged()` and `merged_to_split()` as needed, with merged layout "bubbling up" in more and more functions.

The long-term goal of these changes is to enable support for nested child profiles in the tools, but - one step after the other ;-)

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/736
Acked-by: John Johansen <john.johansen@canonical.com>
2021-04-27 10:16:58 +00:00
John Johansen
bf70a8e4ec Merge get_new_profile_filename(): use better variable name
The function decides on the filename of a profile, therefore use 'filename' as variable name instead of the somewhat confusing 'profile' and 'full_profilename'.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/749
Acked-by: John Johansen <john.johansen@canonical.com>
2021-04-27 00:37:51 +00:00
John Johansen
ea61100ab6 Merge .gitignore: Add aa-features-abi and utils coverage files
I propose this addition for master and 3.0 (to keep the file in sync, and because most additions \[except `utils/test/coverage-report.txt`\] already make sense in 3.0)

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/748
Acked-by: John Johansen <john.johansen@canonical.com>
2021-04-27 00:33:34 +00:00
John Johansen
a0844f194e Merge Rewrite gen-dbus in python
The generated files are exactly the same.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/747
Acked-by: John Johansen <john.johansen@canonical.com>
2021-04-27 00:31:42 +00:00
Christian Boltz
54eb2baae7
get_new_profile_filename(): use better variable name
The function decides on the filename of a profile, therefore use
'filename' as variable name instead of the somewhat confusing 'profile'
and 'full_profilename'.
2021-04-25 20:35:37 +02:00
Christian Boltz
5c0609453c
.gitignore: Add aa-features-abi and utils coverage files 2021-04-25 20:10:48 +02:00
Christian Boltz
2b00d21e2e
Rewrite gen-dbus in python
The generated files are exactly the same.
2021-04-25 18:31:38 +02:00
John Johansen
8aa15c7fbe Merge Detect endless #include loop when parsing profiles
If an include file includes itsself (for example if local/foo has '#include <local/foo>'), print a warning instead of calling load_include() again and again.

This fixes a crash when hitting such a case: RecursionError: maximum recursion depth exceeded while calling a Python object

Fixes: https://bugzilla.suse.com/show_bug.cgi?id=1184779 for the tools. The parser will also need a fix.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/742
Acked-by: John Johansen <john.johansen@canonical.com>
2021-04-21 20:06:49 +00:00
Mikhail Morfikov
92e27f5566 abstractions: Add trash abstraction
I noticed that some apps have support for moving files to trash, but
currently there's no abstraction that would provide rules to make this
action possible.

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/160
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/738
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2021-04-21 13:01:43 -07:00
John Johansen
cf809ea928 parser: Fix make DEBUG=1
When building the parser with DEBUG=1 enabled the build fails with
the following error and warnings

    In file included from parser_main.c:47:0:
    parser_main.c: In function ‘void auto_tune_parameters()’:
     parser_main.c:1421:35: error: ‘estimate_jobs’ was not declared in this scope
        PDEBUG("Auto tune: --jobs=%d", estimate_jobs);
                                   ^
    parser.h:201:37: note: in definition of macro ‘PDEBUG’
      fprintf(stderr, "parser: " fmt, ## args); \
                                     ^~~~
    parser_main.c:1421:35: note: suggested alternative: ‘estimated_jobs’
        PDEBUG("Auto tune: --jobs=%d", estimate_jobs);
                                   ^
    parser.h:201:37: note: in definition of macro ‘PDEBUG’
      fprintf(stderr, "parser: " fmt, ## args); \
                                     ^~~~
    parser.h:201:41: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long int’ [-Wformat=]
      fprintf(stderr, "parser: " fmt, ## args); \
                                         ^
    parser_main.c:1428:5: note: in expansion of macro ‘PDEBUG’
         PDEBUG("Auto tune: --jobs=%d", jobs);
     ^~~~~~
    Makefile:234: recipe for target 'parser_main.o' failed

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/745
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2021-04-21 12:57:05 -07:00
Christian Boltz
30323a2ded
Detect endless #include loop when parsing profiles
If an include file includes itsself (for example if local/foo has
'#include <local/foo>'), print a warning instead of calling
load_include() again and again.

This fixes a crash when hitting such a case:
    RecursionError: maximum recursion depth exceeded while calling a Python object

Fixes: https://bugzilla.suse.com/show_bug.cgi?id=1184779 for the tools.
The parser will also need a fix.
2021-04-16 00:00:02 +02:00
Christian Boltz
e54b13a97a
write_piece(): use merged profile names
... and adjust the callers accordingly.

While on it, simplify the write_piece() code that prepends '^' to hats a bit.
2021-04-15 12:53:08 +02:00
Christian Boltz
0e5dca1083
serialize_profile(): use merged profile names in parameter
... and adjust all callers accordingly.
2021-04-15 12:53:08 +02:00
Christian Boltz
0802f34b35
deduplicate code in read_profile() and autodep()
if a profile doesn't have an attachment specified and the profile name
starts with '/', set the attachment to the profile name. This allows to
have one add_profile() call instead of two very similar ones.
2021-04-15 12:53:08 +02:00
Christian Boltz
8f84e125fd
simplify logic in read_profile()
exit early if profile_data is empty (which means we did read an empty
file). This allows to simplify the if conditions to "if active_profile:"
and "else:".
2021-04-15 12:53:08 +02:00
Christian Boltz
e04c8151bb
autodep(): use merged profile names
This means getting rid of several merged_to_split() and
split_to_merged() calls.
2021-04-15 12:53:08 +02:00
Christian Boltz
5cf7a28130
change get_profile() to return merged profile names
... and adjust its only caller accordingly.
2021-04-15 12:53:08 +02:00
Christian Boltz
5a68d96c11
extend create_new_profile() tests to cover required hats 2021-04-15 12:53:08 +02:00
Christian Boltz
c65206f16c
create_new_profile(): use and return merged profile names
... and adjust all callers and the tests.

For bonus points ;-) this also removes a hasher usage, and extends the
test to check that only the expected profile gets created.
2021-04-15 12:53:08 +02:00
Christian Boltz
286761271b
Store extra profiles in 'extra_profiles' ProfileList
... instead of the 'extras' hasher.

Also adjust all code that previously used 'extras' to use
'extra_profiles'. This affects get_profile() and read_profile().
2021-04-15 12:53:08 +02:00
Christian Boltz
179168f566
ProfileList: add get_profile_and_childs()
... and a test for it
2021-04-15 12:53:08 +02:00
Christian Boltz
b1a1b5dc1b
ProfileList: allow storing actual profile data
Add a prof_storage parameter to add_profile() to hand over the actual
profile data/rules as ProfileStorage.

Also adjust several tests to hand over a (dummy) ProfileStorage object.

Note: For now, the parameter is optional because it needs some more changes
in aa.py to be really useable. This will change in a later commit.
2021-04-15 12:53:08 +02:00
Christian Boltz
ba53ff3045
Change ask_the_questions() to work with merged profile names
... instead of converting log_dict to traditional [profile][hat] layout
in do_logprof_pass().

A nice side effect is that we get sorting the main profile before its
hats for free and can remove the sorting code.

Also update a comment in ask_rule_questions().

Finally, adjust aa-mergeprof so that it hands over a merged log_dict (using
split_to_merged())
2021-04-15 12:53:08 +02:00
Christian Boltz
a20865008f
collapse_log(): return merged profile names
... instead of the old [profile][hat] structure.

This needs changes in do_logprof_pass() when calling ask_the_questions()
(using merged_to_split() for now).

Also adjust test-libapparmor-test_multi.py logfile_to_profile() to
expect the merged structure.
2021-04-15 12:53:08 +02:00
Christian Boltz
bd3b62a8a1
Change collapse_log to internally use merged profile names
... and convert them back to the [profile][hat] layout at the end so
that callers still get the expected result.

As a side effect, log_dict no longer needs to be a hasher().
2021-04-15 12:53:08 +02:00
Christian Boltz
338a7774f1
parse_profile_data(): return merged profile names
... instead of the old [profile][hat] structure.

This needs changes in read_profile() (now using the merged profile name)
and attach_profile_data() (using merged_to_split() for now).

Also adjust test-aa.py to expect the merged structure.
2021-04-15 12:53:08 +02:00
Christian Boltz
d3816b5bcf
add split_to_merged()
... to convert a traditional compat['foo']['bar'] to a profile['foo//bar'] list
2021-04-15 12:53:08 +02:00
Christian Boltz
64a261f5ba
parse_profile_data(): use merged profile names internally
Change parse_profile_data() to internally use merged profile names
(`foo//bar`) instead of separate profile and hat, and only split it up
again to the [profile][hat] layout at the very end with
merged_to_split().

A nice side effect is that we get rid of a hasher() usage.

parse_profile_data() also gets changed to use `hat = None` (instead of
`hat = profile`) if not inside a child profile. As a result,
parse_profile_start() and one of its tests need a small change.

Besides that small change, calling code should not see a difference, and
the tests also stay working.
2021-04-15 12:53:08 +02:00
Christian Boltz
24e0631bd2
Add combine_profname() to combine profile name parts into joint name
... and add some tests for it.
2021-04-15 12:53:07 +02:00
Christian Boltz
4642d4c9c3
add merged_to_split()
... and a little test for it.

This function is meant to convert a merged foo['profile//hat'] to
old-style foo_compat['profile']['hat'].
2021-04-15 12:53:05 +02:00
John Johansen
472383a429 Merge Increase include and abi rule test coverage to 100%
... by adding some tests.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/741
Acked-by: John Johansen <john.johansen@canonical.com>
2021-04-14 22:36:01 +00:00
Christian Boltz
86edd48487
Increase include and abi rule test coverage to 100%
... by adding some tests.
2021-04-15 00:24:38 +02:00
Mikhail Morfikov
f19f9ae1fa abstractions: update video abstraction
the video abstraction currently it only contains the following rules:

  @{sys}/class/video4linux r,
  @{sys}/class/video4linux/** r,

Judging by the v4l path, this abstraction should be used whenever some
app wants to use for instance a webcam or other USB cameras to stream
video usually in chat apps. I was testing some apps, and it looks like
the following rules are needed to make the video streaming possible:

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/159
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/740
Signed-off-by: John Johansen <john.johansen@canonical.com>
2021-04-14 14:51:09 -07:00
John Johansen
3ee4f9c75f Merge 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.

Also run severity tests with the official severity.db instead of the slightly outdated copy in test/.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/737
Acked-by: John Johansen <john.johansen@canonical.com>
2021-04-14 21:23:35 +00:00
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
Steve Beattie
d270b2c3d0 utils: move and rename write_header() to ProfileStorage.get_header()
Merge branch 'cboltz-move-write_header' into 'master'

As a preparation, change change_profile_flags() to use ProfileStorage,
and replace some regex usage with startswith() to make the code easier
to read and (probably) more performant.

See the individual commits for details.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/734
Acked-by: Steve Beattie <steve@nxnw.org>
2021-04-11 22:36:59 +00: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