当前位置: 首页 > 科技观察

SELinux故障排除和陷阱详解

时间:2023-03-12 09:57:39 科技观察

【.com快译】维护我们服务器的安全是一项艰巨的工作,尤其是在与第三方解决方案提供商打交道时。在许多情况下,挑战在于要求禁用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→web服务器上的httpd_t和httpd_port_t,进程在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/解决方案:正确标记文件。用正确的标签替换上下文:#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="套接字:[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="套接字:[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"开发="dm-0"ino=437068scontext=system_u:system_r:NetworkManager_t:s0tcontext=unconfined_u:object_r:etc_t:s0tclass=filepermissive=0注意:上下文NetworkManager_t和etc_t中涉及的一些元素需要权限,需要访问不同的文件和集合套接字创建创建一个具有策略必要权限的类型强制(.te)文件:#vimy_wicd.temodulemy_wicd1.0;require{typeNetworkManager_t;typeetc_t;classipx_socketcreate;classax25_socket{createioctl};classappletalk_socket{createioctl};classfilesetattr;}#=============NetworkManager_t===============allowNetworkManager_tetc_t:filesetattr;allowNetworkManager_tself:appletalk_socket{createioctl};allowNetworkManager_tself:ax25_socket{createioctl};allowNetworkManager_tself:ipx_socketcreate;to编译策略,安装包selinux-policy-devel,并生成策略包:#make-f/usr/share/selinux/devel/Makefilemy_wicd.pp要激活刚刚生成的模块,运行:#semodule-imy_wicd.pp策略错误r消息:我在尝试访问我的网站时在日志中看到SELinux错误。对SELinux错误消息进行故障排除时的常见陷阱之一是根据找到的所有错误消息创建策略。在大多数情况下,如果安装了setroubleshoot包,同样的提示会给出所有可能的解决方法,从最好到最坏列出。要查看今天生成的setroubleshoot提示,运行:#journalctl-tsetroubleshoot--sincetodayDec0813:08:33lab.example.comsetroubleshoot[12013]:failedtoretrieverpminfofor/var/www/html/index.htmlDec0813:08:31lab20troubleshoot.]:SELinux正在阻止/usr/sbin/httpdfromgetattraccessonthefile/var/www/html/index.html.ForcompleteSELinuxmessagesrun:sealert-l011df984-4eb6-4079-98ab-cba173c4342eDec0813:08:34lab.example.comsetroubleshoot[12013]:SELinuxrisventing/httpdfromgetattraccessonthefile/var/www/html/index.html.*****Pluginrestorecon(99.5confidence)建议************************如果你想修复标签/var/www/html/index.htmldefaultlabelshouldbehttpd_sys_content_t.Thenyoucanrunrestorecon.Theaccessattemptmayhavebeenstopdedduetoinsufficientpermissionstoaccessaparentdirectory,inwhichcasetrytochangethefollowingcommandaccordingly.Do#/sbin/restorecon-v/var/www/html/index.html*****Plugincatchall(1.49confidence)建议**************************如果你相信thttpdshouldbeallowedgetattaccessontheindex.htmlfilebydefault.Thenyoushouldreportthisasabug.Youcangeneratealocalpolicymoduletoallowthisaccess.Doallowthisaccessfornowbyexecuting:#ausearch-c'httpd'--raw|audit2allow-Mmy-httpd#semodule-X300-imy-httpd.pp在这里,最好的解决方案是修复文件的标签摘要SELinux可能很困难,但如果您应用此处介绍的几个概念并了解服务的组成部分,您就可以应对它提出的各种挑战。请记住:SELinux是一个标签系统。原标题:SELinux故障排除与陷阱,作者:AlexCallejas