Sub topic 1
Why file permissions are not enough: DAC and MAC
This chapter explains why standard Linux permissions cannot contain a compromised service, and how SELinux's mandatory access control closes that gap. It is the foundation for the rest of the tutorial. Later chapters cover modes, contexts, types, labeling, booleans, ports and denials.
Prerequisites
This chapter assumes you already have the following:
- You are comfortable with the Linux command line and
sudo. - You understand file ownership and the permission bits (
rwxfor user, group and other). - You can manage services with
systemctland read logs withjournalctl. - You have basic familiarity with a web server such as Apache httpd or nginx.
- You have a RHEL-family system (RHEL, Rocky, AlmaLinux or Fedora) with SELinux enabled, if you want to run the commands.
How discretionary access control works
Standard Linux permissions are a form of discretionary access control (DAC). Access decisions rest on two things: the identity of the process (its user ID and group IDs) and the permission bits and owner of the object it touches.
The word "discretionary" matters. The owner of a file decides who may access it. If you own a file, you can run chmod or chown and grant access to anyone. The system does not stop you, even if the change is a mistake.
A typical check looks like this:
ls -l /etc/shadow /var/www/html/index.html
id apacheFor a web server, the process runs as the apache user. It can read any file that the apache user, the apache group, or "other" is permitted to read. The kernel asks one question: do the IDs and mode bits allow this?
The limits of DAC
DAC works well for separating ordinary users from each other. It is weak at containing a service that has been compromised, for these reasons:
- Access follows the user, not the program. Every process running as a given user has all of that user's rights. A web server and a shell started by that user are treated the same.
- Owners can loosen permissions. A process that owns a file can make it world-readable. An attacker who controls the process can do the same.
- Root bypasses it. A process running as root (UID 0), or holding powerful capabilities, ignores permission bits for most operations. A compromised root service can read
/etc/shadowand write almost anywhere. - World-readable data is open to everyone. Many files are readable by "other" because ordinary programs need them. A hijacked web server can read all of them.
- There is no rule about what a program should do. DAC does not know that Apache should serve web content and should not read SSH keys or connect to a database port.
Suppose an attacker exploits a flaw in a PHP application and gets code execution as apache. Under DAC alone, that attacker can read anything apache or "other" can read, such as /etc/passwd, application configuration, or home directories with loose modes. The attacker can also open network connections and run any program the user may run.
How mandatory access control works
Mandatory access control (MAC) adds a second layer of checks. A central policy, written by the administrator and the distribution, decides what each process may do. Neither users nor the owners of files can override it. That is why it is called "mandatory".
SELinux is the MAC implementation in the Linux kernel on RHEL-family systems. It works through the Linux Security Modules (LSM) framework. Instead of asking only "which user is this?", SELinux asks "which kind of program is this, and is that kind of program allowed to perform this operation on this kind of object?"
To answer that, SELinux attaches a label called a security context to every process, file, directory, socket and port. The policy contains rules that say which process labels may act on which object labels. Chapters later in this tutorial cover labels in detail. For now, remember that a running Apache process carries a label such as httpd_t, and web content carries a label such as httpd_sys_content_t.
DAC and MAC run together
SELinux does not replace DAC. Both must allow an operation before it succeeds.
- The kernel checks DAC first (owner, group, mode bits).
- If DAC allows the operation, the kernel then consults the SELinux policy.
- If either check denies, the operation fails.
This ordering has two practical consequences:
- SELinux can only take access away. It never grants something that DAC refuses.
- A denial can come from either layer, and the symptom looks the same. Both return "Permission denied".
A worked comparison
The difference is easiest to see with a file that DAC allows but SELinux blocks. This example needs a RHEL-family system with Apache installed and SELinux in enforcing mode. Later chapters explain each command.
sudo dnf install -y httpd
sudo mkdir -p /srv/web
echo "hello from /srv/web" | sudo tee /srv/web/index.html
sudo chmod 755 /srv/web
sudo chmod 644 /srv/web/index.html
ls -l /srv/web/index.html
ls -Z /srv/web/index.htmlOutput similar to this appears:
-rw-r--r--. 1 root root 20 Nov 28 04:50 /srv/web/index.html
unconfined_u:object_r:var_t:s0 /srv/web/index.htmlHere is how each layer sees the file:
- DAC: the mode
644lets "other" read the file. Theapacheuser can read it. - MAC: the file has type
var_t, a generic label for files under/srv. The policy does not lethttpd_treadvar_tfiles. SELinux would deny Apache when it serves the file.
If you pointed Apache at /srv/web with a DocumentRoot directive, visitors would receive a 403 error even though the permission bits look correct. The audit log would record the denial. This is the classic first SELinux surprise for administrators. The fix is to label the content correctly, not to change the permission bits. Later chapters cover that fix.
What confinement means in practice
A confined service is a service whose process runs in its own SELinux domain, with only the access the policy grants to that domain. On RHEL-family systems, the targeted policy confines most network-facing services, including httpd, sshd, named, postfix and many others.
Return to the compromised PHP application. With SELinux enforcing, the attacker still has code execution as apache, but the process is also bound to httpd_t. The policy limits what that domain can do:
- It may read files labeled as web content, but not user home directories or SSH keys.
- It may bind only to ports labeled for HTTP.
- It may not make outbound network connections unless a boolean allows it.
- It may not run arbitrary programs or load unrelated files as code.
The attack still happened, but the damage is smaller. This is called damage containment. It is the reason to learn SELinux well enough to leave it enabled.
Comparing the two models
| Aspect | DAC (standard permissions) | MAC (SELinux) |
|---|---|---|
| Who sets the rules | The file owner, or root | The system policy |
| Can an owner change access? | Yes, with `chmod` or `chown` | No, only a policy change or relabeling by a privileged administrator |
| What identifies the subject | User and group IDs | A security context, including a type or domain |
| Does root bypass it? | Largely, yes | No, root is also confined to its domain |
| Default stance | Whatever the mode bits permit | Denied unless a policy rule allows it |
| Granularity | Read, write, execute for owner, group, other | Many classes and permissions, such as `name_bind` on a port or `connectto` on a socket |
| Scope | Files and directories | Files, processes, sockets, ports, devices, IPC and more |
One caution about the root row: root is bound by SELinux only if the process runs in a confined domain. A logged-in administrator on the default targeted policy usually runs in the unconfined_t domain, which the policy treats very permissively. A system service started by systemd, such as httpd, runs in its own confined domain, even though it started as root.
Common misunderstandings
- "SELinux replaces file permissions." It does not. Both layers must allow access.
- "If
chmod 777does not fix it, the file system is broken." A 777 mode satisfies DAC only. A SELinux denial persists. Avoidchmod 777as a diagnostic, because it also weakens DAC. - "SELinux is only for servers handling secrets." It protects any exposed service, because the policy is based on what a program should do, not on how secret the data is.
- "Disabling SELinux is a harmless shortcut." It removes the containment layer, and later re-enabling can require a full filesystem relabel. The rest of this tutorial shows how to fix problems without turning SELinux off.
Checking whether SELinux is active
A quick check tells you which layer you are dealing with. The next chapter covers modes in detail.
getenforceThe command prints Enforcing, Permissive or Disabled. In Enforcing mode the policy is applied and violations are blocked. In Permissive mode violations are only logged. With Disabled, SELinux does nothing.
Key takeaways
- DAC decides access by user identity and permission bits, and owners control those bits.
- MAC decides access by a central policy that owners and users cannot override.
- SELinux checks run after DAC checks. Both must allow an operation.
- A confined service such as httpd is limited to what its domain is allowed, even if an attacker takes over the process.
- A "Permission denied" error with correct mode bits often points to a SELinux label problem.
Exercise
An Apache process running as the apache user reads /srv/web/index.html. The file has mode 644, and the SELinux type on it is var_t. The policy does not allow httpd_t to read var_t files. Is the read allowed with SELinux in enforcing mode? Name which layer blocks it and which layer allows it.
Show answer
The read is denied. DAC allows it, because mode 644 lets "other", and so the apache user, read the file. SELinux (MAC) blocks it, because the policy has no rule letting the httpd_t domain read var_t files. Both layers must allow an operation, so the result is a denial and an AVC entry in the audit log.
Exercise
Why does an attacker who has taken over a confined httpd process still have limited reach, even though the process was started by root and the attacker can change file modes?
Show answer
The process is bound to the httpd_t domain, and the SELinux policy is mandatory. File owners and users cannot change it by using chmod or chown. The policy allows httpd_t only specific actions, such as reading web content and binding to HTTP ports. Root status does not lift these limits for a confined domain.