【问题标题】:SELinux fcontext after svn checkoutsvn结帐后的SELinux fcontext
【发布时间】:2016-03-01 16:02:03
【问题描述】:

我想首先说明没有真正的问题,一切都按我预期的那样工作,但是我遇到了一种我无法解释的奇怪行为,因此请向更熟练的工程师寻求一些见解。

观察到 SELinux 如何应用 fcontext 映射定义的奇怪行为。

让我从打印适用于我的案例的 SELinux fcontext 策略开始:

[root@ip-10-0-0-40 wp-content]# semanage fcontext -l | grep "^/var/www.*httpd_sys_rw_content_t:s0\s$"
/var/www/svn(/.*)?                                 all files          system_u:object_r:httpd_sys_rw_content_t:s0
/var/www/html(/.*)?/uploads(/.*)?                  all files          system_u:object_r:httpd_sys_rw_content_t:s0
/var/www/html(/.*)?/wp-content(/.*)?               all files          system_u:object_r:httpd_sys_rw_content_t:s0
/var/www/html(/.*)?/sites/default/files(/.*)?      all files          system_u:object_r:httpd_sys_rw_content_t:s0
/var/www/html(/.*)?/sites/default/settings\.php    regular file       system_u:object_r:httpd_sys_rw_content_t:s0
/var/www/moodledata(/.*)?                          all files          system_u:object_r:httpd_sys_rw_content_t:s0
/var/www/moodle/data(/.*)?                         all files          system_u:object_r:httpd_sys_rw_content_t:s0
/var/www/gallery/albums(/.*)?                      all files          system_u:object_r:httpd_sys_rw_content_t:s0
/var/www/html/owncloud/data(/.*)?                  all files          system_u:object_r:httpd_sys_rw_content_t:s0
/var/www/html/configuration\.php                   all files          system_u:object_r:httpd_sys_rw_content_t:s0

从命令中可以看出,我对允许 httpd 在 /var/www 中写入的 fcontext 策略感兴趣。

我正在设置 WordPress 安装,所以我的目光锁定在这些政策上:

/var/www/html(/.*)?/uploads(/.*)?                  all files          system_u:object_r:httpd_sys_rw_content_t:s0
/var/www/html(/.*)?/wp-content(/.*)?               all files          system_u:object_r:httpd_sys_rw_content_t:s0

从策略 RegExp 中,我可以确定我需要什么样的目录结构。让我们为项目创建一个目录并结帐。

[root@ip-10-0-0-40 /]# mkdir -p /var/www/html/sun
[root@ip-10-0-0-40 /]# cd /var/www/html/sun
[root@ip-10-0-0-40 sun]# svn co http://server/ .

让我们检查一下我们是否应用了正确的 fcontext:

[root@ip-10-0-0-40 sun]# cd /var/www/html/sun/public/wp-content
[root@ip-10-0-0-40 wp-content]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 index.php
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 languages
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 mu-plugins
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 plugins
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 themes
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 uploads

太棒了!只是出于兴趣和仔细检查,让我们尝试恢复 fcontext 并看看会发生什么:

[root@ip-10-0-0-40 wp-content]# restorecon -Rv /var/www/html/sun/
[root@ip-10-0-0-40 wp-content]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 index.php
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 languages
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 mu-plugins
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 plugins
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 themes
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 uploads

太棒了!按预期工作。

为了完成测试,让我们模拟一个预期的失败。让我们在 /html/ 之外创建一个项目目录,如下所示:/var/www/sun 并结帐。

[root@ip-10-0-0-40 wp-content]# mkdir -p /var/www/sun
[root@ip-10-0-0-40 wp-content]# cd /var/www/sun/
[root@ip-10-0-0-40 sun]# svn co http://server/ .

让我们检查一下我们是否应用了正确的 fcontext:

[root@ip-10-0-0-40 sun]# cd /var/www/sun/public/wp-content/
[root@ip-10-0-0-40 wp-content]# ls –Z
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.php
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 languages
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 mu-plugins
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 plugins
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 themes
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 uploads

奇怪,我期待看到 httpd_sys_content_t(默认 fcontext),让我们尝试恢复默认:

[root@ip-10-0-0-40 wp-content]# restorecon -Rv /var/www/sun
...Output omitted
[root@ip-10-0-0-40 wp-content]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.php
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 languages
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 mu-plugins
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 plugins
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 themes
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 uploads

/var/www/sun 使用 restorecon 可以正常工作,但是……难题是:

为什么 /var/www/sun 里面的 svn co 使用了不存在的策略?策略匹配 fcontext 但不匹配此位置:

/var/www/html(/.*)?/wp-content(/.*)?               all files          system_u:object_r:httpd_sys_rw_content_t:s0

index.php 也有不同的 fcontext,但目录有相同的:httpd_sys_rw_content_t

【问题讨论】:

    标签: svn selinux


    【解决方案1】:

    我有同样的问题,“为什么 wp-content 目录会自动标记为 httpd_sys_rw_content_t 甚至在 /var/www/html 的 fcontext 规则之外?”。不过,我现在找到了答案,并想把它贴在这里。

    这是由 SELinux 的一项称为文件名转换的功能引起的,该功能是一种有助于创建正确标记的文件和目录的策略机制。

    在您的示例中,目录 /var/www/sun/public/var/www 继承 httpd_sys_content_t

    通常,在 /var/www/sun/public 中创建的名为 wp-content 的目录也将继承 httpd_sys_content_t

    但是在 apache.if 中有一个文件名转换规则,它说任何应该用 httpd_sys_content_t 创建的名为“wp-content”的目录应该被创建作为 httpd_sys_rw_content_t

    在CentOS 7中,安装selinux-policy-devel包,你可以在/usr/share/selinux/devel/include/contrib/apache.if中看到定义

    filetrans_pattern($1, httpd_sys_content_t, httpd_sys_rw_content_t, dir, "wp-content")
    

    您可以按如下方式测试该理论:

    [root@dt-laptop-centos7 /]# mkdir junk
    [root@dt-laptop-centos7 /]# ls -aldZ junk
    drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 junk
    [root@dt-laptop-centos7 /]# chcon -t httpd_sys_content_t junk
    [root@dt-laptop-centos7 /]# ls -aldZ junk
    drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 junk
    [root@dt-laptop-centos7 /]# cd junk
    [root@dt-laptop-centos7 junk]# mkdir wp-otherdir
    [root@dt-laptop-centos7 junk]# mkdir wp-content
    [root@dt-laptop-centos7 junk]# ls -alZ
    drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 .
    dr-xr-xr-x. root root system_u:object_r:root_t:s0      ..
    drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_rw_content_t:s0 wp-content
    drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 wp-otherdir
    

    您可以看到 wp-content 的上下文自动设置为 httpd_sys_rw_content_twp-otherdir 只是继承 httpd_sys_content_t强>

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-31
      相关资源
      最近更新 更多