Linux AppArmor Explained: Confine Programs Before They Hurt You


You install a program. It runs as your user, or worse, as root. If that program gets compromised — or turns out to be malicious — it can read your files, open network connections, and do almost anything you can do.

AppArmor fixes this by putting programs in a cage. It’s a mandatory access control system built into the Linux kernel, and it ships by default on Ubuntu, Debian, openSUSE and SUSE. Unlike SELinux, which many beginners find intimidating, AppArmor profiles are plain text files you can actually read.

How AppArmor Works

Every program gets a profile — a list of exactly what it’s allowed to do. Which files it can read. Which directories it can write to. Whether it can access the network. If a program tries something outside its profile, AppArmor blocks it and logs the attempt.

Profiles run in two modes:

  • Enforce mode — violations are blocked. This is the secure mode.
  • Complain mode — violations are logged but allowed. Use this while building a profile, so you can see what the program needs without breaking it.

Check What’s Running

sudo aa-status # show loaded profiles and their modes
sudo apparmor_status # same thing, different name
ls /etc/apparmor.d/ # see the profiles installed on your system

On a fresh Ubuntu system you’ll already see profiles for programs like dhclient, cupsd and the Firefox snap. AppArmor is protecting you before you even knew it existed.

Read a Profile

Profiles live in /etc/apparmor.d/. Open one:

cat /etc/apparmor.d/usr.sbin.dhclient

You’ll see lines like:
/etc/dhcp/dhclient.conf r,
/var/lib/dhcp/ rw,
capability net_raw,
network inet raw,

Each line is a permission. r means read, w means write, rw means both. The profile lists every file the program needs — and nothing else. If the program tries to read /etc/shadow, AppArmor says no.

Create Your Own Profile

The easiest way is to let AppArmor learn. Put a program in complain mode, use it normally for a while, then generate a profile from the logs:

sudo aa-complain /usr/bin/myprogram # log violations, don't block
# ... use the program normally for a day ...
sudo aa-logprof # walk through the logs, approve rules
sudo aa-enforce /usr/bin/myprogram # now enforce the profile

aa-logprof asks you about each action the program took — allow it or deny it — and builds the profile for you. It’s the fastest way to cage a program without breaking it.

When Something Breaks

If a program suddenly stops working after you enable a profile, check the logs first:

sudo dmesg | grep -i apparmor
sudo journalctl -t kernel | grep -i apparmor

You’ll see exactly which file or capability was denied. Either the profile needs that permission (add it), or the program was doing something suspicious (good — the cage worked).

AppArmor vs SELinux

Both do the same job — mandatory access control. SELinux is more powerful and more complex, and it’s the default on Red Hat systems. AppArmor is simpler, path-based, and the default on Ubuntu and Debian. You don’t need to pick a winner: use whichever your distribution ships. The important thing is that something is confining your programs.

The Takeaway

Firewalls protect the network. Permissions protect files. AppArmor protects everything else — it limits what a compromised program can do even after it gets in. Run `sudo aa-status` today and see what’s already caged on your system. Then cage one more program. That’s defense in depth, and it costs you nothing.

Comments

Popular Posts