自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解SELinux故障排查和陷阱規(guī)避

譯文
運(yùn)維 服務(wù)器運(yùn)維
為SELinux排查故障可能很難,但是如果您了解服務(wù)的組件,就能應(yīng)對(duì)它帶來(lái)的任何難題。

[[379152]]

【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è)命令:

  1. # getenforce 
  2. Enforcing 
  3. # sestatus 
  4. SELinux status:                 enabled 
  5. SELinuxfs mount:                /sys/fs/selinux 
  6. SELinux root directory:         /etc/selinux 
  7. Loaded policy name:             targeted 
  8. Current mode:                   enforcing 
  9. Mode from config file:          enforcing 
  10. Policy MLS status:              enabled 
  11. Policy deny_unknown status:     allowed 
  12. Memory protection checking:     actual (secure) 
  13. Max kernel policy version:      32 

最佳實(shí)踐告訴我們,我們測(cè)試一個(gè)新的第三方應(yīng)用程序時(shí),應(yīng)該在許可模式下臨時(shí)配置SELinux,以便確定哪些策略或布爾值(更改行為的簡(jiǎn)單字符串)必不可少。運(yùn)行該命令:

  1. # setenforce 0 

查看日志,您可以找到SELinux使應(yīng)用程序正常運(yùn)行所需的條件。

SELinux試圖告訴我什么?

SELinux中生成警報(bào)的錯(cuò)誤只有四個(gè)主要原因:

  1. 標(biāo)簽。
  2. SELinux需要知道。
  3. SELinux策略及/或應(yīng)用程序可能有錯(cuò)誤。
  4. 您的信息可能被泄露。

最后一種情況是由于對(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)行:

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

如果您知道擁有相等標(biāo)簽的文件,請(qǐng)運(yùn)行:

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

針對(duì)這兩種情況,恢復(fù)文件的默認(rèn)上下文:

  1. # restorecon -vR /srv/myweb 

標(biāo)簽問(wèn)題:如果某個(gè)文件被移動(dòng)而不是被拷貝,它保留原始標(biāo)簽。

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

解決方案:正確標(biāo)記文件。

將上下文更換成正確的標(biāo)簽:

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

更換擁有參考標(biāo)簽的上下文:

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

針對(duì)這兩種情況,恢復(fù)文件的默認(rèn)上下文:

  1. # restorecon -vR /var/www/html/ 

SELinux需要知道

服務(wù)定制:Web服務(wù)器將在端口8585上偵聽(tīng)請(qǐng)求。

要把所需的端口添加到上下文,請(qǐng)運(yùn)行:

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

為服務(wù)添加功能:Web服務(wù)器將能夠發(fā)送電子郵件。

要啟用郵件發(fā)送功能,開(kāi)啟布爾值,運(yùn)行:

  1. # setsebool -P httpd_can_sendmail 1 

-P標(biāo)志使變更在布爾值中具有持久性。

要獲得所有布爾值,運(yùn)行:

  1. # getsebool -a 

要檢查布爾值的狀態(tài),運(yùn)行:

  1. # semanage boolean -l 

策略故障排查

服務(wù)未運(yùn)行:使用wicd而不是NetworkManager服務(wù)來(lái)處理無(wú)線連接。

檢查audit.log文件,查找被拒絕的訪問(wèn):

  1. # grep denied audit.log | cut -d{ -f2 | sort -n | uniq -u 
  2.  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 
  3.  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 
  4.  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 
  5.  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 
  6.  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)限:

  1. # vi my_wicd.te 
  2. module my_wicd 1.0; 
  3.   
  4. require { 
  5.         type NetworkManager_t; 
  6.         type etc_t; 
  7.         class ipx_socket create
  8.         class ax25_socket { create ioctl }; 
  9.         class appletalk_socket { create ioctl }; 
  10.         class file setattr; 
  11.   
  12. #============= NetworkManager_t ============== 
  13. allow NetworkManager_t etc_t:file setattr; 
  14. allow NetworkManager_t self:appletalk_socket { create ioctl }; 
  15. allow NetworkManager_t self:ax25_socket { create ioctl }; 
  16. allow NetworkManager_t self:ipx_socket create

要編譯策略,安裝軟件包selinux-policy-devel,并生成策略軟件包:

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

要激活剛生成的模塊,運(yùn)行:

  1. # 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)行:

  1. # journalctl -t setroubleshoot --since today 
  2. Dec 08 13:08:33 lab.example.com setroubleshoot[12013]: failed to retrieve rpm info for /var/www/html/index.html 
  3. 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 
  4. 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. 
  5.                                                                 
  6.  *****  Plugin restorecon (99.5 confidence) suggests   ************************ 
  7.                                                                 
  8.  If you want to fix the label 
  9.  /var/www/html/index.html default label should be httpd_sys_content_t. 
  10.  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. 
  11.  Do 
  12.  # /sbin/restorecon -v /var/www/html/index.html 
  13.                                                                 
  14.  *****  Plugin catchall (1.49 confidence) suggests   ************************** 
  15.                                                                 
  16.  If you believe that httpd should be allowed getattr access on the index.html file by default
  17.  Then you should report this as a bug. 
  18.  You can generate a local policy module to allow this access. 
  19.  Do 
  20.  allow this access for now by executing: 
  21.  # ausearch -c 'httpd' --raw | audit2allow -M my-httpd 
  22.  # 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】

 

責(zé)任編輯:華軒 來(lái)源: 51CTO
相關(guān)推薦

2013-05-06 16:36:55

SELinuxSELinux故障

2010-09-27 13:25:39

無(wú)線信號(hào)

2025-03-26 05:00:00

AIprompt交互效果

2014-08-22 09:10:46

2013-08-26 14:18:12

SELinux

2010-08-30 19:51:08

DHCP故障

2013-04-10 13:52:23

2011-05-31 14:22:23

2011-05-12 12:34:26

無(wú)線交換機(jī)交換機(jī)

2009-08-06 10:36:10

VMware上網(wǎng)設(shè)置VMware故障排查

2019-12-09 10:40:15

YAMLBashKubernetes

2021-09-26 19:39:58

MogDB故障數(shù)據(jù)庫(kù)

2010-10-14 13:55:24

無(wú)線故障排查

2022-04-18 09:07:54

Linux網(wǎng)絡(luò)延遲

2010-04-19 15:38:18

無(wú)線交換機(jī)故障

2015-10-09 16:42:16

GDB 排查Python程序故障

2017-03-24 09:50:00

2013-02-28 10:15:14

Ubuntu性能調(diào)優(yōu)故障排查

2024-12-04 16:44:51

2018-11-26 08:40:43

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)