Working with AppArmor
1. Checking AppArmor Status
| Command | Description | Output |
|---|---|---|
| apparmor_status | Displays current AppArmor profile status and loaded policies | apparmor_status |
| aa-status | Alternative command to check AppArmor status (alias for apparmor_status) | aa-status |
Note: The output includes loaded profiles, enforcing mode status, and any processes not confined by AppArmor.
2. Listing AppArmor Profiles
| Command | Description | Notes |
|---|---|---|
| apparmor_status | Displays the status of AppArmor profiles | Shows which profiles are enforcing, complaining, or not loaded |
| aa-status | Alias for apparmor_status |
Same functionality as apparmor_status |
Example: Listing AppArmor Profiles
$ apparmor_status
apparmorfs on /sys/kernel/security/apparmor type debugfs (rw,relatime)
24 profiles in enforce mode
/usr/bin/firefox
/usr/bin/pulseaudio
/usr/sbin/cupsd
...
3. Setting Profile Modes
| Mode | Description | Configuration |
|---|---|---|
| disabled | AppArmor is completely disabled for the profile. | mode=disabled |
| enforce | Enforces the profile rules strictly. Denies access not explicitly allowed. | mode=enforce |
| complain | Logs violations but does not enforce restrictions. Allows all actions. | mode=complain |
Example: Setting Profile Mode
# Set profile to complain mode
sudo apparmor_parser -r /etc/apparmor.d/usr.bin.vim
# Change to enforce mode
sudo apparmor_parser -r /etc/apparmor.d/usr.bin.vim
# Check current mode
sudo aa-status
4. Viewing Profile Definitions
| Profile Property | Description | Example |
|---|---|---|
| apparmor_parser | Command-line tool to parse and load AppArmor profiles | apparmor_parser -r /etc/apparmor.d/usr.bin.firefox |
| /etc/apparmor.d/ | Directory where AppArmor profile definitions are stored | /etc/apparmor.d/usr.bin.firefox |
| /sys/kernel/security/apparmor/profiles | Virtual filesystem path to view loaded profiles | cat /sys/kernel/security/apparmor/profiles |
5. Creating Custom Profiles
Example: Basic AppArmor Profile Structure
# /etc/apparmor.d/usr.bin.myapp
#include <abstractions/base>
/usr/bin/myapp {
# Profile name and path
/usr/bin/myapp rix,
# File access rules
/etc/myapp.conf r,
/var/log/myapp.log w,
# Network access
network inet stream,
}
| Directive | Description | Usage |
|---|---|---|
| include | Includes abstractions or other profiles | #include <abstractions/base> |
| /path/to/executable | Defines executable path and permissions | /usr/bin/myapp rix, |
| file_access_rule | Defines file access permissions | /etc/myapp.conf r, |
| network | Defines network access permissions | network inet stream, |
Note: AppArmor profiles must be loaded using
apparmor_parser or via systemd to take effect.
Warning: Incorrectly configured profiles can block essential system functions or prevent applications from running.
Workflow: Creating and Loading a Custom AppArmor Profile
- Create a new profile file in
/etc/apparmor.d/ - Define the application path and permissions
- Include necessary abstractions (e.g.,
<abstractions/base>) - Load the profile using
apparmor_parser -r /etc/apparmor.d/<profile_name> - Verify the profile is active with
apparmor_status
6. Updating Profiles
| Profile Action | Description | Command | Notes |
|---|---|---|---|
| Modify Profile | Editing an existing AppArmor profile file | sudo nano /etc/apparmor.d/<profile_name> |
Changes take effect immediately after saving |
| Reload Profile | Reapply a modified profile to the running system | sudo apparmor_parser -r /etc/apparmor.d/<profile_name> |
Useful for testing changes without restarting services |
| Update Profile | Replace a profile with a new version (e.g., from package update) | sudo apparmor_parser -r /etc/apparmor.d/<profile_name> |
May require service restart to apply changes |
Example: Updating a Profile
sudo nano /etc/apparmor.d/usr.bin.nginx
# Make changes to the profile file
sudo apparmor_parser -r /etc/apparmor.d/usr.bin.nginx
Note: Always backup existing profiles before modifying them.
Warning: Incorrectly modified profiles can lead to service failures or security vulnerabilities.
7. Disabling Profiles
| Profile | Status | Command | Notes |
|---|---|---|---|
| /etc/apparmor.d/usr.bin.vim | Enabled | sudo apparmor_status |
Shows active profiles |
| /etc/apparmor.d/usr.sbin.apache2 | Enabled | sudo aa-status |
Alternative status command |
Example: Temporarily disabling a profile
sudo aa-disable /etc/apparmor.d/usr.bin.vim
sudo apparmor_status
Example: Permanently disabling a profile
sudo ln -s /etc/apparmor.d/usr.bin.vim /etc/apparmor.d/disable/
sudo apparmor_parser -r /etc/apparmor.d/disable/usr.bin.vim
Note: Disabling profiles removes security restrictions. Only disable when necessary for debugging or compatibility.
Warning: Disabling AppArmor profiles can expose the system to security vulnerabilities. Ensure that you understand the implications before proceeding.
8. Reloading Profiles
AppArmor profiles can be reloaded to apply changes without restarting the service. This is useful for updating policies in a live environment.
| Command | Description | Notes |
|---|---|---|
| apparmor_parser -r | Reloads a profile from a file | Must specify the path to the updated profile file |
| apparmor_parser -r -T | Test reloads a profile without applying changes | Useful for validating syntax before applying |
Example: Reloading a Profile
# Reload a profile from file
sudo apparmor_parser -r /etc/apparmor.d/usr.bin.vim
# Test reload without applying changes
sudo apparmor_parser -r -T /etc/apparmor.d/usr.bin.vim
Note: When reloading a profile, the system will temporarily allow access to resources that were previously restricted by the old policy. This can introduce a brief security window.
Warning: If the new profile contains syntax errors, the reload will fail and the old policy remains active. Always test changes in a non-production environment.
9. Debugging Profile Issues
| Issue | Description | Solution |
|---|---|---|
| Profile not loading | AppArmor profile fails to load due to syntax errors or missing permissions. | Check logs with journalctl -u apparmor and fix profile syntax. |
| Denials in logs | Applications are denied access to resources even with a profile loaded. | Use sudo ausearch -m apparmor to find denials and adjust profile rules. |
| Profile conflicts | Multiple profiles conflict or override each other. | Ensure profile names are unique and check for overlapping rules in /etc/apparmor.d/. |
Note: AppArmor logs can be found in
/var/log/syslog or via journalctl.Workflow: Troubleshoot AppArmor Profile Issues
- Run
apparmor_statusto check loaded profiles. - Review logs for denials or errors using
journalctl. - Validate profile syntax with
sudo apparmor_parser -r /etc/apparmor.d/<profile_name>. - Adjust profile rules if needed and reload with
sudo systemctl reload apparmor.
10. Understanding AppArmor vs SELinux
| Feature | AppArmor | SELinux |
|---|---|---|
| Policy Model | Path-based access control | Role-based access control (RBAC) |
| Policy Language | Simple, file-based rules | Complex, policy modules |
| Learning Curve | Easy to understand and configure | Steep learning curve |
| Default Enforcement | Disabled by default | Enabled by default in many distributions |
| Performance Impact | Minimal impact | Moderate to high impact |
| Flexibility | Less flexible for complex policies | Highly flexible and granular |
| Use Case | Simple security requirements | Advanced security requirements |
AppArmor vs SELinux: Key Differences
- AppArmor uses path-based access control, making it easier to configure and understand for basic use cases.
- SELinux uses a more complex role-based model, offering more granular control but requiring deeper knowledge.
- AppArmor is often preferred in environments where simplicity and ease of use are prioritized.
- SELinux is chosen for systems requiring strict compliance and advanced security policies.