Sub topic 7
Tuning Service Behavior with Booleans
This chapter assumes you can use the command line and sudo, manage services with systemctl, and read security contexts such as httpd_t and httpd_sys_content_t. You should know that SELinux denies anything its policy does not allow. It also helps to have run Apache httpd or nginx on a RHEL-family system with SELinux in enforcing mode.
What a boolean is
The targeted policy ships with rules for each confined service. Some behaviors are useful on some servers and risky on others. Examples are letting a web server open outbound connections, send mail, or read home directories. The policy authors group these optional rules behind switches called booleans.
A boolean is a named on/off setting in the loaded policy. When it is off, the rules it controls are inactive and SELinux denies the matching access. When it is on, those rules become active. You do not edit or recompile any policy. You flip a switch that the policy already contains.
Booleans are the preferred fix after correct labels. A boolean grants one narrow, well-understood behavior and the policy maintainers have reviewed it. A custom module written from denials often grants more than you intended.
Listing and reading booleans
Three commands cover most work with booleans.
getseboolprints the current value of one boolean, or all with-a.semanage boolean -llists booleans with their current value, their default value, and a description.setseboolchanges a value.
getsebool httpd_can_network_connect
getsebool -a | grep httpd | head
sudo semanage boolean -l | grep httpd_can_network_connectThe first command prints a line such as httpd_can_network_connect --> off. The semanage boolean -l output has columns for the boolean name, the current state, the default state, and a description. The description is often enough to tell you whether a boolean is the right one.
To find booleans for a service, filter by the service's domain prefix. Booleans for Apache start with httpd_, for Samba with samba_ or smbd_, and for NFS with nfs_. The description column also helps when you search by behavior.
sudo semanage boolean -l | grep -i 'network'
sudo semanage boolean -l | grep -i 'home'Changing a boolean: runtime and persistent
A boolean has two states that can differ: the value in the running kernel, and the value stored on disk that is restored at boot. The -P flag controls which one you change.
| Command | Changes running policy | Survives reboot |
|---|---|---|
| `setsebool name on` | Yes | No |
| `setsebool -P name on` | Yes | Yes |
Use the plain form to test a fix. If it works, repeat it with -P. If you forget -P, the service works until the next reboot and then breaks again with no obvious cause.
getsebool httpd_can_network_connect
sudo setsebool -P httpd_can_network_connect on
getsebool httpd_can_network_connectThe -P option rebuilds and reloads part of the policy store, so it can take a few seconds. This is normal.
You can see which booleans differ from their defaults, which is useful when you inherit a server.
sudo semanage boolean -l -CThe -C option shows only locally customized booleans. This is the quickest way to audit what an administrator has changed on a machine.
A worked example: proxying to a backend
Suppose Apache acts as a reverse proxy in front of an application listening on 127.0.0.1:3000. You configure ProxyPass and reload httpd. Requests return 503 Service Unavailable, and the Apache error log reports a failed connection to the backend. Files and ports are fine, and the application works when you call it with curl.
The cause is that httpd_t is not allowed to make outbound TCP connections to arbitrary ports. First confirm that SELinux is responsible and see what it denied.
sudo ausearch -m avc -ts recent | audit2whyThe output includes text similar to the following. The exact wording varies by version.
type=AVC msg=audit(...): avc: denied { name_connect } for pid=1234 comm="httpd" dest=3000 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:ephemeral_port_t:s0 tclass=tcp_socket permissive=0
Was caused by:
The boolean httpd_can_network_connect was set incorrectly.
Description:
Allow httpd to can network connect
Allow access by executing:
# setsebool -P httpd_can_network_connect 1Read the denial like this. The permission is name_connect, the source domain is httpd_t, and the target is a TCP port labeled ephemeral_port_t. audit2why names the boolean that would allow it. Enable it and retest.
sudo setsebool -P httpd_can_network_connect on
curl -I http://localhost/If the backend port carried a label that httpd is already allowed to connect to, such as http_port_t, you would not need this boolean. Chapter material on port labeling covers that alternative. When the backend is a database, a narrower boolean such as httpd_can_network_connect_db allows connections only to database ports.
Choose the narrowest boolean
Several booleans overlap, and the broad ones are tempting. Compare them before you enable one.
| Boolean | What it allows | Prefer when |
|---|---|---|
| `httpd_can_network_connect` | httpd connects to any TCP port | You proxy to arbitrary backend ports |
| `httpd_can_network_connect_db` | httpd connects to database ports only | A web app talks to MySQL or PostgreSQL |
| `httpd_can_sendmail` | httpd sends mail | A web app sends mail through the local MTA |
| `httpd_enable_homedirs` | httpd reads user home directories | You serve `~user` content |
| `httpd_unified` | httpd content types share access rules | Scripts need to write to content types they otherwise only read |
Confirm any name on your own system with semanage boolean -l, since the policy version determines which booleans exist. Policy updates can add, rename, or remove them.
A boolean has a security cost. Enabling httpd_can_network_connect means that a compromised web application can now open connections to any port the host can reach. This includes internal services. The policy is weaker after the change, so enable only what the service needs, and record why.
Inspecting what a boolean controls
If a description is too vague, look at the rules behind it. The sesearch tool, from the setools-console package, can list the rules that depend on a boolean.
sudo dnf install -y setools-console
sudo sesearch -A -b httpd_can_network_connectThe -A option searches allow rules and -b restricts results to rules guarded by the named boolean. The output lists each source type, target type, class and permission that the boolean controls. A long list tells you the boolean is broad.
Common errors
- Forgetting
-P. The fix disappears after a reboot. Check withsemanage boolean -l -C, where a boolean whose current and default values differ is a sign of this mistake. - Setting the boolean to the wrong value.
setseboolacceptson,off,1, and0. A typo in the name returns an error, but a boolean enabled when you meant to disable another is silent. - Using a boolean when the label is wrong. If
ausearchshows the target asdefault_toruser_home_tfor a file that httpd should read, no boolean will help. Relabel the file. - Assuming a boolean exists. Names differ between services and releases. Search with
semanage boolean -lrather than relying on memory or an old blog post. - Restarting the service and expecting a change. Booleans apply to the policy, not the process. Most changes take effect for new access attempts at once, so a restart is normally unnecessary.
- Mixing up
setseboolandsemanage boolean -m. Both can persist a value.semanage boolean -m --on namechanges only the stored default and does not change the running policy, whereassetsebool -Pdoes both.
Documenting the change
Because booleans live in the policy store and not in a service's configuration file, they are easy to forget. Keep them with your deployment notes or configuration management. A short script or Ansible task that sets each needed boolean makes a rebuilt server behave like the original.
#!/usr/bin/env bash
set -euo pipefail
# Booleans required by the reverse proxy role
sudo setsebool -P httpd_can_network_connect on
sudo setsebool -P httpd_can_sendmail off
sudo semanage boolean -l -CSetting a value that is already correct is harmless, so this script is safe to run repeatedly.
Exercise
An Apache reverse proxy returns 503 errors after you add a ProxyPass to a backend on port 3000. ausearch -m avc -ts recent | audit2why reports that httpd_can_network_connect was set incorrectly. Which command fixes the problem so that it survives a reboot, and how do you check that it is recorded as a local change?
Show answer
Run sudo setsebool -P httpd_can_network_connect on. The -P flag writes the value to the policy store as well as the running policy. Check it with sudo semanage boolean -l -C, which lists booleans changed from their defaults. getsebool httpd_can_network_connect should print on.
Exercise
You enabled a boolean with setsebool httpd_can_sendmail on (no -P) and mail worked. After a reboot, mail from the web application fails again. Why, and what do you do?
Show answer
Without -P the change affected only the running policy, and the boot process restored the stored default of off. Run sudo setsebool -P httpd_can_sendmail on to change both the running value and the persistent value.