Skip to content
System

SELinux: fixing a denied access without disabling protection

Avatar illustré de Julien Macé 3 min read
Tux examine un dossier serveur protégé par SELinux
Un refus SELinux se lit dans le contexte du fichier et dans les événements AVC.

A service starts, the port listens, then the application receives an access denial on a file or directory. On Rocky Linux 9 or RHEL 9, SELinux may be the cause, but disabling its protection masks the problem instead of fixing it.

The diagnosis starts from the active mode, the context of the concerned path, and the AVC message. This method allows correcting a specific service without turning the server into a permissive machine.

Tux examines a server folder protected by SELinux
A SELinux refusal can be read in the file’s context and in the AVC events.

Read the denial before modifying a rule

Start by confirming that SELinux is indeed enforcing its policy. Enforcing means denials are blocked. Permissive logs them without blocking. Disabled removes this control after a reboot.

getenforce
sestatus

If the service fails on /srv/monapp, look at the SELinux type assigned to the path, not just its Unix permissions. An HTTP process can have rwx rights on the directory and remain blocked if the type does not suit it.

ls -Zd /srv /srv/monapp
ls -Z /srv/monapp

The journal then provides the process, the path, and the denied action. On RHEL and Rocky, ausearch targets AVC events when the audit package is present.

sudo ausearch -m AVC -ts recent
sudo journalctl -t setroubleshoot --since "15 minutes ago"

Keep track of the test time and the service name. Without this event, a random command found could grant broader access than needed.

Correct the context of an application path

A frequent case concerns a directory moved outside /var/www. For content served by Apache or Nginx, first apply the type intended by the policy, then save the rule for future relabels.

sudo semanage fcontext -a -t httpd_sys_content_t "/srv/monapp(/.*)?"
sudo restorecon -Rv /srv/monapp
ls -Zd /srv/monapp

The last ls -Zd should show httpd_sys_content_t on the content. If the application needs to write into a designated subdirectory, do not give a writable type to the entire tree. Isolate the data folder, then use an appropriate type like httpd_sys_rw_content_t only on that folder.

sudo semanage fcontext -a -t httpd_sys_rw_content_t "/srv/monapp/data(/.*)?"
sudo restorecon -Rv /srv/monapp/data

The Red Hat SELinux documentation details the types and booleans provided with the policy. For network blocking or access to a remote database, first look for a documented boolean before creating a local module.

Restart the service and check the result

Only restart the concerned service, then replay the action that failed. A successful start does not always validate the writing of a file or access to a socket.

sudo systemctl restart monapp
sudo systemctl status monapp --no-pager
sudo ausearch -m AVC -ts recent

The control passes when the business function responds and no new AVC corresponds to the test. An old AVC may remain in the history, so compare the event time with that of the restart.

When to leave the policy intact

setenforce 0 can confirm a diagnosis for a few minutes on a test VM, but it is not a production fix. A line SELINUX=disabled in /etc/selinux/config removes control after rebooting and complicates a return to a clean policy.

I would prefer a persistent context change with semanage fcontext followed by restorecon. It describes the role of the path and survives a relabel. This logic targets SELinux on RHEL, Rocky, or Fedora. Ubuntu mainly uses AppArmor, whose profiles and logs follow a different model.

Before making a change on a remote server, keep a second SSH session open. If the service depends on a shared path or a network mount, validate the mount and its labels before modifying the SELinux policy.

Share Facebook X WhatsApp LinkedIn Pinterest Reddit Email