centos selinux应用支持
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.
1. Core Components of SELinux Application Support
SELinux’s application support relies on three key concepts to enforce security:
- Security Context: A label (e.g.,
system_u:system_r:nginx_t:s0) assigned to files, processes, and ports. It consists of three parts:- User: Identity of the user running the process (e.g.,
system_u). - Role: Role of the user/process (e.g.,
system_rfor system processes). - Type: The most critical part, defining the “domain” a process runs in (e.g.,
nginx_tfor Nginx) and the objects it can access.
- User: Identity of the user running the process (e.g.,
- Policies: Rules that define allowed interactions between subjects (processes) and objects (files/ports). CentOS defaults to the targeted policy, which focuses on securing common network services (e.g., Nginx, Apache, FTP) while leaving untargeted processes with fewer restrictions.
- Modes: SELinux operates in three modes:
- Enforcing: Actively enforces policies (default in CentOS).
- Permissive: Logs policy violations but does not block them (useful for debugging).
- Disabled: Completely turns off SELinux (not recommended for production).
2. Default Application Support
SELinux includes built-in policies for popular CentOS applications, ensuring they run with minimal privileges. Examples include:
- Web Servers: Nginx (
nginx_t) and Apache (httpd_t) are protected by default. Policies restrict access to web content directories (e.g.,/var/www/htmlwithhttpd_sys_content_t) and prevent unauthorized network connections. - FTP Servers: vsftpd (
vsftpd_t) is configured to access specific directories (e.g.,/var/ftpwithpublic_content_t) and limits user home directory access unless explicitly permitted. - Databases: MySQL/MariaDB (
mysqld_t) is restricted to its data directory (e.g.,/var/lib/mysqlwithmysqld_db_t) and prevents shell access. - SSH: The SSH daemon (
sshd_t) is limited to authenticated user sessions and restricts port usage to 22 by default.
3. Configuring SELinux for Applications
To tailor SELinux to specific application needs, administrators use tools like semanage, chcon, and setsebool:
- Changing File/Directory Context: Use
chconto modify the security context of an application’s files. For example, to allow Nginx to serve files from a custom directory (/data/web), run:To make the change permanent, usesudo chcon -R -t httpd_sys_content_t /data/websemanage fcontextandrestorecon:sudo semanage fcontext -a -t httpd_sys_content_t "/data/web(/.*)?" sudo restorecon -Rv /data/web - Managing Ports: Use
semanage portto 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 - Adjusting Boolean Values: Booleans are on/off switches for specific policy rules. For example, to allow Apache to connect to the network (e.g., for PHP-FPM):
Usesudo setsebool -P httpd_can_network_connect=1getsebool -ato list all available booleans.
4. Troubleshooting Application Issues
When an application fails due to SELinux, follow these steps to diagnose and resolve the issue:
- Check SELinux Status: Verify SELinux is enabled and in enforcing mode:
sestatus - Review Logs: Examine
/var/log/audit/audit.log(or/var/log/messagesif auditd is disabled) for “AVC denied” messages. Useausearchto filter relevant logs:sudo ausearch -m AVC -ts recent - Generate Policy Fixes: Use
audit2whyto interpret logs andaudit2allowto create custom policies. For example:
Note: Custom policies should be reviewed to avoid overly permissive rules.sudo ausearch -m AVC -ts recent | audit2why sudo ausearch -m AVC -ts recent | audit2allow -M myapp_policy sudo semodule -i myapp_policy.pp
5. Advanced Application Protection
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):
- Create a policy file (
/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 } ; - Compile and load the module:
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.
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: centos selinux应用支持
本文地址: https://pptw.com/jishu/746410.html
