What SELinux is: enforcing, permissive, getenforce
This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.
Key points
- Standard permissions are DAC (the owner decides); SELinux is MAC (the system policy decides) and is checked after DAC.
- Every process and file carries a context:
user:role:type:level; decisions are based on the type (httpd_t,httpd_sys_content_t). - Modes: enforcing (blocks and logs), permissive (only logs), disabled.
getenforce,sestatus— check;setenforce 0/1— temporary; permanent —/etc/selinux/config.- When there is a problem, do not turn SELinux off: diagnose with permissive, then fix with a context, boolean or port.
Why SELinux
Classic Linux permissions are called DAC (Discretionary Access Control): a file's owner decides who gets what, and root can do everything. The problem: if nginx or httpd is compromised, the attacker can do everything that process can do.
SELinux adds MAC (Mandatory Access Control): for each process the system policy states exactly which types of files, ports and processes it may touch. A file's owner cannot change the policy.
| Scenario | DAC only | DAC + SELinux |
|---|---|---|
A compromised httpd tries to read /etc/shadow | Depends on permissions | Blocked: httpd_t cannot read shadow_t |
| httpd tries to connect to port 4444 (a backdoor) | Allowed | Blocked: the port is not http_port_t |
| httpd serves a file from a user's home directory | Yes, if the mode is 755 | Blocked: user_home_t |
The order of checks: DAC first, then SELinux. Both must allow it.
The context: a label on everything
[ali@srv-web01 ~]$ id -Z unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 [ali@srv-web01 ~]$ ps -eZ | grep -E "httpd|sshd" | head -n 2 system_u:system_r:httpd_t:s0 1588 ? 00:00:00 httpd system_u:system_r:sshd_t:s0-s0:c0.c1023 1032 ? 00:00:00 sshd [ali@srv-web01 ~]$ ls -Z /var/www/html/index.html /etc/shadow unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/index.html system_u:object_r:shadow_t:s0 /etc/shadow
A context has four parts: user:role:type:level.
| Part | Example | Role |
|---|---|---|
| user | system_u | SELinux user |
| role | object_r | Role |
| type | httpd_t, httpd_sys_content_t | The main decision is based on this |
| level | s0 | MLS/MCS level |
In daily work you deal only with the type. The policy consists of rules like "a httpd_t process may read httpd_sys_content_t files". This is the targeted policy — RHEL uses it by default: network services are confined, while user processes (unconfined_t) are barely restricted.
Modes
| Mode | What it does | When |
|---|---|---|
| Enforcing | Blocks what is forbidden and logs it | Always — the production standard |
| Permissive | Blocks nothing, only logs | Briefly, for diagnosis |
| Disabled | Completely off, files are not labelled | Not recommended |
[ali@srv-web01 ~]$ getenforce Enforcing [ali@srv-web01 ~]$ sestatus SELinux status: enabled SELinuxfs mount: /sys/fs/selinux SELinux root directory: /etc/selinux Loaded policy name: targeted Current mode: enforcing Mode from config file: enforcing Policy MLS status: enabled Policy deny_unknown status: allowed Memory protection checking: actual (secure) Max kernel policy version: 33
Changing the mode
[ali@srv-web01 ~]$ sudo setenforce 0 [ali@srv-web01 ~]$ getenforce Permissive [ali@srv-web01 ~]$ sudo setenforce 1 [ali@srv-web01 ~]$ grep ^SELINUX= /etc/selinux/config SELINUX=enforcing
| How | Effect |
|---|---|
setenforce 0 / setenforce 1 | Permissive / enforcing — until reboot |
SELINUX= in /etc/selinux/config | Permanent, applied at boot |
Kernel parameter enforcing=0 | For one boot (in recovery situations) |
semanage permissive -a httpd_t | Make only one process type permissive |
Important: moving from disabled to enforcing requires relabelling the whole file system (touch /.autorelabel and reboot) — it takes a long time and leads to mistakes. So never set SELinux to disabled; if needed, a temporary permissive is enough.
The "turn SELinux off" advice
The first step of many guides on the internet is setenforce 0. That is like leaving the door open: the problem is "solved", but the protection is gone too. The right approach:
- Reproduce the problem and confirm SELinux is the cause (
ausearch -m AVC -ts recent). - Identify the cause: a wrong context (4.2), a disabled boolean (4.3), a non-standard port (4.3).
- Make a precise fix and test in enforcing mode.
The remaining lessons of the unit solve these three causes one by one.
Lab: get to know SELinux
In the Red Hat simulator, on srv-web01:
- Check the mode:
[ali@srv-web01 ~]$ getenforce Enforcing [ali@srv-web01 ~]$ sestatus | grep -E "mode|policy name"
- Look at the contexts of processes and files:
[ali@srv-web01 ~]$ ps -eZ | grep httpd [ali@srv-web01 ~]$ ls -Z /var/www/html/ [ali@srv-web01 ~]$ ls -Zd /home/ali
- Write down the type of three files:
/var/www/html(httpd_sys_content_t),/home/ali(user_home_dir_t),/etc/shadow(shadow_t). - (On your own VM)
setenforce 0,getenforce, then be sure to runsetenforce 1.
Check: getenforce shows Enforcing at the end.
Check yourself
5 questions · answers are shown instantly · 3 correct answers complete the lesson
1What kind of access control is SELinux?
2Which part of the context are SELinux decisions based on?
3What happens in permissive mode?
4Which file is edited to change the SELinux mode permanently?
5Why is disabling SELinux not recommended?
Comments
Sign in to comment.