Sub topic 5
The targeted policy and what else exists
This chapter assumes you can run commands with sudo, read file permissions, and manage services with systemctl. It also assumes you have a RHEL-family system (RHEL, Rocky, AlmaLinux or Fedora) with SELinux enabled. You should already know what a security context is and how a type decides access. Here you will learn which policy your system loads, what that policy confines, and how it differs from the other policy types.
What a policy type is
SELinux enforces rules from a loaded policy. The policy is a compiled set of rules plus the labeling information that goes with it. You can install more than one policy type on a system, but only one is active at a time. The SELINUXTYPE setting in /etc/selinux/config picks which one is loaded at boot.
The policy type decides two things:
- Which processes are confined. A confined process runs in a domain, and the policy limits what that domain can touch.
- Which parts of the security context are used for decisions. Every policy type fills in all four fields (user, role, type, level), but policies differ in how much they rely on each field.
On RHEL-family systems the default is targeted. The other types you may meet are mls and, on some distributions, minimum.
Checking which policy is loaded
Use sestatus to see the active policy type, and read the config file to see what will load at the next boot.
sestatus
grep -E '^SELINUX(TYPE)?=' /etc/selinux/config
ls /etc/selinuxIn the sestatus output, look at these lines:
Loaded policy nameis the policy type running now, for exampletargeted.Policy MLS statusshows whether the multi-level fields are active.Policy deny_unknown statusshows how the kernel treats operations the policy does not describe.Max kernel policy versionshows the highest binary policy format the kernel supports.
The directory /etc/selinux holds one subdirectory per installed policy type. A typical RHEL system has only targeted. A directory that exists there does not mean the policy is active. Only the value of SELINUXTYPE at boot decides that.
The targeted policy
The targeted policy confines selected network-facing and system services and leaves everything else alone. A process that has no specific domain runs in an unconfined domain, so SELinux does not restrict it beyond normal Linux permissions.
The idea behind this design is practical. Services such as httpd, sshd, named, postfix and many others are the most likely entry points for an attacker. The policy writes tight rules for those services. Interactive users and ordinary administration tools run unconfined, so the system stays usable without a rule for every command.
Confined and unconfined domains
You can see the difference by listing process domains.
ps -eZ | grep -E 'httpd|sshd|systemd' | head
id -ZTypical results:
- Apache processes show
system_u:system_r:httpd_t:s0. They are confined, andhttpd_thas specific rules. - Your own login shell usually shows
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023. It is unconfined. - Some services started by systemd, such as
sshd, run in their own domains (sshd_t).
Confined does not mean fully isolated. The domain still gets whatever the policy grants it. The policy for httpd_t allows reading files labeled httpd_sys_content_t, binding to ports labeled http_port_t, and so on. Everything not granted is denied.
The unconfined domains are a policy choice
The unconfined_t domain is not a gap in SELinux. It is a domain the targeted policy defines on purpose, with a rule that allows almost everything. This has two consequences you should remember:
- A compromise of a process running as
unconfined_tis not limited by SELinux. Only DAC applies. - A service you install from a third party, with no policy module, usually runs in
unconfined_service_twhen systemd starts it, or inherits an unconfined domain. SELinux does nothing for it until you confine it.
You can count the confined domains your system knows about:
sudo semanage login -l
sudo semanage user -l
seinfo -t 2>/dev/null | tail -n 1The seinfo command comes from the setools-console package. If it is missing, install that package before using seinfo or sesearch.
How users map in the targeted policy
The targeted policy maps Linux users to SELinux users through semanage login. By default, all regular logins map to unconfined_u through the __default__ entry, and root maps to unconfined_u as well.
sudo semanage login -lExample output:
| Login name | SELinux user | MLS/MCS range |
|---|---|---|
| `__default__` | `unconfined_u` | `s0-s0:c0.c1023` |
| `root` | `unconfined_u` | `s0-s0:c0.c1023` |
You can map a Linux user to a confined SELinux user such as user_u or staff_u when you need to restrict what that user can run. That is a separate hardening topic. For service confinement, the important point is that the targeted policy puts most of its effort into the type field and treats the user and role fields lightly.
What the targeted policy relies on
In the targeted policy, access decisions come mostly from type enforcement. The role and user fields are present in every context, but the rules that matter most are of the form "domain X may do action Y on type Z".
The level field (s0 and categories such as c0.c1023) is there too. In the targeted policy it is used for MCS (Multi-Category Security), not for strict multi-level separation. MCS lets the system tell apart objects that share a type, which is how containers and virtual machines are isolated from each other. A later chapter covers it.
Other policy types
MLS
MLS stands for Multi-Level Security. It implements the Bell-LaPadula model, in which each process and file has a sensitivity level, such as s0 for unclassified and higher numbers for more sensitive data. The rules then enforce "no read up, no write down", so a process cannot read data above its level or write data to a lower level.
Key differences from targeted:
- It confines far more of the system. Almost every process runs in a restricted domain.
- Users log in with a sensitivity range, and administrators assign it.
- It needs the
selinux-policy-mlspackage, and you must relabel the whole filesystem when you switch to it. - It suits environments with formal classification requirements, such as government or defense systems, and certified deployments.
- Much third-party software does not work without writing policy for it.
MLS is a poor choice for a general web or application server. Run it only when a requirement or certification demands it.
Minimum
Some distributions ship a minimum policy. It is a targeted-style policy that confines only a small chosen set of services, and you add more over time. It uses less configuration effort than targeted at the start, but it gives less protection. RHEL does not recommend it for production. Check whether your distribution packages it before you plan around it.
Comparison
| Policy | Confined processes | Typical use | Effort to run |
|---|---|---|---|
| `targeted` | Selected services and daemons | General servers and desktops | Low |
| `mls` | Nearly all processes | Classified or certified systems | High |
| `minimum` | A small chosen subset | Specialised setups on some distributions | Medium |
Changing the policy type safely
Switching policy type is not like switching the mode. The file labels on disk belong to the old policy, so a new policy will find labels it does not understand. Treat the change as a planned migration.
The general order is:
- Install the new policy package, for example
selinux-policy-mls. - Set the mode to permissive in
/etc/selinux/config, so a bad label does not stop boot. - Change
SELINUXTYPEto the new type. - Create the autorelabel marker with
sudo touch /.autorelabel. - Reboot. The system relabels every file, which can take a long time on large disks.
- Review the denials in the audit log, fix them, and only then return to enforcing mode.
sudo dnf install selinux-policy-mls
sudo sed -i 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config
sudo sed -i 's/^SELINUXTYPE=.*/SELINUXTYPE=mls/' /etc/selinux/config
sudo touch /.autorelabelRun this on a test machine first. Do not switch a production host to MLS because it sounds stricter.
Common errors
- Assuming a directory in
/etc/selinuxmeans the policy is active. ChecksestatusforLoaded policy name. - Assuming every process is confined. Check with
ps -eZ. A service running inunconfined_service_thas no SELinux restriction. - Expecting MLS rules on a targeted system. The level field exists in targeted, but it only provides MCS separation.
- Editing
SELINUXTYPEwithout relabeling. Labels from the old policy cause denials or boot failures. - Missing tools.
sesearchandseinfocome fromsetools-console, andsemanagecomes frompolicycoreutils-python-utils.
Where this fits in the workflow
When you confine a new service, the first question is whether the targeted policy already has a domain for it. If it does, you fix problems with labels, ports and booleans, which the next chapters cover. If it does not, the service runs unconfined and you have a choice: leave it, or write a policy module, which is a last resort covered later.
Exercise
A colleague says "SELinux is in enforcing mode, so every process on this server is restricted by it." Which command shows whether that is true for the web server and for your own shell, and what would you expect to see for each under the targeted policy?
Show answer
Run ps -eZ | grep httpd for the web server and id -Z for your shell. Under the targeted policy, the Apache processes show the confined domain httpd_t, so SELinux restricts them. Your login shell usually shows unconfined_t, so SELinux does not restrict it beyond normal permissions. The statement is false. Enforcing mode only means the loaded policy is applied, and the targeted policy leaves unconfined domains alone.
Exercise
You find /etc/selinux/mls on a server, but sestatus reports Loaded policy name: targeted. Which policy is active, and which file decides what loads at the next boot?
Show answer
The targeted policy is active, because sestatus reports the policy loaded now. The directory only shows that the MLS policy files are installed. The SELINUXTYPE setting in /etc/selinux/config decides which policy loads at the next boot.