Rocky Linux 10 Tutorial / Diagnose SELinux denials, labels and file permissions

In Rocky Linux 10, SELinux is a security feature that, in addition to normal file permissions, checks via policy what types of files and resources a process can access . When access errors occur, rather than turning off SELinux, you should check the current mode, the security context of the file and process, and the actual denial logs together to distinguish from regular permission issues.

Reasons why access may be denied even if regular permissions are sufficient

The method of determining access by the owner, group of a file, and rwx permissions is called DAC (Discretionary Access Control). SELinux (Security-Enhanced Linux) provides MAC (Mandatory Access Control), which restricts access according to policies. Since there are accesses that must satisfy both checks, chmod alone cannot solve all permission issues.

For example, even if a file that a web server needs to read is 644 readable by anyone, reading may be denied if the SELinux policy does not allow that type for the web server. Conversely, even if the type matches, if the file is a 600 owned by root, a web server process running with normal user privileges cannot read it.

SELinux also functions differently from a firewall. A firewall controls communication paths, while SELinux restricts process actions according to policies. Even if a response called 403 Forbidden comes for an HTTP (Hypertext Transfer Protocol) request—a web communication protocol—communication to the server was completed, but processing might have been denied due to reasons such as file permissions, server settings, or SELinux. You do not open the firewall or turn off SELinux just because of the response code.

Read the current mode and security context

getenforce
sestatus
grep -E '^(SELINUX|SELINUXTYPE)=' /etc/selinux/config
id -Z
ps -eZ | head
ls -Zd /etc /srv

getenforce shows the current operating mode, and sestatus shows the mode of the loaded policy and configuration files together. Do not assume that the value of /etc/selinux/config is the same as the current mode. The Rocky Linux 10.2 VM in the source lab is in Enforcing mode both currently and in configuration, and it used the targeted policy.

Mode Policy Violation Access Points to Consider When Judging
Enforcing Blocked according to the policy Check general permissions and service settings as well.
Permissive Does not block due to SELinux policy but records actions that would be denied Failures due to general permissions or other security features may continue.
Disabled SELinux protection is not applied When reactivating, file labeling maintenance and other tasks may be necessary.

The command-line manual of the SELinux project explains the roles of modes and policies. Changing the mode is an operation with a wide impact. This exercise maintained Enforcing mode and did not use setenforce 0 or configuration file changes as a troubleshooting procedure.

Security context is the security identification information assigned to processes or files. The context attached to a file is sometimes called a label. The following is the context of the test file in the source lab.

unconfined_u:object_r:httpd_sys_content_t:s0
  • unconfined_u: SELinux user. Distinguished from the Linux username of the file owner.
  • object_r: Role of objects such as files.
  • httpd_sys_content_t: Type of the file. This is key for comparing whether read access to web content is allowed in this exercise.
  • s0: Indicates the security level. In some environments, ranges and categories are additionally appended at the end of the context.

The type of process is also called a domain. The Apache worker process in the source lab was run as httpd_t. The fact that a file can be read in the administrator shell is different from the fact that the httpd_t process can read it. The policy makes decisions based on the process domain, the target type, and the requested action together.

Prepare a normal response without opening external ports.

Use a dedicated VM without existing web services. Do not change a production Apache configuration or deliberately mislabel real content. Bind the test server only to 127.0.0.1:80 and send requests with curl inside the VM. Do not add host port forwarding or firewall allowances.

The source lab environment was Rocky Linux 10.2, Apache packages httpd-2.4.63-13.el10_2.8, selinux-policy-targeted-42.1.18-4.el10_2.4, audit-4.0.3-5. Before installation, Apache was not present, and for the test, 12 packages including dependencies were installed. At other times, dependencies and versions may differ, so review the proposed installation transaction.

sudo dnf install httpd
rpm -q httpd selinux-policy-targeted audit
systemctl is-active httpd
systemctl is-enabled httpd
sudo ss -lntp '( sport = :80 )'

After installation, if the service is already running or another program is using port 80, stop here and check the existing configuration. On this VM, Apache was stopped and set not to start automatically, and the port was free. Do not set the service to start automatically on boot.

Comparing the denied logs requires ausearch from the audit package and the running auditd. On this VM, they were already prepared. If there is no tool, prepare it with sudo dnf install audit and check if the next service query is active. If the audit service is abnormal, inspect log collection first before starting the blocking practice.

command -v ausearch
systemctl is-active auditd

First, save the original Apache configuration to a separate location. If the backup path already exists, stop and choose a new path.

sudo bash <<'BASH'
set -eu
backup=/root/selinux-lab17-before
test ! -e "$backup"
mkdir -m 700 "$backup"
cp -a /etc/httpd/conf/httpd.conf "$backup/httpd.conf"
BASH
sudoedit /etc/httpd/conf/httpd.conf

In /etc/httpd/conf/httpd.conf, find the activated Listen 80 line and replace it with the following single line. Existing server configurations with other Listen or virtual hosts are not the subject of this practice. Specifying both the IP and port means it will only listen on that address, which can be confirmed in Apache's official description of address and port binding .

Listen 127.0.0.1:80

Check to ensure you do not overwrite existing test files and create a public string file to be used only for this lecture.

sudo bash <<'BASH'
set -eu
file=/var/www/html/lf-selinux17.txt
test ! -e "$file"
printf 'selinux-lab17-ok\n' > "$file"
chmod 644 "$file"
restorecon -v "$file"
BASH
sudo httpd -t && sudo systemctl start httpd
sudo ss -lntp '( sport = :80 )'
ps -eZ | grep httpd
ls -lZ /var/www/html/lf-selinux17.txt
curl --noproxy '*' --max-time 5 -sS -w 'HTTP %{http_code}\n' \
  http://127.0.0.1/lf-selinux17.txt

httpd -t is a configuration syntax check. It will not start if there are errors. This time, the warnings AH00558, indicating that the server name could not be confirmed, and Syntax OK appeared together, but the start was successful. Distinguish warnings from syntax errors and check the actual status and the receiving address.

The expected results are receiving address 127.0.0.1:80, process type httpd_t, file mode 644, and file type httpd_sys_content_t. The request should show the following strings and status. --noproxy '*' of curl ensures that loopback requests bypass the environment’s proxy settings.

selinux-lab17-ok
HTTP 200

Change only the file type to verify SELinux denial.

First, leave the mode 644 and the file content as they are, and intentionally change the type of one test file to an inappropriate var_t. chcon changes the context of the current file. This is a command to reproduce the block and is not a recommended type setting for allowing the service.

sudo chcon -t var_t /var/www/html/lf-selinux17.txt
ls -lZ /var/www/html/lf-selinux17.txt
curl --noproxy '*' --max-time 5 -sS -o /dev/null \
  -w 'HTTP %{http_code}\n' http://127.0.0.1/lf-selinux17.txt
sudo ausearch --input-logs -m AVC,USER_AVC -ts recent -c httpd -i

The source lab result was HTTP 403. The file content and general permissions remained the same, but the type became var_t, and the audit log showed a record where the httpd_t process was denied the getattr operation of querying this file's attributes. To make part of the record easier to read, it can be summarized as follows.

avc: denied { getattr }
comm=httpd
path=/var/www/html/lf-selinux17.txt
scontext=system_u:system_r:httpd_t:s0
tcontext=unconfined_u:object_r:var_t:s0
tclass=file
permissive=0

AVC (Access Vector Cache) refers to the cache related to access decisions and is also used as the type name for denial records. scontext is the requesting process, tcontext is the target, and tclass is the target object class, such as file. Curly braces indicate the denied action. permissive=0 shows that access to this record was not handled permissively. Time, process, file, and action should be aligned with the actual failure to avoid misattributing old records as the cause.

According to the official ausearch manual , --input-logs uses the log location specified in auditd.conf as its input. In a piped standard-input environment such as the source lab's automated SSH commands, querying without this option produced <no matches>. Specifying the option revealed the AVC records already saved in the audit log. An initially empty search alone does not establish that SELinux is unrelated.

-ts recent specifies the recent range, -c httpd specifies the command name, and -i specifies the output interpreting the numeric value. Multiple search conditions narrow down the results. Since the file name may sometimes be recorded only as name, do not attach excessive path conditions from the beginning; instead, compare the file name, inode, and time of the resulting events.

Recover with the type that matches the path and request again.

When recovering incorrect labels in a typical web content path, compare the current type with the default value of the path. matchpathcon 's -V checks that the context on the disk matches the default.

matchpathcon -V /var/www/html/lf-selinux17.txt
sudo restorecon -nv /var/www/html/lf-selinux17.txt
sudo restorecon -v /var/www/html/lf-selinux17.txt
ls -lZ /var/www/html/lf-selinux17.txt
curl --noproxy '*' --max-time 5 -sS -w 'HTTP %{http_code}\n' \
  http://127.0.0.1/lf-selinux17.txt

matchpathcon -V in a mismatched state shows the current type and the expected type, and the exit status may not be 0. restorecon -nv only shows the labels that will change and does not actually modify them. After verification, if you run restorecon -v, the file's type returned to httpd_sys_content_t and the request also became HTTP 200.

restorecon official manual explains that the default behavior changes the type portion of the existing context. Therefore, the mere fact that unconfined_u remains in the output does not definitively indicate recovery failure. The focus of this exercise is on the type and the actual access results.

This method is used when the default label of the path is suitable for service purposes. For web content in a new custom path, first define a suitable persistent file-context mapping. Running restorecon alone applies the configured default for that path; it does not invent a web-content policy. Do not forcibly relabel the entire disk or immediately convert logs to allowed modules without understanding the existing policy.

This time, just change the general file permissions and compare.

After restoring the type, change only the file mode to 600. Since the file is owned by root, the Apache worker process cannot read it. You can also check not only the file but the owner and traversal permissions of the parent directory with namei -l.

sudo chmod 600 /var/www/html/lf-selinux17.txt
ls -lZ /var/www/html/lf-selinux17.txt
namei -l /var/www/html/lf-selinux17.txt
curl --noproxy '*' --max-time 5 -sS -o /dev/null \
  -w 'HTTP %{http_code}\n' http://127.0.0.1/lf-selinux17.txt
sudo tail -n 10 /var/log/httpd/error_log
sudo ausearch --input-logs -m AVC,USER_AVC -ts recent -c httpd -i
sudo chmod 644 /var/www/html/lf-selinux17.txt
curl --noproxy '*' --max-time 5 -sS -w 'HTTP %{http_code}\n' \
  http://127.0.0.1/lf-selinux17.txt

This time 403 came up again, but the type was correct as httpd_sys_content_t. The request to revert to 644 came back as 200. In the Apache error log, both the general permission denial and the previous SELinux denials showed the term Permission denied, so this term alone cannot serve as a judgment criterion.

If you query the recent section, the AVC from the previous SELinux test may be visible as well. In the actual comparison, the section was narrowed to query after the start time of the general permission test, and <no matches> was checked. To query for a specific start time today, you can enter your test time as follows. If it has passed midnight, specify the date in the correct locale format.

sudo ausearch --input-logs -m AVC,USER_AVC -ts 23:38:33 -c httpd -i

The above time is an example from actual test records and should be replaced with the time of the reader's execution. It distinguishes between date format errors and the case where there are no records matching the conditions. If it is first denied in the general permission check, SELinux denials may not be newly recorded. However, this could also be due to other reasons such as log collection/search conditions or record suppression, so the mere fact that there is no AVC does not completely exclude SELinux.

Test file status HTTP results Verified evidence
644 + httpd_sys_content_t 200 Test string normal response
644 + var_t 403 AVC denial of getattr for httpd_t, permissive=0
644 + restored httpd_sys_content_t 200 Verify recovery with the same file and same request
600 + httpd_sys_content_t 403 The working process cannot read files owned by root, no new AVC in this test section
Restore permissions to 644 200 Recover without changing SELinux mode

Clean up the lab and apply the diagnostic sequence

After confirming that the test files are restored to normal type and permissions, stop Apache. Check the file contents and paths, and delete only the files created in this practice. After verifying that there are no other changes, restore the backed-up Apache configuration. If any separate edits were made in the middle, do not overwrite them with the backup; check the differences first.

sudo chmod 644 /var/www/html/lf-selinux17.txt
sudo restorecon -v /var/www/html/lf-selinux17.txt
cat /var/www/html/lf-selinux17.txt
sudo systemctl stop httpd
sudo rm -- /var/www/html/lf-selinux17.txt
sudo diff -u /root/selinux-lab17-before/httpd.conf /etc/httpd/conf/httpd.conf

Execute the next restoration only after personally confirming that there is only one line difference in Listen and no other edits in the above comparison. The exit status 1 of diff means there are differences, so the difference contents must be read.

sudo cp -a /root/selinux-lab17-before/httpd.conf /etc/httpd/conf/httpd.conf
sudo cmp /root/selinux-lab17-before/httpd.conf /etc/httpd/conf/httpd.conf
systemctl is-active httpd
systemctl is-enabled httpd
sudo ss -lntp '( sport = :80 )'
getenforce

In the source lab, after confirming that the only difference in the settings was Listen, the original was restored. Apache is stopped and automatic start is disabled, and port 80 reception has disappeared. Installed packages and diagnostic logs were retained, and the original SELinux configuration files, firewall files, runtime policies, and management network settings were preserved.

In actual failures, the process is narrowed down in the following order: reproduce the failure behavior → check service status and general permissions → check processes and target types → compare AVC at the relevant time → make minimal modifications for the intended purpose → re-verify the same request . If type, permissions, and mode are changed all at once, it is impossible to know which action resolved the issue, so each condition is compared individually.