Sub topic 6
Labeling Files Persistently with `semanage fcontext` and `restorecon`
Assume you can use the Linux command line, sudo, standard file permissions, and a RHEL-family system with SELinux enabled. This chapter uses Apache httpd as the example service.
Why SELinux labels matter
Linux file permissions provide discretionary access control (DAC). The file owner can usually decide which users and groups may read or write a file. SELinux adds mandatory access control (MAC). A service such as Apache runs in a restricted SELinux domain, commonly httpd_t. That domain can access only file types allowed by policy.
A file can have correct owner and mode bits but still be inaccessible to Apache because its SELinux type is wrong.
For example, files under /var/www/html normally use the httpd_sys_content_t type. If you move a web root to /srv/web, SELinux does not automatically know that Apache should read the files there.
Persistent file labeling
Use semanage fcontext to store a labeling rule in the SELinux policy configuration. Use restorecon to apply that rule to files that already exist.
The following rule matches /srv/web and everything below it:
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/web(/.*)?"
sudo restorecon -Rv /srv/web
ls -Zd /srv/webThe command has three important parts:
-aadds a new file-context rule.-t httpd_sys_content_tassigns the type Apache normally uses for read-only web content."/srv/web(/.*)?"is a regular expression. It matches the directory itself and all descendants.
restorecon -R applies the rule recursively. The -v option prints each change, which helps you verify what happened.
A context has four fields in the usual SELinux display:
system_u:object_r:httpd_sys_content_t:s0For this task, the type field, httpd_sys_content_t, is the key value. The other fields still matter to SELinux, but the type determines the ordinary service access decision.
Verify and maintain the rule
List rules that may match a path with semanage fcontext -l:
sudo semanage fcontext -l | grep -E '^/srv/web'You can also compare the current label with the label expected by policy:
matchpathcon /srv/web
ls -Zd /srv/webIf you change the rule later, use -m rather than -a:
sudo semanage fcontext -m -t httpd_sys_rw_content_t "/srv/web/uploads(/.*)?"
sudo restorecon -Rv /srv/web/uploadsUse httpd_sys_rw_content_t only for directories that Apache must write, such as an upload directory. Keep ordinary application files read-only with httpd_sys_content_t. A writable label grants additional capability and should not be applied to the entire web root without a reason.
`restorecon` versus `chcon`
chcon changes a file's current label directly. It can be useful for a short experiment, but it does not create the persistent policy rule. A full relabel, or sometimes a file restore operation, can remove that manual change.
The durable pattern is:
- Define the path rule with
semanage fcontext. - Apply it with
restorecon. - Verify the resulting context.
- Test the service.
Remember that SELinux labels do not replace DAC permissions. Apache still needs directory traversal permission and read permission from the normal owner, group, and mode bits.
Exercise
You moved Apache content to /srv/web, and the files have mode 644, but Apache receives permission denied. What persistent fix should you apply?
Show answer
Add a semanage fcontext rule assigning httpdsyscontent_t to /srv/web and its descendants, run restorecon -Rv /srv/web, then verify the label with ls -Zd.