svn checkout后的SELinux fcontext

时间:2016-03-01 16:02:04

标签: svn selinux

我首先要说的是没有真正的问题,一切都按照我的预期运作,但是我遇到了一个我无法解释的奇怪行为,所以寻找更多熟练工程师的一些见解。

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

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

我正在设置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 按预期工作,但是......难题是:

为什么svn co里面的/ var / www / sun使用了不存在的策略?策略匹配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

1 个答案:

答案 0 :(得分:2)

我有同样的问题,“为什么 wp-content 目录自动标有 httpd_sys_rw_content_t ,即使在 / var / www / html的fcontext规则之外也是如此?“但是,我现在找到了答案,并认为我会在这里发布。

这是由SELinux的一个名为Filename Transitions的功能引起的,它是一种帮助创建正确标记的文件和目录的策略机制。

在您的示例中,目录 / 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_t ,而 wp-otherdir 只是继承 httpd_sys_content_t