USD ($)
$
United States Dollar
Euro Member Countries
India Rupee
د.إ
United Arab Emirates dirham
ر.س
Saudi Arabia Riyal

SELinux and Mandatory Access Control

Lesson 24/40 | Study Time: 20 Min

Security-Enhanced Linux (SELinux) implements Mandatory Access Control (MAC) to enforce fine-grained security policies beyond traditional discretionary access controls. SELinux controls access to files, processes, and resources based on predetermined policies enforcing the principle of least privilege.

Understanding SELinux requires familiarity with its operating modes, security contexts (labels), policy management using tools like semanage, and troubleshooting techniques. Proper SELinux configuration significantly strengthens Linux system security by preventing unauthorized or potentially harmful actions.

SELinux Operating Modes

SELinux provides mandatory access control by enforcing security policies on Linux systems. The following modes determine how these policies are applied and how violations are handled.


Toggle modes at runtime using:

bash
sudo setenforce 0 # Set permissive mode
sudo setenforce 1 # Set enforcing mode


Check status with:

bash
sestatus

Security Contexts and Labels

SELinux labels (contexts) assign identities to files, processes, and ports controlling access.


Context format: user:role:type:level

E.g., a web server content file might have context system_u:object_r:httpd_sys_content_t:s0.

Tools like ls -Z and ps -Z display contexts for files and processes.

Policy Management with semanage

The semanage utility manages SELinux policy components such as booleans, file contexts, ports, and modules.


Key commands:


1. List booleans:

bash
sudo semanage boolean -l


2. Change boolean value:

bash
sudo semanage boolean -m --on httpd_can_network_connect


3. List and add file contexts:

bash
sudo semanage fcontext -l # List
sudo semanage fcontext -a -t httpd_sys_content_t "/my/custom/path(/.*)?" # Add context


4. Manage ports for services:

bash
sudo semanage port -a -t http_port_t -p tcp 8080


After modifying contexts or policies, apply changes with restorecon.

SELinux Troubleshooting


1. Review audit logs for denial messages in /var/log/audit/audit.log or with ausearch.

2. Use audit2allow tool to generate policy modules that allow denied actions when appropriate.

3. Temporarily set services or domains to permissive without disabling SELinux globally:

bash
sudo semanage permissive -a httpd_t


4. Common troubleshooting steps include verifying file contexts, enabling required booleans, and ensuring correct port contexts.