Commit graph

45 commits

Author SHA1 Message Date
Alexander Mikhalitsyn
4b7e868e54 parser: simple_tests: mount: mark ok_[16-19] tests as bad
These tests contains incompatible mount options and broken
after ("parser: add conflicting flags check for options= conditionals")

Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
Acked-by: John Johansen <john.johansen@canonical.com>
2023-03-28 20:07:05 -07:00
Mark Grassi
844a4dc393 Change string formatting method in Python tests 2023-02-19 16:54:38 -05:00
Georgia Garcia
4fb9b3d42b utils: add userns python tool support
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2022-11-04 12:39:18 +00:00
Georgia Garcia
ef54144357 parser tests: add userns simple tests
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2022-10-27 17:54:42 +00:00
Mark Grassi
c57138f255 Order imports and module-level dunder name assignments. 2022-08-21 11:15:07 -04:00
Mark Grassi
dc384c48a8 Use triple double-quoted strings for docstrings. 2022-08-21 11:15:07 -04:00
Mark Grassi
96f7121944 Fix most PEP 8 whitespace, indentation, and major line length violations. 2022-08-21 11:15:07 -04:00
Christian Boltz
5a2fb8569c
Set (instead of compare) exresult
Interestingly this accidentally worked because `if exresult` is true for
both a non-empty string ("PASS") as well as a real `True` value.

Found by Mark Grassi as part of
https://gitlab.com/apparmor/apparmor/-/merge_requests/906
2022-08-14 12:33:56 +02:00
Mark Grassi
7581c9e113 Speed up list creations, and change lists to tuples where appropriate.. 2022-06-26 22:18:56 -04:00
Christian Boltz
c09e0b6ef3
import is_skippable_file from apparmor.common
... instead of indirectly using it via apparmor.aa
2021-08-24 22:47:39 +02:00
Christian Boltz
6f15bea74c
Merge hat parsing into ProfileStorage.parse()
... and into parse_profile_start_line() (which is used by
ProfileStorage.parse()).

With this change, the section handling RE_PROFILE_HAT_DEF in
parse_profile_data() becomes superfluous.

A nice side effect is that two simple_tests parse failures get
accidently ;-) fixed.
2021-05-23 17:52:03 +02:00
Christian Boltz
accc380326
Add in_preamble parameter to profile loading/parsing functions
in_preamble keeps track of the current parsing position.

It's True while parsing the preamble of a profile file, and when loading
an include file that is included in the preamble (typically tunables/*).

While inside a profile or parsing abstractions/*, it is False.

This commit only hands the information around and keeps in_preamble
updated, but it doesn't really get used yet.

Also adjust the tests to hand over the additional parameter to
parse_profile_data().
2021-03-07 21:33:36 +01:00
Christian Boltz
f7e6f795c3
parse_profile_data(): Use BooleanRule
... and save rules at the right place (ProfileList) where they actually
get written when writing the profile.

This is also a bugfix - the previous code saved boolean variables at a
wrong place, and they were silently lost when writing the profile.

Extend cleanprof_test.{in,out} to ensure that this doesn't break again.

Also remove boolean_bad_[2-4] from the test-parser-simple-tests.py
exception_not_raised list because these test profiles now get correctly
detected as invalid.
2020-12-25 18:03:41 +01:00
Steve Beattie
74df38e284 parser sanity tests: add more mount options tests
Attempt to get clarity on what is valid syntax for mount options and
fstype options.

Note that simple_tests/mount/bad_opt_27.sd is marked TODO, as the
parser accepts it but should not.

Also mark the tests as expecting to fail to raise an exception by the
python utils.

Signed-off-by: Steve Beattie <steve.beattie@canonical.com>
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/607
Signed-off-by: John Johansen <john.johansen@canonical.com>
2020-09-29 04:39:16 -07:00
John Johansen
0a52cf81e3 parser: add support for autobind sockets
af_unix allows for sockets to be bound to a name that is autogenerated.
Currently this type of binding is only supported by a very generic
rule.

  unix (bind) type=dgram,

but this allows both sockets with specified names and anonymous
sockets. Extend unix rule syntax to support specifying just an
auto bind socket by specifying addr=auto

eg.

  unix (bind) addr=auto,

It is important to note that addr=auto only works for the bind
permission as once the socket is bound to an autogenerated address,
the addr with have a valid unique value that can be matched against
with a regular

  addr=@name

expression

Fixes: https://bugs.launchpad.net/apparmor/+bug/1867216
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/521
Signed-off-by: John Johansen <john.johansen@canonical.com>
2020-09-29 03:34:56 -07:00
Christian Boltz
429bfa0a07 Merge branch 'cboltz-abs-include' into 'master'
Change internal include file storage to absolute paths

See merge request apparmor/apparmor!562

Acked-by: Steve Beattie <sbeattie@ubuntu.com>
2020-06-17 11:12:25 +00:00
John Johansen
0c9884550c parser: support enforce, kill and unconfined profile modes
The enforce profile mode is the default but specifying it explicitly
has not been supported. Allow enforce to be specified as a mode. If
no mode is specified the default is still enforce.

The kernel has supported kill and unconfined profile modes for a
long time now. And support to the parser so that profiles can make
use of these modes.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/440
Fixes: https://gitlab.com/apparmor/apparmor/-/issues/7
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Steve Beattie <sbeattie@ubuntu.com>
2020-06-10 05:35:37 -07:00
Christian Boltz
8296c107cf
Drop profile_dir parameter from ProfileList get_all_merged_variables()
This parameter is superfluous and unused since some commits.

Also adjust all callers.
2020-06-05 20:02:33 +02:00
Christian Boltz
d19735340a
Detect invalid trailing commas in variable definitions
Trailing commas in variable values are not allowed (unless they are
quoted). Fix the regex to avoid "eating" the comma, and add a check to
detect invalid commas.

As usual, add some tests, and remove some testcases from the
exception_not_raised list.
2020-05-27 13:32:44 +02:00
Christian Boltz
0629215f2a
Check for variable names not ending with }
... and add a test to ensure that everything works as expected.

Note that broken variable names like '@{foo' match the (quite
permissive) regex, but are invalid nevertheless.
2020-05-27 13:32:44 +02:00
Christian Boltz
e9b8139cee
Check variable errors when parsing simple_tests
... by calling active_profiles.get_all_merged_variables()

Also remove vars/vars_bad_add_assignment_1.sd from the
exception_not_raised list again - now it raises an exception as
expected.
2020-05-27 13:32:44 +02:00
Christian Boltz
e5d38807df
Store variables in active_profiles (ProfileList)
... instead of filelist[file]['lvar'], and also write them from there.

Also fix detection of variable definitions inside a profile, which is
not allowed.

Note that ProfileList has a different write order than the old code -
first includes, then variable definitions. This makes more sense because
typical profiles first include tunables/global, and then define
additonal variables (that might use variables from tunables/global) or
extend variables defined in tunables/global.

This change also fixes some problems with the simple_test test profiles.
The "adding to non-existing variable" check currently doesn't exist,
which "fixes" lp:1331856.

OTOH this also means that such cases are not detected, therefore add
vars_bad_add_assignment_1.sd to the exception_not_raised list.

The check will be re-added in a later commit
in get_all_merged_variables().
2020-05-27 13:32:44 +02: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
6c4054fa91
Better descriptions why some example profiles fail with the tools 2020-05-03 22:07:25 +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
99b476510f
Remove 'owner link' tests from list of known-broken tests
... because they work now :-)
2019-04-23 00:22:25 +02: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
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
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
45922c6d21
make utils tests less verbose
Given the big number of tests, printing a dot for each test (instead of
multiple lines) is enough ;-)
2018-04-08 20:18:30 +02:00
Christian Boltz
dc7c702188 utils tests: ignore tests for 'include if exists'
... and some exotic includes that are not supported by the tools yet
2018-03-16 21:37:17 +00:00
Christian Boltz
26af640fda
tools: Mark profiles with multiple rules in one line as known-failing
The tools don't support having multiple rules in one line (they expect
\n after each rule), therefore mark some of the bare_include_tests as
known failures.
2018-01-23 22:40:07 +01:00
Christian Boltz
00bec36bed Ignore test failures about duplicated conditionals in dbus rules
Since r3634, the tools allow any order of dbus conditionals.

Quoting the r3634 patch description:

  This patch eases the restriction on the ordering at the expense of the
  utils no longer being able to detect and reject a single attribute that
  is repeated multiple times. In that situation, only the last occurrence
  of the attribute will be honored by the utils.

It seems nobody tested with all test profiles generated ;-) so we have to
add some exceptions to the "does not raise an exception" list now.



Acked-by <timeout> for trunk and 2.11
2017-04-20 13:05:53 +02:00
Christian Boltz
054d8f795f test-parser-simple-tests.py: No longer skip testing generated_perms_leading profiles
FileRule understands leading permissions, so the reason to skip those
(generated) test profiles in test-parser-simple-tests.py is gone.

However, the gen-xtrans.pl script generates profiles with a not-so-valid
mix of uppercase and lowercase, for example "Pux" and "Cux". The parser
accepts this, but the tools complain about such rules. Therefore add the
affected profiles to the exception list.

In total, this means we now test 319 of the 380 generated_perms_leading
test profiles.

The patch also moves some lines around to get the \-escaped profiles
out of the mixed uppercase/lowercase exec rule section.


Acked-by: Seth Arnold <seth.arnold@canonical.com>
2017-03-03 13:14:03 +01:00
Tyler Hicks
ea0732becc utils: Require apparmor.aa users to call init_aa()
Introduce an apparmor.aa.init_aa() method and move the initialization
code of the apparmor.aa module into it. Note that this change will break
any external users of apparmor.aa because global variables that were
previously initialized when importing apparmor.aa will not be
initialized unless a call to the new apparmor.aa.init_aa() method is
made.

The main purpose of this change is to allow the utils tests to be able
to set a non-default location for configuration files. Instead of
hard-coding the location of logprof.conf and other utils related
configuration files to /etc/apparmor/, this patch allows it to be
configured by calling apparmor.aa.init_aa(confdir=PATH).

This allows for the make check target to use the in-tree config file,
profiles, and parser by default. A helper method, setup_aa(), is added
to common_test.py that checks for an environment variable containing a
non-default configuration directory path prior to calling
apparmor.aa.init_aa(). All test scripts that use apparmor.aa are updated
to call setup_aa().

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Suggested-by: Christian Boltz <apparmor@cboltz.de>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2017-03-02 21:21:53 +00:00
Tyler Hicks
8935457c63 utils: Don't enforce ordering of dbus rule attributes
https://launchpad.net/bugs/1628286

The utils were enforcing that the dbus rule attributes were strictly
ordered in the following fashion:

 bus -> path -> interface -> member -> peer

However, the parser has always accepted the attributes in any order. If
the system contained a profile which did not use the strict ordering
enforced by the utils, the utils would refuse to operate at all.

This patch eases the restriction on the ordering at the expense of the
utils no longer being able to detect and reject a single attribute that
is repeated multiple times. In that situation, only the last occurrence
of the attribute will be honored by the utils.

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2017-02-28 23:04:24 +00:00
Christian Boltz
1c4a885e27 Switch utils to python3
As discussed a while ago, switch the utils (including their tests) to
use python3 by default. While on it, drop usage of "env" to always get
the system python3 instead of a random one that happens to live
somewhere in $PATH.

In practise, this patch doesn't change much - AFAIK openSUSE, Debian and
Ubuntu already patch aa-* to use python3.

Also add a note to README to officially deprecate Python 2.x.
(I won't break Python 2.x support intentionally - unless some future
change gives me a very good reason to finally drop Python 2.x support.)



Acked-by: Seth Arnold <seth.arnold@canonical.com>
(since 2016-08-23, but the commit had to wait for the FileRule series
 because it touches test-file.py)
2016-10-01 20:57:09 +02:00
Christian Boltz
aaa244c5ec [14/38] Use FileRule and FileRuleset
Change aa.py to use FileRule and FileRuleset for parsing and saving
profiles.

In detail, this means:
- add 'file' to the list of rule classes to enable it at various places
- store file rules in aa[profile][hat]['file'] (not 'path' as before)
  to be consistent with the FileRule name
- drop the no longer needed delete_path_duplicates() - this is now
  handled by FileRuleset like in all other rule classes.
  (same change in cleanprofile.py)
- replace usage of RE_PROFILE_BARE_FILE_ENTRY and RE_PROFILE_PATH_ENTRY
  with FileRule.match()
- drop write_path_rules() and write_paths() and replace them with the
  new write_file() function.
- adjust several code sections to use write_file() and 'file' instead of
  'path'

FileRule doesn't drop optional keywords ('allow' and 'file'), therefore
adjust cleanprof_test.out to the changed behaviour. (If someone insists
on dropping optional keywords in aa-cleanprof, that's something for a
future patch.)

Also adjust the list of known failures in test-parser-simple-tests.py -
switching to FileRule avoids several test failures (and introduces a few
new ones ;-)




IMPORTANT:

This patch introduces a "brain split" which means
- parsing and writing the profile and aa-cleanprof use the new location
  (aa[profile][hat]['file'])
- aa-logprof and aa-genprof still save data to the old location
  (aa[profile][hat]['allow']['path']) and probably ask superfluous
  questions because there are no rules existing in the old location

TL;DR: don't try aa-logprof or aa-genprof with only this patch applied.

I know this isn't ideal, but still better than an even bigger and
totally unreadable patch ;-)



Acked-by: Steve Beattie <steve@nxnw.org>
2016-10-01 19:54:48 +02:00
Tyler Hicks
cbaa772320 utils: Handle the safe/unsafe change_profile exec modes
https://launchpad.net/bugs/1584069

This patch adds support for the safe and unsafe exec modes for
change_profile rules. The logic is pretty simple at this point because
the kernel's default for exec modes changed in newer versions.
Therefore, this patch simply retains any specified exec mode in parsed
rules. If an exec mode is not specified in a rule, there is no attempt
to force the usage of "safe" because older kernels do not support it.

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2016-07-20 17:24:11 -05:00
Christian Boltz
a293f066b5 [6/9] Use DbusRule and DbusRuleset
Change aa.py to use DbusRule and DbusRuleset in profile_storage,
parse_profile_data() and write_dbus. This also means we can drop the
now unused parse_dbus_rule() and write_dbus_rules() functions.

Raw_DBUS_Rule in rules.py is now also unused and can be dropped.


Also shorten the list of known-failing tests in
test-parser-simple-tests.py. Even if the list of removals doesn't look
too long, the generated_dbus/* removals mean 1989 tests now cause the
expected failures.

OTOH, I had to add 4 tests to the known-failing list:
- 3 tests with a "wrong" order of the conditionals which the parser
  accepts (which is slightly surprising, because usually we enforce the
  order of rule parts)
- one test fails because the path in the path= conditional doesn't start
  with / or a variable. Instead, it starts with an alternation, which
  wouldn't be allowed in file rules.

Those 4 failures need more investigation, but shouldn't block this
patchset.


Finally, adjust test-regex_matches.py to import RE_PROFILE_DBUS from
apparmor.regex instead of apparmor.aa.


Acked-by: Seth Arnold <seth.arnold@canonical.com>
2016-05-23 23:17:37 +02:00
Christian Boltz
c3c13b1f2d Add simple_tests/profile/profile_ns_bad8.sd to utils test exception list
parser/tst/simple_tests/profile/profile_ns_bad8.sd was added in r3376
(trunk) / r3312 (2.10 branch) and contains the profile name ':ns/t'
which misses the terminating ':' for the namespace.

Unfortunately the tools don't understand namespaces yet and just use the
full profile name. This also means this test doesn't fail as expected
when tested against the utils code.

This patch adds profile_ns_bad8.sd to the exception list of
test-parser-simple-tests.py.


Acked-by: Steve Beattie <steve@nxnw.org> for trunk and 2.10.
2016-02-19 00:22:59 +01:00
Christian Boltz
a7179191f9 Use PtraceRule
Change aa.py to use PtraceRule and PtraceRuleset in profile_storage(),
parse_profile_data() and write_ptrace(). This also means we can drop the
now unused parse_ptrace_rule() and write_ptrace_rules() functions.

Raw_Ptrace_Rule in rules.py is now also unused and can be dropped.

Also adjust logparser.py to include the peer in the result, and shorten
the list of known-failing tests in test-parser-simple-tests.py.


Acked-by: John Johansen <john.johansen@canonical.com>
2015-12-27 01:20:37 +01:00
Christian Boltz
6756a0771d Several fixes for variable handling
Parsing variables was broken in several ways:
- empty quotes (representing an intentionally empty value) were lost,
  causing parser failures
- items consisting of only one letter were lost due to a bug in RE_VARS
- RE_VARS didn't start with ^, which means leading garbage (= syntax
  errors) was ignored
- trailing garbage was also ignored

This patch fixes those issues in separate_vars() and changes
var_transform() to write out empty quotes (instead of nothing) for empty
values.

Also add some tests for separate_vars() with empty quotes and adjust
several tests with invalid syntax to expect an AppArmorException.

var_transform() gets some tests added.

Finally, remove 3 testcases from the "fails to raise an exception" list
in test-parser-simple-tests.py.



Acked-by: John Johansen <john.johansen@canonical.com> for trunk and 2.9
(which also implies 2.10)

Note: 2.9 doesn't have test-parser-simple-tests.py, therefore it won't
get that part of the patch.
2015-12-12 12:59:13 +01:00
Christian Boltz
3a6087bef6 Change aa.py to use SignalRule and SignalRuleset
This means:
- import the classes instead of RE_PROFILE_SIGNAL
- simplify signal rule parsing a lot
- drop the (now unused) functions parse_signal_rule() and write_signal_rules()
- change write_signal() to use the SignalRuleset class

Also drop the now unused Raw_Signal_Rule from rules.py.

Finally, drop most parser signal tests from the "known wrong results"
blacklist in test-parser-simple-tests.py because those tests succeed
with SignalRule.


Acked-by: Kshitij Gupta <kgupta8592@gmail.com>
2015-11-24 00:09:37 +01:00
Christian Boltz
3fa19feb43 Parse all parser simple_tests with the utils code
Add a testcase that parses all tests in the parser/tst/simple_tests/
directory with parse_profile_data() to ensure that everything with valid
syntax is accepted, and that all tests marked as FAIL raise an
exception.

This already resulted in
- several patches to fix low-hanging fruits (including some bugs in the
  parser simple_tests itsself)
- a list of tests that don't behave as expected. Those files get their
  expected result reverted to make sure we notice any change in the
  tools behaviour, especially changing to the really expected resulted.
  This method also makes sure that the testcase doesn't report any of
  the known failures.
- a 5% improvement in test coverage - mostly caused by nearly completely
  covering parse_profile_data.
- addition of some missing testcased (as noticed by missing coverage),
  for example several "rule outside of a profile" testcases.

As indicated above, the tools don't work as expected on all test
profiles - most of the failures happen on expected-to-fail tests that
pass parse_profile_data() without raising an exception. There are also
some tests failing despite valid syntax, often with rarely used syntax
like if conditions and qualifier blocks.

Most of the failing (generated) tests are caused by features not
implemented in the tools yet:
- validating dbus rules (currently we just store them without any parsing)
- checks for conflicting x permissions
- permissions before path ("r /foo,")
- 'safe' and 'unsafe' keywords for *x rules
- 'Pux' and 'Cux' permissions (which actually mean PUx and CUx, and get
  rejected by the tools - ideally the generator script should create
  PUx and CUx tests instead)

skip_startswith excludes several generated tests from being run. I know
that skip_startswith also excludes tests that would not fail, but the
generated filenames (especially generated_x/exact-*) don't have a
pattern that I could easily use to exclude less tests - and I'm not too
keen to add a list with 1000 single filenames ;-)


Acked-by: John Johansen <john.johansen@canonical.com>
2015-10-20 23:00:56 +02:00