Sub topic 4
Type enforcement: how domains and types decide access
This chapter explains the rule that does most of the work in the targeted SELinux policy. It assumes you can read a security context (user:role:type:level) and that you know the difference between discretionary and mandatory access control from earlier chapters. You should also be comfortable running ls, ps, and sudo on a RHEL-family system with SELinux enabled.
The core idea
Type enforcement is the part of SELinux that compares two labels and decides whether an action is allowed. Every process has a type, and every object it might touch also has a type. An object can be a file, a directory, a network port, or a socket.
- A domain is the type of a process. Examples are
httpd_tfor Apache andsshd_tfor the SSH daemon. - A type on an object describes what kind of thing it is. Examples are
httpd_sys_content_tfor web content andetc_tfor configuration files.
The policy contains allow rules. Each rule names a source domain, a target type, an object class, and a set of permissions. If no rule matches, SELinux denies the request. This is called default deny.
allow httpd_t httpd_sys_content_t:file { read getattr open };Read this rule left to right: a process in domain httpd_t may read, get attributes of, and open objects of class file that carry the type httpd_sys_content_t.
The Linux permission bits still apply first. SELinux is a second check. A request must pass both. A file with mode 644 that has the wrong type is still blocked for httpd_t. A file with the right type but mode 600 owned by another user is still blocked by normal permissions.
Seeing domains and types in practice
Run these commands on a system with Apache installed and running.
ps -eZ | grep httpd
ls -Zd /var/www/html
ls -Z /etc/passwdTypical output looks like this:
system_u:system_r:httpd_t:s0 1021 ? 00:00:00 httpd
unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html
system_u:object_r:passwd_file_t:s0 /etc/passwdThe web server runs in httpd_t. The web root is labeled httpd_sys_content_t. The password file is labeled passwd_file_t. The policy has a rule letting httpd_t read httpd_sys_content_t. It has no rule letting httpd_t read passwd_file_t. So even if Apache is compromised and runs as a user that could read /etc/passwd under normal permissions, SELinux still blocks the read.
How a process enters its domain
A process does not choose its own domain. The kernel assigns it during execve(), using a type transition rule. The rule depends on the domain of the parent process and the type of the executable file.
For Apache, systemd runs in init_t and starts /usr/sbin/httpd, which is labeled httpd_exec_t. A transition rule says that when init_t executes a file of type httpd_exec_t, the new process gets the domain httpd_t.
type_transition init_t httpd_exec_t:process httpd_t;Three things must be true for the transition to work:
- The parent domain may execute the file type.
- The new domain has
httpd_exec_tas an entry point. - The parent domain is allowed to transition to the new domain.
This is why a wrong label on an executable can break a service. If you copy a binary to a custom path and it picks up the type user_home_t or default_t, the service may start in the wrong domain. A service that starts as unconfined_service_t runs without confinement, and SELinux protects nothing.
The diagram below shows the full sequence.
Object classes and permissions
An allow rule is specific to an object class. The class file has permissions such as read, write, append, execute, and getattr. The class dir has search, add_name, and remove_name. The class tcp_socket has name_bind and name_connect.
This matters when you debug. A denial for dir search means the process could not traverse a directory on the way to a file. The file's own label may be correct, but a parent directory may carry a type the domain cannot search. You need to check every directory in the path.
| Class | Typical permissions | Example denial cause |
|---|---|---|
| `file` | read, write, open, execute | Content labeled `default_t` |
| `dir` | search, read, add_name | Parent directory labeled `var_t` |
| `tcp_socket` | name_bind, name_connect | Port not labeled `http_port_t` |
| `process` | transition, signal | Binary has the wrong entry-point type |
Attributes group types
Policies with thousands of types would be hard to write if every rule named individual types. SELinux uses attributes to group types. A rule can name an attribute instead of a single type, and it then applies to every type that carries that attribute.
For example, the attribute file_type covers all file types, and domain covers all process domains. Web content types such as httpd_sys_content_t and httpd_user_content_t share attributes that other rules use.
You do not usually write these by hand. You do need to know that when sesearch shows a rule naming an attribute, the rule can apply to more types than the one you are looking at.
Confined and unconfined domains
In the targeted policy, most network-facing services run in their own confined domain. Ordinary logged-in users usually run in unconfined_t. An unconfined domain has allow rules that permit nearly everything, so type enforcement does not restrict it much.
Check which one you have:
id -Z
ps -eZ | grep -E 'httpd|sshd|unconfined' | headDo not assume that a service is confined because SELinux is enforcing. Confirm the domain with ps -eZ.
Why mislabeled files cause most failures
Most denials in day-to-day administration come from a mismatch between a domain and a type, and not from a missing rule. The usual causes are:
- Files moved with
mvkeep their old label, becausemvpreserves the context. Files created withcpor a new file take the label from the destination directory. - Content placed in a custom directory such as
/srv/webgetsvar_tordefault_t, whichhttpd_tcannot read. - A service listens on a port that has no matching port type.
The fix in each case is to change the label or the port type to match what the policy already allows. You do not need to change the rules. Later chapters cover semanage fcontext, restorecon, and port labeling in detail.
Reproducing a type enforcement denial
This example shows a denial caused by a single wrong label. It assumes Apache is installed and serving /var/www/html.
echo "hello" | sudo tee /var/www/html/test.html
sudo chcon -t user_home_t /var/www/html/test.html
ls -Z /var/www/html/test.html
curl -s -o /dev/null -w "%{http_code}\n" http://localhost/test.html
sudo ausearch -m avc -ts recent | tail -n 3
sudo restorecon -v /var/www/html/test.html
curl -s -o /dev/null -w "%{http_code}\n" http://localhost/test.htmlThe first curl returns 403. The audit log shows that httpd_t was denied read on a file of type user_home_t. Permission bits were fine the whole time. After restorecon resets the label to httpd_sys_content_t, the second curl returns 200.
The chcon change here is temporary in the sense that restorecon reverts it. Chapter material on semanage fcontext covers how to make a label persist.
Summary of the decision flow
When a confined process requests access, the kernel evaluates in this order:
- Normal discretionary permissions (owner, group, mode, ACLs).
- The SELinux allow rules for the source domain, target type, object class, and permission.
- If either denies the request, the request fails. SELinux denials are written to the audit log as AVC messages.
Exercise
Apache runs in httpdt and fails to read /srv/web/index.html with a 403, although the file mode is 644 and the directory is searchable by all users. ls -Z shows the file type is defaultt. Explain why the read is denied, and say which part of the system you would change to fix it.
Show answer
Linux permission bits pass, but SELinux type enforcement is a second check. The policy has an allow rule for httpdt on httpdsyscontentt, and no rule for httpdt on defaultt, so default deny applies and an AVC is logged. Fix it by changing the label on the file (and the directory tree) to httpdsyscontent_t, persistently with semanage fcontext followed by restorecon. You should not add a new allow rule or disable SELinux.
Exercise
A binary was copied to /opt/app/server and started by systemd. ps -eZ shows it running as unconfinedservicet instead of its intended domain. What is the likely cause?
Show answer
The executable has a type that has no type transition rule from initt into the intended domain, so it did not enter that domain at execve. The file's label should be the entry-point type for the service (for example httpdexec_t for Apache), applied persistently with semanage fcontext and restorecon.