Commit graph

403 commits

Author SHA1 Message Date
Christian Boltz
23af115fa5
store include rules (also) in 'inc_ie' (IncludeRueset)
... and write them (only) from 'inc_ie' (IncludeRuleset), which can
handle both "include" and "include if exists" rules.

This duplicates storage of include rules because 'include' is still used
and needed at various places that work on/with the include rules.

With this, we get removal of duplicate include lines insinde a profile
in aa-cleanprof "for free" - extend cleanprof_test.in to confirm this.
2020-05-20 18:50:20 +02:00
Christian Boltz
e841c866d3
ProfileList: add __repr__() with list of filenames
Even if incomplete (it doesn't print the whole ProfileList content),
this should make debugging easier.
2020-05-11 22:46:29 +02:00
Christian Boltz
77a22979ed
ProfileList: Add support for alias rules
... and also some tests
2020-05-10 13:37:05 +02:00
John Johansen
13bea6f4a7 Merge Introduce and use AbiRule to handle abi rules
See the individual commits for details.

PR: https://gitlab.com/apparmor/apparmor/-/merge_requests/525
Acked-by: John Johansen <john.johansen@canonical.com>
2020-05-10 02:24:01 +00:00
Christian Boltz
66d3d92de3
Use AbiRule in ProfileStorage (for abi rules inside profiles)
This also drops the last usage of write_abi(), drop it.
2020-05-09 21:02:06 +02:00
Christian Boltz
d003cf814c
Use AbiRule for profile preamble 2020-05-09 21:02:06 +02:00
Christian Boltz
5efccd146e
ProfileList: Use AbiRuleset and add add_abi()
... and also tests for it
2020-05-09 21:02:06 +02:00
Christian Boltz
50d896705f
Add AbiRule and testsuite
This is probably the shortest rule class we'll ever have because it can
inherit most things from IncludeRule.
2020-05-09 21:02:06 +02:00
Christian Boltz
e9557284a3
re_match_include_parse() raise bug exceptions only for include
... but not for abi rules, which (according to the simple_tests
profiles) do not share these bugs)

For unquoted paths, make sure that the path doesn't include whitespace.
2020-05-09 21:02:06 +02:00
Christian Boltz
8ca486715f
extend re_match_include_parse() to also match abi rules
... and add some tests
2020-05-09 20:08:48 +02:00
Christian Boltz
874d3385a3
Stop writing to filelist[file]['profiles']
... because after the previous three commits, nothing reads/needs this
anymore

Note: file_name in ask_exec() was only used in the (dropped) filelist
usage.
2020-05-08 23:00:35 +02:00
Christian Boltz
01e46ab453
Add profiles_in_file() to ProfileList
... and some tests for it.
2020-05-08 22:37:45 +02:00
Christian Boltz
34a0457090
parse_profile_data(): better way to check for duplicate hats
Instead of checking filelist[file]['profiles'] for duplicate hats, check
profile_data[profile][hat].

With this, the duplicate hat check is done in the same way as the check
for duplicate profiles and child profiles.

Also add tests for duplicate child profiles and duplicate hats.
2020-05-08 22:15:10 +02:00
Steve Beattie
e6cf0d44d1 utils + parser: minor test cleanups
Fix a typo, some shellcheck warnings, and move one test script from using bash to dash.

See merge request apparmor/apparmor!512

Signed-off-by: Steve Beattie <steve.beattie@canonical.com>
Acked-by: Christian Boltz <opensuse@cboltz.de>
https://gitlab.com/apparmor/apparmor/-/merge_requests/512
2020-05-06 20:35:28 +00:00
Steve Beattie
253073b798
utils test: fix typo in simple_tests output
Signed-off-by: Steve Beattie <steve.beattie@canonical.com>
2020-05-05 11:54:49 -07:00
Christian Boltz
256f55925c
Drop profile repo code from utils
The profile repo is dead since years and most likely won't come back, so
there's no point in keeping and maintaining the code for uploading and
downloading profiles.
2020-05-05 13:09:16 +02:00
Christian Boltz
4b7108f1e5
Store and write 'include if exists' rules in the preamble
... using the new storage in ProfileList.

Also add a test rule to cleanprof.{in,out} to ensure the rule is kept.
2020-05-04 22:14:52 +02:00
Christian Boltz
07b52134f4
Extend ProfileList to store and write include rules
- add_inc_ie() stores include and include if exists rules
- get_clean() and get_raw() return the profile preamble (currently only
  the include rules)

Also add tests for the new functions.
2020-05-04 22:14:44 +02:00
Christian Boltz
1569136180
Add get_clean_unsorted() to BaseRuleset
This is similar to get_clean(), but keeps the original rule order
instead of sorting them.

This is useful for include rules in the preamble, where the order might
be relevant - for example if the first include defines a variable that
is then used or extended in the second include file.
2020-05-04 21:59:02 +02:00
Christian Boltz
c670d294eb
Rename TestAdd and its functions to TestAdd_profile
This is a follow-up of 5983598ef5
2020-05-04 21:59:02 +02:00
Steve Beattie
64be247aae utils: ProfileList: rename add() to add_profile()
Merge branch 'cboltz-profile-list-rename-add' into 'master'

See merge request apparmor/apparmor!502
Acked-by: Steve Beattie <steve.beattie@canonical.com>
2020-05-04 19:47:17 +00:00
Christian Boltz
5983598ef5
ProfileList: rename add() to add_profile()
This makes the syntax more clear, and is a preparation to allow adding
some more things (like global includes and variable definitions)
2020-05-03 22:37:20 +02:00
Christian Boltz
6c4054fa91
Better descriptions why some example profiles fail with the tools 2020-05-03 22:07:25 +02:00
Christian Boltz
8661676eb4 Merge branch 'cboltz-utils-include-if-exists' into 'master'
Add support for 'include if exists' to the tools

See merge request apparmor/apparmor!499

Acked-by: Steve Beattie <sbeattie@ubuntu.com>
2020-05-03 19:59:11 +00:00
Christian Boltz
6643d0b07a
Add a 'include if exists' rule to cleanprof testcases
... to ensure it is kept.
2020-05-03 13:58:37 +02:00
Christian Boltz
295bb6469a
Enable usage of IncludeRule in the tools
For now, only 'include if exists' rules will be handled by IncludeRule.
Using it also for 'include' rules needs some more code changes so that
included files still get checked etc.

Also remove some testcases from test-parser-simple-tests.py unknown_line
which no longer fail.
2020-05-03 13:55:03 +02:00
Christian Boltz
4df5ac780d
Add IncludeRule and IncludeRuleset including tests
These classes are meant to handle 'include' and 'include if exists'
rules.

Due to restrictions in re_match_include_parse(), some cases in
is_covered_localvars() and is_equal_localvars() can't be reached in the
unittests.

Also, IncludeRule isn't used in aa-logprof (yet?), which means
logprof_header_localvars() result format isn't decided yet, and
therefore not tested.

This means test coverage for the new classes isn't 100% this time ;-)
2020-05-03 13:41:19 +02:00
Christian Boltz
7b009a909e
Remove superfluous self-cast in Invalid*Test 2020-05-02 22:13:34 +02:00
Christian Boltz
6b9b928f9d
Add tests for re_match_include_parse()
Also extend tests for re_match_include() to make sure it doesn't match
"include if exists" rules.
2020-05-02 19:44:36 +02:00
John Johansen
57c3d8e125 Merge test that '\*' from audit.log gets correctly escaped
convert_expression_to_aare() is expected to convert it to AARE `\\\*`

PR: https://gitlab.com/apparmor/apparmor/-/merge_requests/479
Acked-by: John Johansen <john.johansen@canonical.com>
2020-04-14 07:25:36 +00:00
Christian Boltz
c9c4f4ce9c
test that '\*' from audit.log gets correctly escaped
convert_expression_to_aare() is expected to convert it to AARE '\\\*'
2020-04-13 19:46:05 +02:00
Christian Boltz
ef0d675824
Get rid of is_covered_aare_compat()
This function was introduced as a temporary (ahem...) solution in
95404bb2f3 but was never really correct.
It checked against other_value.regex (as a string!) and, while this was
somewhat generous in the results, could have unintended side effects.

Better error out on the safe side and add/keep a few superfluous rules
than having a wrong match in is_covered() and miss to add/keep a rule
that would be needed.

The perfect solution would be to really compare one AARE against the
other as the parser does. I'm not too keen to implement this in python,
and will wait until someone provides this function (which the parser
already has) via libapparmor ;-)
2020-04-13 15:13:12 +02:00
John Johansen
0349cf2d0a libapparmor: logparse: fix RECORD_INVALID for valid log
v2:
- parse partial log line broken at \n
- add testcase_dbus_10.* for partial log line
- remove quotes from  testcasw_dbus_09.profile

The following log format has been seen in the wild, and currently results
in a RECORD_INVALID

    [4835959.046111] audit: type=1107 audit(1561053426.749:186): pid=640 uid=103 auid=4294967295 ses=4294967295 msg='apparmor="ALLOWED" operation="dbus_method_call"  bus="system" path="/org/freedesktop/systemd1" interface="org.freedesktop.systemd1.Manager" member="LookupDynamicUserByName" mask="send" name="org.freedesktop.systemd1" pid=20596 label="/usr/sbin/sshd" peer_pid=1 peer_label="unconfined"
                      exe="/usr/bin/dbus-daemon" sauid=103 hostname=? addr=? terminal=?'

Test parsing the above message with and without the \n embedded between
peer_label= and exec=

Acked-by: Seth Arnold <seth.arnold@canonical.com>
Signed-off-by: John Johansen <john.johansen@canonical.com>
2019-07-02 01:01:37 -07:00
John Johansen
9144e39d25 Revert "utils/test-network.py: fix failing testcase"
This reverts commit 378519d23f.
this commit was meant for the 2.13 branch not master

Signed-off-by: John Johansen <john.johansen@canonical.com>
2019-06-14 01:05:16 -07:00
Steve Beattie
378519d23f utils/test-network.py: fix failing testcase
When dc010bc034 was
backported to the apparmor-2.13 branch (in commit
75236d62e2), it did not take into
account cb8c3377ba, which creates the
common/list_af_names.sh script as used in the test case, was not also
backported to the apparmor-2.13 branch.

Change the test case to get the list of network AF names via the same
make invocation taken by the utils/vim/create-apparmor.vim.py script
before the common/list_af_names.sh existed.

PR: https://gitlab.com/apparmor/apparmor/merge_requests/391
Signed-off-by: Steve Beattie <steve.beattie@canonical.com>
Signed-off-by: John Johansen <john.johansen@canonical.com>
2019-06-14 00:04:00 -07:00
Christian Boltz
836caca462
collapse_log: ignore events from null-* profiles
If final_name still includes null-*, that's most likely caused by nested
execs which aren't supported by the tools yet. Ignoring them is better
than creating a useless null-* hat.

Note: The tools always had this restriction, so this is not a regression ;-)

Also note that test-libapparmor-test_multi expects that null-* hats get
created (which makes sense because the one-line log sniplets don't have
any exec indication), therefore add an optional parameter to keep this
behaviour for the tests.
2019-05-09 18:14:18 +02:00
Christian Boltz
387d1646c8
Merge handle_hashlog() into collapse_log()
Now that all log events arrive in hashlog, having a separate 'prelog' no
longer makes sense. Changing collapse_log() to accept 'hashlog' directly
removes that level of indirection.
2019-05-09 17:29:26 +02:00
Christian Boltz
c5f0097f65
make 'prelog' non-global
It's only used by two functions:
- handle_hashlog() - writes to prelog, and now returns it
- collapse_log() - reads prelog, and now gets it as parameter
2019-05-09 17:29:26 +02:00
Christian Boltz
c77426ed62
Introduce 'final_name' to hashlog and handle exec choices
'final_name' by default is the profile name, but ask_exec() will change
it for the target profile (which is a null-* profile at this stage)
based on exec mode choice. ask_addhat() will also change it based on the
chosen hat.

Choosing "deny" or "unconfined" will result in an empty final_name and
ignoring these log events.

All other choices set final_name to the full profile name ("foo" for Px,
"foo//bar" for Cx, current profile for ix).

Also fix the order of handling log events - since ask_exec() changes the
hashlog final_name, it has to run first so that ask_addhat() (which
"only" adjusts the hat name in final_name) and handle_hashlog() can work
with the updated profile name.

Finally, update test-libapparmor-test_multi.py to ignore final_name when
checking if hashlog is empty, and fix the call order of ask_exec() etc.
2019-05-09 17:29:26 +02:00
Christian Boltz
48cc1b2837
add a split_name() function to split a profile name
... into profile and hat.

Also change several places to use split_name().
2019-05-09 17:15:35 +02:00
Christian Boltz
305b378bfd
Delete apparmor/aamode.py and its testsuite
After the logparser cleanup, aamode.py and its overly complicated
permission handling is no longer needed.
2019-05-09 17:15:35 +02:00
Christian Boltz
45a3d8920a
Drop unused 'pid' parameter from ReadLog.__init__()
... and self.pid which is also unused.

This simple change also means to adjust all the code that uses ReadLog.
We get rid of log_pid in aa.py, and have to change lots of test-*
2019-05-09 17:15:35 +02:00
Christian Boltz
9a92909a89
Change read_log to return only hashlog
self.log is unused, drop it.

Also change all places that call read_log() to match the simplified
return value.
2019-05-09 17:15:35 +02:00
Christian Boltz
d2663b148a
Transform handle_children() to ask_exec()
The only remaining job of handle_children() was to handle exec events.
(And recursively calling itsself if it hits nested log events, but
logparser.py never created such a log structure.)

Therefore:
- drop the dead code handling nested log (type != str)
- rename the remaining function to ask_exec()
- drop checks for typ = 'exec' (now done as part of the for loop
- drop the "else" branch for unknown event types
- change 'return' to 'continue' because ask_exec handles all exec events
  in a loop instead of being called multiple times
- oh, and of course switch over to using hashlog

Finally, change do_logprof_pass() and the tests to call ask_exec()
instead of handle_children().

While on it, update a comment in test-translations.py which held the
last reference to handle_children().
2019-05-09 17:15:35 +02:00
Christian Boltz
560fb6fabe
Use hashlog for change_hat log events
Adjust logparser.py to store change_hat events in hashlog.

In aa.py,
- split off ask_addhat() from handle_children()
- change ask_addhat() to use hashlog
- call ask_addhat() from do_logprof_pass()

Also call ask_addhat() in test-libapparmor-test_multi.py to keep it in
sync with do_logprof_pass().
2019-05-03 00:12:46 +02:00
Christian Boltz
8b1b10babd
Add tests for parse_event_for_tree() with invalid log lines
Also convert test-logparser.py to AATest.
2019-05-02 01:10:18 +02:00
Christian Boltz
a43c1da287
Remove superfluous code and checks in path log handling
In logparser parse_event_for_tree() path event handling, drop mapping
permissions for request_mask because request_mask never gets used.
Also drop the validate_log_mode() call because the function has its own,
more strict check since the last commit.

In aamode.py, drop the now unused validate_log_mode() and
hide_log_mode() functions and the LOG_MODE_RE regex.

Finally, drop the validate_log_mode() tests from test-aamode.py
2019-05-02 01:10:18 +02:00
Christian Boltz
3d3667f38b
move path log event handling to hashlog
In logparser.py parse_event_for_tree, convert path handling to hashlog.
While on it, include 'owner' as part of hashlog so that aa.py doesn't
need to guess.

Also switch to a simple for loop instead of using log_str_to_mode() from
aamode.py to convert denied_mask to hasher keys (which would have been
needed to allow merging of several log events for the same path anyway).
Note that the check for 'mrawlk' (intentionally without 'x') is more
strict than the validate_log_mode(), but it should still cover all file
permissions. (validate_log_mode() also allows things like 'Px', which
we'll never hit in a logfile.)

In aa.py collapse_log() update the handling of path events to match the
additional [owner] key in hashlog/prelog. This makes the owner detection
in collapse_log() superfluous.

In aa.py handle_children(), remove 'path' handling from the 'path' or
'exec' section, and add an 'if True:' to avoid lots of whitespace
changes.

In aamode.py, drop the now unused split_mode() function, and
AA_OTHER_REMOVE() that was only used by split_mode().

Finally, remove sample log events with null-* hats from the list of
known failures in test-libapparmor-test_multi.py (we no longer filter
out null-* hats), and fix whitespace in two expected profiles.
2019-05-02 01:10:18 +02:00
Christian Boltz
6b63f49ad4
Remove a level of indirection on logparser.py
logparser.py puts each log event on a big "stack" in self.pid. Later,
handle_children() in aa.py then converts that (named 'log' in aa.py) to
the prelog hasher.

This commit changes logparser.py to create the prelog structure itsself
(named hashlog), which
- removes one level of indirection
- probably saves some memory because the hashlog automatically
  de-duplicates events

This commit does this for capability, network and signal events, and
adds the infrastructure needed for all event/rule types.

In aa.py, the new function handle_hashlog() copies the hashlog content
to prelog. OTOH, the now superfluous code handling capability, network
and signal events gets removed from handle_children().
Long-term, hashlog will replace log in aa.py. When this is done,
handle_hashlog() will be replaced by a simple prelog = hashlog.

logparser.py gets a new function init_hashlog() to initialize hashlog
for each profile. It also gets changed to store capability, network and
signal events into hashlog instead of storing them in self.pid.

hashlog uses the full profile name as key, which is the first baby step
to support nested child profiles. (for now, handle_hashlog() still
splits the profile name into profile and hat.)

Known issue: The new implementation doesn't handle exec yet, which means
that events get lost at the exec boundary (= in cases aa-logprof asks
which execute mode to use). This will be fixed in a later commit.
2019-05-01 21:22:36 +02:00
Christian Boltz
99b476510f
Remove 'owner link' tests from list of known-broken tests
... because they work now :-)
2019-04-23 00:22:25 +02:00