詳解SELinux故障排查和陷阱規(guī)避
譯文【51CTO.com快譯】維護(hù)我們服務(wù)器的安全是一項(xiàng)艱巨的工作,與第三方解決方案提供商打交道時(shí)尤為如此。在許多情況下,面臨的挑戰(zhàn)是要求禁用SELinux,以便應(yīng)用程序可以順利運(yùn)行。幸好,這種情況越來(lái)越少了。在大多數(shù)情況下,一番分析足以找到正確的故障排查或解決方法。
SELinux是一個(gè)標(biāo)簽系統(tǒng),它告訴我們系統(tǒng)中的每個(gè)文件、目錄或?qū)ο蠖加袑?duì)應(yīng)的標(biāo)簽(Label)。策略控制這些元素之間的交互關(guān)系,內(nèi)核則執(zhí)行這些規(guī)則。
兩個(gè)最重要的概念是標(biāo)簽(文件、進(jìn)程和端口等)和類型強(qiáng)制(根據(jù)進(jìn)程的類型將進(jìn)程彼此隔離開(kāi)來(lái))。
標(biāo)簽使用的格式為:user:role:type:level(可選)。
要找出當(dāng)前配置,請(qǐng)運(yùn)行g(shù)etenforce和sestatus兩個(gè)命令:
- # getenforce
- Enforcing
- # sestatus
- SELinux status: enabled
- SELinuxfs mount: /sys/fs/selinux
- SELinux root directory: /etc/selinux
- Loaded policy name: targeted
- Current mode: enforcing
- Mode from config file: enforcing
- Policy MLS status: enabled
- Policy deny_unknown status: allowed
- Memory protection checking: actual (secure)
- Max kernel policy version: 32
最佳實(shí)踐告訴我們,我們測(cè)試一個(gè)新的第三方應(yīng)用程序時(shí),應(yīng)該在許可模式下臨時(shí)配置SELinux,以便確定哪些策略或布爾值(更改行為的簡(jiǎn)單字符串)必不可少。運(yùn)行該命令:
- # setenforce 0
查看日志,您可以找到SELinux使應(yīng)用程序正常運(yùn)行所需的條件。
SELinux試圖告訴我什么?
SELinux中生成警報(bào)的錯(cuò)誤只有四個(gè)主要原因:
- 標(biāo)簽。
- SELinux需要知道。
- SELinux策略及/或應(yīng)用程序可能有錯(cuò)誤。
- 您的信息可能被泄露。
最后一種情況是由于對(duì)攻擊漏洞進(jìn)行了修改或避免了活動(dòng)跟蹤,不過(guò)在這兩種情況下,都必須查看這些警報(bào),這點(diǎn)暫且不介紹。
標(biāo)簽
標(biāo)簽問(wèn)題:/srv/myweb中的文件未正確標(biāo)記,因而無(wú)法訪問(wèn)。
SELinux為同一服務(wù)所涉及的每個(gè)元素分配一個(gè)標(biāo)簽:
- 二進(jìn)制文件:/usr/sbin/httpd→httpd_exec_t
- 配置文件:/etc/httpd→httpd_config_t
- 日志文件:/var/log/httpd→httpd_log_t
- 內(nèi)容目錄:/var/www/html→httpd_sys_content_t
- 啟動(dòng)腳本:/usr/lib/systemd/system/httpd.service→httpd_unit_file_t
- 進(jìn)程:/usr/sbin/httpd→httpd_t
- 端口:80/tcp,443/tcp→httpd_t和httpd_port_t
在Web服務(wù)器上,在httpd_t上下文中運(yùn)行的進(jìn)程只能與帶有httpd_something_t標(biāo)簽的對(duì)象進(jìn)行交互。
解決方案:正確標(biāo)記文件。
如果您知道正確的標(biāo)簽,請(qǐng)運(yùn)行:
- # semanage fcontext -a -t httpd_sys_content_t '/srv/myweb(/.*)?'
如果您知道擁有相等標(biāo)簽的文件,請(qǐng)運(yùn)行:
- # semanage fcontext -a -e /srv/myweb /var/www
針對(duì)這兩種情況,恢復(fù)文件的默認(rèn)上下文:
- # restorecon -vR /srv/myweb
標(biāo)簽問(wèn)題:如果某個(gè)文件被移動(dòng)而不是被拷貝,它保留原始標(biāo)簽。
- $ mv index.html /var/www/html/
解決方案:正確標(biāo)記文件。
將上下文更換成正確的標(biāo)簽:
- # chcon -t httpd_system_content_t /var/www/html/index.html
更換擁有參考標(biāo)簽的上下文:
- # chcon --reference /var/www/html/ /var/www/html/index.html
針對(duì)這兩種情況,恢復(fù)文件的默認(rèn)上下文:
- # restorecon -vR /var/www/html/
SELinux需要知道
服務(wù)定制:Web服務(wù)器將在端口8585上偵聽(tīng)請(qǐng)求。
要把所需的端口添加到上下文,請(qǐng)運(yùn)行:
- # semanage port -a -t http_port_t -p tcp 8585
為服務(wù)添加功能:Web服務(wù)器將能夠發(fā)送電子郵件。
要啟用郵件發(fā)送功能,開(kāi)啟布爾值,運(yùn)行:
- # setsebool -P httpd_can_sendmail 1
-P標(biāo)志使變更在布爾值中具有持久性。
要獲得所有布爾值,運(yùn)行:
- # getsebool -a
要檢查布爾值的狀態(tài),運(yùn)行:
- # semanage boolean -l
策略故障排查
服務(wù)未運(yùn)行:使用wicd而不是NetworkManager服務(wù)來(lái)處理無(wú)線連接。
檢查audit.log文件,查找被拒絕的訪問(wèn):
- # 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所涉及的一些元素需要權(quán)限,需要訪問(wèn)不同的文件和套接字創(chuàng)建。
創(chuàng)建type enforcement (.te)文件,擁有策略的必要權(quán)限:
- # vi my_wicd.te
- module my_wicd 1.0;
- require {
- type NetworkManager_t;
- type etc_t;
- class ipx_socket create;
- 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_socket create;
要編譯策略,安裝軟件包selinux-policy-devel,并生成策略軟件包:
- # make -f /usr/share/selinux/devel/Makefile my_wicd.pp
要激活剛生成的模塊,運(yùn)行:
- # semodule -i my_wicd.pp
策略錯(cuò)誤信息:試圖訪問(wèn)我的網(wǎng)站時(shí),我在日志中看到了SELinux錯(cuò)誤。
為SELinux錯(cuò)誤信息排查故障時(shí)常見(jiàn)的陷阱之一是,根據(jù)發(fā)現(xiàn)的所有錯(cuò)誤信息創(chuàng)建策略。在大多數(shù)情況下,如果setroubleshoot包已安裝,同樣的提醒會(huì)給出所有可能的變通選項(xiàng),按最好到最差的順序排列。
要查看今天生成的setroubleshoot提醒,運(yùn)行:
- # 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 want to 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 access on the index.html file by default.
- Then you should report this as a bug.
- You can generate a local policy module to allow this access.
- Do
- allow this access for now by executing:
- # ausearch -c 'httpd' --raw | audit2allow -M my-httpd
- # semodule -X 300 -i my-httpd.pp
在這里,最好的解決辦法就是修復(fù)文件的標(biāo)簽。
小結(jié)
為SELinux排查故障可能很難,但如果運(yùn)用這里介紹的幾個(gè)概念,并了解服務(wù)的組成,您就可以處理它帶來(lái)的各種難題。
切記:SELinux是一種標(biāo)簽系統(tǒng)。
原文標(biāo)題:SELinux troubleshooting and pitfalls,作者:Alex Callejas
【51CTO譯稿,合作站點(diǎn)轉(zhuǎn)載請(qǐng)注明原文譯者和出處為51CTO.com】