Sub topic 3
Reading security contexts on files, processes and users
This chapter assumes you are comfortable with the Linux command line and sudo, and that you understand file ownership and permission bits. It also assumes you have a RHEL-family system (RHEL, Rocky, AlmaLinux or Fedora) with SELinux in enforcing or permissive mode. You should know what a process and a service are, and have a web server such as Apache httpd available to inspect.
Every decision SELinux makes depends on labels. This chapter teaches you to read those labels on files, processes, users and ports. Later chapters change labels and fix denials, and they all rely on this skill.
What a security context is
A security context, also called a label, is a string that SELinux attaches to every subject (a process) and every object (a file, directory, socket, port and so on). The policy compares the subject's context with the object's context and decides whether the requested action is allowed.
A context has four colon-separated fields:
user:role:type:levelFor example, the Apache web root typically has this context:
system_u:object_r:httpd_sys_content_t:s0| Field | Example | Meaning |
|---|---|---|
| User | `system_u` | The SELinux user. It is not the same thing as a Linux account. |
| Role | `object_r` | Connects SELinux users to domains. Files always use the placeholder `object_r`. |
| Type | `httpd_sys_content_t` | The label the policy rules mostly match on. |
| Level | `s0` | Sensitivity, plus optional categories such as `s0:c10,c20`. Used by MLS and MCS. |
On a targeted system (the default policy), the type field does almost all of the work. Names follow a convention: a type ending in _t is a type, and a process type is usually called a domain. Domains such as httpd_t label processes. Types such as httpd_sys_content_t label files. The next chapter explains how rules connect them.
Reading file contexts with ls -Z
Add -Z to ls to show the context. Use -d to look at a directory itself instead of its contents.
ls -Zd /var/www/html /etc/passwd /tmp /home
ls -Z /var/www/htmlTypical output looks like this:
system_u:object_r:httpd_sys_content_t:s0 /var/www/html
system_u:object_r:passwd_file_t:s0 /etc/passwd
system_u:object_r:tmp_t:s0 /tmp
system_u:object_r:home_root_t:s0 /homeNotice that the type reflects the purpose of the file, not its owner or its permissions. /etc/passwd is root:root with mode 0644, but its SELinux type is passwd_file_t. A confined service can only touch it if the policy has a rule for passwd_file_t.
You can combine -Z with -l for long listings. The context appears before the owner column:
ls -lZ /var/www/htmlOther tools accept similar options. stat -c '%C %n' FILE prints the context, and id -Z and ps -Z cover users and processes. cp, mv and install also have context options, but those matter when you fix labels, which a later chapter covers.
Where file labels come from
A file's label is stored in an extended attribute named security.selinux. You can see the raw value:
getfattr -n security.selinux /var/www/htmlTwo rules decide which label a new file gets:
- A new file normally inherits the type of its parent directory.
- The policy may define a type transition. For example, a file created by a particular domain in a particular directory may get a different, specific type.
A common consequence follows from rule 1. If you create a file in your home directory (type user_home_t) and then move it with mv into /var/www/html, it keeps user_home_t. mv preserves the original label, while cp creates a new file that inherits the label of the destination directory. This is one of the most frequent causes of an httpd "403 Forbidden" with correct permission bits.
Reading process contexts with ps -Z
The same four fields appear on processes. Use ps -Z (or ps -eZ for all processes):
ps -eZ | grep -E 'httpd|sshd|chronyd'
ps -Z -p $$Example output:
system_u:system_r:httpd_t:s0 1423 ? 00:00:02 httpd
system_u:system_r:sshd_t:s0-s0:c0.c1023 901 ? 00:00:00 sshd
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 2210 pts/0 00:00:00 bashFor processes, the role is system_r for system daemons, and the type is the domain. Look at the three lines above:
httpd_tis a confined domain. Policy rules limit what Apache can read, write and connect to.sshd_tis also confined.unconfined_tis the domain of an interactive login shell on the targeted policy. Processes running in it face very few SELinux restrictions.
The level field may show a range like s0-s0:c0.c1023. That is a low-to-high sensitivity range with a category set. You can ignore it for now; it matters for MLS and container isolation, covered in later chapters.
A process gets its domain at start time. When systemd starts /usr/sbin/httpd, the binary is labeled httpd_exec_t, and a type transition rule moves the new process into httpd_t. The same binary started from a shell you run as an administrator does not follow the same path, because the systemd unit file is the intended launch route. This is why you test service behavior with systemctl start, not by running the daemon by hand.
ls -Z /usr/sbin/httpd
systemctl status httpd --no-pager | head -n 5
ps -eZ | grep httpdIf a service appears in an unexpected domain, such as unconfined_service_t or init_t, its executable is probably mislabeled or it has no policy module. Check the executable's label first.
Reading user contexts with id -Z
Your login session also has a context:
id -ZOn a default RHEL-family install, a normal login typically prints:
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023This is an SELinux user identity, which is separate from your Linux account. The kernel maps Linux accounts to SELinux users through a login mapping. List it with:
sudo semanage login -l
sudo semanage user -lA default mapping looks like this:
| Login name | SELinux user | MLS/MCS range |
|---|---|---|
| `__default__` | `unconfined_u` | `s0-s0:c0.c1023` |
| `root` | `unconfined_u` | `s0-s0:c0.c1023` |
Any Linux account without its own entry uses __default__. An administrator can map a regular account to a more restricted SELinux user such as user_u, but most servers leave everyone unconfined and rely on confining the services instead. That is the focus of this tutorial.
Reading port contexts
Network ports carry types as well. A service may bind to or connect to a port only if the policy allows its domain to use that port's type. You cannot see a port label with ls, so use semanage:
sudo semanage port -l | grep -E '^(http_port_t|ssh_port_t)'Example output:
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
ssh_port_t tcp 22ss can show the process context that owns a listening socket:
sudo ss -tlnpZPort 8888 is not in the list above, so a confined Apache cannot bind to it until you label it. A later chapter covers that fix.
Putting it together
The following command sequence inspects every kind of context for an Apache server. Run it on a test host and compare the output with what you expect:
getenforce
id -Z
ls -Zd /var/www/html
ls -Z /usr/sbin/httpd
ps -eZ | grep httpd
sudo semanage port -l | grep '^http_port_t'If SELinux is disabled, getenforce prints Disabled and the labels may be missing or show unlabeled_t. In that case, enable SELinux first, as the previous chapter on modes describes.
The diagram shows an important ordering point. SELinux does not replace normal permissions. Both checks must pass. A request that Linux permissions allow can still be denied by SELinux, and a request SELinux allows can still be denied by permission bits.
Common errors when reading contexts
- Reading the wrong field. Most troubleshooting needs the third field, the type. The user and role fields rarely explain a denial on the targeted policy.
- Confusing
-Zon a directory with-Zon its contents.ls -Z /srv/webshows the files inside.ls -Zd /srv/webshows the directory. - Seeing
?in the context column. This usually means the file has no label or SELinux is disabled. If the system runs in enforcing mode with?labels, the filesystem may not support extended attributes. - Seeing
unlabeled_t. Files created while SELinux was disabled have no label. They need to be relabeled before a confined service can use them. - Assuming
chmodorchownfixes a label problem. Those commands change DAC attributes only. - Seeing a service in
unconfined_service_t. The service was started from a binary with no transition rule, so no domain confines it.
Exercise
On a test server, ls -Zd /srv/web prints unconfined_u:object_r:default_t:s0, and ps -eZ | grep httpd shows the domain httpd_t. Which field of the file context is most likely to explain why Apache cannot read the directory, and what would you need to see there instead for a standard web content directory?
Show answer
The type field, default_t. Apache runs in the httpd_t domain, and the policy has no rule letting httpd_t read generic default_t files. A standard web content directory would show a type such as httpd_sys_content_t. The user field unconfined_u is not the cause.
Exercise
You copy index.html from /home/alice into /var/www/html with mv, and the page returns 403 even though the file is mode 0644. Which command shows the likely cause, and why does cp behave differently from mv?
Show answer
Run ls -Z /var/www/html/index.html. It will probably show user_home_t. mv keeps the file's existing label, while cp creates a new file that inherits the type of the destination directory, httpd_sys_content_t.