Chapter 1. Getting started with SELinux


Enhance your system’s security by understanding the core concepts of Security Enhanced Linux (SELinux). Knowledge of the SELinux architecture, packages, and operation modes helps to manage your system policies effectively.

1.1. Introduction to SELinux

Security Enhanced Linux (SELinux) is an implementation of Mandatory Access Control (MAC) that strengthens system security. By enforcing granular policies restricting process interactions with files and network resources, SELinux mitigates compromised applications and prevents unauthorized access.

Standard Discretionary Access Control (DAC) bases access policies on user, group, and other permissions. This model prevents system administrators from creating fine-grained security policies. For example, DAC cannot restrict specific applications to only view log files while allowing other applications to append data to the same log files.

SELinux adds an additional layer of protection by enforcing Mandatory Access Control (MAC) policies. These policies answer the question: May <subject> do <action> to <object>? For example: May a web server access files in users' home directories?

Every process and system resource has a special security label called an SELinux context. An SELinux context, sometimes referred to as an SELinux label, is an identifier that abstracts system-level details to focus on the security properties of the entity. This identifier provides a consistent way to reference objects in the SELinux policy and removes ambiguity found in other identification methods. For example, a file can have multiple valid path names on a system that uses bind mounts.

The SELinux policy uses the contexts in rules that define how processes interact with system resources. By default, the policy denies all interaction unless a rule explicitly grants access.

For more information, refer to selinux(8) man page and man pages listed by the man -k selinux command when the selinux-policy-doc package is installed on your system.

Note

SELinux policy rules are checked after DAC rules. If DAC rules deny access, SELinux policy rules are not evaluated, and no SELinux denial is logged.

SELinux contexts have four fields: user, role, type, and security level. The type field is the most important for the SELinux policy. Most policy rules that define allowed interactions between processes and system resources use SELinux types rather than the full SELinux context. SELinux types end with _t. For example, the type context for the web server is httpd_t. Files and directories in /var/www/html/ use the httpd_sys_content_t type. Files and directories in /tmp and /var/tmp/ use tmp_t. The type for web server ports is http_port_t.

A policy rule permits Apache (the web server process running as httpd_t) to access files and directories with a context normally found in /var/www/html/ and other web server directories (httpd_sys_content_t). No allow rule exists in the policy for files normally found in /tmp and /var/tmp/, so access is not permitted. With SELinux, even if Apache is compromised and a malicious script gains access, it still cannot access the /tmp directory.

Figure 1.1. An example how can SELinux help to run Apache and MariaDB in a secure way.

Diagram showing how SELinux allows Apache to access /var/www/html/ but denies access to /data/mysql/

As the previous scheme shows, SELinux allows the Apache process running as httpd_t to access the /var/www/html/ directory. It denies access to /data/mysql/ because no allow rule exists for the httpd_t and mysqld_db_t type contexts. Conversely, the MariaDB process running as mysqld_t accesses /data/mysql/ but is denied access to /var/www/html/, which is labeled as httpd_sys_content_t.

1.2. Benefits of running SELinux

Use Security Enhanced Linux (SELinux) to reduce privilege escalation attacks and enforce data confidentiality. SELinux restricts how processes interact with files and system resources. This limits the impact of compromised applications.

Running SELinux provides the following security benefits:

  • All processes and files are labeled. SELinux policy rules define how processes interact with files and with each other. Access is allowed only if an SELinux policy rule allows it.
  • SELinux provides fine-grained access control. Traditional UNIX permissions use Linux user and group IDs. SELinux goes beyond this. SELinux uses all available information: user, role, type, and security level.
  • SELinux policy is defined by administrators and enforced system-wide.
  • SELinux can reduce privilege escalation attacks. Processes run in domains. This separates them from each other. SELinux policy rules define how processes access files and other processes. If a process is compromised, the attacker can only access that process’s normal functions. They can only access files that the process is configured to access. For example, if the Apache HTTP Server is compromised, an attacker cannot read files in user home directories. The only exception is if you add a specific SELinux policy rule to allow it.
  • SELinux can enforce data confidentiality and integrity, and can protect processes from untrusted inputs.

SELinux enhances existing security solutions. It does not replace them. You still need antivirus software, secure passwords, and firewalls. When you run SELinux, continue to follow good security practices. Keep software up to date. Use strong passwords. Use firewalls.

1.3. SELinux examples

Security-Enhanced Linux (SELinux) provides benefits in many real scenarios. Real-world examples show how SELinux stops privilege attacks, config errors, and common bugs through process isolation and user limits.

SELinux adds security in several ways:

  • The default action is deny. If no SELinux policy rule allows access, SELinux blocks it. For example, if a policy does not allow a process to open a file, access is denied.
  • Confined users have fewer rights. You can map Linux users to confined SELinux users to apply security rules. For example, mapping a Linux user to user_u stops that user from running sudo and su.
  • SELinux domains increase process and data separation. Use domains to define which processes can access specific files and directories. For example, an attacker who compromises a Samba server cannot access files used by MariaDB databases.
  • SELinux stops config errors. Attackers can exploit Domain Name System (DNS) zone transfers to inject false data. If you run Berkeley Internet Name Domain (BIND) as a DNS server on RHEL without limits on zone transfers, SELinux protects your system. The default policy stops the named daemon and other processes from using zone transfers to update zone files [1].
  • SELinux stops path traversal attacks. Attackers can exploit Apache bugs to access files using ../. SELinux in enforcing mode stops the httpd process from accessing files outside its allowed paths.
  • SELinux prevents kernel NULL pointer attacks. Attackers can exploit a vulnerability in the mmap function to place code on a null page (CVE-2019-9213). SELinux in enforcing mode prevents this attack.
  • SELinux prevents *PTRACE_TRACEME exploits.* You can use the deny_ptrace boolean to protect your systems from the PTRACE_TRACEME vulnerability (CVE-2019-13272). This blocks attackers from gaining root access.
  • SELinux prevents NFS misconfigurations. Use the nfs_export_all_rw and nfs_export_all_ro booleans to prevent Network File System (NFS) misconfigurations. For example, SELinux can prevent accidentally sharing /home directories.

1.4. SELinux architecture and packages

The SELinux kernel subsystem enforces security policies and the systemd daemon strengthens access control to protect system services. Identify the packages required to install and maintain SELinux to ensure you have the necessary utilities for administration.

SELinux is a Linux Security Module (LSM) built into the Linux kernel. It enforces security policies, which you manage, to control access. SELinux intercepts access requests and checks them against the loaded policy. If the policy permits the request, SELinux allows the action. Otherwise, SELinux blocks the action and reports an error.

SELinux caches decisions, such as allowing or denying access, in the Access Vector Cache (AVC). This cache improves performance by reducing policy rule checks. SELinux policy rules apply only if Discretionary Access Control (DAC) rules allow access first. SELinux logs audit messages to /var/log/audit/audit.log using the type=AVC identifier.

In RHEL 10, the systemd daemon manages system services; systemd starts and stops all services, and users and processes communicate with systemd by using the systemctl utility. The systemd daemon checks the SELinux policy and verifies the labels of the calling process and the unit file to authorize access. This approach strengthens access control to critical system capabilities, such as starting and stopping system services.

The systemd daemon also acts as an SELinux Access Manager. It retrieves the label of the process running systemctl or sending a D-Bus message. The daemon then looks up the label of the unit file that the process wanted to configure. Finally, systemd queries the kernel to determine if the policy allows the specific access between the process label and the unit file label. This confinement restricts compromised applications. Policy writers can also use these controls to confine administrators.

If the SELinux policy denies D-Bus communication between two processes, the system logs a USER_AVC denial message and the communication times out. D-Bus communication is bidirectional.

Important

To avoid incorrect SELinux labeling and subsequent problems, ensure that you start services by using a systemctl start command.

RHEL 10 provides the following packages for working with SELinux:

  • policies: selinux-policy-targeted, selinux-policy-mls
  • tools: policycoreutils, policycoreutils-gui, libselinux-utils, policycoreutils-python-utils, setools-console, checkpolicy

1.5. SELinux states and modes

Enforcing, permissive, and disabled SELinux modes help effectively manage system security. You can use these states to enforce policies or troubleshoot access denials without disrupting system operations.

SELinux can run in one of three modes: enforcing, permissive, or disabled.

  • Enforcing mode is the default, and recommended, mode of operation; in enforcing mode SELinux operates normally, enforcing the loaded security policy on the entire system.
  • In permissive mode, the system acts as if SELinux is enforcing the loaded security policy. This includes labeling objects and logging access denials. However, the system does not actually deny any operations. While not recommended for production systems, permissive mode can be helpful for SELinux policy development and debugging.
  • Disabled mode is strongly discouraged. The system avoids enforcing the SELinux policy and does not label persistent objects such as files. This makes it difficult to enable SELinux in the future.

Use the setenforce utility to change between enforcing and permissive mode. Changes made with setenforce do not persist across reboots. To change to enforcing mode, enter the setenforce 1 command as the Linux root user. To change to permissive mode, enter the setenforce 0 command. Use the getenforce utility to view the current SELinux mode:

# getenforce
Enforcing
# setenforce 0
# getenforce
Permissive
# setenforce 1
# getenforce
Enforcing

In Red Hat Enterprise Linux, you can set individual domains to permissive mode while the system runs in enforcing mode. For example, to make the httpd_t domain permissive:

# semanage permissive -a httpd_t

Note that permissive domains are a powerful tool that can compromise security of your system. Red Hat recommends to use permissive domains with caution, for example, when debugging a specific scenario.



[1] Text files that include DNS data, such as hostname to IP address mappings.
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat Documentation

Legal Notice

Theme

© 2026 Red Hat
Back to top