SELinux 基础

首先,快速浏览一下三种不同的 SELinux 模式。
SELinux 可以处于强制、许可或者禁用模式。

  • Enforcing(强制):

这是默认设置。
在强制模式下,如果系统上发生违反定义策略的事情,该操作将被阻止并记录。

  • Permissive(允许):

这种模式实际上不会阻止或者拒绝任何事情的发生,但是它会记录在强制模式下通常会被阻止的任何事情。
如果我们可能想测试一个从未使用过 SELinux 的 Linux 系统并且想了解我们可能遇到的任何问题,那么这是一个很好的模式。
在许可模式和强制模式之间切换时不需要重新启动系统。

  • Disabled(禁用):

Disabled 完全关闭,根本不记录任何内容。
为了切换到禁用模式,需要重新启动系统。
此外,如果我们从禁用模式切换到许可或者强制模式,还需要重新启动系统。

查看当前 SELinux 状态

如前所述,CentOS/RHEL 默认在强制模式下使用 SELinux,我们可以通过几种方法来检查和确认这一点。
我最喜欢的是“getenforce”和“sestatus”命令。

[jack@onitroad ~]# getenforce
Enforcing
[jack@onitroad ~]# 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
Max kernel policy version:      28

如上所示,这两个都表明我们目前处于强制模式。

更改 SELinux 模式

我们还可以通过多种方式更改 SELinux 的模式,包括仅运行时选项或者在重启时持续存在的永久设置。

SELinux 运行时配置

在强制模式和许可模式之间切换的最快方法之一是使用“setenforce”命令。
我们可以使用“setenforce 0”切换到许可模式,或者使用“setenforce 1”切换到强制模式。

[jack@onitroad ~]# getenforce
Enforcing
[jack@onitroad ~]# setenforce 0
[jack@onitroad ~]# getenforce
Permissive
[jack@onitroad ~]# setenforce 1
[jack@onitroad ~]# getenforce
Enforcing

请注意,这只会更改运行时设置,如果我们执行系统重启,存储在 /etc/selinux/config 文件中的选项将在下次启动时使用。
我们不能在运行时禁用 selinux,因为切换到禁用模式或者从禁用模式切换需要系统重新启动。

SELinux 持久化配置

我们可以编辑 /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 three 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 强制模式时,系统有问题,我们可以排除故障并调查问题以尝试修复它,这比将SELinux直接禁用要好。
关闭 SELinux 应该被视为最后的手段。

首先使用“yum”安装 setroubleshoot-server 包。

[jack@onitroad ~]# yum install setroubleshoot-server -y

有了这个包,我们得到了“sealert”命令,这我们发现任何问题以及显示修复问题的推荐方法。

在本例中,我在 /root 目录中创建了一个 index.html 文件,然后将其移动到 /var/www/html 以便 Apache 提供服务。

[jack@onitroad ~]# vim index.html
[jack@onitroad ~]# mv index.html /var/www/html/

但是,当我尝试在 Firefox 中查看索引文件时,没有显示 index.html 页面内容,并且在 /var/log/messages 文件中出现以下错误。

Aug 28 00:15:51 localhost setroubleshoot: SELinux is preventing /usr/sbin/httpd from getattr access on the file /var/www/html/index.html. For complete SELinux messages. run sealert -l 284cb2c9-1c2e-4708-a48d-415123f558aa
Aug 28 00:15:51 localhost python: SELinux is preventing /usr/sbin/httpd from getattr access on the file /var/www/html/index.html.#012#012*  Plugin restorecon (99.5 confidence) suggests   #012#012If you want to fix the label. #012/var/www/html/index.html default label should be httpd_sys_content_t.#012Then you can run restorecon.#012Do#012# /sbin/restorecon -v /var/www/html/index.html#012#012*  Plugin catchall (1.49 confidence) suggests   **#012#012If you believe that httpd should be allowed getattr access on the index.html file by default.#012Then you should report this as a bug.#012You can generate a local policy module to allow this access.#012Do#012allow this access for now by executing:#012# grep httpd /var/log/audit/audit.log | audit2allow -M mypol#012# semodule -i mypol.pp#012

这实质上是说 Apache 无法访问 index.html 文件,因为它具有不正确的 SELinux 上下文。
该文件的 SELinux 上下文如下所示,其中包含 'ls' 中的 -Z 选项。

[jack@onitroad ~]# ls -laZ /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 .
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 ..
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 index.html

由于此文件是在 /root 目录中创建的,因此它具有“admin_home_t”的 SELinux 上下文,默认情况下,Apache 将仅提供具有“httpd_sys_content_t”上下文的文件。
日志表明,这可以通过运行 restorecon 命令来修复,该命令将修复文件的 SELinux 上下文,果然如此,页面现在可以正确加载。

[jack@onitroad ~]# restorecon -v /var/www/html/index.html
restorecon reset /var/www/html/index.html context unconfined_u:object_r:admin_home_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

进一步的信息也记录在 /var/log/audit/audit.log 文件中,但是内容不是很容易阅读。
这是 'sealert' 命令派上用场的地方。

[jack@onitroad ~]# sealert -a /var/log/audit/audit.log
-------------------------------------------------------------------------------
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.
Do
# /sbin/restorecon -v /var/www/html/index.html

-a 将显示所有警报,但它也可用于查看 /var/log/messages 文件中可能提供的特定代码。

如何在 CentOS/RHEL 7 中启用或者禁用 SELinux

安全增强型 Linux (SELinux) 在基于 CentOS/RHEL 的 Linux 操作系统中默认启用并以强制模式运行,并且有充分的理由提高整体系统安全性。

尽管如此,有时我们可能想要暂时或者永久禁用 SELinux。

日期:2020-07-07 20:55:07 来源:oir作者:oir