Your AppArmor Profile Is Doing Nothing: Practical Path Confinement on Linux
On the Debian 13 host I used for this article, aa-status reports 106 loaded profiles, 7 in enforce mode, and 23 in complain mode. The kernel module is enabled, the service manager is happy, and the shell I ran the checks from still reports unconfined.
That gap is not a bug in AppArmor. It is the normal consequence of a mental model mismatch. AppArmor is not a label on an inode, and a profile is not "security" in the abstract. It is a list of path permissions attached to a specific executable, enforced from the moment that executable is called. If any link in that chain is wrong, you get a loaded profile that confines nothing.
Here is how the chain works, where it breaks, and a profile that actually confines a small service.
The model: a path whitelist attached to an executable
AppArmor is a Linux Security Module that mediates by path, not by inode label. A profile contains rules like "this program may read this file, map that library, execute this helper, and use this capability." Anything not allowed is denied. There is no implicit allow inside a profile.
The unit of confinement is the profile, and a profile with an attachment path is applied when that path is executed:
profile notes-api /usr/local/bin/notes-api {
...
}
Children inherit the profile. Moving to a different profile requires an explicit exec transition: px (discrete profile), cx (child profile), ix (inherit), or ux (unconfined). That is the whole model. Everything confusing about AppArmor follows from it.
Two consequences matter immediately:
- A process with no profile is unconfined. AppArmor adds nothing to DAC for that process.
- Being in a profile is binary. You are either confined by a named profile or you are not. "AppArmor is enabled" says nothing about which.
Loaded is not attached
The first trap is thinking that apparmor_parser -r starts confining something. It does not. It hands policy to the kernel. Attachment happens later, at execve(), when the kernel resolves the binary path and finds a matching profile.
I built a small C probe that prints its own profile from /proc/self/attr/current, then opens two files: one allowed by the profile, one not.
profile aalab-probe /tmp/aalab/probe {
#include <abstractions/base>
/tmp/aalab/probe mr,
/etc/hostname r,
/proc/*/attr/current r,
}
sudo apparmor_parser -Q /tmp/aalab/prof # compile only, no kernel load
sudo apparmor_parser -r /tmp/aalab/prof # load and replace
/tmp/aalab/probe /etc/hostname
/tmp/aalab/probe /tmp/aalab/secret.txt
Output on kernel 6.12.117 with AppArmor 4.1.0:
profile=aalab-probe (enforce)
open(/etc/hostname) -> fd=5 errno=0 ok
profile=aalab-probe (enforce)
open(/tmp/aalab/secret.txt) -> fd=-1 errno=13 DENIED
The parent shell was unconfined. The profile attached anyway, because an unconfined process executing a profiled binary matches the attachment condition. You do not need aa-exec to confine a normal systemd service; you need the profile loaded before the service execs.
Notice also that reading /proc/self/attr/current needed its own rule. Default deny covers /proc, not just /etc. The first version of my probe was denied on that path, so the profile line was missing from its output — a useful reminder: the profile is a whitelist for every mediated class, including introspection.
Five ways the profile quietly misses
1. Path is the identity, so hardlinks and symlinks matter
AppArmor matches the path used at open time. A hardlink gives the same inode a second path, and that second path needs its own rule. I created a hardlink and allowed only the first name:
90890 /tmp/aalab7/allowed.txt
90890 /tmp/aalab7/other.txt
open(/tmp/aalab7/allowed.txt) -> fd=5 errno=0 ok
open(/tmp/aalab7/other.txt) -> fd=-1 errno=13 DENIED
Same inode. Different answer. SELinux would follow the label; AppArmor follows the name. (Paths shortened for readability.)
Symlinks behave the same way, but the surprise is usually in the other direction. I opened /etc/os-release in enforce mode under the profile above. It succeeded, even though the profile only allows /etc/hostname.
readlink -f /etc/os-release
# /usr/lib/os-release
The file is a symlink to /usr/lib/os-release, and abstractions/base contains:
/{usr/,}lib{,32,64}/** r,
So the resolved path was already allowed by a broad library rule. Your /etc rule was never consulted. Before writing a rule for a path, resolve it:
readlink -f /usr/local/bin/notes-api
namei -l /etc/notes-api/config.yml
If the binary is a symlink into /opt or a versioned directory, write the rule for the target. If a config file is a symlink into /etc, write the rule for the target, or replace the symlink with a real file you control.
2. deny wins, and by default it does not log
Inside a profile, deny overrides allow regardless of order or specificity. I loaded a profile with a broad allow and a narrow deny:
/etc/** r,
deny /etc/hostname r,
profile=aalab-probe (enforce)
open(/etc/hostname) -> fd=-1 errno=13 DENIED
That is the useful case: default deny already blocks everything, so an explicit deny is only needed to carve a hole out of a broader allow inherited from an abstraction or a development-time rule. It is not "extra security" on top of default deny.
The trap is the logging. From apparmor.d(5):
Specifies that permissions requests that match the rule should be denied without logging. Can be combined with 'audit' to enable logging.
So deny /srv/app/.env r, fails silently. If you want to know when something tried, write audit deny /srv/app/.env r,. And because deny is not a default-deny rule but an explicit one, it still denies when the profile is in complain mode.
3. Complain mode is not a dry run
Complain mode relaxes the default deny: requests with no matching rule are allowed and logged as ALLOWED. It does not turn the profile into a no-op. Explicit deny rules still deny. Rules that do exist still apply, including bad ones.
I loaded the same probe with flags=(complain):
profile=aalab-probe (complain)
open(/tmp/aalab/secret.txt) -> fd=5 errno=0 ok
apparmor="ALLOWED" operation="open" class="file" profile="aalab-probe"
name="/tmp/aalab/secret.txt" requested_mask="r" denied_mask="r"
Note the field name: denied_mask="r" even though the operation was allowed. The audit record describes what the profile would have denied, not what happened. That is exactly what makes complain mode useful for policy development, and exactly why it is a poor way to "test" a service: the program now takes code paths it would never reach under enforcement, so the next denial you discover may be somewhere you have never been.
Also expect log volume. A chatty service under complain mode produces one record per would-be denial, which can flood a log quickly. Scope your collection window before you start.
4. A clean parse is not enforcement
apparmor_parser -Q compiles the profile and skips the kernel load. It is a syntax check, not a capability check. On this host, profiles containing unix and dbus rules compiled and loaded successfully, yet the kernel's feature list contains no unix or dbus entry:
ls -1 /sys/kernel/security/apparmor/features
capability
caps
domain
file
io_uring
mount
namespaces
network_v8
policy
ptrace
query
rlimit
signal
The classes you can actually mediate are the ones listed there. Debian's own apparmor.d(5) still carries a note that network, D-Bus, and Unix-socket rules are "not supported on Debian yet," while this kernel exposes network_v8. Both statements can be true across versions and kernels. The operational lesson is not to trust either list: check features/ on the machine you are shipping to, then test the behavior you care about.
Profiles also pin a policy ABI, usually through an abi <abi/4.0>, line inside the abstractions. That pin is why a profile written on one release can fail to compile on another, and why upgrading AppArmor can turn a working profile into a startup failure. Treat the ABI line as part of the profile's compatibility contract.
5. The cache and the boot path
apparmor_parser caches compiled policy in /var/cache/apparmor. Loading with -r may reuse a cache entry if it is newer than the profile file. After you edit an included abstraction, the profile file's timestamp has not changed, so the parser can happily reload stale policy.
sudo apparmor_parser -r -W --skip-read-cache /etc/apparmor.d/usr.local.bin.notes-api
-W writes the new cache entry; --skip-read-cache forces compilation. Use both when you change anything under /etc/apparmor.d/abstractions/ or tunables/.
At boot, Debian uses a oneshot unit, not a daemon. The parts that matter:
ConditionSecurity=apparmor
Before=sysinit.target
Type=oneshot
ExecStart=/lib/apparmor/apparmor.systemd reload
ExecReload=/lib/apparmor/apparmor.systemd reload
ExecStop=/bin/true
RemainAfterExit=yes
WantedBy=sysinit.target
Two details are worth knowing. ConditionSecurity=apparmor means the unit is skipped entirely on a kernel without the LSM, so check the condition result rather than assuming it ran; an active unit only means policy was loaded, not that your service is confined by it. ExecStop=/bin/true is deliberate: a normal restart would stop the unit and unload all profiles from running processes, which cannot be undone by starting it again. Debian neutralizes stop so that systemctl restart apparmor reloads policy instead of tearing down confinement. If you genuinely want to unload everything, the unit's own comment points at aa-teardown. Do not run that on a production host unless you mean it.
A profile that actually confines a service
Here is a complete example for a small HTTP service, notes-api, installed as /usr/local/bin/notes-api, reading /etc/notes-api/config.yml, writing state under /var/lib/notes-api, using /run/notes-api for a socket, and binding an unprivileged port.
Save as /etc/apparmor.d/usr.local.bin.notes-api (the file name convention mirrors the executable path with slashes replaced by dots):
#include <tunables/global>
profile notes-api /usr/local/bin/notes-api {
#include <abstractions/base>
#include <abstractions/nameservice>
# The binary: read, plus executable mmap for its own code and plugins.
/usr/local/bin/notes-api mr,
# Configuration: read-only, except the env file, which systemd reads.
/etc/notes-api/ r,
/etc/notes-api/** r,
deny /etc/notes-api/env r,
# State: create, read, write, lock; owner-only for private data.
/var/lib/notes-api/ rw,
/var/lib/notes-api/** rwk,
owner /var/lib/notes-api/private/** rw,
# Runtime socket.
/run/notes-api/ rw,
/run/notes-api/** rwk,
# Application tree: read-only.
/srv/notes-api/ r,
/srv/notes-api/** r,
# Network mediation, where the kernel supports it.
network inet stream,
}
Why each piece is there:
-
mris the conventional pair:rto read the binary,mto allowPROT_EXECmappings for its code and any plugins it loads. A profile with onlyron the binary still started in my test, because the initial exec does not need an explicit mmap rule, but code that later maps executable pages does needm. Keepmron the main binary and addmto plugin or JIT directories. -
abstractions/basesupplies the loader, libc, locale,/dev/null, and a handful of/procfiles. Without it, a dynamically linked binary cannot start. It also brings the broad/{usr/,}lib/**rule that made the symlink example work. -
abstractions/nameservicesupplies the NSS libraries and the usual/etc/passwd,/etc/group,/etc/hosts, and resolver paths. Only include it if the service resolves names. -
/var/lib/notes-api/needswon the directory itself to create or unlink entries;/** rwkcovers the files and the lock permission. -
ownerrequires the file's owner to match the task's euid/fsuid. It is a useful extra constraint on private data, not a replacement for DAC. -
deny /etc/notes-api/envis the carve-out case: the service may read its config directory, but the environment file is loaded by systemd viaEnvironmentFile=, so the confined process has no reason to open it. Addauditin front if you want to see attempts. -
network inet streamis only meaningful if the kernel exposes network mediation. If it does not, the rule is inert; the service's binding is still governed by DAC and capabilities.
The matching unit:
[Unit]
Description=Notes API
After=network-online.target
Wants=network-online.target
[Service]
User=notes
Group=notes
ExecStart=/usr/local/bin/notes-api --config /etc/notes-api/config.yml
Restart=on-failure
StateDirectory=notes-api
RuntimeDirectory=notes-api
EnvironmentFile=/etc/notes-api/env
NoNewPrivileges=yes
AppArmorProfile=notes-api
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
[Install]
WantedBy=multi-user.target
AppArmorProfile= takes a profile name, not a file path. The profile must already be loaded in the kernel or the unit fails to start; prefix the value with - only if you deliberately want a missing profile to be ignored. The option is documented as system-services-only, and it has existed since systemd 210, so it is not a new-feature gamble.
Ordering is usually already correct: apparmor.service is Before=sysinit.target and ordinary services start after sysinit.target, so profiles load first. If you add or edit a profile while the service is running, reload the profile and restart the service. Confinement is applied at exec; an already-running process keeps the policy it started with.
StateDirectory= and RuntimeDirectory= create the directories, set ownership, and add mount dependencies, but they do not grant AppArmor permissions. You still need the /var/lib/notes-api/** rwk and /run/notes-api/** rwk rules.
ProtectSystem=strict makes /usr, /boot, and /etc read-only inside the unit's mount namespace. That is a different mechanism from AppArmor and complements it: the namespace prevents writes, the profile limits reads and execs. Do not cargo-cult flags=(attach_disconnected) to "fix" namespace-related denials. The manual is explicit that it attaches disconnected objects to the namespace and warns that it can cause aliasing and access to objects that should not be allowed; it is a debugging and policy-development tool.
The workflow that does not waste an afternoon
The package split matters. apparmor ships the parser, aa-status, and the profiles; apparmor-utils ships the learning tools.
sudo apt install apparmor-utils
# Generate a skeleton and iterate in complain mode.
sudo aa-genprof /usr/local/bin/notes-api
# Later, after exercising the service, review proposed rules.
sudo aa-logprof
# Switch modes without editing the file.
sudo aa-complain /usr/local/bin/notes-api
sudo aa-enforce /usr/local/bin/notes-api
aa-genprof puts the profile in complain mode, marks the log, and asks you to exercise the application. Each scan parses the violations and offers rules; when you finish, it sets the profile to enforce. aa-logprof is the same review loop without the initial generation. Accept rules selectively. The tool proposes what the program did, not what it should be allowed to do, and a service that was compromised during profiling will happily teach the tool to allow the compromise.
For a service you cannot easily restart, test the profile against a copy of the binary first:
sudo aa-exec -p notes-api -- /usr/local/bin/notes-api --version
Keep a second SSH session open when profiling anything that can affect your login path. A bad profile on sshd or the login stack is a lockout, and recovery may require console access.
Verification, not vibes
After loading and restarting, prove the three separate things:
# 1. AppArmor is enabled and policy is loaded.
sudo aa-status --enabled; echo $?
# 2. The profile exists and is in the mode you think.
sudo aa-status --verbose | grep -A2 'notes-api'
# 3. This process is actually confined.
sudo cat /proc/"$(pgrep -f '/usr/local/bin/notes-api' | head -1)"/attr/current
# notes-api (enforce)
aa-status exit codes are script-friendly: 0 enabled with policy, 1 not enabled, 2 enabled with no policy, 3 control files unavailable, 4 insufficient privilege. Reading /sys/kernel/security/apparmor/profiles requires root; as an unprivileged user it is simply Permission denied, not an empty list. Do not misread that as "no profiles."
Denials look like this:
apparmor="DENIED" operation="open" class="file" profile="notes-api"
name="/srv/notes-api/.env" pid=1234 comm="notes-api"
requested_mask="r" denied_mask="r" fsuid=997 ouid=997
requested_mask and denied_mask tell you what the program asked for and what the profile refused. profile tells you which profile made the decision, which is how you distinguish "my profile is wrong" from "some other profile is in the stack." fsuid and ouid explain owner rule failures.
sudo journalctl -k --since "-15 min" | grep 'apparmor="DENIED"'
If the host runs auditd, the same records may be in /var/log/audit/audit.log instead, and the kernel ring buffer may only show a subset. Check both before concluding that a denial did not happen.
Where AppArmor fits
AppArmor is the right tool when you want an administrator-controlled, path-based whitelist around an unmodified binary, with policy that is readable in a code review. It is the wrong tool when you need to confine an application you can modify and cannot rely on root-loaded policy — that is what Landlock is for. It is also not a substitute for dropping capabilities: the profile can allow capability net_bind_service, but it should not be the reason a process holds it.
Use all three, plus systemd's namespace options, and keep the boundaries straight:
- AppArmor: which paths and classes a named program may use.
- Capabilities: which privileged operations a process may perform.
- Landlock: a process's own unprivileged, inherited path restriction.
- systemd sandboxing: mount, PID, network, and user namespaces around the unit.
None of them replaces the others, and none of them is proven by a green service status.
The checklist
- Resolve the real path of the binary and every file it touches.
- Put
mron the binary so its code and plugin mappings work;ralone may start but fails later. - Syntax-check with
apparmor_parser -Q, then load with-r -W. - Prove attachment with
/proc/PID/attr/current, not with the profile count. - Prove the denial you expect, then prove the allow you expect.
- Check
features/before trustingnetwork,unix, ordbusrules. - Remember that
denyis silent,complainis not permissive, and a loaded profile is not an attached one.
The profile is not the security. The verification is.
Sources
-
apparmor.d(5), AppArmor 4.1.0 — profile syntax, access modes, exec transitions,deny/audit/ownerqualifiers,attach_disconnected,mediate_deleted: https://manpages.debian.org/trixie/apparmor/apparmor.d.5.en.html -
apparmor_parser(8)—-a,-r,-R,-Q,-W,--skip-read-cache, cache location: https://manpages.debian.org/trixie/apparmor/apparmor_parser.8.en.html -
aa-status(8)— output, filters, exit codes: https://manpages.debian.org/trixie/apparmor/aa-status.8.en.html -
aa-genprof(8), AppArmor 4.1.0 — complain-to-enforce workflow: https://manpages.debian.org/trixie/apparmor-utils/aa-genprof.8.en.html -
systemd.exec(5)—AppArmorProfile=, added in systemd 210, system services only: https://manpages.debian.org/trixie/systemd/systemd.exec.5.en.html - Linux kernel documentation, AppArmor LSM — enabling via
security=apparmor,CONFIG_LSM: https://docs.kernel.org/admin-guide/LSM/apparmor.html - Debian 13 host observations: kernel 6.12.117, AppArmor 4.1.0-1, 106 loaded profiles / 7 enforcing / 23 complaining, feature list from
/sys/kernel/security/apparmor/features,apparmor.serviceunit from/usr/lib/systemd/system/apparmor.service,abstractions/baseandabstractions/nameservicecontents, and the probe, hardlink, symlink, deny-precedence, and complain-mode results quoted above.
Top comments (0)