Commit graph

5348 commits

Author SHA1 Message Date
Christian Boltz
cfdbcb20cc
add include_list_recursive()
This function returns a list of all includes in a profile and its
included files.

It's split off from is_known_rule() and get_file_perms() which shared
lots of common code to recursively get the include list. These functions
now use include_list_recursive().
2020-05-20 18:57:54 +02:00
Christian Boltz
6f1245ede9
Convert is_known_rule() to use inc_ie / IncludeRule
While on it, also add a missing check to avoid checking the same file
twice.
2020-05-20 18:57:54 +02:00
Christian Boltz
9f1b2f4014
collapse_log(): avoid accidently initializing aa[profile]
... or calling is_known_rule() on events for non-existing hats.

It's the usual hasher() "fun" again - accessing a non-existing element
will create its parent.

In theory this commit might be worth a backport. In practise, it doesn't cause
any visible problem.

However, starting with the next commit, it will cause lots of test errors.

Also add a missing is_known_rule() call for dbus rules, which might have
caused similar hasher() "fun".
2020-05-20 18:57:54 +02:00
Christian Boltz
fa67815698
Convert get_file_perms() to use inc_ie / IncludeRule 2020-05-20 18:54:53 +02:00
Christian Boltz
0cfef21713
IncludeRuleset: add get_all_full_paths()
This function returns a list of full paths of all includes.

Also add some tests.
2020-05-20 18:54:53 +02:00
Christian Boltz
057c916032
parse_profile_data(): call load_include() for IncludeRule
rule_obj.get_full_paths() handles directories and non-existing files
(depending on 'if exists'), so we can simply feed the resulting list
into load_include()
2020-05-20 18:54:53 +02:00
Christian Boltz
f3f597ff0b
IncludeRule: add get_full_paths()
This function returns a list of paths for an include rule. This can be
- a single path if the include file is a file and exists
- a single path if the include file doesn't exist, but doesn't have
  'if exists' (this will cause a 'file not found' error when used)
- a list of paths if the include is a directory
- an empty list if the rule has 'if exists' and the file doesn't exist

Also add some tests for get_full_paths()
2020-05-20 18:54:53 +02:00
Christian Boltz
d799195e0a
move is_skippable_file() from aa.py to common.py
IncludeRuleset needs to import it (next commit), which is impossible
when it lives in aa.py (would cause recursive import loop because aa.py
imports IncludeRuleset)
2020-05-20 18:54:53 +02:00
Christian Boltz
3f0f7154f7
Let aa-cleanprof remove duplicate preamble rules
Technically, this is done in the new function
delete_preamble_duplicates() in ProfileList.

Also add some tests to ensure this works as expected.
2020-05-20 18:54:53 +02:00
Christian Boltz
0cf15d54b6
aa-mergeprof: drop separate code asking for include rules
... because this is now done via IncludeRule, and keeping the separate
code would mean asking twice.

Note that the user interface changes slightly.

The old workflow was

    1 - #include <foo>
    2 - #include <bar>
    3 - #include <baz>
    [select 2 and (A)dd, then get the next question for the remainder]

    1 - #include <foo>
    2 - #include <baz>
    [(A)dd another one, or (I)gnore the remainder]

The new workflow will ask separately for each abstraction, and you can
(A)dd or (I)gnore it. This is probably easier to understand because it's
basically a yes/no question.
2020-05-20 18:52:16 +02:00
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
John Johansen
a7a727b1b0 Merge Make save_profiles() usable for aa-mergeprof
... and replace the aa-mergeprof code that asks about saving the profile with a call to save_profiles().

PR: https://gitlab.com/apparmor/apparmor/-/merge_requests/536
Acked-by: John Johansen <john.johansen@canonical.com>
2020-05-18 08:27:10 +00:00
John Johansen
69eeca80dd Merge aa-mergeprof: simplify internal parameter handover
Instead of stuffing two variables into a list and split them again, hand them over as plain separate variables.

PR: https://gitlab.com/apparmor/apparmor/-/merge_requests/535
Acked-by: John Johansen <john.johansen@canonical.com>
2020-05-18 08:22:26 +00:00
John Johansen
500acf7119 Merge valid_include(): move out unrelated check
alid_include() checks if the given include file exists or is whitelisted in cfg\['settings'\]\['custom_includes'\].

The check if that include file is already part of the profile is unrelated to that. Move it to match_includes() where it fits much better (and drop the now superfluous profile parameter from valid_include())

In theory is_known_rule() should prevent that case from ever happening, but let's restrict this commit to moving the code around and keep this check just to be sure.

While on it, add some documentation to both functions.

PR: https://gitlab.com/apparmor/apparmor/-/merge_requests/534
Acked-by: John Johansen <john.johansen@canonical.com>
2020-05-18 08:19:54 +00:00
John Johansen
c4d1cfa487 Merge split off ask_rule_questions() from ask_the_questions()
Also rename aa.py delete_duplicates() and make ruletypes a parameter.

See the commit messages for details.

This reduces usage of global variables.

The final change is that aa-genprof now asks about preamble rules that are \*Rule classes (currently `abi` and `include if exists`).

PR: https://gitlab.com/apparmor/apparmor/-/merge_requests/532
Acked-by: John Johansen <john.johansen@canonical.com>
2020-05-18 08:16:48 +00:00
Christian Boltz
34d4d9f5af
Make save_profiles() usable for aa-mergeprof
... and replace the aa-mergeprof code that asks about saving the profile
with a call to save_profiles().
2020-05-17 16:50:01 +02:00
Christian Boltz
ed0f2fe505
aa-mergeprof: simplify internal parameter handover
Instead of stuffing two variables into a list and split them again, hand
them over as plain separate variables.
2020-05-14 23:29:31 +02:00
Christian Boltz
7f7fc973f8
valid_include(): move out unrelated check
valid_include() checks if the given include file exists or is
whitelisted in cfg['settings']['custom_includes'].

The check if that include file is already part of the profile is
unrelated to that. Move it to match_includes() where it fits much
better (and drop the now superfluous profile parameter from
valid_include())

In theory is_known_rule() should prevent that case from ever happening,
but let's restrict this commit to moving the code around and keep this
check just to be sure.

While on it, add some documentation to both functions.
2020-05-14 22:48:20 +02:00
Christian Boltz
e0f9b7cb07 Merge branch 'cboltz-fail-verbose' into 'master'
read_profile(): don't fail silently

See merge request apparmor/apparmor!530

Acked-by: Steve Beattie <steve.beattie@canonical.com> for 2.11..master
2020-05-12 19:43:44 +00:00
John Johansen
d1dca1f2d4 Merge ProfileList: add __repr__() with list of filenames
Even if incomplete (it doesn't print the whole ProfileList content), this should make debugging easier.

PR: https://gitlab.com/apparmor/apparmor/-/merge_requests/531
Acked-by: Steve Beattie <steve.beattie@canonical.com>
2020-05-12 07:44:12 +00:00
Christian Boltz
85f2cd4ed4
Let aa-mergeprof ask about more preamble (*Rule) rules
For now, that means aa-mergeprof will ask for `abi` and `include if
exists` rules (currently hardcoded).

This needs storing of `active_profiles` in the Prof object - the
preamble `abi` and `include if exists` rules are stored there.

Since several functions expect an `include` dict, add an empty one to
ProfileList to prevent lots of errors and breakage. It can be removed
again when handling of `include` rules gets moved to IncludeRule.
2020-05-11 23:41:25 +02:00
Christian Boltz
3e0f4e1dba
rename aa.py delete_duplicates() and make ruletypes a parameter
Rename delete_duplicates() to delete_all_duplicates() to make the
function easier grep-able - the *rule classes have delete_duplicates()
which might be confused with the old name.

Also hand over 'ruletypes' as parameter to delete_all_duplicates()
instead of using the global variable.
2020-05-11 23:03:23 +02:00
Christian Boltz
ca693db3b0
split off ask_rule_questions() from ask_the_questions()
ask_the_questions() stays the "main" function, loops over all profiles,
and calls ask_rule_questions() for each profile.

ask_rule_questions() asks the questions for all events in a specific
profile or hat.

This reduces the usage of global variables in ask_rule_questions().
2020-05-11 23:01:06 +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
af8b9dc5bb
read_profile(): don't fail silently
If a profile file can't be read, print a warning so that the user is
aware of the problem.
2020-05-11 22:23:20 +02:00
Steve Beattie
da2ce3ac67 utils: Use ProfileList to handle aliases
Merge branch 'cboltz-alias' into 'master'

Acked-by: Steve Beattie <steve.beattie@canonical.com>
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/526
2020-05-11 08:51:42 +00:00
Christian Boltz
cb44e6d47d Merge branch 'cboltz-drop-is-active-profile' into 'master'
Drop is_active_profile()

See merge request apparmor/apparmor!529

Acked-by: Steve Beattie <steve.beattie@canonical.com>
2020-05-11 08:37:16 +00:00
Christian Boltz
a5434e562d Merge branch 'cboltz-drop-write-rules' into 'master'
Drop / inline write_rules()

See merge request apparmor/apparmor!528

Acked-by: Steve Beattie <steve.beattie@canonical.com>
2020-05-10 23:03:47 +00:00
Christian Boltz
ae70ecfbaa Merge branch 'cboltz-vim-alias' into 'master'
apparmor.vim: allow leading whitespace for alias rules

See merge request apparmor/apparmor!527

Acked-by: Steve Beattie <steve.beattie@canonical.com> for 2.11..master
2020-05-10 22:54:34 +00:00
Christian Boltz
710d5ae466
Drop is_active_profile()
... and inline it at the only place that uses it
2020-05-10 23:49:16 +02:00
Christian Boltz
5c48eb7aa7
Drop / inline write_rules()
The TODO in write_rules() was long solved - remove the safety net type
check, it's no longer needed.

Without this, the function becomes a simple .get_rules_clean() call
which can easily be inlined into write_piece() at the two places where
it's called.
2020-05-10 18:09:14 +02:00
Christian Boltz
3c9c162951
write_alias(): drop depth parameter
... which is always '0' anyways, and therefore superfluous
2020-05-10 17:42:55 +02:00
Christian Boltz
8855c60f23
move write_alias to ProfileList
It's only needed in the preamble, which makes ProfileList the perfect
place.
2020-05-10 17:40:53 +02:00
Christian Boltz
fc6bd72781
Drop alias handling from ProfileStorage
ProfileStorage handles the profile content, but not the preamble. This
also means it will never see any aliases.
2020-05-10 17:39:02 +02:00
Christian Boltz
c636580f18
apparmor.vim: allow leading whitespace for alias rules 2020-05-10 17:24:09 +02:00
Christian Boltz
c0da20cf2c
Use ProfileList to handle aliases 2020-05-10 13:37:05 +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
John Johansen
676df24182 Merge Replace filelist[file_name]['profiles'] with using ProfileList
Replace usage of filelist\[file_name\]\['profiles'\] with the `active_profiles` ProfileList which already has the same data.

Add `profiles_in_file()` to ProfileList as a nice interface to get the list of profiles in a file.

For more details, check the individual commits.

PR: https://gitlab.com/apparmor/apparmor/-/merge_requests/523
Avked-by: John Johansen <john.johansen>
2020-05-09 19:16:54 +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
3d13ddb30a
Un-hardcode 'include' and 'IncludeRule' in IncludeRule
... as a preparation to recycle the class
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
b96fd81184
split out common parts of RE_INCLUDE and RE_ABI
... into RE_MAGIC_OR_QUOTED_PATH
2020-05-09 18:57:25 +02:00
John Johansen
28cdd08131 Merge add binutils/aa-status compiled files to .gitignore
Make so git ignores the generated files that result from building aa-status.

https://gitlab.com/apparmor/apparmor/-/merge_requests/524
Acked-by: John Johansen <john.johansen@canical.com>
2020-05-09 15:53:45 +00:00
Christian Boltz
c9ce3d3ab4
add binutils/aa-status compiled files to .gitignore 2020-05-09 13:29:24 +02:00
Steve Beattie
5574af0390
parser tests: convert makefile error tests to python script
Make the error and warning message tests a first class test script; in
gitlab CI, failures would not necessarily result in output being
captured.

Signed-off-by: Steve Beattie <steve.beattie@canonical.com>
Acked-by: John Johansen <john.johansen@canonical.com>
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/522
2020-05-08 15:44:41 -07:00