Sub topic 7
Mandatory access control with SELinux or AppArmor
This chapter assumes you can read systemd units and journal output, know Unix file permissions, and have root access to a RHEL-family or Debian-family test machine. It also assumes you have a baseline scan from the OpenSCAP chapter.
Why CIS requires mandatory access control
Standard Linux permissions are discretionary access control (DAC). The owner of a file decides who can read it, and root bypasses the checks. A compromised web server running as apache can read anything apache can read.
Mandatory access control (MAC) adds a second check that the kernel enforces from a policy the process cannot change. Even if an attacker takes over a daemon, the policy limits which files, ports and capabilities that daemon can use. Both checks must allow an action.
The CIS Benchmarks have a "Mandatory Access Control" section. Which system it names depends on the distribution:
| Distribution family | MAC system | How it identifies things |
|---|---|---|
| RHEL, Rocky, Alma, Fedora | SELinux | Labels on files, processes and ports |
| Ubuntu, Debian, SUSE | AppArmor | Profiles tied to executable paths |
Do not install both on one host. Use the one your distribution ships and supports.
SELinux on RHEL-family systems
The CIS recommendations for SELinux are:
- SELinux is installed.
- SELinux is not disabled in the bootloader configuration.
- The policy type is
targeted(or stricter). - The mode is
enforcing. - No services run as
unconfined_service_t. setroubleshootandmcstransare not installed.
Check the current state first:
getenforce
sestatus
sudo grubby --info=ALL | grep -E 'selinux=0|enforcing=0' || echo "no bootloader overrides"Item 2 matters because a selinux=0 or enforcing=0 kernel argument overrides the configuration file. If the grep finds either, remove it:
sudo grubby --update-kernel=ALL --remove-args="selinux=0 enforcing=0"Set the persistent mode in the configuration file:
# /etc/selinux/config
SELINUX=enforcing
SELINUXTYPE=targetedMoving from disabled to enforcing
Do not go straight from disabled to enforcing. Files created while SELinux was off have no labels, and a system with unlabeled files can fail to boot or start services. Use this sequence:
- Set
SELINUX=permissivein/etc/selinux/config. - Create the relabel flag with
sudo touch /.autorelabel, then reboot. The relabel can take several minutes on large filesystems. - Run the workload in permissive mode and collect denials.
- Fix each denial (see below), then set
SELINUX=enforcingand reboot.
Reading and fixing denials
SELinux records blocked actions as AVC (access vector cache) messages in the audit log. setroubleshoot can explain them, but CIS wants it removed on servers, so use ausearch instead:
sudo ausearch -m AVC,USER_AVC -ts recent
sudo ausearch -m AVC -ts today | audit2whyMost denials have one of three causes, and you should try the fixes in this order:
- Wrong file label. Set the correct default label, then apply it.
- A policy boolean is off. Turn on the existing switch that permits the behavior.
- A genuinely new behavior. Only then write a local policy module.
# Wrong label: content served from a non-default directory
sudo semanage fcontext -a -t httpd_sys_content_t '/srv/web(/.*)?'
sudo restorecon -Rv /srv/web
# Boolean: let the web server make outbound network connections
getsebool httpd_can_network_connect
sudo setsebool -P httpd_can_network_connect onsemanage comes from the policycoreutils-python-utils package. Use chcon only for quick tests. Its changes are lost on the next restorecon or relabel, while semanage fcontext persists.
Finding unconfined services
The CIS check for unconfined services looks for daemons running in the unconfined_service_t domain. This happens when a service binary has a generic label, usually because it was installed in a non-standard location:
ps -eZ | grep unconfined_service_tEither relabel the binary so it transitions into a proper domain, or record an exception for it. Do not switch the whole system to permissive to hide one service.
AppArmor on Ubuntu and Debian
AppArmor confines programs by path. A profile lists what an executable may read, write, execute and which capabilities it may use. The CIS recommendations are:
- AppArmor is installed.
- AppArmor is enabled in the bootloader configuration.
- All profiles are in enforce mode (or at least loaded).
- No processes are unconfined if a profile exists for them.
Check and install:
sudo apt install apparmor apparmor-utils apparmor-profiles
sudo aa-statusIf the bootloader line does not enable AppArmor, add the kernel arguments and regenerate the configuration:
# /etc/default/grub
GRUB_CMDLINE_LINUX="apparmor=1 security=apparmor"sudo update-grub
sudo rebootCIS also fails hosts where the bootloader contains apparmor=0, so check for that.
Complain mode and enforce mode
AppArmor has two profile modes. In complain mode the kernel logs violations but allows them. In enforce mode it blocks them. This mirrors permissive and enforcing in SELinux, so use the same workflow: observe in complain mode, adjust, then enforce.
# Put one profile in complain mode, exercise the service, then review the log
sudo aa-complain /etc/apparmor.d/usr.sbin.rsyslogd
sudo journalctl -k | grep 'apparmor="ALLOWED"'
# Update the profile from logged events, then enforce it
sudo aa-logprof
sudo aa-enforce /etc/apparmor.d/usr.sbin.rsyslogd
# Confirm what is enforced and what runs unconfined
sudo aa-status
sudo aa-unconfinedCommon mistakes
- Fixing denials by disabling MAC. The scan then passes only if you also fix the underlying cause. A host in permissive mode fails the "mode is enforcing" check.
- Relabeling with
chcon. The label disappears afterrestorecon, and the service breaks again later. - Ignoring the bootloader. A correct config file does not help if the kernel command line overrides it.
- Skipping the rescan. Run the OpenSCAP profile again and confirm that the MAC rules now pass.
Exercise
After a reboot, sestatus reports "Current mode: permissive" even though /etc/selinux/config says SELINUX=enforcing. Name one likely cause and the command you would use to find it.
Show answer
The kernel command line probably contains enforcing=0 (or selinux=0), which overrides the configuration file. Run sudo grubby --info=ALL | grep -E 'selinux=0|enforcing=0' or cat /proc/cmdline to find it, then remove it with sudo grubby --update-kernel=ALL --remove-args="enforcing=0 selinux=0" and reboot.
Exercise
A web server returns "403 Forbidden" for files in /srv/web, and ausearch -m AVC shows denials against the label var_t. What two commands fix the problem persistently?
Show answer
Run sudo semanage fcontext -a -t httpd_sys_content_t '/srv/web(/.*)?' to record the correct default label, then sudo restorecon -Rv /srv/web to apply it to the existing files.