AppArmor is the path-based mandatory access control system that ships enabled on Ubuntu and is available on most Debian-derived distros. It confines a process to a declared set of file paths, capabilities, and network operations — turning a remote code execution from “anything goes” into “anything inside this jail”. This article covers how to write a new profile, audit it without breaking the workload, and put it into enforce mode with eyes open.
How to verify
Inspect what is currently confined, and how:
sudo aa-status
sudo journalctl -k --since "1 hour ago" | grep -i apparmor
sudo apparmor_parser -p /etc/apparmor.d/usr.sbin.nginx 2>&1 | head
sudo cat /sys/kernel/security/apparmor/profiles | head -20
sudo aa-unconfined --paranoid
aa-status is the authoritative view: how many profiles are loaded, how many in enforce vs complain mode, and which processes are running confined. aa-unconfined lists every listening daemon that has no profile — a useful gap report.
What’s happening
An AppArmor profile is a text file under /etc/apparmor.d/ named after the binary path (with / replaced by .). When the kernel execs a binary that matches a loaded profile, the process inherits that profile and every file/network operation it attempts is checked against the rules. Operations outside the rules are either logged (complain mode) or denied (enforce mode).
The two-mode design is the safety net. You author a profile in complain mode, exercise the workload through every realistic code path (login, upload, batch job, edge case), and harvest the resulting audit log entries with aa-logprof. The tool offers a guided merge: “the process tried to read /var/lib/myapp/cache — allow this for this profile?” After the soak, you flip to enforce and the same operations that were merely logged are now blocked.
Two operations matter at scale: profile updates need apparmor_parser -r to load without a restart, and child execs need explicit transitions (Px, Cx, Ux) so an unconfined helper doesn’t accidentally inherit the parent’s confinement and break.
The procedure
- Confirm AppArmor is enabled in the kernel.
cat /sys/module/apparmor/parameters/enabledreturnsY. If not,apparmor=1 security=apparmoron the kernel command line. - Generate a starting profile with
aa-genprof.sudo aa-genprof /usr/local/bin/myapp— start the workload in another shell, exercise it, then come back to the prompt and answer the questions. Generated rules land in/etc/apparmor.d/usr.local.bin.myapp. - Put the profile in complain mode.
sudo aa-complain /usr/local/bin/myapp. The process now runs unrestricted but every rule violation appears in the audit log. - Soak for at least one full operational cycle — a week is typical, longer if you have monthly batch jobs that the profile must permit.
- Harvest violations with
aa-logprof.sudo aa-logprofwalks everyDENIEDentry since the last harvest and asks how to handle it. The tool writes the chosen allow/deny back into the profile, with the right abstraction (@{HOME},@{PROC}) where it can. - Flip to enforce.
sudo aa-enforce /usr/local/bin/myapp && sudo systemctl restart myapp. Watchjournalctl -k -f | grep apparmorfor the first hour. A denied operation that you missed in the soak shows up asDENIEDwithrequested_maskanddenied_maskfields. - Reload the profile cleanly after every edit.
sudo apparmor_parser -r /etc/apparmor.d/usr.local.bin.myapp. The kernel swaps the in-memory profile without restarting the workload. - Commit the profile to your config repo and ship it via configuration management. AppArmor profiles are infrastructure — version them and apply them the same way you apply
nginx.conf.
Common pitfalls
- A child binary with
Cx(transition into a contained subprofile) needs the subprofile defined in the same file, or the exec fails closed.Pxtransitions to a separate profile that must exist;Uxstrips confinement entirely and is rarely what you want. networkrules are coarse —network inet tcpmatches all IPv4 TCP. AppArmor cannot do per-port filtering on its own; combine with UFW for that.- The
@{HOME}tunable expands to every user’s home, not just the running user’s. For a daemon, prefer an explicit path like/var/lib/myapp/. - Profiles for binaries inside snaps live under
/var/lib/snapd/apparmor/profiles/and are regenerated on every snap update — hand edits are lost. Use snap’s interface system to grant additional access. - A profile in enforce mode that mistakenly denies
signaloperations can prevent systemd from stopping the daemon. Always keep a recovery rule:signal (receive) set=(term, kill) peer=unconfined,.
In the engagements we run, AppArmor profiles are part of the standard hardening checklist for any internet-facing daemon. We pair each profile with a Loki dashboard that surfaces every DENIED entry — the kind of detail that comes with Managed Operations, because a silent deny is a profile bug waiting to break the next deploy.