How to Use Ubuntu apparmor to Sandbox Vulnerable Applications

The Failure of Traditional File Permissions

In the standard Linux security model, process permissions are tied directly to the user who executed the process. If a developer runs a Python web application as the www-data user, that Python process has the exact same permissions as the www-data user.

While this sounds secure, it is structurally flawed. If the Python application contains a Remote Code Execution (RCE) vulnerability, a hacker can hijack the process. Because the process is running as www-data, the hacker can now read any file, execute any script, and open any network socket that www-data has access to. The standard Discretionary Access Control (DAC) model cannot differentiate between the Python app performing its normal duties and the Python app executing a malicious payload injected by a hacker.

To solve this, Ubuntu heavily relies on AppArmor (Application Armor). AppArmor is a Mandatory Access Control (MAC) system implemented directly inside the Linux kernel. It completely ignores standard user permissions. Instead, it binds a strict security profile to the specific binary executable. Even if the application is running as root, AppArmor can physically prevent the application from writing to the disk or opening a network socket.

Step 1: Understanding AppArmor Profiles

AppArmor operates using profiles, typically stored in /etc/apparmor.d/. The profile name mirrors the absolute path of the executable it protects, replacing slashes with dots (e.g., usr.sbin.nginx).

AppArmor profiles run in two distinct modes:

  1. Complain Mode: AppArmor allows the application to do whatever it wants, even if it violates the profile rules. However, every violation is logged to the system audit log. This is used for profiling and debugging.
  2. Enforce Mode: The kernel strictly enforces the profile rules. If the application attempts an unauthorized action, the kernel intercepts the system call, violently denies it, and logs the blocked attempt.

Step 2: Installing the Utilities

While AppArmor is active in the Ubuntu kernel by default, the tools to build custom profiles are not always installed.

sudo apt update
sudo apt install apparmor-utils -y

Step 3: Creating a Custom Profile (aa-genprof)

Suppose you have a custom, highly vulnerable binary located at /opt/custom_app/server_bin. You want to sandbox it so it can only read files in /opt/custom_app/data/ and cannot access the network.

Instead of writing the complex profile syntax from scratch, AppArmor provides an interactive profiling tool called aa-genprof.

sudo aa-genprof /opt/custom_app/server_bin

The tool will create a blank profile, place it into Complain mode, and prompt you to go to another terminal and run your application. It acts like a recording studio.

In a separate terminal, execute /opt/custom_app/server_bin and perform all of its normal, legitimate functions (read the data files, process the information, etc.).

Return to the aa-genprof terminal and press S to Scan the system logs. AppArmor will analyze what the application just did and present you with a series of questions. For example, it might say: “The application attempted to read /opt/custom_app/data/config.json. Allow or Deny?”

You press A to allow it. AppArmor automatically translates your answers into the correct profile syntax and saves the file in /etc/apparmor.d/opt.custom_app.server_bin.

Step 4: Analyzing the Profile Syntax

If you open the generated profile, you will see the syntax is surprisingly human-readable.

sudo nano /etc/apparmor.d/opt.custom_app.server_bin
#include <tunables/global>

/opt/custom_app/server_bin {
  #include <abstractions/base>

  # Allow reading the configuration file
  /opt/custom_app/data/config.json r,

  # Allow reading and writing to the data directory
  /opt/custom_app/data/* rw,

  # Explicitly deny access to standard bash shells
  deny /bin/bash x,
  deny /bin/sh x,
}

In this profile, the application is granted r (Read) and w (Write) permissions exclusively to its designated data folder. Because there is no network inet rule present, the kernel will inherently block the application from establishing outbound network connections. Even if a hacker successfully triggers an RCE vulnerability to execute a reverse shell, the kernel will kill the attempt instantly.

Step 5: Enforcing the Profile

Once you are satisfied with the profile, you must switch it from Complain mode to Enforce mode.

sudo aa-enforce /opt/custom_app/server_bin

To verify the status of all AppArmor profiles on the system, run:

sudo aa-status

The output will list how many profiles are loaded, how many are in complain mode, and how many are strictly enforced.

Step 6: Troubleshooting Blocked Applications

If an application suddenly stops working after an update, AppArmor might be blocking it. You should immediately check the kernel audit logs (or syslog) using dmesg or grep.

sudo grep -i apparmor /var/log/syslog | tail -n 20

Look for lines containing apparmor="DENIED". The log will tell you exactly which profile triggered the block and what file or capability the application was trying to access. You can then edit the profile to grant the missing permission and reload the AppArmor service to apply the fix:

sudo systemctl reload apparmor

Conclusion

AppArmor shifts Linux security from a user-centric model to a binary-centric model. By meticulously recording an application’s legitimate behavior and enforcing it at the kernel level, Ubuntu administrators can ensure that even if a custom web application is fundamentally vulnerable to exploitation, the malicious payload is physically incapable of breaking out of the sandbox and compromising the wider server.

RELATED POSTS

  • How to Install and Configure the Proxmox Backup Server (PBS) Client on Debian
  • How to Install and Configure the Postfix Mail Server on Ubuntu
  • How to Join an Ubuntu Linux Machine to a Windows Active Directory Domain using SSSD
  • How to Configure systemd-networkd for Bonded Interfaces on Ubuntu
  • How to Configure High Availability with Keepalived and HAProxy on Ubuntu
  • Get the best tech tips delivered straight to your inbox.

    Join thousands of readers mastering Apple, Google, Microsoft, and Linux.