Sub topic 6
AppArmor and SELinux Defaults
This chapter assumes you can use a Linux shell, install packages, manage systemd services, and understand ordinary file permissions. You will compare two mandatory access control systems: AppArmor on Ubuntu and Debian, and SELinux on Rocky Linux.
Two layers of access control
Linux first checks discretionary access control (DAC). DAC includes the file owner, group, permission bits, and access control lists. A process normally needs suitable Unix permissions before it can read or write a file.
Mandatory access control (MAC) adds another policy layer. MAC can deny an action even when the process has sufficient Unix permissions. The policy is controlled by the operating system rather than only by the file owner.
A useful model is:
process request
|
v
Unix permissions and ACLs
|
v
AppArmor or SELinux policy
|
v
allow or denyA denial from MAC often looks confusing because ls -l shows permissions that appear correct.
AppArmor on Ubuntu and Debian
AppArmor is a MAC system that restricts programs by path-based profiles. A profile names a program and describes the files, capabilities, and other resources that it may use.
Ubuntu enables AppArmor by default on standard server installations. Many Debian installations also use AppArmor, although the exact package and profile set depends on how Debian was installed.
Check whether AppArmor is active:
sudo aa-statusThe output lists loaded profiles and whether they are in enforce or complain mode.
An enforcing profile blocks actions that violate its rules. A complain-mode profile records violations but usually allows the action. Complain mode helps while developing or troubleshooting a profile, but it does not provide the same protection as enforce mode.
AppArmor profiles are commonly stored below /etc/apparmor.d/. After changing a profile, reload it with the AppArmor tools or restart the affected service. Do not edit a profile without checking its syntax and reviewing the resulting logs.
SELinux on Rocky Linux
SELinux uses security labels and a policy that describes which labeled processes may access which labeled resources. The label is part of the file metadata. This lets SELinux distinguish, for example, web content from a private user file even when Unix ownership and mode bits are similar.
Rocky Linux enables SELinux in enforcing mode by default. Check its state with:
getenforce
sestatusThe main states are:
- Enforcing: policy violations are blocked and logged.
- Permissive: violations are logged but not blocked.
- Disabled: SELinux is not active. Enabling it later requires careful preparation.
Inspect a file's SELinux context with ls -Z:
ls -Z /var/www/htmlA service may need a particular context before it can read or write a path. When a file is moved or copied, its label may not match the location. restorecon restores the default label for a path:
sudo restorecon -Rv /var/www/htmlFor a permanent nonstandard location, administrators usually define the desired mapping with semanage fcontext, then run restorecon. Do not solve a denial by giving the service unrestricted access or by disabling SELinux.
Troubleshooting differences
| Question | AppArmor | SELinux |
|---|---|---|
| Main policy model | Program paths and profiles | Process and resource security labels |
| Status command | `aa-status` | `getenforce`, `sestatus` |
| Typical evidence | AppArmor denial logs | AVC denial logs and labels |
| Common correction | Adjust or reload a profile | Correct labels, booleans, or policy |
When a service fails, first reproduce the failure and inspect the service journal. Then check the MAC system's logs. On SELinux systems, an AVC denial is a useful clue:
sudo ausearch -m AVC -ts recentOn AppArmor systems, search the kernel or system journal for AppArmor denial messages. Confirm that the denied operation belongs to the expected service before changing policy.
A frequent mistake is to run chmod 777 after a denial. That changes DAC permissions but cannot override MAC. Another mistake is to disable the security system permanently when the real issue is a missing label or a narrowly scoped rule.
Choosing between the defaults
The choice affects administration more than application source code. AppArmor can feel direct when you think in terms of “this program may access these paths.” SELinux requires more attention to labels, types, booleans, and policy tools, but its labeling model works well for complex services and controlled enterprise environments.
Check vendor documentation before choosing a base image. A vendor may publish AppArmor profiles, SELinux policy modules, or supported combinations of both. A team that already operates one system confidently may prefer its existing tools over a theoretical security difference.
Exercise
A web service can read a file according to its owner and mode bits, but it still receives "permission denied" on Rocky Linux. What should you check first?
Show answer
Check whether SELinux is enforcing, inspect the file and process contexts with ls -Z and service tools, and review recent AVC denials. Correct the label or policy rather than changing the mode to 777.