Linux Advanced 03 — SELinux Contexts, Booleans, and Denials

Linux course index · Linux Advanced category · Lesson 3 of 9

AI disclosure — AI-generated content, posted as is. AI can make mistakes. This lesson draws on the linked Open Education posts and adds original explanations or lab examples where useful. The newly written explanations and examples may be freely reused. Linked source material retains its own terms.

Learning goal

Diagnose ordinary permission and SELinux policy problems separately and set a persistent file context.

Concepts and setup

SELinux adds a mandatory policy check to ordinary mode bits and ACLs. A mode of 0644 does not guarantee that a confined service can read a file. Enforcing applies policy, permissive records would-be denials, and disabled does not enforce. Booleans are documented policy toggles, not general fixes.

Use an enforcing Red Hat-family VM with Apache and the SELinux administration tools. The original site’s Apache example uses ls -Z, and its Boolean example uses getsebool. Keep enforcement on while following these lab steps.

Inspect policy and current labels

getenforce
ls -Zd /var/www/html
matchpathcon /var/www/html/index.html
getsebool httpd_can_network_connect
sudo ausearch -m AVC -ts recent

Possible output

Enforcing
system_u:object_r:httpd_sys_content_t:s0 /var/www/html
/var/www/html/index.html system_u:object_r:httpd_sys_content_t:s0
httpd_can_network_connect --> off
<no matches>

Inspect denial timestamps and the process, object type, and operation before acting. No matches does not prove that an HTTP request succeeded.

Set a persistent context for an unused lab content path

sudo install -d -m 0755 /srv/oe-linux-content
printf 'SELinux lab\n' | sudo tee /srv/oe-linux-content/index.html
sudo chmod 0644 /srv/oe-linux-content/index.html
sudo semanage fcontext -a -t httpd_sys_content_t '/srv/oe-linux-content(/.*)?'
sudo restorecon -Rv /srv/oe-linux-content
ls -Z /srv/oe-linux-content/index.html
matchpathcon -V /srv/oe-linux-content/index.html

Possible output

SELinux lab
Relabeled /srv/oe-linux-content/index.html from ... to ...:httpd_sys_content_t:s0
...:httpd_sys_content_t:s0 /srv/oe-linux-content/index.html
/srv/oe-linux-content/index.html verified.

The lab checks labels, not actual HTTP service. To roll back, remove the exact lab rule with semanage fcontext -d ‘/srv/oe-linux-content(/.*)?’ and remove the new fixture.

Questions only

Quick-check questions

  1. Why can chmod 777 fail to resolve the denial?
  2. What does restorecon do?

Assessment questions

  1. A web server cannot read a new DocumentRoot. Which three layers would you check?
  2. Why use a persistent fcontext rule rather than a one-off label change?
  3. When is changing a Boolean justified?

Questions and answers

Quick-check answers

  1. Why can chmod 777 fail to resolve the denial?
    Answer — SELinux is a separate check. Broadening normal access does not correct a policy denial or label.
  2. What does restorecon do?
    Answer — It applies the context expected by the configured file-context rules.

Assessment answer key

  1. A web server cannot read a new DocumentRoot. Which three layers would you check?
    Answer — Check normal permissions and traversal, Apache configuration and user, and SELinux labels plus AVC evidence.
  2. Why use a persistent fcontext rule rather than a one-off label change?
    Answer — A future relabel can apply the expected context again.
  3. When is changing a Boolean justified?
    Answer — When the intended workload actually requires the documented behavior, the relevant Boolean controls it, and a narrower solution is not appropriate.

Source articles on this site

Continue the course

Previous lesson 02 — NFS and On-Demand Mounting with AutoFS. Next lesson 04 — Firewall Zones and Runtime Rules.

Linux course index · Linux Advanced category · Lesson 3 of 9

Leave a Reply