Sub topic 12
Querying the Loaded Policy with sesearch
This chapter assumes you can read a security context such as system_u:system_r:httpd_t:s0. It also assumes you know that type enforcement decides access between a process domain and an object type. You should have used getsebool, semanage fcontext and ausearch on a RHEL-family system. Earlier chapters showed how to fix denials by relabeling files, setting booleans and labeling ports. This chapter shows how to ask the loaded policy directly what a domain is allowed to do, so you can pick the right fix before you change anything.
Why query the policy
The loaded SELinux policy is a large set of rules. On a stock RHEL-family system it holds many thousands of allow rules. Guessing which type or boolean a service needs wastes time. sesearch reads the policy that is loaded in the running kernel and prints the rules that match your filters. It answers questions like these:
- What can
httpd_tdo to files labeledhttpd_sys_content_t? - Which boolean turns on a rule that allows
httpd_tto make network connections? - Which domains are allowed to write to
var_log_t? - Is there already a rule that would permit the access
audit2whycomplained about?
sesearch is part of the setools-console package. Install it if the command is missing.
sudo dnf install -y setools-console
sesearch --versionsesearch only reads policy. It never changes the system, so it is safe to run on production hosts. Running it as a normal user usually works, because the policy file under /etc/selinux/ is readable. If you get a permission error, use sudo.
The shape of a policy rule
Every rule you will search for has four parts: a source type, a target type, an object class and a set of permissions.
allow httpd_t httpd_sys_content_t:file { getattr open read };Read this as: processes in domain httpd_t may get attributes of, open and read objects of class file that carry type httpd_sys_content_t. The class matters. file, dir, lnk_file, tcp_socket and process each have their own permission lists. Allowing read on file says nothing about dir.
Some rules are conditional. They sit inside an if (boolean) block and apply only when that boolean is on. This is how booleans change behavior. The rule exists in the policy all the time, but it is inactive until the boolean is enabled.
Another detail trips people up. Policy rules often name an attribute instead of a single type. An attribute is a named group of types. For example, httpd_sys_content_t belongs to the attribute httpdcontent, and a rule may grant access to the whole attribute. A search for the plain type name finds such rules only when sesearch expands attributes, which it does by default for the type you give it.
Searching allow rules
The main option is -A, which selects allow rules. Add -s for the source type and -t for the target type. Add -c to limit the object class and -p to limit permissions.
# Everything httpd_t may do to httpd_sys_content_t
sesearch -A -s httpd_t -t httpd_sys_content_t
# Only file class
sesearch -A -s httpd_t -t httpd_sys_content_t -c file
# Does httpd_t have write on that type?
sesearch -A -s httpd_t -t httpd_sys_content_t -c file -p writeThe third command usually prints nothing. An empty result is itself an answer: no rule, conditional or not, lets httpd_t write to httpd_sys_content_t files. That is why a web application cannot modify its own content under the default labels, and why writable directories get a different type such as httpd_sys_rw_content_t.
Compare it with the writable type:
sesearch -A -s httpd_t -t httpd_sys_rw_content_t -c file -p writeThis returns a rule, which tells you the fix for an application that must write is to label its data directory with that type. You learn that without trial and error.
Reading the output
A typical line looks like this:
allow httpd_t httpd_sys_rw_content_t:file { append create getattr ioctl link lock open read rename setattr unlink write };Conditional rules are printed with their boolean in brackets, and the line starts with a marker showing whether the rule is currently active:
DT allow httpd_t unreserved_port_t:tcp_socket name_connect; [ httpd_can_network_connect ]
ET allow httpd_t unreserved_port_t:tcp_socket name_connect; [ httpd_can_network_connect ]The first two letters describe the conditional state. E means the rule is enabled right now and D means it is disabled. The second letter, T or F, says whether the rule belongs to the true or false branch of the condition. A line starting DT is in the true branch of a boolean that is currently off, so the rule is not active. The name in brackets is the boolean to flip. Your output may differ in exact rule lists between policy versions, but the format is the same.
Finding the boolean that enables an access
The most useful trick is to search for the access that was denied and see whether a boolean controls it. Suppose a reverse proxy gets denied connecting to a backend on port 9000. audit2why points at name_connect on a tcp_socket. Search for that:
sesearch -A -s httpd_t -c tcp_socket -p name_connect -CThe -C option includes the conditional expression in the output, so you see the boolean names beside each rule. Several booleans may show up for different target port types. Pick the one whose target type matches the port you need. Then confirm and enable it:
getsebool httpd_can_network_connect
sudo setsebool -P httpd_can_network_connect onYou can also list only the rules gated by a specific boolean:
sesearch -A -b httpd_can_network_connect -s httpd_tThe -b option filters on the boolean name. Use it before turning a boolean on, so you know what it grants. Some booleans are broad. httpd_can_network_connect allows outbound connections to nearly any port, while a narrower one such as httpd_can_network_connect_db covers only database ports. Choosing the narrowest boolean that solves the problem keeps the confinement tight.
Other useful searches
sesearch supports more than allow rules.
| Option | Rule type | Typical question |
|---|---|---|
| `-A` | allow | What access is permitted? |
| `--dontaudit` | dontaudit | Which denials are silently hidden? |
| `-T` | type_transition | Which type does a new file or process get? |
| `--role_allow` | role allow | Which roles may transition to which? |
| `-b NAME` | filter | Only rules controlled by this boolean |
| `-C` | output flag | Show the conditional expression |
Two of these deserve a closer look.
dontaudit rules
A dontaudit rule tells the kernel to deny an access without writing an AVC message. If a service fails and the audit log shows nothing, a hidden denial is a likely cause. Search for it:
sesearch --dontaudit -s httpd_t -t shadow_tTo make the hidden denials visible, rebuild the policy without dontaudit rules, then reproduce the problem and read the log.
sudo semodule -DB # disable dontaudit rules and rebuild
# reproduce the failure, then check the log
sudo ausearch -m avc -ts recent
sudo semodule -B # restore normal behaviorDo not leave dontaudit disabled. The log grows quickly because many harmless denials are normally suppressed.
Type transitions
A type transition rule explains why a new object gets a particular label. When httpd_t creates a file in a directory labeled httpd_sys_rw_content_t, the new file inherits a type by default. A transition rule can assign a different one. When files appear with an unexpected label, search for transitions:
sesearch -T -s httpd_t -t httpd_sys_rw_content_t
sesearch -T -t httpd_exec_t -c processThe second command shows which domain a process enters when it runs a binary labeled httpd_exec_t. This is how init_t becomes httpd_t when systemd starts Apache.
Working from a real denial
Here is a short workflow that combines the tools from earlier chapters with sesearch. Say ausearch -m avc -ts recent | audit2why reports that httpd_t was denied write on a file labeled var_t.
- Note the source type, target type, class and permission from the denial.
- Check whether the policy allows it at all:
``bash sesearch -A -s httpd_t -t var_t -c file -p write ``
- If nothing prints, the policy has no rule for that type. Look for a type that does allow the write, for example:
``bash sesearch -A -s httpd_t -c file -p write | grep -v '^ *DT' ``
The grep hides rules that depend on disabled booleans. Remove it to see those as well.
- Choose the type that matches the purpose, such as
httpd_sys_rw_content_tfor data orhttpd_log_tfor logs, and label the path withsemanage fcontextandrestorecon. - Reproduce the request and confirm the denial is gone.
If sesearch shows that no existing type permits the access, the case is a candidate for a custom module, which a later chapter covers. Writing a module without checking first often creates a rule that already existed behind a boolean.
Common errors and gotchas
- Empty output is not an error. It means no matching rule. Check the spelling of type names with
seinfo -t | grep httpd, since a typo also gives empty output. - Wrong class. Searching without
-creturns many classes. Searching with the wrong class returns nothing, even when the access is allowed for another class. - Reading the wrong policy.
sesearchreads the loaded policy by default. Modules you installed withsemodule -iappear only after loading. To inspect a different policy file, pass its path as the last argument. - Forgetting attributes. A rule may allow a whole attribute. If you search by target type and see rules you did not expect, they come from attribute expansion.
- Trusting a boolean's name. A boolean name suggests its effect but does not define it. Run
sesearch -A -b NAMEbefore enabling it. - Confusing policy with labels.
sesearchtells you what the policy allows between types. It says nothing about which type a given file actually has. Usels -Zfor that.
A quick diagram of the decision
Exercise
A web application running as httpdt cannot write uploaded files to /srv/uploads. The directory is labeled httpdsyscontentt. Which sesearch command checks whether the policy lets httpdt write to files with that type, and what fix do you expect if the result is empty and a search against httpdsysrwcontent_t returns a write rule?
Show answer
Run "sesearch -A -s httpdt -t httpdsyscontentt -c file -p write". An empty result means no rule allows the write. Because "sesearch -A -s httpdt -t httpdsysrwcontentt -c file -p write" returns a rule, label the directory with that type: "sudo semanage fcontext -a -t httpdsysrwcontent_t '/srv/uploads(/.*)?'" followed by "sudo restorecon -Rv /srv/uploads".
Exercise
The output of sesearch -A -C shows the line "DT allow httpdt unreservedportt:tcpsocket nameconnect; [ httpdcannetworkconnect ]". What do the first two letters mean, and what should you check before enabling the boolean?
Show answer
The D means the conditional rule is currently disabled, and the T means it sits in the true branch of the condition. The rule becomes active when the boolean is turned on. Before enabling it, run "sesearch -A -b httpdcannetworkconnect -s httpdt" to see everything the boolean grants, and check whether a narrower boolean covers the need.