对CentOS和Red Hat上的SELinux问题进行故障排除

时间:2020-01-09 10:43:53  来源:igfitidea点击:

说明

禁用SELinux而不是进行故障排除和了解阻止某些内容的原因,这将删除系统安全性的关键部分。但是,如何解决并发现SELinux阻塞应用程序的根本原因呢?使用名为setroubleshoot的工具。

Setroubleshoot用简单的英文解释了为什么阻止脚本或者应用程序执行。该工具还会为我们提供有关解决问题的建议,其中可能涉及运行简单命令。

审核SELinux审核日志

我们可以通过打开SELinux生成的审核日志来调查SELinux问题,而无需任何工具。该日志位于/var/log/audit/audit.log。但是,除非我们确切知道要寻​​找什么并且有很多空闲时间,否则我们将很难理解日志。

图1 SELinux审核日志

所以你会怎么做?好吧,这就是两个非常重要的软件包,称为Setools和Setroubleshoot的地方。他们会将我们在日志中看到的gobbledegook转换为更有意义的东西。

安装Setools和疑难解答

  • 使用授予管理权限的帐户登录到服务器或者台式机。
  • 打开命令外壳。
  • 使用Yum安装setroubleshoot软件包。
yum install setroubleshoot setools

SELinux警报

现在,我们有了一个称为Sealert的工具,该工具可以分析SELinux使用的审核日志。 Sealert将扫描日志文件并报告,然后生成包含所有发现的SELinux问题的报告。

要从命令行运行Sealert,我们需要将其指向SELinux审核日志。

sealert -a /var/log/audit/audit.log

每个报告将描述每个问题并说明如何解决它们。 Sealert生成的输出示例如下所示。我将其截短以使报告更易于阅读。

100% donefound 1 alerts in /var/log/audit/audit.log
--------------------------------------------------------------------------------

SELinux is preventing /usr/sbin/httpd from getattr access on the file /myapps/app1/html/index.html.

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

If you want to allow httpd to have getattr access on the index.html file
Then you need to change the label on /myapps/app1/html/index.html
Do
# semanage fcontext -a -t FILE_TYPE '/myapps/app1/html/index.html'
where FILE_TYPE is one of the following: sssd_var_lib_t, public_content_t, anon_inodefs_t,

[..truncated..]

httpd_sys_ra_content_t, httpd_sys_rw_content_t, httpd_sys_rw_content_t, httpd_w3c_validator_content_t.
Then execute:
restorecon -v '/myapps/app1/html/index.html'

*****  Plugin catchall (17.1 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:
# grep httpd /var/log/audit/audit.log | audit2allow -M mypol
# semodule -i mypol.pp

报告的最重要部分位于每个警报的末尾。此处说明了如何解决问题。例如,上面的报告显示了以下解决方案。

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:
# grep httpd /var/log/audit/audit.log | audit2allow -M mypol
# semodule -i mypol.pp

给定的解决方案将创建SELinux策略,然后将其应用于有问题的文件,在我的示例中,该文件是为HTML文件分配了错误的SELinux文件上下文。

我们可以立即确定原因是Apache Web服务器(usr / sbin / httpd)试图访问名为index.html的文件。我们可以获得关于SELinux为何阻止Apache访问index.html的非常详细的摘要。

为了简单快速地获得解决方案,以解决允许Apache访问index.html文件的问题。

我们希望Apache(httpd)对index.html文件具有getattr访问权限,以便在用户访问我们的网站时将其提供给用户。提供给我们的解决方案说明我们需要执行以下操作:

  • 通过运行以下命令为其创建策略,将适用于Apache的SELinux文件上下文(httpd_sys_content_t)应用于index.html文件:
semanage fcontext -a -t httpd_sys_content_t '/webapps/app1/html/index.html'
  • 创建了策略后,我们现在可以随时使用restorecon命令将其应用于文件。
restorecon -v '/mywebapps/app1/html/index.html'