Beyond the Basics: Hardening Your Linux Server with AppArmor

Beyond the Basics: Hardening Your Linux Server with AppArmor


Standard Linux permissions are great, but they have a fundamental limitation: they only care about who is accessing a file, not what the program is doing. If a legitimate program (like a web server) gets compromised, it can potentially do anything the user running it can do. This is where Mandatory Access Control (MAC) systems like AppArmor come in.

AppArmor moves beyond user-based permissions. It confines individual programs to a specific set of rules, defining exactly which files they can access and what capabilities they can use. If a program tries to do something outside its profile, AppArmor steps in and blocks it. It’s a powerful layer of defense for any Linux server.

While some are familiar with SELinux, AppArmor is often considered easier to learn and manage, making it a perfect starting point for hardening your systems. Let’s explore how to use it.

Is AppArmor Running?

AppArmor is installed and enabled by default on many distributions, including Ubuntu, Debian, and openSUSE. You can check its status with a single command:

sudo aa-status

This command will show you if AppArmor is enabled and list all the profiles that are currently loaded, telling you which ones are in enforce mode (actively blocking) and which are in complain mode (only logging violations).

Understanding Profiles and Modes

The heart of AppArmor is its profiles. Each profile is a simple text file located in /etc/apparmor.d/ that defines the rules for a specific application.

Profiles operate in two key modes:

  1. Enforce Mode: This is the active, blocking mode. Any action the application attempts that is not explicitly allowed in its profile will be denied and logged.
  2. Complain Mode: In this mode, AppArmor logs all policy violations, but it doesn’t actually block anything. This is incredibly useful for developing and testing new profiles without breaking the application.

You can easily switch a profile between modes. For example, to put the nginx profile into complain mode:

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

And to put it back into enforce mode:

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

Practical Guide: Generating a New Profile

The best way to understand AppArmor is to create a profile. For this, you’ll need the apparmor-utils package.

sudo apt-get install apparmor-utils

Let’s create a profile for the tcpdump utility, a common network analysis tool. Imagine we want to restrict it so it can only write capture files to a specific directory.

Step 1: Put an existing profile (if any) into complain mode. This ensures the application runs without being blocked while we generate the new profile.

# This command will likely fail if no profile exists, which is fine.
sudo aa-complain /usr/sbin/tcpdump 2>/dev/null

Step 2: Generate the new profile. The aa-genprof tool will scan the system logs for AppArmor events related to the executable you specify.

sudo aa-genprof /usr/sbin/tcpdump

Now, aa-genprof is watching. In another terminal, run the tcpdump command and perform the actions you want to allow.

# In a second terminal
sudo tcpdump -i eth0 -w /tmp/capture.pcap -c 10

Step 3: Build the profile. Go back to your first terminal (running aa-genprof). Press S to scan the logs for events. It will find the actions tcpdump just performed and ask you how to handle them. You’ll see prompts asking if you want to allow file access, network capabilities, and more. You can choose (A)llow, (D)eny, or (I)gnore for each event.

Go through the interactive process, allowing the legitimate actions of tcpdump.

When you’re done, press F to finish. This saves the new profile in /etc/apparmor.d/ and puts it into enforce mode.

Refining Profiles with Logs

What happens when an application’s needs change, or you missed something during profile generation? The aa-logprof utility is the answer. It scans the logs for you and interactively prompts you to update existing profiles based on recent denials.

sudo aa-logprof

This is the standard workflow for maintaining AppArmor profiles. When a program misbehaves or is updated, you run aa-logprof to adjust its profile accordingly.

Conclusion

AppArmor provides a significant security boost with a relatively low barrier to entry. By creating profiles for your applications—especially those that are network-facing—you can drastically reduce their potential attack surface. It forces you to define what a program is supposed to do and blocks everything else.

It takes some practice, but learning to confine your applications is a fundamental step in moving beyond basic permissions and truly hardening your Linux systems.