详解SELinux故障排查和陷阱规避

维护我们服务器的安全是一项艰巨的工作,与第三方解决方案提供商打交道时尤为如此。在许多情况下,面临的挑战是要求禁用SELinux,以便应用程序可以顺利运行。幸好,这种情况越来越少了。在大多数情况下,一番分析足以找到正确的故障排查或解决方法。

SELinux是一个标签系统,它告诉我们系统中的每个文件、目录或对象都有对应的标签(Label)。策略控制这些元素之间的交互关系,内核则执行这些规则。

两个最重要的概念是标签(文件、进程和端口等)和类型强制(根据进程的类型将进程彼此隔离开来)。

标签使用的格式为:user:role:type:level(可选)。

要找出当前配置,请运行getenforce和sestatus两个命令:

# getenforce 

Enforcing 

# sestatus 

SELinux status:                 enabled 

SELinuxfs mount:                /sys/fs/selinux 

SELinux root directory:         /etc/selinux 

Loaded policyname:             targeted 

Current mode:                   enforcing 

Modefrom config file:          enforcing 

Policy MLS status:              enabled 

Policy deny_unknown status:     allowed 

Memory protection checking:     actual (secure) 

Max kernel policy version:      32 

最佳实践告诉我们,我们测试一个新的第三方应用程序时,应该在许可模式下临时配置SELinux,以便确定哪些策略或布尔值(更改行为的简单字符串)必不可少。运行该命令:

# setenforce 0 

查看日志,您可以找到SELinux使应用程序正常运行所需的条件。

SELinux试图告诉我什么?

SELinux中生成警报的错误只有四个主要原因:

标签。

SELinux需要知道。

SELinux策略及/或应用程序可能有错误。

您的信息可能被泄露。

最后一种情况是由于对攻击漏洞进行了修改或避免了活动跟踪,不过在这两种情况下,都必须查看这些警报,这点暂且不介绍。

标签

标签问题:/srv/myweb中的文件未正确标记,因而无法访问。

SELinux为同一服务所涉及的每个元素分配一个标签:

二进制文件:/usr/sbin/httpd→httpd_exec_t

配置文件:/etc/httpd→httpd_config_t

日志文件:/var/log/httpd→httpd_log_t

内容目录:/var/www/html→httpd_sys_content_t

启动脚本:/usr/lib/systemd/system/httpd.service→httpd_unit_file_t

进程:/usr/sbin/httpd→httpd_t

端口:80/tcp,443/tcp→httpd_t和httpd_port_t

在Web服务器上,在httpd_t上下文中运行的进程只能与带有httpd_something_t标签的对象进行交互。

解决方案:正确标记文件。

如果您知道正确的标签,请运行:

# semanage fcontext -a -t httpd_sys_content_t '/srv/myweb(/.*)?' 

如果您知道拥有相等标签的文件,请运行:

# semanage fcontext -a -e /srv/myweb /var/www 

针对这两种情况,恢复文件的默认上下文:

# restorecon -vR /srv/myweb 

标签问题:如果某个文件被移动而不是被拷贝,它保留原始标签。

$ mv index.html /var/www/html/ 

解决方案:正确标记文件。

将上下文更换成正确的标签:

# chcon -t httpd_system_content_t /var/www/html/index.html 

更换拥有参考标签的上下文:

# chcon --reference /var/www/html/ /var/www/html/index.html 

针对这两种情况,恢复文件的默认上下文:

# restorecon -vR /var/www/html/ 

SELinux需要知道

服务定制:Web服务器将在端口8585上侦听请求。

要把所需的端口添加到上下文,请运行:

# semanage port -a -t http_port_t -p tcp 8585 

为服务添加功能:Web服务器将能够发送电子邮件。

要启用邮件发送功能,开启布尔值,运行:

# setsebool -P httpd_can_sendmail 1 

-P标志使变更在布尔值中具有持久性。

要获得所有布尔值,运行:

# getsebool -a 

要检查布尔值的状态,运行:

# semanage boolean -l 

策略故障排查

服务未运行:使用wicd而不是NetworkManager服务来处理无线连接。

检查audit.log文件,查找被拒绝的访问:

# grep denied audit.log | cut -d{ -f2 | sort -n | uniq -u 

create } for  pid=2670 comm="wicd" scontext=system_u:system_r:NetworkManager_t:s0 tcontext=system_u:system_r:NetworkManager_t:s0 tclass=appletalk_socket permissive=1 

create } for  pid=2670 comm="wicd" scontext=system_u:system_r:NetworkManager_t:s0 tcontext=system_u:system_r:NetworkManager_t:s0 tclass=ax25_socket permissive=1 

ioctl }for  pid=2670 comm="wicd" path="socket:[52681]" dev="sockfs" ino=52681 ioctlcmd=0x8b01 scontext=system_u:system_r:NetworkManager_t:s0 tcontext=system_u:system_r:NetworkManager_t:s0 tclass=ax25_socket permissive=1 

ioctl }for  pid=2670 comm="wicd" path="socket:[52684]" dev="sockfs" ino=52684 ioctlcmd=0x8b01 scontext=system_u:system_r:NetworkManager_t:s0 tcontext=system_u:system_r:NetworkManager_t:s0 tclass=appletalk_socket permissive=1 

setattr }for  pid=2214 comm="wicd" name="dhclient.conf.template" dev="dm-0" ino=437068 scontext=system_u:system_r:NetworkManager_t:s0 tcontext=unconfined_u:object_r:etc_t:s0 tclass=file permissive=0 

注意:上下文NetworkManager_t和etc_t所涉及的一些元素需要权限,需要访问不同的文件和套接字创建。

创建type enforcement (.te)文件,拥有策略的必要权限:

# vi my_wicd.te 

module my_wicd 1.0; 


require { 

        type NetworkManager_t; 

        type etc_t; 

class ipx_socketcreate; 

class ax25_socket {create ioctl }; 

class appletalk_socket {create ioctl }; 

        class file setattr; 

} 


#============= NetworkManager_t ============== 

allow NetworkManager_t etc_t:file setattr; 

allow NetworkManager_t self:appletalk_socket {create ioctl }; 

allow NetworkManager_t self:ax25_socket {create ioctl }; 

allow NetworkManager_t self:ipx_socketcreate; 

要编译策略,安装软件包selinux-policy-devel,并生成策略软件包:

# make -f /usr/share/selinux/devel/Makefile my_wicd.pp 

要激活刚生成的模块,运行:

# semodule -i my_wicd.pp 

策略错误信息:试图访问我的网站时,我在日志中看到了SELinux错误。

为SELinux错误信息排查故障时常见的陷阱之一是,根据发现的所有错误信息创建策略。在大多数情况下,如果setroubleshoot包已安装,同样的提醒会给出所有可能的变通选项,按最好到最差的顺序排列。

要查看今天生成的setroubleshoot提醒,运行:

# journalctl -t setroubleshoot --since today 

Dec 08 13:08:33 lab.example.com setroubleshoot[12013]: failed to retrieve rpm info for /var/www/html/index.html 

Dec 08 13:08:34 lab.example.com setroubleshoot[12013]: SELinux is preventing /usr/sbin/httpd from getattr access on the file /var/www/html/index.html. For complete SELinux messages run: sealert -l 011df984-4eb6-4079-98ab-cba173c4342e 

Dec 08 13:08:34 lab.example.com setroubleshoot[12013]: SELinux is preventing /usr/sbin/httpd from getattr access on the file /var/www/html/index.html. 


 *****  Plugin restorecon (99.5 confidence) suggests   ************************ 


If you wantto fix the label 

/var/www/html/index.html default label should be httpd_sys_content_t. 

Then you can run restorecon. The access attempt may have been stopped due to insufficient permissions to access a parent directory, in which case try to change the following command accordingly. 

 Do 

# /sbin/restorecon -v /var/www/html/index.html 


 *****  Plugin catchall (1.49 confidence) suggests   ************************** 


If you believe that httpd should be allowed getattr accesson the index.html file by default. 

Then you should report this as a bug. 

You can generate alocal policy module to allow this access. 

 Do 

allow this accessfor now by executing: 

# ausearch -c'httpd' --raw | audit2allow -M my-httpd 

 # semodule -X 300 -i my-httpd.pp 

在这里,最好的解决办法就是修复文件的标签。

小结

为SELinux排查故障可能很难,但如果运用这里介绍的几个概念,并了解服务的组成,您就可以处理它带来的各种难题。

切记:SELinux是一种标签系统。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容