Commit graph

33 commits

Author SHA1 Message Date
Christian Boltz
90e5294578 load variables in ask_the_questions()
Variables can be used in several rule types (from the existing *Rule
classes: change_profile, dbus, ptrace, signal). It seems nobody uses
variables with those rules, otherwise we'd have received a bugreport ;-)

I noticed this while working on FileRule, where usage of variables is
more common. The file code in bzr (not using a *Rule class) already
loads the variables, so old versions don't need changes for file rule
handling.

However, 2.10 already has ChangeProfileRule and therefore also needs
this fix.


Acked-by: Seth Arnold <seth.arnold@canonical.com> for trunk and 2.10.
2016-05-10 14:34:40 +02:00
Christian Boltz
4918107a6f Improve exception handling
Instead of always showing a backtrace,
- for AppArmorException (used for profile syntax errors etc.), print only
  the exceptions value because a backtrace is superfluous and would
  confuse users.
- for other (unexpected) exceptions, print backtrace and save detailed
  information in a file in /tmp/ (including variable content etc.) to
  make debugging easier.

This is done by adding the apparmor.fail module which contains a custom
exception handler (using cgitb, except for AppArmorException).

Also change all python aa-* tools to use the new exception handler.

Note: aa-audit did show backtraces only if the --trace option was given.
This is superfluous with the improved exception handling, therefore this
patch removes the --trace option. (The other aa-* tools never had this
option.)


If you want to test the behaviour of the new exception handler, you can
use this script:

#!/usr/bin/python

from apparmor.common import AppArmorException, AppArmorBug
from apparmor.fail import enable_aa_exception_handler

enable_aa_exception_handler()

# choose one ;-)
raise AppArmorException('Harmless example failure')
#raise AppArmorBug('b\xe4d bug!')
#raise Exception('something is broken!')


Acked-by: Seth Arnold <seth.arnold@canonical.com>
2015-07-06 22:02:34 +02:00
Christian Boltz
2754e2964b Move re_match_include() to regex.py and improve it
The function is basically a wrapper around a regex, so regex.py is a
much better home.

While on it, rename the regex to RE_INCLUDE, change it to named matches,
use RE_EOL to handle comments and compile it outside the function, which
should result in a (small) performance improvement.

Also rewrite re_match_include(), let it check for empty include
filenames ("#include <>") and let it raise AppArmorException in that
case.

Finally, adjust code calling it to the new location, and add some tests
for re_match_include()


Acked-by: Kshitij Gupta <kgupta8592@gmail.com>
2015-06-19 21:41:41 +02:00
Christian Boltz
5a0d64a70e Get variable names in aa-mergeprof ask_the_questions() in sync with aa.py
Add two variable references (aa and changed) in aa-mergeprof
ask_the_questions() so that the code can use the short name and be more
in sync with aa.py ask_the_questions().

With this patch applied, the "for ruletype in ['capability', 'network']:"
block is in sync, with the exception of the sections that intentionally
differ:
- the check for the profile mode
- the default button selection based on profile mode
- the seen_events counter

The patch also includes some minor whitespace fixes.


Acked-by: Steve Beattie <steve@nxnw.org>
2015-06-08 22:25:44 +02:00
Christian Boltz
59c5683526 Add support for change_profile rules to aa-mergeprof
Acked-by: Steve Beattie <steve@nxnw.org>
2015-06-06 14:19:59 +02:00
Christian Boltz
cac52fbf23 Import some aa.py functions into aa-mergeprof by name
This allows to drop the "apparmor.aa." prefix in ask_the_question() to
get the code more in sync with aa.py ask_the_question().


Acked-by: Steve Beattie <steve@nxnw.org>
2015-06-06 14:17:25 +02:00
Christian Boltz
8f3688c8d7 aa-mergeprof: move creating the headers for capabilty and network rules inside the loop
Move the code to set q.headers, q.functions and q.default for network
and capability rules inside the "while not done" loop. This ensures to
always have valid headers (for example, after changing the audit
qualifier, the severity was "lost" before) and avoids some duplicated
code.

Also drop a useless "if True:" condition and change the whitespace of
the following lines.


Acked-by: Steve Beattie <steve@nxnw.org>
2015-06-06 14:09:38 +02:00
Christian Boltz
bc259033f9 Replace duplicate code in aa-mergeprof with a loop
Now that the handling for capability and network rules is the same,
wrap the former network rule-only code with
    for ruletype in ['capability', 'network']:
and delete the superfluous ;-) capabiltiy code block.

Needless to say that future updates for other rule types will be
quite easy ;-)


Acked-by: Steve Beattie <steve@nxnw.org>
2015-06-06 14:08:47 +02:00
Christian Boltz
902f88b0bb Add and use logprof_header() and logprof_header_localvars() in *Rule classes
BaseRule:
- add logprof_header() - sets the 'Qualifier' (audit, allow/deny) header
  if a qualifier is specified, calls logprof_header_localvars() and then
  returns an array of headers to display in aa-logprof and aa-mergeprof
- add logprof_header_localvars() - dummy function that needs to be
  implemented in the child classes

NetworkRule: add logprof_header_localvars() - adds 'Network Family'
and 'Socket Type' to the headers

CapabilityRule: add logprof_header_localvars() - adds 'Capability' to
the headers

Also change aa-mergeprof to use rule_obj.logprof_header() for network
and capability rules. This means deleting lots of lines (that moved to
the *Rule classes) and also deleting the last differences between
capabiltiy and network rules.

Finally add tests for the newly added functions.


Acked-by: Steve Beattie <steve@nxnw.org>
2015-06-06 14:04:11 +02:00
Christian Boltz
babebceaf3 Unify code for network and capability rules in aa-mergeprof
This means:

a) for capability rules:
   -  move audit and deny to a new "Qualifier" header (only displayed if
      non-empty)
   -  always display options, even if only one is available
   -  use available_buttons(), which means to add the CMD_AUDIT_* button
   -  add handling for CMD_AUDIT_* button
   -  CMD_ALLOW: only add rule_obj if the user didn't select a #include
   -  move around some code to get it in sync with network rule handling

b) for network rules
   -  move audit and deny to a new "Qualifier" header (only displayed if
      non-empty)
   -  call rule_obj.severity() (not implemented for network rules, does
      nothing)
   -  change messages to generic 'Adding %s to profile.'
   -  move around some code to get it in sync with capability rule
      handling

The only remaining difference is in q.headers[] and the variables
feeding it:
- capability rules show "Capability: foo"
- network rules show "Network Family: foo" and "Socket type: bar"



Acked-by: Steve Beattie <steve@nxnw.org>
2015-06-06 14:02:02 +02:00
Christian Boltz
b79fbc9be4 Change aa-logprof and aa-mergeprof to read the severity from CapabilityRule
Note: the   != sev_db.NOT_IMPLEMENTED:   check in aa-mergeprof is
superfluous for capabilities, but will become useful once this code
block is used for other rule types.


Acked-by: Steve Beattie <steve@nxnw.org>
2015-06-06 14:01:01 +02:00
Christian Boltz
0fd30653fd Use generic names in aa-mergeprof
Replace rule-specific names with generic names:
- s/'capability'/ruletype/
- s/cap_obj/rule_obj/
- s/'network'/ruletype/
- s/net_obj/rule_obj/

Also set ruletype at the beginning of each block.

The long-term goal is to have
    for ruletype in ['capability', 'network', ...]:
with common code to handle all rule types, and having common names makes
it easier to compare the blocks.


Acked-by: Steve Beattie <steve@nxnw.org>
2015-05-29 23:03:51 +02:00
Christian Boltz
0461c60a6e cleanup superfluous variable assignments in aa-mergeprof
aa-mergeprof has some sections where it first resets the 'deleted'
variable, and then overwrites it again a line or two later.
This patch removes the superfluous variable resets.


Acked-by: Steve Beattie <steve@nxnw.org>
2015-05-29 13:01:37 +02:00
Christian Boltz
c795a1f228 Update aa-mergeprof to use the NetworkRule(set) class layout
aa-mergeprof still used the old aa[profile][hat][allow]['netdomain']
which no longer gets populated. This resulted in not asking for merging
any network rules.

This patch changes ask_the_question() to the NetworkRule(set) layout.
Besides that,
- don't ask for network rules that are already covered.
  Using is_known_rule() also fixes
  https://bugs.launchpad.net/apparmor/+bug/1382241
- include the audit keyword in the "Network Family" headline
  (I'd prefer to just use the get_clean() rule, but that's another topic)
- hide "(A)llow" when merging a deny rule
- as a side effect of using NetworkRule, fix crashes for 'network,' and
  'network foo,' rules

To avoid having to repeat the list of available "buttons" and the logic
to update that list, add a available_buttons() function that returns the
list of available buttons depending on rule_obj.deny and rule_obj.audit
to aa.py, and import it into mergeprof.

I tested all changes manually.


Acked-by: Steve Beattie <steve@nxnw.org>
2015-05-29 01:12:38 +02:00
Christian Boltz
7728556972 Update aa-mergeprof to use the CapabilityRule(set) class layout
aa-mergeprof still used the old aa[profile][hat][allow]['capability']
which no longer gets populated - which resulted in not asking for
merging any capabilities.

Actually (and funnily),
-                if other.aa[profile][hat].get(allow, False):
-                    continue
resulted in never merging capability rules even before the change to
CapabilityRule(set) - this was meant as optimization, but a "not" was
missing in the condition ;-) so it always skipped capability rules.

The patch changes ask_the_question to the CapabilityRule(set) layout.
Besides that,
- include the audit and deny keywords in the "Capability" headline
  (I'd prefer to just use the get_clean() rule, but that's another topic)
- hide "(A)llow" when merging a deny rule
- don't ask for capabilities that are already covered

Also delete match_cap_includes() from aa.py, which is no longer used.


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

Bug: https://launchpad.net/bugs/1382241
2015-05-29 00:17:02 +02:00
Christian Boltz
bb56f039a8 Fix raising AppArmorException in aa-mergeprof
aa-mergeprof failed to fail ;-) when it should raise an AppArmorException.
Instead, it failed with

    AttributeError: 'module' object has no attribute 'AppArmorException'

I confirmed this bug in trunk and 2.9.


Acked-by: Steve Beattie <steve@nxnw.org> for trunk and 2.9.
2015-05-18 01:35:51 +02:00
Christian Boltz
da8230fe95 remove superfluous/outdated comment about m / ix
For the records: the valid statement from the comment is:
    # m is not implied by ix


Acked-by: Kshitij Gupta <kgupta8592@gmail.com>
2015-01-30 21:08:17 +01: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
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
Kshitij Gupta
52cd4835e2 Refractor prompts in utils.
The following patch:
- creates a class for prompt questions moving away from Perl hash hack
for the purpose.
- moves some functions to the methods for that class
- fix options being incorrectly passed to questionPrompt in aa-mergeprof

Acked-by: Christian Boltz <apparmor@cboltz.de>
2014-10-07 18:36:01 +05:30
Kshitij Gupta
b7ff639d73 Fix warnings generated due to unnamed arguments in translatable strings.
This patch:
- replaces unnamed arguments with named arguments wherever more than 1
one arguments ware present in a message
- minor fix in aa-unconfined for pname argument in 2 strings
- updated pot files (as a side-effect of testing with make)

Acked-by: Christian Boltz <apparmor@cboltz.de>
2014-09-14 23:47:00 +05:30
Christian Boltz
9e4c01f8d3 Add a warning to aa-mergeprof --help that the syntax will change in the
future. Also remove --auto, which is not implemented yet.
2014-09-04 01:49:47 +02:00
Christian Boltz
356e7cb199 aa-mergeprof: honor -d parameter
(it was ignored until now)

Acked-by: Kshitij Gupta <kgupta8592@gmail.com>.
2014-08-04 20:19:08 +02:00
Christian Boltz
241544ad8a modify aa-mergeprof to:
- allow users to merge two profiles (2-way merge) using aa-mergeprof by
  making the third profile optional
- re-enable code that cleaned up base and other profile and using it in
  deleted count (was disabled due to pyflakes thinking it was unused)

Patch by Kshitij Gupta <kgupta8592@gmail.com>


Acked-by: Christian Boltz <apparmor@cboltz.de>
2014-07-29 12:39:12 +02:00
Christian Boltz
be6cb6724d fix aa-mergeprof to
- import apparmor.ui as aaui
- call aaui.UI_*() instead of apparmor.aa.UI_*()
- use apparmor.aamode.AA_MAY_EXEC instead of apparmor.aa.AA_MAY_EXEC


Acked-by: Kshitij Gupta <kgupta8592@gmail.com>.
2014-07-28 00:24:26 +02:00
Steve Beattie
ec7676bdec utils/aa-*: adjust python shebang lines to ease rewriting to an
alternate python if installed via the python-tools-setup.py script.
2014-02-14 14:42:19 -08:00
Steve Beattie
f652178a02 aa-mergeprof: fixup some of the whitespace issues 2014-02-13 08:31:59 -08:00
Steve Beattie
d318ff6100 Fix up last pyflakes issues with tools 2014-02-13 08:24:02 -08:00
Steve Beattie
15a95e3b36 Fix up some more pyflakes issues with the tools 2014-02-13 08:20:59 -08:00
Steve Beattie
32e0931588 Fix up execute permissions that were lost in the merger for some reason. 2014-02-12 16:59:27 -08:00
Steve Beattie
f989dd0132 Merge in Kshitij Gupta <kgupta8592@gmail.com>'s rewrite of the
logprof/genprof and related utilities in python. Because the branch that
was worked on was not based on the apparmor tree, not all of the history
can be maintained for files that are not newly created or entirely
rewritten in the branch.

(This merge also includes a subsequent commit to the branch
I was merging from which includes my missed bzr add of
utils/apparmor/translations.py)
2014-02-12 15:54:00 -08:00