Ubuntu AppArmor
Introduction
AppArmor is one of Ubuntu's core security features that helps protect your system from both known and unknown vulnerabilities. Unlike traditional access control systems that focus on who can access files (user permissions), AppArmor focuses on what programs can access, creating a powerful layer of application isolation.
Think of AppArmor as a security guard for your applications - it restricts what files a program can access and what actions it can perform, even if that program is compromised or malicious. This approach, known as Mandatory Access Control (MAC), significantly reduces the potential damage from security breaches.
Understanding AppArmor Basics
What is AppArmor?
AppArmor ("Application Armor") is a Linux Security Module (LSM) that allows system administrators to restrict programs' capabilities with per-program profiles. These profiles can allow or deny access to:
- File operations (read, write, execute)
- Network capabilities
- Linux capabilities (privileged operations)
- Mount points and more
How AppArmor Differs from Traditional Security
Let's compare traditional Unix permissions with AppArmor protection:
AppArmor Modes
AppArmor profiles can run in two primary modes:
- Enforce Mode: Actively blocks prohibited actions and logs violations
- Complain Mode: Permits all actions but logs what would have been blocked (learning mode)
Getting Started with AppArmor
Checking AppArmor Status
Let's verify that AppArmor is running on your Ubuntu system:
sudo aa-status
Example output:
apparmor module is loaded.
31 profiles are loaded.
28 profiles are in enforce mode.
/sbin/dhclient
/usr/bin/firefox
...
3 profiles are in complain mode.
/usr/bin/pidgin
...
0 processes have profiles defined.
0 processes are in enforce mode.
0 processes are in complain mode.
Installing AppArmor Tools
If not already installed, you can get the AppArmor utilities with:
sudo apt update
sudo apt install apparmor-utils
Working with AppArmor Profiles
AppArmor profiles are text files that define what resources an application can access. Profiles are stored in /etc/apparmor.d/
.
Examining an Existing Profile
Let's look at a simplified example of the Firefox profile:
# /etc/apparmor.d/usr.bin.firefox
/usr/bin/firefox {
# Firefox needs to read configuration files
/etc/firefox/** r,
/etc/mozilla/** r,
# Firefox needs to write to the user's Firefox directory
owner @{HOME}/.mozilla/firefox/** rwk,
# Firefox needs to read fonts
/usr/share/fonts/** r,
# Firefox needs network access
network inet stream,
network inet6 stream,
# Include common rules
#include <abstractions/base>
#include <abstractions/user-download>
#include <abstractions/audio>
}
Understanding Profile Syntax
The profile syntax includes:
- Path patterns (with wildcards like
*
and**
) - Access modes (
r
for read,w
for write,x
for execute) - Owner matching (
owner
keyword) - Network access rules
- Capability rules
- Include statements for common rule sets
Creating a Simple AppArmor Profile
Let's create a simple profile for a hypothetical custom script called mybackup
:
- First, create the program we want to secure:
sudo nano /usr/local/bin/mybackup
Add this content:
#!/bin/bash
# Simple backup script
SOURCE_DIR="$1"
BACKUP_DIR="$2"
if [ -z "$SOURCE_DIR" ] || [ -z "$BACKUP_DIR" ]; then
echo "Usage: mybackup <source_dir> <backup_dir>"
exit 1
fi
cp -r "$SOURCE_DIR" "$BACKUP_DIR"
echo "Backup completed!"
Make it executable:
sudo chmod +x /usr/local/bin/mybackup
- Generate a basic AppArmor profile in complain mode:
sudo aa-genprof /usr/local/bin/mybackup
- Exercise the program to generate learning data:
/usr/local/bin/mybackup /home/user/documents /home/user/backups
- Finish the profile generation:
Press s
when prompted to scan the system log for AppArmor events, then f
to finish.
- View and edit the generated profile:
sudo nano /etc/apparmor.d/usr.local.bin.mybackup
The generated profile might look something like:
# Last Modified: Sat Mar 13 10:00:00 2025
#include <tunables/global>
/usr/local/bin/mybackup {
#include <abstractions/base>
#include <abstractions/bash>
/usr/local/bin/mybackup r,
/bin/bash ix,
/bin/cp ix,
owner /home/*/documents/ r,
owner /home/*/documents/** r,
owner /home/*/backups/ rw,
owner /home/*/backups/** rw,
}
- Put the profile in enforce mode:
sudo aa-enforce /usr/local/bin/mybackup
Managing AppArmor
Common AppArmor Commands
Here are some essential commands for working with AppArmor:
# List all profiles and their status
sudo aa-status
# Generate a new profile
sudo aa-genprof /path/to/program
# Update an existing profile
sudo aa-logprof
# Put a profile in complain mode
sudo aa-complain /path/to/program
# Put a profile in enforce mode
sudo aa-enforce /path/to/program
# Disable a profile
sudo ln -s /etc/apparmor.d/program /etc/apparmor.d/disable/
sudo apparmor_parser -R /etc/apparmor.d/program
# Enable a disabled profile
sudo rm /etc/apparmor.d/disable/program
sudo apparmor_parser -r /etc/apparmor.d/program
Reading AppArmor Logs
AppArmor logs violations to the system log. You can view them with:
sudo aa-notify -s 1 --display $DISPLAY
Or by checking the logs directly:
sudo journalctl -f | grep apparmor
Example violation log:
Mar 13 10:15:30 ubuntu kernel: [42187.614237] audit: type=1400 audit(1647152130.257:83): apparmor="DENIED" operation="open" profile="/usr/local/bin/mybackup" name="/etc/passwd" pid=2854 comm="mybackup" requested_mask="r" denied_mask="r" fsuid=1000 ouid=0
Real-World Examples
Securing a Web Server
Let's create a simple profile for securing nginx:
sudo aa-genprof /usr/sbin/nginx
After running nginx and testing it thoroughly, the profile might look like:
#include <tunables/global>
/usr/sbin/nginx {
#include <abstractions/base>
#include <abstractions/nginx>
#include <abstractions/openssl>
# Main executable
/usr/sbin/nginx mr,
# Configuration
/etc/nginx/** r,
# Web content
/var/www/** r,
# Logs
/var/log/nginx/*.log w,
# Server functionality
capability net_bind_service,
capability setgid,
capability setuid,
# Network access
network inet stream,
network inet6 stream,
# Process management
/run/nginx.pid rw,
}
Securing Custom Applications
For developers, you can integrate AppArmor into your deployment process:
- Develop in complain mode for learning
- Test extensively to capture all legitimate access patterns
- Analyze and refine profiles
- Deploy in enforce mode
- Monitor for legitimate access that was missed
Troubleshooting AppArmor
Common Issues and Solutions
-
Application doesn't work with AppArmor enabled:
- Check logs:
sudo journalctl -f | grep apparmor
- Temporarily switch to complain mode:
sudo aa-complain /path/to/program
- Update profile:
sudo aa-logprof
- Check logs:
-
False positives in profile:
- Edit profile manually:
sudo nano /etc/apparmor.d/path.to.program
- Add necessary rules
- Reload:
sudo apparmor_parser -r /etc/apparmor.d/path.to.program
- Edit profile manually:
-
AppArmor status shows "inactive":
- Enable AppArmor:
sudo systemctl enable --now apparmor
- Verify boot parameters don't include
apparmor=0
- Enable AppArmor:
Debugging Tools
AppArmor includes several debugging tools:
# Profile troubleshooting wizard
sudo aa-logprof
# Profile generation tool
sudo aa-genprof /path/to/program
# Profile verification
sudo apparmor_parser -p /etc/apparmor.d/program
Advanced Features
Using AppArmor Abstractions
Abstractions are reusable sets of rules in /etc/apparmor.d/abstractions/
:
#include <abstractions/base>
#include <abstractions/user-tmp>
#include <abstractions/python>
Using Variables in Profiles
AppArmor supports variables for more flexible profiles:
@{HOME}/.config/myapp/** rw,
@{PROC}/@{pid}/status r,
Creating Profile Namespaces
For more complex deployments, you can use namespaces:
sudo mkdir /etc/apparmor.d/mycompany/
sudo nano /etc/apparmor.d/mycompany/myapp
Summary
AppArmor provides a powerful but approachable security layer for Ubuntu systems:
- It restricts what applications can access, not just who can access files
- It provides protection against both known and unknown vulnerabilities
- It can run in complain mode for learning or enforce mode for protection
- Profiles can be generated automatically and refined manually
- It integrates with Ubuntu's security infrastructure
By implementing AppArmor profiles for your critical applications, you significantly reduce the attack surface and potential impact of security breaches.
Additional Resources
Exercises
- Install AppArmor utilities and check the status of AppArmor on your system.
- Create a simple script and generate an AppArmor profile for it.
- Examine existing profiles in
/etc/apparmor.d/
and try to understand their structure. - Put a profile in complain mode, test the application, and check the logs for denied actions.
- Create a profile for a network service you use and test it thoroughly.
By completing these exercises, you'll gain practical experience with AppArmor and be better equipped to secure your Ubuntu systems.
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)