Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 555388 - sys-apps/apparmor doesn't include systemd units
Summary: sys-apps/apparmor doesn't include systemd units
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Hardened (show other bugs)
Hardware: All Linux
: Normal normal with 1 vote (vote)
Assignee: Michael Palimaka (kensington)
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: install-systemd-unit
  Show dependency tree
 
Reported: 2015-07-19 19:50 UTC by Dainius Masiliūnas
Modified: 2017-12-07 09:49 UTC (History)
6 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
apparmor@.service (without apparmor target) (apparmor@.service,271 bytes, text/plain)
2015-07-20 18:38 UTC, Dainius Masiliūnas
Details
apparmor.target (file_555388.txt,122 bytes, text/plain)
2015-07-30 15:02 UTC, Dainius Masiliūnas
Details
apparmor@.service (with apparmor target) (file_555388.txt,286 bytes, text/plain)
2015-07-30 15:06 UTC, Dainius Masiliūnas
Details
Example drop-in file (nginx.service.d/apparmor.conf) (file_555388.txt,113 bytes, text/plain)
2015-07-31 00:13 UTC, Dainius Masiliūnas
Details
Example drop-in file (nginx.service.d/apparmor.conf) (file_555388.txt,87 bytes, text/plain)
2015-08-01 16:02 UTC, Dainius Masiliūnas
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Dainius Masiliūnas 2015-07-19 19:50:24 UTC
AppArmor does not include systemd units, making it difficult to start with AppArmor enabled.

Reproducible: Always

Steps to Reproduce:
1. Install sys-apps/apparmor
2. Run `systemctl enable apparmor` or similar
Actual Results:  
Unit is not found

Expected Results:  
AppArmor profiles should be loaded on startup

Upstream doesn't provide units yet, but a number of distributions provide their own ones (notably Arch: https://aur.archlinux.org/packages/apparmor/ ). The units aren't very difficult to maintain so there shouldn't be problems shipping them.

Looking at how Arch does it, it looks a bit incorrect. For one, writing to a log file is bad, since journald captures all stderr by itself (and the wait for filesystem availability caused by that is not acceptable for something this critical). In addition, running scripts to launch it is needlessly fragile, and blindly adding every profile in /etc/apparmor doesn't sound like the best practice; templates can be used to add individual profiles instead.

Finally, some thought should be put into making certain that profiles are loaded before the system boots. When systemd is installed with USE=apparmor, services can be taught to fail to start if their respective apparmor profiles are not loaded, increasing security in case something goes wrong.

I'll test and attach the unit files I use in my personal system for this a bit later.
Comment 1 Michael Palimaka (kensington) gentoo-dev 2015-07-20 12:29:16 UTC
I don't use systemd so can't work on this.
Comment 2 Dainius Masiliūnas 2015-07-20 18:38:24 UTC
Created attachment 407288 [details]
apparmor@.service (without apparmor target)

Attached the template service file I currently use for loading the profiles.

It's used per-AppArmor-profile, as in:

systemctl start apparmor@bin.ping

to load the /etc/apparmor.d/bin.ping profile into the kernel. Each profile can be started, stopped, restarted, enabled and disabled individually. When enabled, the loading happens very early in the boot process, and any possible errors are logged by journald.

To further improve this, I think an apparmor.target unit can be made, and the template changed to have WantedBy=apparmor.target, to get the ability to disable and re-enable all the service instances registered with systemd with a single command.

In addition, some unit overrides could be used to force associated services not to start if the corresponding profiles aren't loaded for added security. I'll look into how best to achieve that a bit later.
Comment 3 Dainius Masiliūnas 2015-07-30 15:02:33 UTC
Created attachment 407994 [details]
apparmor.target

This is the target file I use now. The nice thing about this is that it allows disabling and re-enabling all the defined AppArmor profile services at once by using `systemctl disable apparmor.target` and `systemctl enable apparmor.target`.

I considered also adding `RequiredBy=basic.target` to the Install section, but that's probably too extreme (it would stop booting if there were any issues in the enabled AppArmor profiles and open a rescue shell instead).
Comment 4 Dainius Masiliūnas 2015-07-30 15:06:41 UTC
Created attachment 407996 [details]
apparmor@.service (with apparmor target)

And the corresponding apparmor@.service to make use of the target.
Comment 5 Dainius Masiliūnas 2015-07-31 00:13:14 UTC
Created attachment 408038 [details]
Example drop-in file (nginx.service.d/apparmor.conf)

And here's an example of a drop-in file that makes absolutely certain that a particular service doesn't even start if its AppArmor profile isn't loaded; and also automatically starts the AppArmor profile when attempting to load the associated service (however, this is not a substitute for enabling the AppArmor profile service, since that would leave manually launched instances vulnerable).

That means that if there's something wrong with the profile or profile storage, the associated daemon doesn't start, but everything else does, allowing debugging without compromising security.

Unfortunately there's no way to automate making such drop-in files, as far as I know, since one needs to know the service and profile names. Administrators should make such drop-in files themselves for each service that needs to be protected by AppArmor. Thankfully it's not particularly hard: `mkdir /etc/systemd/system/name.service.d`, `nano /etc/systemd/system/name.service.d/apparmor.conf` (or 01apparmor.conf or what have you), then fill in the three fields with the profile file name. The first two fields make sure the profile loading services are done, whereas the last one queries libapparmor itself to independently verify that the profile(s) loaded successfully (this particular functionality requires the "apparmor" USE flag to be enabled in systemd).
Comment 6 Dainius Masiliūnas 2015-08-01 16:02:19 UTC
Created attachment 408118 [details]
Example drop-in file (nginx.service.d/apparmor.conf)

Attached a fixed example, the last one didn't actually do anything due to syntax errors.

I removed the AppArmorProfile directive because for me it results in:
  Failed at step APPARMOR spawning /usr/sbin/nginx: No such file or directory
I'm not sure what gives, since that file definitely exists. But maybe it gets confused, because that directive is for switching profiles, and this is switching to itself.

Alternatively to something like this drop-in, the profile loading service could use RequiredBy= and Before= on the service it protects, but it would have to use drop-ins as well, so it doesn't really matter which solution is used.
Comment 7 Alexandre 2016-04-22 08:32:46 UTC
@Dainius Masiliūnas

I tried the last target and service files you attached and they seems to work, but when I restarted the system apparmor doesn't have any profile loaded any more.

PS. what is the recommended practice to use the extra-profiles? I tried using symlinks to /etc/apparmor.d but won't work. Than did I copy the entire directory to there.

Thanks!
Comment 8 Alexandre 2016-04-22 09:02:38 UTC
Sorry, user error.

I had enabled the profiles using the aa-enforce command instead the systemctl one.
Comment 9 Alexandre 2016-05-29 05:58:48 UTC
Now, I think the best way to enable/disable apparmor profiles on Gentoo would be on the eselect way, but I can't help to implement it because I do not code.
Comment 10 Larry the Git Cow gentoo-dev 2017-12-07 09:49:26 UTC
The bug has been closed via the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=01417d76965fc9cb35171e72694562c71da537c5

commit 01417d76965fc9cb35171e72694562c71da537c5
Author:     Reuben D'Netto <rdnetto@gmail.com>
AuthorDate: 2017-12-06 11:12:46 +0000
Commit:     Michael Palimaka <kensington@gentoo.org>
CommitDate: 2017-12-07 09:49:01 +0000

    sys-apps/apparmor: Added systemd unit.
    
    Closes: https://bugs.gentoo.org/555388
    Closes: https://github.com/gentoo/gentoo/pull/6466

 sys-apps/apparmor/Manifest                  |  2 +-
 sys-apps/apparmor/apparmor-2.11.1-r2.ebuild | 64 +++++++++++++++++++++++++++++
 sys-apps/apparmor/files/apparmor.service    | 14 +++++++
 sys-apps/apparmor/files/apparmor_load.sh    |  2 +
 sys-apps/apparmor/files/apparmor_unload.sh  |  2 +
 5 files changed, 83 insertions(+), 1 deletion(-)