Sub topic 4
Type enforcement: how domains and types decide access
Prerequisites and working model
This chapter assumes that you can use the Linux command line, sudo, standard file permissions, systemctl, and journalctl. You should also know the basics of a web server such as Apache httpd or nginx.
SELinux type enforcement is the rule system that limits what a process may do. It works in addition to normal Linux permissions.
Linux file permissions are discretionary access control (DAC). The file owner or an administrator can change them. SELinux provides mandatory access control (MAC). The SELinux policy decides which access is allowed, even when DAC permissions appear to allow it.
A process runs in a domain. A file, directory, port, or other resource has a type. SELinux rules describe which domains may perform which actions on which types.
For example:
httpd_tis the domain used by a confined Apache process.httpd_sys_content_tis a type for files that Apache may serve.- An allow rule may permit
httpd_tto read files labeledhttpd_sys_content_t. - The same rule may deny Apache access to files labeled
user_home_t.
The type is the most important part of a security context. A context commonly looks like this:
system_u:object_r:httpd_sys_content_t:s0
The fields are the SELinux user, role, type, and level. Type enforcement primarily uses the type field.
How a decision is made
When Apache opens a file, SELinux checks the process domain, the file type, and the requested operation. A simplified decision looks like this:
The check happens after DAC still matters. Apache must pass both checks:
- Linux permissions must allow the operation.
- SELinux policy must allow the domain and type combination.
A successful DAC check cannot override an SELinux denial.
Inspect both sides with these commands:
getenforce
ps -eZ | grep httpd
ls -lZ /var/www/html/index.htmlIf the process shows httpd_t and the file shows httpd_sys_content_t, the labels match the usual read-only web-serving policy. A file with the wrong type can cause a denial even when its owner, group, and mode bits look correct.
Type transitions
SELinux can also assign a new domain when one process starts another. This is called a type transition. For example, a service manager may start a program in a service-specific domain rather than leaving it in the manager's domain. This confines the program before it processes untrusted input.
Do not solve a denial by changing a process into an unrelated domain. The domain must describe the service's intended behavior, and the policy should grant only the required operations.
>callout note > A label is not an ordinary file permission. Changing a label can change access without changing chmod output. Always inspect the full context with ls -Z, ps -Z, or the relevant SELinux tool.
Common errors
- Checking only
ls -l: Usels -Zas well. DAC output does not show SELinux decisions. - Confusing a domain with a file type:
httpd_tnormally identifies the process domain.httpd_sys_content_tidentifies served content. - Using
chconas a permanent fix:chconchanges the current label but can be undone by a relabel or file restoration. Persistent mappings usesemanage fcontextandrestorecon. - Disabling SELinux immediately: First identify the domain, object type, and denied operation. A denial often indicates a mislabeled file or a supported policy option.
Exercise
Apache runs in httpdt, but it cannot read a file labeled userhome_t. Which part of the SELinux decision is most likely missing?
Show answer
The policy usually permits httpdt to read httpdsyscontentt, not userhomet. Correct the file's persistent SELinux labeling rather than changing only its DAC permissions.