Linux15-SELinux

目录

一、查看和配置SELinux生效

二、SELinux上下文

2.1、查看SELinux上下文

2.2、semanage+restorecon命令更改SELinux上下文

三、SELinux布尔值

四、SELinux故障排除


Security Enhanced Linux(SELinux)是一个额外的系统安全层,是一种强制访问控制(MAC)的访问控制手段,基于策略的方式来保护系统中进程。

一、查看和配置SELinux生效

使用命令sestatus查看SELinux的配置情况,实际效果不如直接看配置文件。使用命令getenforce查看SELinux的情况。使用命令setenforce只能临时修改SELinux配置,也就是修改内存中SELinux的配置,重启后失效。selinux本质是一个内核模块,要让修改永久生效,那么就要修改配置文件/etc/selinux/config中的SELINUX参数为,然后重启生效。

SELinux的三种状态:

  1. enforcing,强制模式。默认没有明确的允许就是拒绝。提供保护,同时记录日志。
  2. permissive,许可模式。访问跟策略冲突时允许用户去执行,会把错误信息发送到日志。
  3. disabled,禁用模式。不提供保护,也不记录日志。

setenforce  0 permissive  |  1 enforcing

查看selinux的日志的方法:tail  /var/log/audit/audit.log  |  grep avc。过滤avc可以看到错误信息。

[root@server0 ~]# getenforce
Enforcing
[root@server0 ~]# cat /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

二、SELinux上下文

selinux具有多种上下文:用户、角色、类型、敏感度。默认的目标策略(SELINUXTYPE=targeted)会根据第三个上下文——类型上下文来制定自己的规则。类型上下文通常以 _t 结尾。比如Web服务器的类型上下文是httpd_t,位于/var/www/html中的文件和目录的类型上下文是httpd_sys_content_t,Web服务器端口的类型上下文是http_port_t  等等。如果更改了目录、监听端口,而新的目录、端口不具备相应的类型上下文,那么selinux就禁止访问,从禁止恶意用户访问。

2.1、查看SELinux上下文

每个端口、每个文件都有自己的类型上下文。查看方法有 ls  -lZ、ps  -Z。

selinux保护系统中的进程:

  1. 进程需要监听某个端口
  2. 访问进程自己的家目录
[root@server0 ~]# ps axZ | grep httpd
system_u:system_r:httpd_t:s0     5998 ?        Ss     0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0     5999 ?        S      0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0     6000 ?        S      0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0     6001 ?        S      0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0     6002 ?        S      0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0     6003 ?        S      0:00 /usr/sbin/httpd -DFOREGROUND

[root@server0 ~]# ll -dZ /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/

2.2、semanage+restorecon命令更改SELinux上下文

虽然修改端口和文件的上下文有两个命令,但是为了保证重启依旧生效,只推荐使用semanage+restorecon命令,另一个不记也不要使用!

列出和修改端口的上下文,semanage port的 -l列出 -a增加 -t指定上下文 -p指定协议和端口。

semanage port -l  # 列出端口上下文
semanage port -a -t http_port_t -p tcp 8888  # 端口8888增加web服务的端口上下文

列出和修改文件的上下文,和上面的选项意义一样。
注意(/.*)?正则表达式,表示“匹配后跟任意数量的字符”,它会匹配在表达式前面列出的目录并递归匹配该目录中的所有内容。
注意semanage只是改变了默认的设置,还要改一下已经生成的文件的文件域,用restorecon -Rv file,这样才永久生效。

semanage fcontext -l # 列出文件上下文

semanage fcontext -a -t httpd_sys_content_t '/data/my/custom/dir(/.*)?' # 自己定义的web服务目录/data/my/custom/dir增加web服务的文件上下文
restorecon -Rv /data/my/custom/dir # 还要改一下已经生成的文件的文件域才永久生效

举例,我们把apache的目录改为/virtual,那么配置好以后启动apache,访问网页,也会有如下的报错。配置好上下文以后访问才正常。

[root@server0 ~]# systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
   Active: active (running) since Sat 2021-03-27 14:43:19 CST; 54s ago
  Process: 24773 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
 Main PID: 24777 (httpd)
   Status: "Total requests: 5; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─24777 /usr/sbin/httpd -DFOREGROUND
           ├─24779 /usr/sbin/httpd -DFOREGROUND
           ├─24780 /usr/sbin/httpd -DFOREGROUND
           ├─24781 /usr/sbin/httpd -DFOREGROUND
           ├─24782 /usr/sbin/httpd -DFOREGROUND
           ├─24783 /usr/sbin/httpd -DFOREGROUND
           └─24790 /usr/sbin/httpd -DFOREGROUND

Mar 27 14:43:18 server0.example.com systemd[1]: Starting The Apache HTTP Server...
Mar 27 14:43:19 server0.example.com systemd[1]: Started The Apache HTTP Server.
Mar 27 14:43:25 server0.example.com python[24785]: SELinux is preventing /usr/sbin/httpd from getattr access on the file .

                                                   *****  Plugin catchall_labels (83.8 confidence) suggests   *******************...

[root@server0 ~]# semanage fcontext -a -t httpd_sys_content_t '/virtual(/.*)?'
[root@server0 ~]# restorecon -Rv /virtual/
restorecon reset /virtual context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /virtual/index.html context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
[root@server0 ~]# systemctl restart httpd

三、SELinux布尔值

SELinux布尔值是更改SELinux策略行为的开关。可以启用或禁用。selinux-policy-devel包提供了很多手册(man 8 查看),解释不同布尔值的含义。man -k selinux可以列出这些文档。

yum install selinux-policy-devel.noarch
mandb
man -k selinux

查看当前布尔值设置。

[root@server0 ~]# semanage boolean -l | grep httpd | grep home
httpd_enable_homedirs          (off  ,  off)  Allow httpd to read home directories

设置布尔值,-P永久生效。

[root@server0 ~]# setsebool httpd_enable_homedirs on
[root@server0 ~]# semanage boolean -l | grep httpd | grep home
httpd_enable_homedirs          (on   ,  off)  Allow httpd to read home directories

[root@server0 ~]# setsebool -P httpd_enable_homedirs on
[root@server0 ~]# semanage boolean -l | grep httpd | grep home
httpd_enable_homedirs          (on   ,   on)  Allow httpd to read home directories

四、SELinux故障排除

selinux默认的日志/var/log/audit/audit.log,过滤avc字样来看报错。从日志/var/log/message里也可以发现有用的消息。
安装setroubleshoot-server包,然后使用命令 sealert -a <log>查看selinux报错的详细内容。
或者知道一个selinux UUID(selinux的一个事件号),用sealsert -l <UUID>也可以查看有用的信息。

yum install setroubleshoot-server    # 装包
sealert -a /var/log/audit/audit.log  # 分析一个日志

less /var/log/messages # 从日志里找出UUID
Mar 27 03:23:35 localhost setroubleshoot: SELinux is preventing /usr/sbin/httpd from read access on the file . For complete SELinux messages. run sealert -l 36191ec4-827d-4018-b156-8924c3a992cc
sealert -l 36191ec4-827d-4018-b156-8924c3a992cc # 根据UUID查看详细信息
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苦行僧(csdn)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值