Commit graph

42 commits

Author SHA1 Message Date
Georgia Garcia
e1ae6fa81c utils: aa-genprof fails on lxd with OSError: Read-only file system
On certain lxc containers, when aa-genprof tries to set
printk_ratelimit, it fails with the OSError exception, with the
message "OSError: [Errno 30] Read-only file system" instead of
PermissionError.

Since PermissionError is a subclass of OSError, replace it by broader
OSError exception to include both cases in which running aa-genprof
fails.

Reported-by: Paulo Flabiano Smorigo <paulo.smorigo@canonical.com>
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-02-13 14:19:46 -03:00
Georgia Garcia
cec9ae6dff utils: fix coding style to match PEP8
Annotate exceptions with '  # noqa: ERROR'

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-05-20 13:56:37 -03:00
Maxime Bélair
6632fa6c43 aa-logprof/aa-genprof: Adding support for --allow-all, --output-dir and --no-abstraction 2024-03-28 19:29:08 +00:00
Christian Boltz
7dc0254b90
Add option to log aa-logprof json input and output
Add a json_log option (default: disabled) to logprof.conf that enables
logging of all aa-logprof and aa-genprof input and output to a
/tmp/aa-jsonlog-* file.

This can be useful for debugging, and maybe also to create tests that do
a full aa-logprof run.

This patch introduces a minor behaviour change if aa-logprof errors out
on startup (for example if the config file is broken or the parser can't
be found):

Before:

```
$ aa-logprof --json
{"dialog": "apparmor-json-version","data": "2.12"}

ERROR: Can't find apparmor_parser at /sbin/apparmor_parser
```

After:

```
$ aa-logprof --json

ERROR: Can't find apparmor_parser at /sbin/apparmor_parser
```

Note that the json version line will not be printed if aa-logprof or
aa-genprof error out that early.

If there are no startup errors, the behaviour will not change.
2023-07-30 21:28:35 +02:00
Mark Grassi
d94731ddf4 Resolve string escape sequence DeprecationWarnings 2022-11-21 22:08:42 -05:00
Mark Grassi
98817eecb6 Remove unnecessary regex backslashes 2022-11-21 22:07:49 -05:00
Mark Grassi
c57138f255 Order imports and module-level dunder name assignments. 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
95d89dfbac
Simplify logmark used in syslog
Instead of using `date | md5sum` and parsing the output to get the
actual md5sum (without the stdin filename), use the current unixtime
with a `logmark-` prefix.
2022-02-21 22:52:14 +01:00
Christian Boltz
c0734411ee
aa-genprof: get rid of subprocess with shell=True 2022-02-21 22:43:29 +01:00
Christian Boltz
79e7a204cf
import AppArmorException from apparmor.common
... instead of indirectly using the one imported into apparmor.aa
2021-08-24 22:31:11 +02:00
Christian Boltz
ab6e9b2de2
Add --configdir to all aa-* utils
Since this option is mostly meant for testing, it will not show up in
--help.

aa-notify was the only tool that honored the __AA_CONFDIR env variable.
It still does if --configdir is not given.

Note: Since we now pass confdir= to init_aa() (in most cases None),
setting the default needs to be moved inside the function.
2020-10-29 21:24:15 +01:00
John Johansen
15dc06248c utils: fix make -C profiles check-logprof fails
On arch
  make -C profiles check-logprof

fails with
  *** Checking profiles from ./apparmor.d against logprof

  ERROR: Can't find AppArmor profiles in /etc/apparmor.d
  make: *** [Makefile:113: check-logprof] Error 1
  make: Leaving directory '/build/apparmor/src/apparmor-2.13.3/profiles'

because /etc/apparmor.d/ is not available in the build environment
and aa-logprofs --dir argument, is not being passed to init_aa()
but used to update profiles_dir after the fact.

Fix this by passing profiledir as an argument to init_aa()

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/36
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/663
Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2020-10-22 14:56:09 -07:00
Christian Boltz
c7949ba6c4
Read all profiles on aa-genprof startup instead of later
... which can mean "too late" in some special cases (if a profile
already exists in /etc/apparmor.d/$non_default_filename).

However, the main reason is that without this change
- the new profile will be added to (otherwise empty) active_profiles
- the first do_logprof_pass() will read all profiles, including the new
  one, and add them to active_profiles - which unsurprisingly results in
  an error like `ERROR: Profile /usr/sbin/vsftpd exists in
  /etc/apparmor.d/usr.sbin.vsftpd and /etc/apparmor.d/usr.sbin.vsftpd`

To fix this,
- change do_logprof_pass to never call read_profiles() (and get rid of
  the 'passno' parameter)
- adjust its callers (aa-logprof and aa-genprof) to call read_profiles()
  themself
- move printing the 'Updating AppArmor profiles in $directory.' message
  to read_profiles(), but only display it if requested (to keep the
  current UI behaviour)
2020-05-05 23:56:55 +02:00
Christian Boltz
5abfb08a3e
drop unused 'finishing' in do_logprof_pass()
finishing was always false, resulting in always returning 'NORMAL'.

Remove the variable, remove the unused condition - and change the only
place (aa-genprof) that expected a return value from do_logprof_pass()
to not expect it anymore.

found by Coverity, CID 198637
2019-05-17 22:27:57 +02:00
Christian Boltz
ec741424f8
split get_profile_filename into .._from_profile_name and .._from_attachment
Split get_profile_filename() into
- get_profile_filename_from_profile_name() (parameter: a profile name)
- get_profile_filename_from_attachment() (parameter: an attachment)

Currently both functions call get_profile_filename_orig() (formerly
get_profile_filename()) so the behaviour doesn't change yet.

The most important part of this commit is changing all
get_profile_filename() calls to use one of the new functions to make
clear if they specify a profile or an attachment/executable as
parameter.

As promised, the is_attachment parameter starts to get used in this
patch ;-)

Note: The get_new parameter (which I'll explain in the patch actually
using it) is set to True in all calls to the new functions.
The long term plan is to get rid of it in most cases (hence defaulting
to False), but that will need more testing.
2018-10-23 00:28:37 +02:00
Tyler Hicks
2f0a773506 utils: Point to the correct Profiles wiki page
The URL redirect ends up at a page in the new wiki that doesn't exist.
We have to link directly to the gitlab URL here since the current URL
redirect doesn't let us use a wiki.apparmor.net URL and still reach the
expected Profiles page.

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
2018-09-13 16:46:10 +00:00
Christian Boltz
961e69afe5
aa-genprof: don't crash if setting printk_ratelimit fails
When running aa-genprof in a lxd instance, printk_ratelimit is readonly
and writing to it fails. Instead of crashing with a backtrace, only
print a warning.

References: https://bugs.launchpad.net/apparmor/+bug/1785391
2018-08-05 14:46:13 +02:00
Goldwyn Rodrigues
8cb6ded867 genprof: Use important message as an explanation
While performing genprof, The message to start and scan the program
is mentioned in a separate important message, while it can be
presented as a part of the explanation of the PromptQuestion.

While this will not change the output of text mode, this will help
json clients like yast be more expressive.


Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>

Acked-by: Seth Arnold <seth.arnold@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
2017-07-14 22:32:15 +02:00
Goldwyn Rodrigues
5a1ae88561 json support for logprof and genprof
From: Goldwyn Rodrigues <rgoldwyn@suse.com>

Provides json support to tools in order to interact with other
utilities such as Yast.

The JSON output is one per line, in order to differentiate between
multiple records. Each JSON record has a "dialog" entry which defines
the type of message passed. A response must contain the "dialog"
entry. "info" message does not require a response.

"apparmor-json-version" added in order to identify the communication
protocol version for future updates.

This is based on work done by Christian Boltz.

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>


Acked-by: Christian Boltz <apparmor@cboltz.de>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2017-06-15 18:22:43 +02: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
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
7c02bef563 Get rid of global variable 'logger'
The global variable 'logger' in aa.py is only used by aa-genprof.

This patch changes aa_genprof to use the (new) logger_path() function,
and moves the code for finding the logger path to that function.

Also make the error message more helpful if logger can't be found.


Acked-by: John Johansen <john.johansen@canonical.com>
2015-10-20 22:03:58 +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
064697e791 require logfile only for aa-logprof and aa-genprof
Make sure most tools (for example aa-complain) don't error out if
no logfile can be found. (For obvious reasons, aa-logprof and
aa-genprof will still require a logfile ;-)

This is done by moving code from the global area in aa.py to the new
function set_logfile(), which is called by aa-logprof and aa-genprof.

While on it,
- rename apparmor.filename to apparmor.logfile
- move the error handling for user-specified logfile from aa-genprof
  and aa-logprof to aa.py set_logfile()

Note: I'd have prefered to hand over the logfile as parameter to
do_logprof_pass(), but that would break last_audit_entry_time() in
aa-genprof which requires the log filename before do_logprof_pass()
is called.

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


Acked-by: Seth Arnold <seth.arnold@canonical.com>
2015-02-20 21:36:55 +01: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
Kshitij Gupta
8bc6a176f6 Fix: fix aa-genprof to use new PromptQuestion class
The following patch:
- ensures aa-genprof also uses class based prompt

Acked-by: Christian Boltz <apparmor@cboltz.de>
2014-10-09 01:37:18 +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
0d4257462d aa-genprof failed to set /proc/sys/kernel/printk_ratelimit to 0
(unlimited) because the "if not value:" check matches 0.

This patch replaces the check with "... is None".

It also prints a warning if the old value is None (could in theory 
happen if reading the old value failed).

Acked-by: Steve Beattie <steve@nxnw.org>. Thanks.
2014-06-09 23:47:36 +02:00
Christian Boltz
b2c4934bc1 fix two issues in aa-genprof's last_audit_entry_time():
- convert "tail" result from byte to string to avoid TypeError crash
- use apparmor.filename instead of hardcoded /var/log/audit/audit.log

Acked-by: Kshitij Gupta <kgupta8592@gmail.com>
2014-05-21 21:42:43 +02:00
Christian Boltz
5f3fc904b5 aa-genprof: fix last_audit_entry_time()
last_audit_entry_time() was waiting forever because 
subprocess.check_output() started tail without any parameters.
Fixed by removing shell=True (default is shell=False).

Also fix the regex ("^.*", the dot was missing)


Acked-by: Steve Beattie <steve@nxnw.org>
2014-03-20 00:10:13 +01:00
Steve Beattie
390cfb2cd5 utils: fix apparmor.ui references in aa-genprof
aa-genprof was incorrectly trying to refer to UI_xxx functions in
apparmor.aa rather than the correct apparmor.ui. This patch fixes the
issue.

Signed-off-by: Steve Beattie <steve@nxnw.org>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2014-03-06 10:22:26 -08: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
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
Steve Beattie
c43d4eaa93 Move perl applications that were reimplemented in python by Kshitij
Gupta to the deprecated directory.
2014-02-12 09:59:23 -08:00
Kees Cook
00726ed51a The printk ratelimit needs to be disabled when running genprof via
syslog, otherwise audit events will get dropped.

This runs the risk of having the kernel log wrap around, but that
is a less common case that what this solves. This is a work-around
that will go away when complain info takes a different path in the
future.
2011-08-18 18:15:41 -05:00
Marc Deslauriers
0d9a39bd26 This adds a blurb about the new profiles repository to aa-genprof,
along with a link to the wiki page. This helps users locate profiles
that possibly already exist for the application they are attempting to
confine, and suggests they contribute the profile when they're done.
2011-07-18 10:34:49 -04:00
Kees Cook
14d8bac7b2 Here's an update to rename another chunk of things that still used
"SubDomain" in some way. This leaves only "subdomain.conf" and the
function names internally.

Additionally, I added a "make check" rule to the utils/Makefile to do a
simple "perl -c" sanity check just for good measure.
2011-01-13 13:58:26 -08:00
Kees Cook
723a20ba7d as ACKed on IRC, drop the unused $Id$ tags everywhere 2010-12-20 12:29:10 -08:00
Kees Cook
6717e29909 Here is a patch to standardize on all utils using the "aa-" prefix instead
of a mix of symlinks to non-prefixed comands, and "apparmor_" prefixed
commands.

This also refactors the manpage generation slightly since we no longer
need special cases for the manpages, and drops aa-eventd from the default
list of tools to install (it also lacks a manpage).
2010-11-03 17:03:52 -07:00
Renamed from utils/genprof (Browse further)