SELinux in CentOS: Application Support and Management
SELinux (Security-Enhanced Linux) is a mandatory access control (MAC) system integrated into the Linux kernel, providing fine-grained control over process and user access to system resources. CentOS, as a Red Hat-based distribution, has native support for SELinux, which is enabled by default in most versions (e.g., CentOS 7/8). This support includes a robust framework for configuring, managing, and troubleshooting SELinux policies to secure applications and services.
SELinux’s application support relies on three key concepts to enforce security:
system_u:system_r:nginx_t:s0) assigned to files, processes, and ports. It consists of three parts:
system_u).system_r for system processes).nginx_t for Nginx) and the objects it can access.SELinux includes built-in policies for popular CentOS applications, ensuring they run with minimal privileges. Examples include:
nginx_t) and Apache (httpd_t) are protected by default. Policies restrict access to web content directories (e.g., /var/www/html with httpd_sys_content_t) and prevent unauthorized network connections.vsftpd_t) is configured to access specific directories (e.g., /var/ftp with public_content_t) and limits user home directory access unless explicitly permitted.mysqld_t) is restricted to its data directory (e.g., /var/lib/mysql with mysqld_db_t) and prevents shell access.sshd_t) is limited to authenticated user sessions and restricts port usage to 22 by default.To tailor SELinux to specific application needs, administrators use tools like semanage, chcon, and setsebool:
chcon to modify the security context of an application’s files. For example, to allow Nginx to serve files from a custom directory (/data/web), run:sudo chcon -R -t httpd_sys_content_t /data/web
To make the change permanent, use semanage fcontext and restorecon:sudo semanage fcontext -a -t httpd_sys_content_t "/data/web(/.*)?"
sudo restorecon -Rv /data/web
semanage port to allow applications to use non-default ports. For example, to let Nginx use port 8080:sudo semanage port -a -t http_port_t -p tcp 8080
sudo setsebool -P httpd_can_network_connect=1
Use getsebool -a to list all available booleans.When an application fails due to SELinux, follow these steps to diagnose and resolve the issue:
sestatus
/var/log/audit/audit.log (or /var/log/messages if auditd is disabled) for “AVC denied” messages. Use ausearch to filter relevant logs:sudo ausearch -m AVC -ts recent
audit2why to interpret logs and audit2allow to create custom policies. For example:sudo ausearch -m AVC -ts recent | audit2why
sudo ausearch -m AVC -ts recent | audit2allow -M myapp_policy
sudo semodule -i myapp_policy.pp
Note: Custom policies should be reviewed to avoid overly permissive rules.For advanced use cases, you can create custom SELinux modules to handle unique application requirements. For example, to allow a custom Python application (myapp_t) to access a specific directory (/opt/myapp/data with myapp_data_t):
/etc/selinux/targeted/src/policy/myapp.te):module myapp 1.0;
require {
type myapp_t;
type myapp_data_t;
class dir { read write };
}
allow myapp_t myapp_data_t:dir { read write };
checkmodule -M -m -o myapp.mod myapp.te
semodule_package -o myapp.pp -m myapp.mod
sudo semodule -i myapp.pp
This approach ensures granular control over application permissions without relaxing global policies.
By leveraging these tools and techniques, CentOS administrators can effectively use SELinux to secure applications while maintaining the principle of least privilege.