A service on a VPS already runs as an unprivileged user, so what does AppArmor add? Plenty, because most compromises do not involve the service’s own files. They involve the vulnerable service reading an unexpected path, exec’ing a shell, opening a raw socket, or writing to a directory it was never meant to touch. AppArmor confines each process to a whitelist of paths, capabilities, and network operations, and the kernel refuses anything outside it — even if the process has the right Unix permissions.
This article covers what actually blocks things: default nginx and sshd confinement, reading a denial, and writing and testing a profile for your own service without locking yourself out.
How enforcement works and where profiles live
AppArmor is enabled on every Ubuntu VPS by default. sudo aa-status prints loaded profiles, enforce or complain mode, and confined processes. Profiles live in /etc/apparmor.d/ and are compiled into the kernel at boot by apparmor.service.
Second, AppArmor matches on paths, not inodes: a file moved into an allowed directory becomes accessible at once. That is why profiles are best written after the service is running, using its real paths, and tested with logging on.
What the stock nginx profile blocks
The nginx profile shipped in /etc/apparmor.d/nginx allows reads in /etc/nginx/**, /usr/share/nginx/**, and /var/www/html/**, access to its own PID file and logs, and the network operations nginx needs. It denies nearly everything else. Suppose a PHP upload trick gets an attacker a file in /var/www/html/uploads/; even if they can execute it, AppArmor denies /bin/sh child execution and denies reads of /etc/passwd and /etc/shadow.
To see a denial, run sudo aa-exec -p nginx -- /bin/cat /etc/shadow. It fails with permission denied, and dmesg | tail shows apparmor="DENIED" operation="open" profile="nginx" name="/etc/shadow" ... denied_mask="r" — path, operation, and profile, the three fields you need for every fix.
What the stock sshd profile blocks
Ubuntu’s sshd profile confines /usr/sbin/sshd to reading its configuration, host keys, PAM modules, and nsswitch, and denies shell execution from the daemon itself. Sessions transition to unconfined or to the user’s profile. The daemon cannot be tricked into running a payload, while the sessions it spawns remain ordinary user shells.
If you move the config path, the profile may need an extra rule. The symptom is sshd failing to start with a denial for /etc/ssh/sshd_config.d/; the fix is a one-line /etc/ssh/sshd_config.d/** r,, not disabling AppArmor.
Reading denial logs and turning them into rules
The kernel sends AppArmor events to the audit subsystem. journalctl -k | grep apparmor="DENIED", or dmesg without auditd. Each line carries profile, operation, name, and denied_mask.
aa-logprof turns those into profile text: it walks the log, proposes a rule per denial, and lets you accept or reject each. Run it in complain mode so you see the full set of denials a normal workload produces, not just the first one that killed the service.
sudo aa-complain /etc/apparmor.d/usr.sbin.myapp— log-only modesudo aa-logprof— accept the rules you expect and reject the restsudo apparmor_parser -r /etc/apparmor.d/usr.sbin.myapp— reloadsudo aa-enforce /etc/apparmor.d/usr.sbin.myapp— turn enforcement back on
Writing a minimal profile for a custom service
Start from aa-genprof /opt/myapp/bin/myapp. It writes a stripped-down profile to /etc/apparmor.d/, puts it in complain mode, and asks you to run the service through a typical workload. Accept the abstractions/base include for libc, locales, and DNS, and add only the paths your service truly needs.
A profile for a small Go binary behind nginx allows the binary and its data directory, denies /home and ptrace, and allows only inet stream and inet6 stream networking. To bind port 80, add one capability net_bind_service, line instead of running as root.
Testing that denials happen where you expect
A profile that loads is not a profile that works. Test both directions: the service must succeed at its real job and fail at everything else. The second half is the part people skip.
| Test | Command | Expected result |
|---|---|---|
| Read its own config | aa-exec -p myapp -- cat /etc/myapp/config.yml |
succeeds |
| Read a shadow file | aa-exec -p myapp -- cat /etc/shadow |
DENIED |
| Spawn a shell | aa-exec -p myapp -- /bin/sh |
DENIED |
| Write outside its data dir | aa-exec -p myapp -- touch /tmp/escape |
DENIED |
| Raw packet socket | aa-exec -p myapp -- ping -c1 127.0.0.1 |
DENIED |
Every DENIED above is a real boundary. If one succeeds, the profile is too loose — usually a broad /etc/** r, or /tmp/** rw, accepted during aa-logprof.
Avoiding the two classic mistakes
The first mistake is enforcing a profile never tested in complain mode. A service that silently fails to bind a socket looks like an application bug for hours while the audit log shows the reason in one line. Run complain mode for a full traffic cycle, including certificate reloads, before enforcing.
The second is path drift. An upgrade can move a binary from /usr/sbin/foo to /usr/libexec/foo; the old profile no longer attaches and the binary runs unconfined. Check aa-status after upgrades and add the new path to the profile’s attachment line.
Remember too that AppArmor cannot deny what the process never attempts: it blocks exploitation paths, not vulnerabilities. Pair it with a narrow systemd unit — ProtectSystem=strict, PrivateTmp=yes, NoNewPrivileges=yes. Before building a strategy on it, check what an instance can promise.
Finally, if the service runs containers, note that AppArmor applies to the container process through the runtime’s profile. Docker’s docker-default is a starting point, not a confinement strategy; the same aa-logprof loop works on a custom profile, as long as the container runtime is configured to load it. On a modest VPS the overhead is negligible — profile matching is a few hundred nanoseconds per syscall.
AppArmor is a kernel feature, so on a VPS you rely on the provider exposing securityfs and a kernel built with it enabled. Most KVM hosts do; some containers do not, and aa-status then reports no profiles loaded.
If you need a specific kernel version or LSM stacking with both AppArmor and eBPF confinement, a dedicated server gives you the kernel you install yourself. On a VPS, verify first, then write the profile.




Leave a Reply
You must be logged in to post a comment.