LINUXMIND.DEVTHE GNU/LINUX WORLD

AppArmor on Ubuntu and Debian: a practical MAC hardening guide

Ilustracion editorial plana de un terminal protegido por un escudo, sobre fondo crema

Most Ubuntu and Debian users never think about AppArmor until something stops working. That is the point: it is a Mandatory Access Control (MAC) system that is already installed, already loaded, and already confining services on every default Ubuntu install. Unlike the classic Unix permissions you set with chmod — discretionary access control, where the owner decides — AppArmor profiles decide what a program may do based on its path, regardless of who runs it or what permissions the files have.

What changes for you: confining a service with AppArmor means that even if an attacker compromises it, the damage is bounded by the profile. This is practical, verifiable hardening you can apply today on a stock system.

Check what is already running

AppArmor ships enabled on Ubuntu by default. The first command to run is the status tool, which lists every loaded profile and its mode:

sudo aa-status

You will see a count of profiles in enforce mode, profiles in complain mode and profiles in other states. Enforce means violations are blocked and logged; complain means violations are only logged, which is how you learn what a program needs without breaking it. Profiles live in /etc/apparmor.d/, and shared snippets — the abstractions — sit in /etc/apparmor.d/abstractions/.

On Debian the picture is similar, though you may need to install the tooling first, since the utilities are not always pulled in by default:

sudo apt update
sudo apt install apparmor apparmor-utils apparmor-profiles apparmor-profiles-extra

Extra profiles: the apparmor-profiles package

The default install confines common services, but the apparmor-profiles package adds many more, and apparmor-profiles-extra goes further. These profiles usually ship in complain mode so they cannot break anything until you choose to enforce them. After installing, list what is available and decide profile by profile.

Package What it gives you
apparmor The LSM itself and the parser
apparmor-utils The aa-* tools: status, enforce, complain, genprof, logprof
apparmor-profiles Additional maintained profiles, mostly in complain mode
apparmor-profiles-extra Community profiles, experimental

Switch a profile between modes

The two commands you will use most put a program into enforce or complain mode. You can point them at the profile file or at the binary:

sudo aa-enforce /etc/apparmor.d/usr.sbin.nginx
sudo aa-complain /usr/sbin/nginx

The workflow is always the same, and it is worth memorising: start in complain, exercise the application so it logs everything it needs, turn the logs into profile rules with aa-logprof, test, then enforce. If enforcing breaks the service, drop back to complain and repeat — you lose nothing.

Build a profile from scratch with aa-genprof

When no profile exists for a program you care about, aa-genprof writes one by watching it run. Take a web server as the example — install it, then generate:

sudo aa-genprof /usr/sbin/nginx

The tool asks you to run the program in another terminal and exercise every feature you want it to keep. Each time the daemon logs an access the profile does not yet allow, aa-genprof asks you whether to allow it. Answer deliberately: allow only what the service genuinely needs. When you finish, the tool saves the profile — usually in complain mode — and you can promote it with aa-enforce.

Reading a profile

Profiles are readable text, not binary. A typical file starts with a header and then a set of rules:

/usr/sbin/nginx {
  #include <abstractions/base>
  /etc/nginx/** r,
  /var/log/nginx/*.log w,
  /run/nginx.pid rw,
}

Each line is a path followed by the permissions the program is allowed: r read, w write, k lock, m memory-map, x execute. The #include <abstractions/base> line pulls in a bundle of common allowances — shared libraries, the journal, pseudo-devices — so you do not have to list them by hand. Put local overrides in /etc/apparmor.d/local/ so your changes survive package updates.

What to check when something is blocked

A blocked program rarely says “AppArmor denied this”. From its point of view it just got a permission error. The evidence is in the kernel log, so the first move is to grep for denials:

sudo journalctl -k -g 'apparmor="DENIED"' --no-pager -n 30
sudo dmesg | grep 'apparmor="DENIED"' | tail -20

If auditd is running, the audit log is the richer source:

sudo grep 'apparmor="DENIED"' /var/log/audit/audit.log

Read one line carefully: profile= names the profile doing the blocking, operation= is the action, name= is the path, and requested_mask= versus denied_mask= tell you exactly what permission was refused. That is enough to write the fix. To confirm AppArmor is even the culprit, switch the profile to complain mode and re-run the failing command — if it works in complain and fails in enforce, you have your answer.

Make the fix permanent

There are two clean ways to allow a denied access. The interactive one is aa-logprof, which reads recent denials and offers you rules:

sudo aa-logprof

The manual one is to add a line to the profile’s local override and reload it:

echo '/etc/machine-id r,' | sudo tee -a /etc/apparmor.d/local/usr.sbin.nginx
sudo apparmor_parser -r /etc/apparmor.d/usr.sbin.nginx

Prefer aa-logprof for anything non-trivial: it only proposes what the program actually did, which keeps the profile tight instead of broad. A profile you keep widening by hand stops being security and starts being paperwork.

The bottom line

AppArmor is the easiest security win on a fresh Ubuntu or Debian box precisely because you didn’t have to install or configure it — it is already there. Spend twenty minutes with aa-status to see what is confined, install apparmor-profiles for more coverage, and use the complain-to-enforce loop to confine the services that matter. When something breaks, journalctl -k and aa-logprof will tell you why and let you fix it without loosening the whole profile.

Sources




Leave a Reply

Your email address will not be published. Required fields are marked *