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

原标题:详解SELinux故障排查和陷阱规避

详解SELinux故障排查和陷阱规避
文章图片
维护我们服务器的安全是一项艰巨的工作 , 与第三方解决方案提供商打交道时尤为如此 。 在许多情况下 , 面临的挑战是要求禁用SELinux , 以便应用程序可以顺利运行 。 幸好 , 这种情况越来越少了 。 在大多数情况下 , 一番分析足以找到正确的故障排查或解决方法 。
SELinux是一个标签系统 , 它告诉我们系统中的每个文件、目录或对象都有对应的标签(Label) 。 策略控制这些元素之间的交互关系 , 内核则执行这些规则 。
两个最重要的概念是标签(文件、进程和端口等)和类型强制(根据进程的类型将进程彼此隔离开来) 。
标签使用的格式为:user:role:type:level(可选) 。
要找出当前配置 , 请运行getenforce和sestatus两个命令:
#getenforceEnforcing#sestatusSELinuxstatus:enabledSELinuxfsmount:/sys/fs/selinuxSELinuxrootdirectory:/etc/selinuxLoadedpolicyname:targetedCurrentmode:enforcingModefromconfigfile:enforcingPolicyMLSstatus:enabledPolicydeny_unknownstatus:allowedMemoryprotectionchecking:actual(secure)Maxkernelpolicyversion:32最佳实践告诉我们 , 我们测试一个新的第三方应用程序时 , 应该在许可模式下临时配置SELinux , 以便确定哪些策略或布尔值(更改行为的简单字符串)必不可少 。 运行该命令:
#setenforce0查看日志 , 您可以找到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标签的对象进行交互 。
解决方案:正确标记文件 。
如果您知道正确的标签 , 请运行:
#semanagefcontext-a-thttpd_sys_content_t'/srv/myweb(/.*)?'如果您知道拥有相等标签的文件 , 请运行:
#semanagefcontext-a-e/srv/myweb/var/www针对这两种情况 , 恢复文件的默认上下文:
#restorecon-vR/srv/myweb标签问题:如果某个文件被移动而不是被拷贝 , 它保留原始标签 。
$mvindex.html/var/www/html/【详解SELinux故障排查和陷阱规避】解决方案:正确标记文件 。
将上下文更换成正确的标签:
#chcon-thttpd_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上侦听请求 。
要把所需的端口添加到上下文 , 请运行:
#semanageport-a-thttp_port_t-ptcp8585为服务添加功能:Web服务器将能够发送电子邮件 。
要启用邮件发送功能 , 开启布尔值 , 运行:
#setsebool-Phttpd_can_sendmail1-P标志使变更在布尔值中具有持久性 。
要获得所有布尔值 , 运行:
#getsebool-a要检查布尔值的状态 , 运行:
#semanageboolean-l策略故障排查
服务未运行:使用wicd而不是NetworkManager服务来处理无线连接 。
检查audit.log文件 , 查找被拒绝的访问:
#grepdeniedaudit.log|cut-d{-f2|sort-n|uniq-ucreate}forpid=2670comm="wicd"scontext=system_u:system_r:NetworkManager_t:s0tcontext=system_u:system_r:NetworkManager_t:s0tclass=appletalk_socketpermissive=1create}forpid=2670comm="wicd"scontext=system_u:system_r:NetworkManager_t:s0tcontext=system_u:system_r:NetworkManager_t:s0tclass=ax25_socketpermissive=1ioctl}forpid=2670comm="wicd"path="socket:[52681]"dev="sockfs"ino=52681ioctlcmd=0x8b01scontext=system_u:system_r:NetworkManager_t:s0tcontext=system_u:system_r:NetworkManager_t:s0tclass=ax25_socketpermissive=1ioctl}forpid=2670comm="wicd"path="socket:[52684]"dev="sockfs"ino=52684ioctlcmd=0x8b01scontext=system_u:system_r:NetworkManager_t:s0tcontext=system_u:system_r:NetworkManager_t:s0tclass=appletalk_socketpermissive=1setattr}forpid=2214comm="wicd"name="dhclient.conf.template"dev="dm-0"ino=437068scontext=system_u:system_r:NetworkManager_t:s0tcontext=unconfined_u:object_r:etc_t:s0tclass=filepermissive=0注意:上下文NetworkManager_t和etc_t所涉及的一些元素需要权限 , 需要访问不同的文件和套接字创建 。