Skip to content

Lesson 2 of 9 · 26m

Anatomy of an SELinux Context: Users, Roles, Types, and Levels

Decode labels such as system_u:system_r:httpd_t:s0 and learn which fields matter for everyday server troubleshooting.

An SELinux label looks like alphabet soup until you realize it is a compact security contract. Once you can read it, half the mystery disappears. The other half is understanding which part matters in the common server policies you actually run.

Read a process label

ps -eZ | grep '[h]ttpd'
system_u:system_r:httpd_t:s0  841 ? 00:00:02 httpd
system_u=user  system_r=role  httpd_t=type/domain  s0=sensitivity

On most targeted-policy systems, the type is where the action is. Types group processes and resources into security classes. Roles and users matter more in confined-user or MLS environments; containers and virtual machines use MCS categories heavily.

Processes have domains; objects have types

A running process transitions into a domain such as httpd_t, sshd_t, or container_t. Files, sockets, ports, and other objects receive types such as httpd_sys_content_t, shadow_t, or ssh_port_t. Policy rules connect them:

allow httpd_t httpd_sys_content_t:file { read open getattr };

Read that as: processes in the httpd_t domain may read, open, and stat files labeled httpd_sys_content_t. This is why copying a file into a web root is not enough. Its inherited or preserved label may be wrong for the service domain.

Type enforcement beats memorizing every field

When triaging, focus on type pairs first:

subject domain   -> target type   -> permission   -> object class
httpd_t          -> shadow_t      -> read         -> file

That statement is far more useful operationally than reciting definitions of every SELinux user. If httpd_t tried to read shadow_t, the denial is working as intended. If it tried to read httpd_sys_content_t and still failed, suspect file context drift, policy mismatch, or another access layer.

Transitions explain why labels change

Policy can move processes and objects into new types: init_t executing sshd_exec_t may transition to sshd_t; new files often inherit their parent directory type. Parent directories, unit files, executable labels, and install paths therefore decide which confinement you actually get.

Inspect labels everywhere

ls -Z /var/www/html/index.html
ls -lZ /etc/passwd
ss -lptnZ | head
semanage login -l
semanage user -l

Use -Z as a habit. The moment you hear "permission denied," ask what the process context is, what the object context is, and whether the pairing is plausible.

Field notes

  • In targeted policy, _u and _r often look repetitive; do not ignore them forever, but start diagnosis with the type field.
  • MCS categories are important in containers and virtualization. Two s0 labels can still differ by category set.
  • A file copied with cp -a can preserve an inappropriate source context; a file created fresh may inherit a better one.
  • id -Z shows your shell's context, not necessarily the context of the service you are debugging.

Was this lesson helpful?

Sign in to rate