Lab 165 · security

SELinux & httpd

Deploy httpd and serve content, then reproduce a 403 caused by incorrect SELinux labeling after moving the DocumentRoot. Diagnose the AVC denial and fix it with persistent file context rules, restore labels, and validate access end-to-end.

selinux httpd troubleshooting

Scenario

You are deploying Apache (httpd) on a RHEL system. The service starts and the firewall is configured, but SELinux policy can still block reads when content is moved to a non-standard path. You will serve a known test page, move the DocumentRoot to /webdata, reproduce the 403, confirm the AVC denial in the audit log, and then apply a persistent SELinux fix using file context rules.

Operator context

Do not “solve” this by disabling SELinux. Use the audit trail to identify the policy reason, apply the correct label, and verify the service behaves correctly with enforcement intact.

Objective

  • Install httpd and enable the service at boot.
  • Open HTTP in the firewall and validate local content delivery with curl.
  • Move the DocumentRoot to /webdata and reproduce an SELinux-driven access failure.
  • Confirm the denial using /var/log/audit/audit.log.
  • Fix the issue with semanage fcontext and restorecon.
  • Enable the httpd_enable_homedirs boolean and label UserDir content correctly.
  • Validate labels and boolean state with CLI evidence.

Concepts

  • Service readiness: package installation, systemd enable/start, and local HTTP validation.
  • Firewall gatekeeping: allowing the http service and reloading permanent rules.
  • SELinux labeling as an access control layer independent of UNIX permissions.
  • AVC denials as the primary evidence source for why access is blocked.
  • Persistent labeling with semanage fcontext and enforcement repair with restorecon.
  • Booleans as controlled policy switches (UserDir support with httpd_enable_homedirs).

Walkthrough

Step 1: Install Apache (httpd).
Command
sudo dnf install -y httpd

Package installation is the baseline. Use a non-interactive install so the operation is repeatable in automation and in lab workflows.

Step 2: Enable and start httpd.
Command
sudo systemctl enable --now httpd

This brings the service online and ensures it will start after reboot. A service that is installed but not enabled is not production-ready.

Step 3: Allow HTTP through the firewall.
Commands
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

Permanent firewall rules do not take effect until reload. Treat firewall changes as configuration changes that must be applied and validated explicitly.

Step 4: Create a test page in the default document root.
Command
echo 'SELinux + httpd lab (165)' | sudo tee /var/www/html/index.html

This gives you a known-good content artifact. If the default path works, you have a baseline before introducing a non-standard document root.

Step 5: Verify local access with curl.
Command
curl http://localhost/
SELinux + httpd lab (165)

Validate locally first. This isolates service configuration and SELinux from external routing issues.

Step 6: Move the DocumentRoot to /webdata and restart httpd.
Commands
sudo mkdir -p /webdata
sudo mv /var/www/html/index.html /webdata/index.html
Config note

Update /etc/httpd/conf/httpd.conf to set DocumentRoot "/webdata" and a matching <Directory "/webdata"> block with Require all granted, then restart the service.

Commands
sudo vim /etc/httpd/conf/httpd.conf
sudo systemctl restart httpd

This is the controlled change that will trigger the denial if /webdata is not labeled for httpd content. In production, a path change without SELinux planning is a common cause of “service started but 403” incidents.

Step 7: Confirm the failure and check for SELinux denials.
Commands
curl -I http://localhost/
sudo tail -n 5 /var/log/audit/audit.log
HTTP/1.1 403 Forbidden
type=AVC msg=audit(...): avc:  denied  { read } for  pid=... comm="httpd" name="index.html" ... scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:default_t:s0 tclass=file

The 403 confirms the symptom. The AVC line explains the cause: httpd is running in httpd_t and the target file is labeled default_t, which is not allowed for web content reads under standard targeted policy.

Step 8: Fix labeling with semanage and restorecon.
Commands
sudo semanage fcontext -a -t httpd_sys_content_t '/webdata(/.*)?'
sudo restorecon -Rv /webdata

semanage fcontext creates a persistent mapping so the correct type is applied to the path. restorecon enforces that mapping on the filesystem immediately.

Step 9: Verify access is restored and enable UserDir support.
Commands
curl http://localhost/
sudo setsebool -P httpd_enable_homedirs on

First verify the original incident is resolved. Then enable the boolean needed to serve UserDir content when the environment requires ~/public_html.

Commands
mkdir -p ~/public_html
echo 'UserDir works (SELinux boolean enabled)' > ~/public_html/index.html
sudo restorecon -Rv /home/examuser/public_html

Creating content is not enough. The directory and files must be labeled correctly for httpd to serve them under enforcement.

Step 10: Validate contexts and boolean state.
Commands
ls -Z /webdata /webdata/index.html
getsebool httpd_enable_homedirs

End the change with evidence. Correct labels and correct boolean state are the operational success criteria.

Common breakpoints

curl works on /var/www/html but fails after moving DocumentRoot

This is a classic label drift scenario. Verify the target content under /webdata is not labeled httpd_sys_content_t, then apply a persistent mapping with semanage fcontext and run restorecon.

403 Forbidden with no obvious file permission issue

UNIX permissions are necessary but not sufficient under SELinux. Confirm the denial in /var/log/audit/audit.log and validate the tcontext type matches the expected httpd content types.

semanage: command not found

The tool is not available in the current environment. You cannot create persistent file context mappings without it. Escalate as a missing tooling/package issue rather than attempting to “fix” with temporary labels.

UserDir content still fails after creating ~/public_html

Ensure httpd_enable_homedirs is enabled and that the directory is labeled correctly after restorecon. Treat boolean state and label state as separate checks.

Cleanup checklist

This lab leaves the service installed and content paths in place. Cleanup is documenting what you changed and capturing final evidence (labels and boolean state) so the system can be supported after handoff.

Commands
ls -Z /webdata /webdata/index.html
getsebool httpd_enable_homedirs
curl -I http://localhost/
Success signal

curl returns a successful response, /webdata is labeled httpd_sys_content_t, and httpd_enable_homedirs is set to the expected value for the environment.

Reference

  • dnf install -y <package>: Installs a package non-interactively.
    • -y: Automatically answers “yes” to prompts.
  • systemctl enable --now <unit>: Enables a service at boot and starts it immediately.
    • --now: Starts the unit right away.
  • firewall-cmd --permanent --add-service=http: Adds the HTTP service to the permanent firewall rules.
    • --permanent: Writes the rule to the permanent configuration.
  • firewall-cmd --reload: Reloads firewall rules to apply permanent changes.
  • tee <file>: Writes stdin to a file while also printing to stdout.
    • /var/www/html/index.html: Default httpd document root content path.
  • curl <url>: Sends an HTTP request and prints the response body.
    • -I: Fetches HTTP headers only.
    • http://localhost/: Local validation endpoint for service readiness.
  • mkdir -p <dir>: Creates a directory and parent directories as needed.
    • -p: Creates parents and avoids errors if the directory already exists.
    • /webdata: Non-standard DocumentRoot path used to reproduce SELinux denials.
    • ~/public_html: UserDir content path used with httpd_enable_homedirs.
  • mv <src> <dst>: Moves or renames a file.
    • /var/www/html/index.html: Original content location.
    • /webdata/index.html: New content location under the updated DocumentRoot.
  • vim <file>: Edits a file in a terminal editor.
    • /etc/httpd/conf/httpd.conf: Primary Apache configuration file for DocumentRoot and Directory rules.
  • tail -n 5 <file>: Prints the last N lines of a file.
    • -n 5: Limits output to the last 5 lines.
    • /var/log/audit/audit.log: Audit log source for SELinux AVC denials.
  • semanage fcontext -a -t <type> '<regex>': Adds a persistent SELinux file context mapping.
    • -a: Adds a new mapping entry.
    • -t: Sets the target SELinux type for matched paths.
    • '/webdata(/.*)?': Matches /webdata and all files beneath it.
  • restorecon -Rv <path>: Restores SELinux contexts based on policy mappings.
    • -R: Recursively processes directories.
    • -v: Prints changes as they are applied.
  • setsebool -P <boolean> on|off: Sets an SELinux boolean persistently.
    • -P: Makes the change persistent across reboots.
    • httpd_enable_homedirs: Allows httpd to serve UserDir content under user home directories.
  • getsebool <boolean>: Displays the current value of an SELinux boolean.
  • ls -Z <path>: Displays SELinux contexts for files and directories.
    • -Z: Prints SELinux security context information.
    • httpd_sys_content_t: Expected type for httpd-readable static web content.