Sub topic 3
Reading Security Contexts on Files, Processes and Users
Assume you are comfortable with Linux permissions, sudo, systemctl, and basic Apache or nginx administration. This chapter focuses on reading SELinux labels. The next step is using those labels to understand type enforcement.
What a security context contains
A security context is a label that SELinux attaches to an object or process. A common context has four fields:
user:role:type:levelFor example:
system_u:object_r:httpd_sys_content_t:s0Each field has a different purpose:
| Field | Example | Meaning |
|---|---|---|
| SELinux user | `system_u` | SELinux identity assigned to the object |
| Role | `object_r` | Role associated with the object |
| Type | `httpd_sys_content_t` | Label used by the policy to make access decisions |
| Level | `s0` | MLS or MCS sensitivity and category information |
The type is usually the most important field in a targeted policy. A file under /var/www/html normally has the type httpd_sys_content_t. Apache processes normally run in the httpd_t domain. The policy can allow httpd_t to read httpd_sys_content_t while denying access to unrelated types.
SELinux fields do not replace normal ownership and permission bits. Both systems can deny an operation. A request must pass the normal discretionary access control (DAC) check and the SELinux mandatory access control (MAC) check.
Reading file contexts
Use ls -Z to display the context of a file:
ls -lZ /var/www/html/index.html
ls -Zd /var/www/htmlExample output might look like this:
-rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/index.htmlThe -Z option adds the SELinux context. The -d option makes ls show the directory itself rather than its contents.
Use find when you need to inspect a directory tree:
find /var/www/html -maxdepth 2 -printf '%p\n' | while read -r path; do
ls -Zd "$path"
doneA file copied from another location may keep an unsuitable type. For example, a file labeled default_t may be readable through DAC but still be inaccessible to httpd_t.
Reading process and user contexts
Use ps -eZ to display process contexts:
ps -eZ | grep '[h]ttpd'Typical Apache output includes a process context such as:
system_u:system_r:httpd_t:s0 1234 ? 00:00:00 httpdFor a process, the type field is its SELinux domain. Here, httpd_t identifies the confined Apache domain. The domain describes what the process may do under the loaded policy.
Use id -Z to display the current login context:
id -ZA user context might be:
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023The SELinux user is not necessarily the same as the Linux username. Linux users are mapped to SELinux users, and that mapping determines which roles and domains they may use.
Reading port labels
SELinux also labels network ports. List ports assigned to the HTTP port type:
sudo semanage port -l | grep '^http_port_t'A result might include:
http_port_t tcp 80, 443, 8080This label allows a web-server domain to bind to those ports when the policy permits it. A service listening on TCP port 8888 may still fail if that port has no suitable SELinux label.
Comparing the fields
When investigating a service problem, identify the process domain first. Then compare it with the type on the file, directory, or port involved. The level may matter on systems using MLS or container categories, but ordinary targeted-policy troubleshooting usually starts with the type.
Exercise
Apache runs as httpd_t, but /srv/web/index.html is labeled default_t. Which context field is the likely problem, and what should you inspect next?
Show answer
The type field is likely wrong. Inspect the directory tree with ls -Z, then check the persistent file-context rules with semanage fcontext -l.