Commit graph

647 commits

Author SHA1 Message Date
Christian Boltz
a98426820b improve check_for_apparmor() for better readability and (minor)
performance improvement

Patch by Peter Maloney <peter.maloney@brockmann-consult.de>

Acked-by: Christian Boltz <apparmor@cboltz.de>
(previous patch version (with minor difference) also
 Acked-by: Seth Arnold <seth.arnold@canonical.com>)
2014-11-27 18:34:45 +01:00
Christian Boltz
c7626ec0eb Add tests for sub_str_to_mode() and split_log_mode() in aamode.py.
Acked-by: Steve Beattie <steve@nxnw.org>
2014-11-27 13:54:22 +01:00
Christian Boltz
b6dd8bbb81 Fix the usage of the aa hasher in aa.py handle_children() - for
interpreters, it used
    aa[profile][hat]['path'][interpreter_path]['mode']
instead of
	aa[profile][hat]['allow']['path'][interpreter_path]['mode']

The ['allow'] part was missing.


Acked-by: Steve Beattie <steve@nxnw.org>
2014-11-27 00:42:06 +01:00
Steve Beattie
8841086c01 utils: abstract out writing prior segments
This patch pulls out all the common processing for writing out
each of the prior segments that need to be written before writing
the current segment into a function called 'write_prior_segments',
reducing a bunch of ugly duplication.

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2014-11-26 14:32:25 -08:00
Steve Beattie
a5d38d3dc9 utils: fix include prior segments indention
The assignment for setting segments['include'] = True was wrong,
it occured inside the 'if not segments['include'] and True in
segments.values():' block, whereas it needed to always get set outside
of that if test.

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2014-11-26 14:28:21 -08:00
Christian Boltz
21b6e70865 fix some performance killer bugs in aa.py
This patch
- fixes a check that used if "aa[profile][hat][incname]:" instead of
  "if aa[profile][hat]['include'].get(incname, False):" ("['include']"
  was missing) which means the performance shortcut was never hit
- avoids auto-created empty and superfluous hashers in
  aa[profile][hat]['allow']['path'] and
  include[incfile][incfile][allow]['path']
- adds the filename to the "Can't find system log" exception

Patch by Peter Maloney <peter.maloney@brockmann-consult.de>


Changes compared to the original patch:
- change back quoting in the exception message to '...'


Acked-By: Christian Boltz <apparmor@cboltz.de>
2014-11-26 20:25:07 +01:00
Steve Beattie
961ab6d6e0 utils: add support for alternate pyflakes versions
Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2014-11-17 12:32:30 -08:00
Kshitij Gupta
a3aee5ce33 The make check for utils was failing due to the raw_input being
present as a function (which it is not in Python3, even though it was
under an if else python version check).

The following patch:
- checks the __builtins__ module for existence of raw_input and sets
it up for Python3

Acked-by: Steve Beattie <steve@nxnw.org>
2014-11-17 18:00:04 +05:30
Christian Boltz
5d148c4ac6 Fix leading whitespace in "(V)iew changes" and cleanup profile writing
The diff displayed by (V)iew changes in aa-logprof lacks leading
whitespace because it bases the whitespace on the indention level of the
closing "}".

Besides that, it uses different values for the indention level for newly
added rule types than for existing rule types. (For example, if a
profile already had a network rule, added network rules will get a
different indention level.) However you won't notice this because
currently it doesn't indent the rules at all ;-)

This patch fixes serialize_profile_from_old_profile() in aa.py so that
it always uses the correct indention level.


Also clean up and simplify how the profile is written in the end (when
matching RE_PROFILE_END) - we already have "write_methods", so we can
just re-use it instead of "manually" calling one write_* function after
the other. Unfortunately dicts don't keep their original order,
therefore I had to introduce "default_write_order".


Finally, add some missing rule types to "segments" to avoid key errors.


Acked-by: Steve Beattie <steve@nxnw.org>
2014-11-15 12:51:24 +01:00
Christian Boltz
23da51b079 fix audit toggle for capability (when asking in logprof)
When aa-logprof asks for adding capability rules, it also offers the
Audi(t) option. Unfortunately, this option does nothing ;-)

This patch fixes ask_the_question() so that it really ;-) allows to
switch the audit flag on and off. It also initializes the "audit"
variable to make sure the next capability doesn't inherit the audit flag
used for the previous capability.


Acked-by: Steve Beattie <steve@nxnw.org>
2014-11-15 01:38:29 +01:00
Christian Boltz
be287de823 fix and improve recursive_print()
This patch for recursive_print() in common.py fixes printing dicts with
py3. It also replaced the tabs() lambda function with a plain string,
and the brace() lambda function with a simple formatstring to make the
code easier to understand.

Also add support for nested lists - for the start and end of each list,
print a [ and ]. Without that, you get a long list of items without an
indicator if/when a new parent list starts.


Acked-by: Steve Beattie <steve@nxnw.org>
2014-11-15 01:08:37 +01:00
Christian Boltz
3364eadafc Add "AppArmorBug" Exception (for things that "should never happen")
Acked-by: Steve Beattie <steve@nxnw.org>
2014-11-14 23:52:38 +01:00
Christian Boltz
365d7036a5 fix "unknown capability: CAP_whatever" in aa-logprof
When aa-logprof asks for a capability, you'll see something like

    WARN: unknown capability: CAP_block_suspend

The reason for the warning and "Severity: unknown" is that severity.db
contains the capability names in uppercase, but ask_the_question() calls
sev_db.rank with the capability in lowercase.

This patch converts the "CAP_$capability" string to uppercase before
doing the lookup.

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



Also add a testcase (written by Steve Beattie) to ensure this stays fixed.

Acked-by: Seth Arnold <seth.arnold@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2014-11-14 02:27:33 +01:00
Christian Boltz
422680714b utils/Makefile: remove superfluous grep -v from pyflakes call
utils/Makefile contains a grep -v "undefined name _". Some manual
testing shows that pyflakes doesn't print any warning about "_", so
this grep is superfluous.

Removing the grep also means we don't need a tempfile for the pyflakes
output anymore, which simplifies the pyflakes call a lot.


Acked-by: Steve Beattie <steve@nxnw.org>
2014-11-14 02:21:23 +01:00
Steve Beattie
1a9505a4ef utils: fix pyflakes issues with test-severity
The recent re-work of the severity.db tests were not verified to
pyflakes clean. All but one of pyflakes co are of marginal impact
(assigning to a variable that isn't later referenced); however, one
legitimate issue it detected is that I inadvertently created two test
cases with the same method name, so only one test case would actually
be used.

The following patch fixes the issues.

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2014-11-13 10:58:50 -08:00
Christian Boltz
4dd956461b Import regexes from apparmor.regex one by one (instead of *) to make
pyflakes happy

Acked-by: Steve Beattie <steve@nxnw.org>
2014-11-13 19:17:10 +01:00
Christian Boltz
e188f04ce4 Move the profile parsing regexes from aa.py to a new file (regex.py)
and add an "import" line so that they are still available in aa.py.


Acked-by: Kshitij Gupta <kgupta8592@gmail.com>
2014-11-12 00:05:04 +01:00
Christian Boltz
c38062f9b0 make clean: delete "common" symlink in all directories
Acked-by: <timeout>
2014-11-10 20:14:35 +01:00
Christian Boltz
6b4cede82c let RE_EOL strip() whitespace from <comment>
Acked-by: Kshitij Gupta <kgupta8592@gmail.com>
2014-11-09 01:33:40 +01:00
Christian Boltz
a3d9de704c aa.py: let parse_audit_allow also match comments
Note: this is v1 of the patch - I'll send a patch for RE_EOL to trim 
whitespace.


Acked-by: Kshitij Gupta <kgupta8592@gmail.com>
2014-11-08 16:47:39 +01:00
Steve Beattie
43f8bd778f utils: rename severity.db unit tests
This commit renames the unit test script for the severity db so that it
will be included in the 'make check' and 'make coverage*' targets.

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2014-11-06 12:44:04 -08:00
Steve Beattie
3bf5e5b1d9 utils: fix Severity(None) condition
This patch fixes Severity.__init__() when it is not given an argument to
raise an AppArmor exception rather than returning a Severity object in
an incompletely initialized state. It also adjusts a test case covering
this situation.

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2014-11-06 12:37:02 -08:00
Steve Beattie
e26f139025 utils: re-work the severity.db unit tests
This patch is a re-work of the severity_test.py tests, to break them
up into individual unit tests, and to add coverage for detecting
an invalid severity database (it does reduce the coverage for walking
profiles to find variable declarations, but that should be pulled out of
the severity handling code anyway).

Note that the last test case will fail, because even though the code
path in Severity.__init__() looks like it will return None if no path
is given, a Severity object in a half-state of initialization will
actually be returned.

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2014-11-06 12:32:49 -08:00
Christian Boltz
c35a4c412d disallow directories as logfile argument in aa-logprof (follow-up
for r2769, which only checked for "exists")

Also allow everything except directories as logfile argument in 
aa-genprof.

Acked-by: Steve Beattie <steve@nxnw.org>
2014-11-05 20:25:44 +01:00
Steve Beattie
06c3e0be75 utils: add python coverage generation
This patch adds support for generating test coverage information for the
python utils.

To view a text based report, in the test subdirectory do:

  make coverage-report

To generate detailed html reports, do:

  make coverage-html

And then point your web browser at
$(YOUR_CURRENT_WORKING_TREE)/utils/test/htmlcov/index.html .
An alternate output location can be specified by setting the
COVERAGE_OUT variable, e.g.

  make coverage-html COVERAGE_OUT=/tmp/coverage/

(the output directory does not need to exist beforehand.)

To generate only the coverage data, do:

  make coverage

or

  make .coverage

(The coverage data generated by python is stored in the .coverage
file.)  This essentially runs make check, using a single python
interpreter, and records which lines and branches of the python code
were exercised.

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: Christian Boltz <apparmor@cboltz.de>
Acked-by: John Johansen <john.johansen@canonical.com>
2014-11-04 13:01:14 -08:00
Peter Maloney
d32e838be3 utils: allow logprof to be passed fifos as logfile arguments
From: Peter Maloney <peter.maloney@brockmann-consult.de>

This patch allows the common idiom 'aa-logprof -f <(SOME COMMAND)' to
work,

Acked-by: Steve Beattie <steve@nxnw.org>
2014-11-04 12:55:26 -08:00
Launchpad Translations on behalf of apparmor-dev
93eb36404d Launchpad automatic translations update. 2014-10-28 05:41:09 +00:00
Steve Beattie
37fc93807a utils: re-org tests Makefile a bit
This patch moves the declaration of phony and quieted make targets
to a single section, to avoid repeated lines. It's not so useful
for just two targets, but future patches will add more targets with
similar attributes.

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: John Johansen <john.johansen@canonical.com>
2014-10-27 11:23:49 -07:00
Launchpad Translations on behalf of apparmor-dev
57a8d2c94c Launchpad automatic translations update. 2014-10-23 05:37:49 +00:00
Launchpad Translations on behalf of apparmor-dev
0fb504868e Launchpad automatic translations update. 2014-10-22 05:36:29 +00:00
Launchpad Translations on behalf of apparmor-dev
53517ee6dc Launchpad automatic translations update. 2014-10-21 05:40:24 +00:00
Christian Boltz
8e2a7304cb fix netrules_access_check()
netrules_access_check() in aa.py checks if 
    type(netrules['rule'][family]) == dict
however this check always returns false (at least with py3, I didn't
test with py2).

This broken type check is the reason for
https://bugs.launchpad.net/apparmor/+bug/1380368
    aa-logprof doesn't propose abstractions for network rules
and 
https://bugs.launchpad.net/apparmor/+bug/1380367
    aa-logprof asks for already existing network rules
which are both fixed with this patch.


The type check is needed because netrules['rule'][family] can be
boolean True (for rules like "network inet,") - see line 2994.

The sock_type in .... .keys() check is there to ensure the hasher doesn't 
automagically add an empty sub-dict, which caused the regression in the 
first version of my patch.


Acked-by: Kshitij Gupta <kgupta8592@gmail.com>
2014-10-20 22:40:42 +02:00
Christian Boltz
9b4516e5ef use correct variable in match_net_includes()
match_net_includes() in aa.py gets "nettype" as parameter, but then uses 
"type" when calling valid_include(). "type" is a global variable, but not 
what we want to use here ;-)


Acked-by: Kshitij Gupta <kgupta8592@gmail.com>
2014-10-20 20:07:24 +02:00
Launchpad Translations on behalf of apparmor-dev
7d3e375e25 Launchpad automatic translations update. 2014-10-20 05:41:55 +00:00
Steve Beattie
bedcb8c19c Merge from lp translations branch: a few de updates, some en_GB additions. 2014-10-17 12:07:51 -07:00
Launchpad Translations on behalf of apparmor-dev
c504acb09e Launchpad automatic translations update. 2014-10-17 05:36:15 +00:00
Steve Beattie
39b9985afb utils: make html version of apparmor.vim manpage
Bug: https://bugs.launchpad.net/bugs/1366572

In the move of the apparmor.vim manpage source from the parser
directory to utils/vim/, the creation of the html version of the
manpage was lost.  This patch fixes that, as well as fixes the vim
Makefile to use the common/Make.rules _clean target (which clears the
pod2htm*.tmp files created by pod2html as well).

It also fixes a bug in common/Make.rules where the _clean target
would report an error when used in a directory where the Makefile
doesn't set the NAME variable, such as utils/vim/.

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2014-10-16 15:50:13 -07:00
Christian Boltz
2758e2e2a1 apparmor.vim: add support for the "hat" keyword
Acked-by: Steve Beattie <steve@nxnw.org>
2014-10-16 23:55:43 +02:00
Christian Boltz
1203923be3 more aa-mergeprof fixes
- change --help for files - "Profile(s) to merge" instead of "base profile"
- display the profile to save when asking to save it
- disable searching for existing network rules in abstractions because 
  it crashes. This doesn't hurt too much, see 
  https://bugs.launchpad.net/apparmor/+bug/1382241


Acked-by: Steve Beattie <steve@nxnw.org>
2014-10-16 23:35:06 +02:00
Christian Boltz
aeebcbc243 various aa-mergeprof fixes
- remove some debug output (which Kshitij intentionally kept in the 
  draft patch)
- add a UI_Info to display which profile will be merged
- disable the mergeprofiles.clear_common() call because it crashes
  (https://bugs.launchpad.net/apparmor/+bug/1382236)
- disable (M)ore (CMD_OTHER) because it crashes
- make (F)inish work everywhere
- change the help text so that it doesn't mention 3-way-merge until we
  implement it
2014-10-16 23:25:33 +02:00
Christian Boltz
31c4dc80a8 fix parsing of "hat foo {" lines
The python utils didn't know about the hat keyword, for example
    hat foo {

This patch changes RE_PROFILE_HAT_DEF to add support for the hat keyword.

Note that this patch only changes profile reading - when saving a 
profile, the ^foo syntax will always be used.

While on it, also convert the regex to named matches - the result 
numbering changed anyway ;-)


Acked-by: Kshitij Gupta <kgupta8592@gmail.com>
2014-10-16 22:03:42 +02:00
Christian Boltz
31a57bdfa9 update the aa-mergeprof manpage to match the new commandline syntax
introduced with the aa-mergeprof patch collection.

Acked-by: Steve Beattie <steve@nxnw.org>
2014-10-16 20:26:45 +02:00
Christian Boltz
4f4eded593 aa-mergeprof: don't ask for includes that are already there
Acked-by: Steve Beattie <steve@nxnw.org>
2014-10-16 20:22:52 +02:00
Christian Boltz
4f9183b23e aa-mergeprof cmdline changes, disable 3-way-merge for now
This is the rebased version of the patch by
Kshitij Gupta <kgupta8592@gmail.com>


(mostly) original patch description:

Changes to facilitate 2-way merge (maybe also 3-way) of multiple
profiles as discussed on IRC

This patch
- moves reset method to reset_aa function
- modifies message displayed to user
- allows processing of multiple files in 2-way merge
- disables 3-way merge till new syntax has been decided

The changes reflect the approach of providing arbitrary number of
files using wildcards or explicitly.

The changes map the profiles in the given files to their respective
files in the local directory specified using -d. Then the merges take
place profile-wise.


Acked-by: Steve Beattie <steve@nxnw.org>.
2014-10-16 20:06:45 +02:00
Christian Boltz
3cb61c5dae highlight comments in alias and link rules with correct color
(in blue, instead of the color used for the rule itsself)

Acked-by: Steve Beattie <steve@nxnw.org>
2014-10-14 21:36:41 +02:00
Christian Boltz
c613d8a884 fix "list index out of range" when allowing inet rules
When adding inet rules in aa-logprof, it crashes with
    IndexError: list index out of range

The reason is that it doesn't display the options if only the raw rule
is available (aka "no abstraction").

This patch checks if options[] is set and otherwise sets selection to
the raw rule.


Acked-by: Steve Beattie <steve@nxnw.org>
2014-10-14 21:28:32 +02:00
Christian Boltz
d11055aa0c Fix "Invalid mode found: /usr/lib64/firefox/firefox.sh"
After several hours of debugging on IRC and flooding paste.opensuse.org ;-)
I finally found the reason - reading(!) from log_dict with a wrong 
key caused the creation of that strange mode in log_dict.keys().

While finding that bug was very hard, the fix is easy - just replace 
"profile" with "aamode". (That probably makes one char per hour of 
debugging...)

To improve that ratio, also add a warning to common.py so that this 
interesting[tm] behaviour of hasher() is at least documented.


Acked-by: Steve Beattie <steve@nxnw.org>
2014-10-14 12:54:39 +02:00
Christian Boltz
7e84f4efe9 If a filename mentioned in audit.log contains an @, aa-logprof crashes with
...
  File "/home/cb/apparmor/HEAD-CLEAN/utils/apparmor/severity.py", line 147, in handle_variable_rank
      variable = regex_variable.search(resource).groups()[0]
	  AttributeError: 'NoneType' object has no attribute 'groups'

handle_variable_rank() checked with   if '@' in resource:
and if it finds it, expects it can match a variable, which means   @{.....}
If a filename contains a   @   this fails.

The patch fixes the if condition so that it does a regex match.

It also adds two testcases for filenames containing @ to make sure they
don't cause a crash and result in the exptected severity rank.


Acked-by: Steve Beattie <steve@nxnw.org>
2014-10-14 12:50:20 +02:00
Christian Boltz
9a960a22a9 prevent adding multiple quotes in file rules
aa-cleanprof (and others?) duplicate quotes in file rules.

If a profile contains
      "/bin/foo bar" mrix,
and I run aa-cleanprof on it several times, I end up with
   """"""/bin/foo bar"""""" mrix,

This patch calls strip_quotes on the pathname.
(If needed, the quotes are re-added when writing the profile - tested 
with aa-cleanprof.)

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


Acked-by: Kshitij Gupta <kgupta8592@gmail.com>
2014-10-11 23:30:46 +02:00
Christian Boltz
7ef2ae9b05 Prevent encoding errors when reading a file
This patch changes open_file_read() and open_file_write() to use 
errors='surrogateescape' (with fallback to 'replace' for py2).

This avoids a crash when reading a logfile with special characters that 
are not utf8-encoded (for example a latin1 "ö"), and also avoids crashes 
at several other places we don't know yet ;-)

The patch also changes open_file_read() and open_file_write() to wrapper
functions, and moves the "real" code to the new open_file_anymode() 
function.

Also, I removed the try/except - it's superfluous because it throws the 
exception without any modifications.


Acked-by: Kshitij Gupta <kgupta8592@gmail.com>
2014-10-10 20:35:32 +02:00