Sub topic 1
Why file permissions are not enough: DAC and MAC
Prerequisites
This chapter assumes you can:
- Use the Linux command line with
sudo. - Read standard file owners and permission bits with
ls -l. - Run a RHEL-family system (RHEL, Rocky, AlmaLinux, or Fedora) with SELinux enabled.
Discretionary and mandatory access control
Linux checks file permissions with discretionary access control (DAC). The file owner decides who can read, write, or run a file with chmod and chown. If a web server process runs as apache and the file is readable by apache, the kernel allows the read, even when the web server has no reason to read that file.
Mandatory access control (MAC) adds a second check that the owner cannot change. SELinux is the MAC system on RHEL-family systems. It allows an action only when both DAC and the SELinux policy allow it. A compromised web server therefore cannot reach files outside its policy, even when the file permissions look open.
Checking and changing the SELinux mode
SELinux runs in one of three modes:
- enforcing: the policy is applied and denied actions are blocked.
- permissive: denied actions are logged but still allowed.
- disabled: SELinux is off.
getenforce
sestatus
sudo setenforce 0 # temporary: permissive until reboot
sudo setenforce 1 # temporary: enforcing againsetenforce changes the mode only until the next reboot. To make a change persistent, edit SELINUX= in /etc/selinux/config.
> Do not switch to disabled to fix a service. Disabling SELinux removes the protection for every service on the system.
Reading security contexts
Every file, process, and port carries a security context in the form user:role:type:level. The type field matters most for everyday work.
ls -Z /var/www/html
ps -eZ | grep httpdThe first command shows the file type, such as httpd_sys_content_t. The second shows the domain of each Apache process, such as httpd_t.
Type enforcement
SELinux uses type enforcement. A process runs in a domain, and a file has a type. The policy contains rules such as "processes in httpd_t may read files of type httpd_sys_content_t." If no rule allows the action, SELinux denies it and writes an AVC (access vector cache) denial to the audit log.
flowchart TD
A["Process runs in domain httpd_t"] --> B{"Policy rule allows httpd_t to access this file type?"}
B -->|Yes| C["Access allowed"]
B -->|No| D["Access denied and AVC logged"]caption: Type enforcement check for a web server reading a file alt: A process in the httpd_t domain asks to access a file. If a policy rule allows that access, the access is allowed. If no rule allows it, the access is denied and an AVC entry is written to the audit log.
Exercise
Exercise
A web server running as httpd_t cannot read a file in /var/www/html that has correct Unix permissions. Which check blocks it, and what should you inspect first?
Show answer
SELinux type enforcement blocks it, because the policy has no rule that lets httpd_t read that file's type. Inspect the file type with ls -Z and the process domain with ps -eZ, then check the audit log for an AVC denial.