Sub topic 12
Querying the Loaded Policy with `sesearch`
This chapter assumes that you can use the Linux command line, read SELinux contexts, and manage services with systemctl. It also assumes that SELinux is enabled on a RHEL-family system and that you have administrator access through sudo.
What `sesearch` Shows
sesearch queries the SELinux policy that is currently loaded in the kernel. It answers questions such as:
- Can the
httpd_tdomain read files labeledhttpd_sys_content_t? - Can
httpd_tbind to ports labeledhttp_port_t? - Which rule grants a particular permission?
- Is access controlled by a conditional rule?
This is different from reading an AVC denial. An AVC denial tells you that an attempted operation was refused. sesearch lets you inspect the policy rules that explain whether an operation should be allowed.
SELinux commonly describes an access decision with four parts:
| Part | Example | Meaning |
|---|---|---|
| Source domain | `httpd_t` | The type assigned to the running web server process |
| Target type | `httpd_sys_content_t` | The type assigned to a web file |
| Object class | `file` | The kind of object being accessed |
| Permission | `read` | The requested operation |
Querying Allow Rules
Use -A to search allow rules. Specify the source type with -s, target type with -t, object class with -c, and permission with -p.
sudo sesearch -A \
-s httpd_t \
-t httpd_sys_content_t \
-c file \
-p readA matching result usually contains an allow rule similar to this:
allow httpd_t httpd_sys_content_t:file { getattr open read };The rule says that a process in the httpd_t domain may perform the listed operations on files labeled httpd_sys_content_t. It does not mean that every process can read those files. The source domain matters.
You can also inspect whether the web server may bind to a labeled port:
sudo sesearch -A \
-s httpd_t \
-t http_port_t \
-c tcp_socket \
-p name_bindIf the query returns no rule, the loaded policy has no matching allow rule for that exact combination. Check the context, class, and permission before changing policy. A missing result can also mean that the service uses a different domain or that the target object has the wrong label.
Conditional Rules and Booleans
Some rules are conditional. SELinux booleans turn groups of rules on or off. For example, outbound network access for httpd_t may depend on httpd_can_network_connect.
Use -C to include conditional information in the search:
sudo sesearch -A -C \
-s httpd_t \
-c tcp_socket \
-p name_connectThe output can show a rule controlled by a boolean. Inspect the current value separately:
getsebool httpd_can_network_connectPrefer enabling an appropriate boolean over creating a custom allow rule. A boolean expresses a documented service choice and is easier to review later.
A Focused Investigation
When a service reports “permission denied,” use this order:
- Identify the process domain with
ps -eZ. - Identify the target context with
ls -Zorsemanage fcontext -l. - Query the exact source type, target type, class, and permission with
sesearch. - Check whether a boolean controls the rule.
- Review the AVC record if the decision remains unclear.
Exercise
A web server runs in httpd_t, but a query for httpd_t reading default_t files returns no rule. What should you check before writing a custom module?
Show answer
Check whether the files have the wrong label. For a web root, define the intended persistent file context with semanage fcontext, apply it with restorecon, and query the corrected target type.