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

译文
运维 服务器运维
为SELinux排查故障可能很难,但是如果您了解服务的组件,就能应对它带来的任何难题。

[[379152]]

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

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

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

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

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

  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 

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

  1. # setenforce 0 

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

SELinux试图告诉我什么?

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

  1. 标签。
  2. SELinux需要知道。
  3. SELinux策略及/或应用程序可能有错误。
  4. 您的信息可能被泄露。

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

标签

标签问题:/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标签的对象进行交互。

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

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

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

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

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

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

  1. # restorecon -vR /srv/myweb 

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

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

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

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

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

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

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

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

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

SELinux需要知道

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

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

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

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

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

  1. # setsebool -P httpd_can_sendmail 1 

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

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

  1. # getsebool -a 

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

  1. # semanage boolean -l 

策略故障排查

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

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

  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所涉及的一些元素需要权限,需要访问不同的文件和套接字创建。

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

  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 

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

  1. # semodule -i my_wicd.pp 

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

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

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

  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 

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

小结

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

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

原文标题:SELinux troubleshooting and pitfalls,作者:Alex Callejas

【51CTO译稿,合作站点转载请注明原文译者和出处为51CTO.com】

 

责任编辑:华轩 来源: 51CTO
相关推荐

2013-05-06 16:36:55

SELinuxSELinux故障

2010-09-27 13:25:39

无线信号

2014-08-22 09:10:46

2013-08-26 14:18:12

SELinux

2013-04-10 13:52:23

2010-08-30 19:51:08

DHCP故障

2011-05-31 14:22:23

2009-08-06 10:36:10

VMware上网设置VMware故障排查

2011-05-12 12:34:26

无线交换机交换机

2010-10-14 13:55:24

无线故障排查

2022-04-18 09:07:54

Linux网络延迟

2021-09-26 19:39:58

MogDB故障数据库

2019-12-09 10:40:15

YAMLBashKubernetes

2010-04-19 15:38:18

无线交换机故障

2013-02-28 10:15:14

Ubuntu性能调优故障排查

2017-03-24 09:50:00

2015-10-09 16:42:16

GDB 排查Python程序故障

2022-11-08 07:40:55

2018-11-26 08:40:43

2009-08-18 14:57:40

服务器故障排查
点赞
收藏

51CTO技术栈公众号