Commit graph

1020 commits

Author SHA1 Message Date
Christian Boltz
566b053bdf Fix regressions caused by init_aa()
With the init_aa() patch series commited, minitools_test.py showed
several test failures - which effectively means the -d option of
aa-complain, aa-cleanprof etc. was broken.

These failures were caused by
- calling init_aa() too late in tools.py - _after_ setting the
  profiledir, which then got overwritten by init_aa()
- calling init_aa() twice (because apparmor.aa gets imported in two
  modules used by aa-cleanprof), which overwrote the manually set values
  on the second run

This patch fixes the call order in tools.py and adds a check to
init_aa() so that it only runs once and ignores additional calls.


Acked-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2017-03-03 13:14:55 +01: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
9a8c6885cb utils: Fix apparmor.easyprof import in test-aa-easyprof.py
The test-aa-easyprof.py script was attempting to do its own special
setup to import the in-tree easyprof module. However, this proved to be
very flaky and resulted in the test periodically failing due to an
AttributeError the first time easyprof.parse_args() was called.

This patch removes the flakiness by trusting that PYTHONPATH is set up
appropriately before the test script is ran. PYTHONPATH is already
initialized appropriately by utils/test/Makefile according to the
USE_SYSTEM make variable.

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2017-03-02 21:25:01 +00:00
Tyler Hicks
7ab65fa5f1 utils: Set parser executable path according to USE_SYSTEM make variable
if USE_SYSTEM is not set, the utils make check target will instruct
test-aa-easyprof.py to provide the path of the in-tree parser executable
to aa-easyprof.

If USE_SYSTEM is set, the default parser path (/sbin/apparmor_parser or
the result of `which apparmor_parser`) is used.

The test-aa-easyprof.py script receives the parser path by checking the
__AA_PARSER environment variable. This environment variable is strictly
used by the test script and not any user-facing code so two leading
underscores were used.

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2017-03-02 21:24:33 +00:00
Tyler Hicks
b98e9df766 utils: Add option to aa-easyprof to specify the apparmor_parser path
When testing against a clean system without the apparmor_parser binary
installed, the test-aa-easyprof.py script ends up skipping profile
verification because it can't find the parser binary. This even causes a
test failure due to the test_genpolicy_invalid_template_policy test.

Adding a --parser option to aa-easyprof is the first step in addressing
this problem.

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2017-03-02 21:24:05 +00:00
Tyler Hicks
7066649144 utils: Set parser base path according to USE_SYSTEM make variable
If USE_SYSTEM is not set, the utils make check target will instruct
test-aa-easyprof.py to provide the path of the in-tree
profiles/apparmor.d directory to aa-easyprof as the parser base
directory.

If USE_SYSTEM is set, the default base directory (/etc/apparmor.d) is
used.

The test-aa-easyprof.py script receives the base path by checking the
__AA_BASEDIR environment variable. This environment variable is strictly
used by the test script and not any user-facing code so two leading
underscores were used.

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
Acked-by: Seth Arnold <seth.arnold@canonical.com>

Bug: https://launchpad.net/bugs/1538306
2017-03-02 21:23:32 +00:00
Tyler Hicks
361b63d30b utils: Accept parser base and include options in aa-easyprof
https://launchpad.net/bugs/1521031

aa-easyprof accepts a list of abstractions to include and, by default,
execs apparmor_parser to verify the generated profile including any
abstractions. However, aa-easyprof didn't provide the same flexibility
as apparmor_parser when it came to where in the filesystem the
abstraction files could exist.

The parser supports --base (defaulting to /etc/apparmor.d) and --Include
(defaulting to unset) options to specify the search paths for
abstraction files. This patch adds the same options to aa-easyprof to
aide in two different situations:

 1) Some Ubuntu packages use aa-easyprof to generate AppArmor profiles
    at build time. Something that has been previously needed is a way
    for those packages to ship their own abstractions file(s) that are
    #included in the easyprof-generated profile. That's not been
    possible since the abstraction file(s) have not yet been installed
    during the package build.

 2) The test-aa-easyprof.py script contains some tests that specify
    abstractions that should be #included. Without the ability to
    specify a different --base or --Include directory, the abstractions
    were required to be present in /etc/apparmor.d/abstractions/ or the
    tests would fail. This prevents the Python utils from being able to
    strictly test against in-tree code/profiles/etc.

I don't like the names of the command line options --base and --Include.
They're not particularly descriptive and the capital 'I' is not user
friendly. However, I decided to preserve the name of the options from
apparmor_parser.

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2017-03-02 21:22:57 +00: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
f30ab46af7 utils: Update the logprof.conf in the test dir to point to in-tree paths
The utils tests should make use of the logprof.conf that resides in
utils/test/ when testing against the in-tree parser and profiles. When
testing against the system, it the utils tests should continue to use
the system logprof.conf.

This patch updates the parser and profiles paths to point to the in-tree
paths. Another patch is needed to get aa.py to honor a non-hardcoded
search path for logprof.conf and other configuration files.

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2017-03-02 21:21:21 +00:00
Tyler Hicks
2db1b83869 utils: Improve error messages when profiles/parser is not found
When aa.py is imported, it looks for a set of profiles and it also looks
for the parser. Both of these paths are configured by logprof.conf but
it isn't always obvious which logprof.conf file was used and, therefore,
it isn't always obvious where aa.py is looking. This patch includes the
paths in the error messages.

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2017-03-02 21:20:45 +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
Tyler Hicks
d4d4d50d84 utils: Fix failing tests in test-aa.py
The merged /usr patches to the policy broke some utils tests due to a
change in the expected output.

Fixes: r3600 update lots of profiles for usrMerge
Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2017-02-28 23:03:25 +00:00
Christian Boltz
984ed2801e Ignore change_hat events with error=-1 and "unconfined can not change_hat"
That's much better than crashing aa-logprof ;-)  (use the log line in
the added testcase if you want to see the crash)

Reported by pfak on IRC.


Acked-by: Seth Arnold <seth.arnold@canonical.com> for trunk, 2.10 and 2.9.
2017-02-23 01:00:36 +01:00
Christian Boltz
633f833a6e Remove re.LOCALE flag
Starting with python 3.6, the re.LOCALE flag can only be used with byte
patterns, and errors out if used with str. This patch removes the flag
in get_translated_hotkey().


References: https://bugs.launchpad.net/apparmor/+bug/1661766


Acked-by: Steve Beattie <steve@nxnw.org> for trunk, 2.10 and 2.9
2017-02-21 18:46:36 +01:00
Christian Boltz
20091ca87d Rename global variable "pid" to "log_pid"
aa.py has a global variable "pid", but it also has several functions
that use "pid" as a local variable name. do_logprof_pass() even uses
both - first, it passes the global variable to ReadLog, and then it
creates a local variable in the "for pid in ..." loop.

This patch renames the global variable to log_pid to get rid of the
confusion.

Note that the global variable is only handed over to ReadLog, and the
only case where its previous content _might_ be used is aa-genprof which
does multipe do_logprof_pass() runs.

Maybe we could even get rid of this variable in aa.py and make it local
to the ReadLog class, but I'm not sure if that would affect aa-genprof
in interesting[tm] ways.


Acked-by: John Johansen <john.johansen@canonical.com>
2017-01-30 20:48:50 +01:00
Christian Boltz
50623fca92 Drop unused global variables in aa.py
Grepping through the code shows that running_under_genprof,
unimplemented_warning, ALL, t, seen and skip are unused, so drop them.


Acked-by: Steve Beattie <steve@nxnw.org>


Also drop a '#    t = hasher()" comment, as noticed by Steve.
2017-01-20 01:20:41 +01:00
Christian Boltz
85178293f5 [7/7] Drop most of aa-mergeprof ask_the_questions()
Replace most of aa-mergeprof ask_merge_questions() with a call to
aa.py ask_the_questions() (which is, besides some small exceptions that
are not relevant for aa-mergeprof, in sync with the dropped code).

The remaining part gets renamed to ask_merge_questions() to avoid
confusion with the function name in aa.py. Also drop the (now
superfluous) parameter.

aa.py ask_the_questions() needs to allow 'merge' as aamode.
While on it, replace the fatal_error() call for unknown aamode with
raising an AppArmorBug.


Acked-by: Seth Arnold <seth.arnold@canonical.com>
2017-01-19 16:54:47 +01:00
Christian Boltz
d1fa70ac22 [6/7] make log_dict a parameter of ask_the_questions()
This allows to hand over any source instead of using the global variable.

Now that the function expects its input as parameter,  get rid of the
global log_dict, which means
- change collapse_log() to initialize log_dict as local variable and
  return it
- change do_logprof_pass() to catch collapse_log()'s return value and
  hand it over to ask_the_questions()
- drop all references to the global log_dict variable
- update test-libapparmor-test_multi to follow the changes

Also fix an if condition that would fail if aa[profile][hat] does not
exist - get() defaults to None if the requested item doesn't exist, and
None.get('file') will raise an Exception.


Acked-by: Seth Arnold <seth.arnold@canonical.com>
2017-01-19 16:52:38 +01:00
Christian Boltz
4ec82daa00 [5/7] move ask_conflict_mode() to aa.py
The function is an exact copy of the code in aa-mergeprof (except
removing the 'self' function parameter and changing the whitespace
level)

Also add a ask_conflict_mode() call to aa.py ask_the_questions().
This is needed for aa-mergeprof, and won't hurt in aa-logprof mode
because handle_children() already handles all exec events.


Acked-by: Seth Arnold <seth.arnold@canonical.com>

Bug: https://launchpad.net/bugs/1522938
2017-01-19 16:48:44 +01:00
Christian Boltz
627856d6b4 [4/7] Copy code to ask for adding hats to aa.py ask_the_questions()
Everything below "if aamode == 'merge':" is an exact copy of the code in
aa-mergeprof (with whitespace changed).

aa-logprof and aa-mergeprof will continue to ignore events from unknown
hats and subprofiles.


Acked-by: Seth Arnold <seth.arnold@canonical.com>
2017-01-19 16:47:35 +01:00
Christian Boltz
cfa0a37e58 [3/7] Copy code to ask for adding includes to aa.py ask_the_questions()
This is an exact copy of the code in aa-mergeprof (with whitespace changed).


Acked-by: Seth Arnold <seth.arnold@canonical.com>
2017-01-19 16:47:05 +01:00
Christian Boltz
ca093f7223 [2/7] replace other.aa with log_dict['merge']
Set log_dict['merge'] = other.aa and aamode = 'merge', and use
log_dict[aamode] everywhere.

This brings aa-mergeprof ask_the_questions() closer to the code in aa.py.


Acked-by: Seth Arnold <seth.arnold@canonical.com>
2017-01-19 16:46:23 +01:00
Christian Boltz
1cae419b4d [1/7] drop traces of 3-way-merge in aa-mergeprof
3-way-merge was never really implemented.

This patch drops all traces of it to make the code more readable and
easier to maintain.


Acked-by: Seth Arnold <seth.arnold@canonical.com>
2017-01-19 16:45:29 +01:00
Christian Boltz
71f385fc83 Handle ldd $? == 1 in get_reqs()
ldd exits with $? == 1 if a file is 'not a dynamic executable'.
This is correct behaviour of ldd, so we should handle it instead of
raising an exception ;-)

Also extend fake_ldd and add a test to test-aa.py to cover this.


Note that 2.10 and 2.9 don't have tests for get_reqs() nor fake_ldd,
so those branches will only get the aa.py changes.


Acked-by: John Johansen <john.johansen@canonical.com> for trunk, 2.10 and 2.9.
2016-12-31 00:48:41 +01:00
Steve Beattie
86e30539cf utils/aa-unconfined: whitespace cleanups for pep8 consistency.
This is what this patch looks like when diff'ed ignoring spacing
changes:

 $ quilt diff | diffstat
  aa-unconfined |   18 +++++++++---------
  1 file changed, 9 insertions(+), 9 deletions(-)
 $ quilt diff --diff 'diff -uw'
 $

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: Christian Boltz <apparmor@cboltz.de>
Acked-by: John Johansen <john.johansen@canonical.com>
2016-12-30 12:22:58 -08:00
Steve Beattie
b72cd7914a utils/aa-unconfined: allow specifying ss/netstat binary locations
This patch allows a user to specify a specific location for ss or
netstat in the invocations of get_pids_ss() or get_pids_netstat().

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2016-12-30 12:20:01 -08:00
Steve Beattie
e4f22f5e27 utils/aa-unconfined: avoid using cat(1) to read /proc/PID/cmdline
This patch adjusts aa-unconfined to avoid using cat(1) to read
/proc/PID/cmdline entries, and instead opens them for reading directly.

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: John Johansen <john.johansen@caanonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2016-12-30 12:18:14 -08:00
Steve Beattie
4da5adbce4 utils/aa-unconfined: fix netstat usage, use ss(8) by default
It was reported that converting the netstat command to examine
processes bound to ipv6 addresses broke on OpenSUSE due to the version
of nettools not supporting the short -4 -6 arguments.

This patch switches to use the ss(8) utility from iproute2 by default
(if ss is found) as netstat/net-tools is deprecated. Unfortunately,
ss's '--family' argument does not accept multiple families, nor
does passing '--family' multiple times with different arguments work
either, so aa-unconfined invokes ss multiple times to gather the
different socket families.

It also fixes the invocation of netstat to use the "--protocol
inet,inet6" arguments instead, which should return the same results
as the short options.

This patch provides command line arguments to manually switch using
one tool or the other, as well as converting the invocations of ss
and netstat to not use a shell, and documents these options in the
aa-unconfined man page.

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: Christian Boltz <apparmor@cboltz.de>
Acked-by: John Johansen <john.johansen@canonical.com>
2016-12-30 12:15:16 -08:00
Steve Beattie
106396289a build: make documentation at tarball creation time, not during build
The latex based techdoc in the parser/ tree adds a number of build
dependencies for downstreams to create it; it also is the primary
element to make the builds unrepeatable. Creating the techdoc and other
documentation when generating a tarball for distribution avoids all
that.

* Makefile: build documentation as part of the tarball creation. Skip
  the libraries/libapparmor directory as it needs to have configure run
  before the manpages can be made.
* changehat/mod_apparmor/Makefile, changehat/mod_apparmor/Makefile,
  utils/Makefile, profiles/Makefile: create separate docs target,
  some of them dummies.
* parser/Makefile: pull the techdoc out of the default build target, add
  an extra_docs target to create it.

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: John Johansen <john.johansen@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2016-12-10 10:25:31 -08:00
Christian Boltz
5c26296b9f logparser.py: improve file vs. network event recognition
Sometimes network events come with an operation keyword looking like
file_perm which makes them look like file events. Instead of ignoring
these events (which was a hotfix to avoid crashes), improve the type
detection.

In detail, this means:
- replace OPERATION_TYPES (which was basically a list of network event
  keywords) with OP_TYPE_FILE_OR_NET (which is a list of keywords for
  file and network events)
- change op_type() parameters to expect the whole event, not only the
  operation keyword, and rebuild the type detection based on the event
  details
- as a side effect, this simplifies the detection for file event
  operations in parse_event_for_tree()
- remove workaround code from parse_event_for_tree()

Also add 4 new testcases with log messages that were ignored before.


References:

a) various bugreports about crashes caused by unexpected operation keywords:
   https://bugs.launchpad.net/apparmor/+bug/1466812
   https://bugs.launchpad.net/apparmor/+bug/1509030
   https://bugs.launchpad.net/apparmor/+bug/1540562
   https://bugs.launchpad.net/apparmor/+bug/1577051
   https://bugs.launchpad.net/apparmor/+bug/1582374

b) the summary bug for this patch
   https://bugs.launchpad.net/apparmor/+bug/1613061



Acked-by: Steve Beattie <steve@nxnw.org> for trunk and 2.10.
2016-12-06 22:24:56 +01:00
Steve Beattie
8cc7b73552 In testing, I did notice one thing not getting turned up, from
netstat -nlp46 output:

  raw6       0      0 :::58                   :::*                    7        1326/NetworkManager

which when asking netstat to display name resolution ends up being:

  raw6       0      0 [::]:ipv6-icmp          [::]:*                  7        1326/NetworkManager

Of course, aa-unconfined doesn't show this, the following patch adds
that, by adding the raw keyword as an alternative to tcp|udp and
accepting a number as an alternative to LISTEN.

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: John Johansen <john.johansen@canonical.com>
2016-12-01 17:32:52 -08:00
John Johansen
79ae8fe55a aa-unconfined currently does not check/display ipv6 fix this
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
Acked-by: Steve Beattie <steve@nxnw.org>
2016-12-01 17:29:03 -08:00
Christian Boltz
e9b7c3ff60 logparser.py parse_event(): always store family, protocol and sock_type
Storing these event details depending on the operation type only makes
things more difficult because it's hard to differenciate between file
and network events.

Note that this happens at the first log parsing stage (libapparmor log
event -> temporary python array) and therefore doesn't add a serious
memory footprint. The event tree will still only contain the elements
relevant for the actual event type.

This change means that lots of testcases now get 3 more fields (all
None) when testing parse_event(), so update all affected testcases.
(test-network doesn't need a change for probably obvious reasons.)

Also rename a misnamed test in test-change_profile.


Acked-by: Seth Arnold <seth.arnold@canonical.com> for trunk and 2.10.
2016-11-19 10:55:03 +01:00
Christian Boltz
0fd2b9835c Beautify RlimitRule coverage report
Some conditions in RlimitRule can never be hit under normal
circumstances, so this patch adds some "pragma: no cover" and
"pragma: no branch" comments to beautify the coverage report.


Acked-by: Seth Arnold <seth.arnold@canonical.com>
2016-11-08 21:23:27 +01:00
Christian Boltz
bb403893ac More test_multi profiles
This patch adds profiles for all log sniplets that are expected to
result in a profile rule.

This also means some changes in test-libapparmor-test_multi.py are
needed:
- split off log_to_profile_skip from log_to_profile_known_failures to
  - only skip tests in log_to_profile_skip (causing a crash or requiring
    user interaction)
  - run tests in log_to_profile_known_failures, but expect a non-equal
    result (caused by not added rules etc.)
- add quite some tests to log_to_profile_known_failures - they were
  skipped before because they didn't have a *.profile file.
- add handling for hats to shorten list of known failures
  This fixes testcase24 and testcase33 (after adjusting the profiles)
  and lots of the new *.profile files.
- since we now have *.profile files for all log events that should result
  in a profile rule, no longer ignore FileNotFoundError


Acked-by: Seth Arnold <seth.arnold@canonical.com>
2016-11-01 21:40:29 +01:00
Christian Boltz
2de8d20bd9 Test log to profile "translation"
This patch adds TestLogToProfile to test-libapparmor-test_multi.py which
"translates" the test_multi log sniplets to a profile, and checks if it
matches the expected profile.

The expected profile for one log event will obviously contain only one
rule, and gets added as *.profile to the test_multi directory.

This patch includes 33 test_multi profiles - which means 83 more need to
be created. Whenever you have some time, add one or two! (Please write
those test_multi profiles manually, without using the tools.)

I know some parts of the test code looks complicated. Unfortunately this
is how things work - compare it with do_logprof_pass() in aa.py...

While on it, set tests = 'invalid' which ensures a failure in case
parse_test_profiles() doesn't set the tests array, and move printing
the test name out of parse_test_profiles() to avoid printing it twice.

A nice side effect of this patch is increased test coverage:
- 30% -> 40% in aa.py (= 250 more lines)
- 52% -> 78% in aamode.py (= 23 more lines)
- 26% -> 68% in logparser.py (= 120 more lines)
- total coverage increases from 57% to 62%


Acked-by: Seth Arnold <seth.arnold@canonical.com>
2016-10-17 21:04:05 +02:00
Christian Boltz
d48c41c2d7 Drop unused 'found' counter from aa.py ask_the_questions()
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2016-10-14 20:24:56 +02:00
Christian Boltz
38cdaa1f6a Drop seen_events counter from aa.py
seen_events is a global variable in aa.py that gets increased at several
places, but isn't used (read or printed) anywhere. Since I can't imagine
how it could become useful, simply drop it.

Also drop an outdated comment in handle_children that lived next to a
seen_events line.


Acked-by: Seth Arnold <seth.arnold@canonical.com>
2016-10-14 20:24:10 +02:00
Christian Boltz
38580e82c6 Rename config_test.py to test-config.py
This little change means that the tests will run as part of 'make check'.


This commit is only a 'bzr mv utils/test/config_test.py utils/test/test-config.py'
without any changes in the file content.


Acked-by: Steve Beattie <steve@nxnw.org>
2016-10-10 23:27:19 +02:00
Christian Boltz
b0b895c51b Move str_to_mode() tests to test-aamode.py
aa_test.py doesn't run in 'make check' because its filename doesn't
match the 'test-*.py' pattern, so this move means the tests now actually
get run.

While on it, migrate test-aamode.py to use the AATest base class, and
migrate the str_to_mode() tests to a tests[] array.

After this move, aa_test.py doesn't do anything anymore, so delete it.


Acked-by: Steve Beattie <steve@nxnw.org>.


Also add another test proposed by Steve:
    (None,  set()),
2016-10-10 23:25:07 +02:00
Christian Boltz
304804addb Drop loadincludes() and mode_to_str() tests from aa_test.py
aa_test.py doesn't run in 'make check' because its filename doesn't
match the 'test-*.py' pattern.

mode_to_str() was dropped as part of the FileRule series, so it's
pointless to keep its tests. (The replacement is totally different and
has full test coverage already.)

loadincludes() still exists, but only testing if the function runs
without errors is not really helpful, so drop this test.

Also drop unused imports and add an explicit import for apparmor.aamode.


Acked-by: Steve Beattie <steve@nxnw.org>
2016-10-10 23:09:41 +02:00
Christian Boltz
93d1539f4f move globbing tests to test-aare.py
aa_test.py doesn't run in 'make check' because its filename doesn't
match the 'test-*.py' pattern.

Move tests for globbing ("plain" globbing and globbing with ext) to
test-aare.py to make sure those tests actually run.

Note: This isn't an exact move - I adjusted some of the tests to make
them more useful, and added some more tests.

Also, glob_path() and glob_path_withext() no longer exist in aa.py.
They moved to the AARE class as part of the FileRule patch series.


Acked-by: Steve Beattie <steve@nxnw.org>
2016-10-10 23:07:16 +02:00
Christian Boltz
65844a5e1f complete test coverage for FileRule
Add a testcase with exec-only permissions (which get ignored by
get_perms_for_path()) to increase FileRule test coverage to 100%.


Acked-by: Steve Beattie <steve@nxnw.org>
2016-10-10 22:08:15 +02:00
Christian Boltz
01dc04c9f9 Drop unused matchregexp(), commonsuffix() and commonprefix() from aa.py
matchregexp() was the last user of convert_regexp() in aa.py, so we
don't need to import it anymore.


Acked-by: Seth Arnold <seth.arnold@canonical.com>
2016-10-03 21:04:24 +02:00
Christian Boltz
164faa3f17 Drop CMD_CONTINUE from ui.py (twice)
The latest version of pyflakes (1.3.0 / python 3.5) complains that
CMD_CONTINUE is defined twice in ui.py (with different texts).

Funnily CMD_CONTINUE isn't used anywhere, so we can just drop both.



Acked-by: Seth Arnold <seth.arnold@canonical.com> for trunk, 2.10 and 2.9
2016-10-03 21:01:29 +02: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
ac6fe7f2e4 [42/38] Drop more unused functions from aa.py
After looking at matchliteral(), I found out that it's only user is
rematchfrag(), which is only called in a) an "if False:" block and
b) match_include_to_path() - and that is only called by the also unused
match_prof_incs_to_path() function.

This patch drops some dead code (like the mentioned "if False:" block)
and the now unused functions
- matchliteral()
- rematchfrag()
- match_include_to_path()
- match_prof_incs_to_path()


This patch is also THE ANSWER to the question when I'll finally consider
this patch series complete.

42. It can't become better than that! ;-)



Acked-by: Steve Beattie <steve@nxnw.org>
2016-10-01 20:22:27 +02:00
Christian Boltz
c9a1a02c83 [41/38] let aa-mergeprof ask about new hats and subprofiles
If a merged profile contains additional hats or subprofiles, the "old"
aa-mergeprof silently created them as additional hasher elements (partly
buggy, because subprofiles would end up as '^/subprofile' instead of
'profile /subprofile'). After switching to FileRule, aa-mergeprof crashes
on new hats or subprofiles.

This patch adds code to ask the user if the new hat or subprofile should
be added - which means this patch replaces two bugs (crash + silently
adding subprofiles and hats) with a new feature ;-)


The new questions also add a new text CMD_ADDSUBPROFILE in ui.py.

Finally, the new "button" combinations get added to test-translations.py.



If you want to test, try to aa-mergeprof this profile (the subprofile
and hat are dummies, nothing ping would really require):


#include <tunables/global>
/{usr/,}bin/ping {
  #include <abstractions/base>
  #include <abstractions/consoles>
  #include <abstractions/nameservice>

  capability net_raw,
  capability setuid,
  network inet raw,
  network inet6 raw,

  /{,usr/}bin/ping mixr,
  /etc/modules.conf r,

  ^hat {
    /bin/hat r,
    /bin/bash px,
  }

  profile /subprofile {
    /bin/subprofile r,
    /bin/bash px,
 }

  # Site-specific additions and overrides. See local/README for details.
  #include <local/bin.ping>
}



Note that this patch is not covered by unittests, but it passed all my
manual tests.



Acked-by: Steve Beattie <steve@nxnw.org>

Bug: https://launchpad.net/bugs/1507469
2016-10-01 20:21:06 +02:00
Christian Boltz
71f67354f3 [40/38] Load all includes in aa-mergeprof ask_the_questions()
aa-mergeprof empties 'includes' when running reset_aa(). The result is
    KeyError: 'abstractions/newly_added_abstraction'
if an include file gets added because it isn't part of 'includes' at
this time. Note that you'll need to add another rule after adding the
include to trigger checking the includes for superfluous rules.


This fixes the regression found by Steve - which isn't really a
regression, "just" one more thing that got more visible with the new
code. Before, it was just an ill-addressed hasher that didn't complain ;-)


Acked-by: Steve Beattie <steve@nxnw.org>
2016-10-01 20:20:27 +02:00
Christian Boltz
7e86ad1602 [39/38] Ignore exec events for non-existing profiles
The switch to FileRule made some bugs visible that survived unnoticed
with hasher for years.

If aa-logprof sees an exec event for a non-existing profile _and_ a
profile file matching the expected profile filename exists in
/etc/apparmor.d/, it asks for the exec mode nevertheless (instead of
being silent). In the old code, this created a superfluous entry
somewhere in the aa hasher, and caused the existing profile to be
rewritten (without changes).

However, with FileRule it causes a crash saying

      File ".../utils/apparmor/aa.py", line 1335, in handle_children
        aa[profile][hat]['file'].add(FileRule(exec_target, file_perm, exec_mode, rule_to_name, owner=False, log_event=True))
    AttributeError: 'collections.defaultdict' object has no attribute 'add'

This patch makes sure exec events for unknown profiles get ignored.



Reproducer:

    python3 aa-logprof -f <(echo 'type=AVC msg=audit(1407865079.883:215): apparmor="ALLOWED" operation="exec" profile="/sbin/klogd" name="/does/not/exist" pid=11832 comm="foo" requested_mask="x" denied_mask="x" fsuid=1000 ouid=0 target="/sbin/klogd//null-1"')

This causes a crash without this patch because
/etc/apparmor.d/sbin.klogd exists, but has
    profile klogd /{usr/,}sbin/klogd {



References: https://bugs.launchpad.net/bugs/1379874



Acked-by: Steve Beattie <steve@nxnw.org> for trunk, 2.10 and 2.9
2016-10-01 20:17:11 +02:00