LINUXMIND.DEVTHE GNU/LINUX WORLD

SELinux on Fedora: a practical guide to enforcing mode, booleans and denials

Ilustracion editorial plana de un escudo por capas con un candado y un proceso detenido en una puerta, SELinux en Fedora

Fedora is one of the few desktop distributions that ships SELinux enabled and enforcing out of the box, and most people meet it the first time it blocks something: a web server that cannot read its own files, a service that will not start, a script that works everywhere except on Fedora. The reflex is setenforce 0 or, worse, editing /etc/selinux/config to disabled. This is a practical guide to the other path: understand what SELinux is doing, read its logs, fix the two things that actually go wrong — a wrong label or a missing boolean — and leave the protection on.

What SELinux actually does

Classic Linux permissions are discretionary: a file belongs to a user, and that user decides who else can read it. SELinux adds mandatory access control on top. Every process runs in a domain, every file carries a label, and a policy written by someone else decides which domains may touch which labels. Even if root asks, the kernel refuses when the policy does not allow it. That is the whole idea: a compromised service stays stuck in its own domain instead of roaming the filesystem as root.

Fedora’s default is the targeted policy, which confines the services that face the network — web servers, SSH, containers, database engines — while leaving ordinary user processes mostly unconfined. It is a big net cast over the places attackers actually reach, not a straitjacket on your desktop.

Which mode is it in?

Two commands answer everything, and you will use them constantly:

getenforce                 # Enforcing, Permissive or Disabled
sestatus                   # policy, current mode, mode from the config
Mode What it does
Enforcing Blocks anything the policy forbids and logs it
Permissive Allows everything but logs what it would have blocked
Disabled No policy at all — and relabelling on the way back is painful

Permissive is your debugging tool, not your resting state. To gather every denial a broken app produces in one run, switch to permissive temporarily — the system keeps working while SELinux writes down what it would have stopped:

sudo setenforce 0          # permissive until the next reboot
# ...reproduce the problem...
sudo setenforce 1          # back to enforcing, no reboot needed

Do not set SELINUX=disabled in /etc/selinux/config to “fix” a problem. Turning SELinux back on later forces a full filesystem relabel, which can take a long time and is easy to get wrong. Permissive gives you the same breathing room and none of the re-entry cost.

Labels: where it usually goes wrong

The most common SELinux problem is not a missing rule but a wrong label — a file that moved, was copied without its context, or was created by the wrong tool. Look at labels with the -Z flag:

ls -Z /var/www/html            # labels on a directory's contents
ps -eZ | grep httpd            # the domain a process runs in
id -Z                          # your own SELinux user, role and type

A web file should carry a type like httpd_sys_content_t. If it shows user_home_t or default_t, httpd cannot read it no matter what the file permissions say. The fix is almost always one command:

sudo restorecon -Rv /var/www/html    # relabel a tree to match the policy

If a whole filesystem looks mislabelled — usually after copying data in from another disk or restoring a backup — relabel everything on the next boot:

sudo touch /.autorelabel
sudo reboot

Booleans: policy switches you are meant to flip

SELinux ships with hundreds of booleans — named on/off switches that adjust the policy without writing a new one. This is the correct way to let a service do something it normally cannot, and it is far better than disabling enforcement:

getsebool -a | grep httpd        # list the httpd-related switches
sudo setsebool -P httpd_can_network_connect on

The -P makes the change persistent across reboots. Common examples: let a web app reach the network, let it send mail, or let a service read a home directory. Before writing any custom policy, always check whether a boolean already exists for what you need.

Reading a denial

When enforcing mode blocks something, it writes an AVC (Access Vector Cache) message to the audit log. That message is the whole story: who, what, and why it was denied.

sudo dnf install policycoreutils-python-utils \
     setroubleshoot-server setools-console
sudo ausearch -m AVC,USER_AVC -ts recent

ausearch filters the audit log; with setroubleshoot-server installed you also get a plain-language explanation, and the GUI sealert turns a raw denial into a sentence and a suggested fix. If the audit daemon is not running, the same messages show up in dmesg:

sudo dmesg | grep -e type=1300 -e type=1400

Turning a denial into a policy — carefully

If no boolean fits and the label is right, you can generate a policy module from the denials themselves:

sudo ausearch -m AVC -ts recent --raw \
  | audit2allow -M mi-servicio
sudo semodule -i mi-servicio.pp

Read the generated rules before you install them. audit2allow will happily grant anything it sees denied, including permissions an attacker would love. audit2allow -w -a explains what each rule allows, and semodule -X 300 -i mi-servicio.pp installs a module at a high priority so it overrides the base policy. Keep custom modules few and documented.

Back up your local changes

Everything you have customised — booleans, file contexts, port labels, users — can be exported and replayed on another machine:

sudo semanage export > selinux-local.txt
# on another host:
sudo semanage import -f selinux-local.txt

Note that semanage export does not include custom policy modules, so keep the .te and .pp files too.

Closing

SELinux has a reputation for being hostile because people meet it at the worst moment, with no idea what it is. In practice you need four tools: getenforce and sestatus to see the state, restorecon to fix labels, setsebool -P to flip a switch, and ausearch to read a denial. Reach for permissive mode while you are debugging and enforcing mode as soon as you are done, and Fedora keeps a layer of protection that a plain permissions model simply cannot offer.

Sources




Leave a Reply

Your email address will not be published. Required fields are marked *