2016-03-01 40 views
0

我想告诉,有没有真正的问题,一切都会按我希望开始,但我遇到一个奇怪的行为,我无法解释,所以寻找一些有识之士从更熟练的工程师。SELinux的fcontext

奇数行为与SELinux的如何应用于fcontext映射定义观察。

让我通过打印它适用于我的情况的SELinux fcontext政策启动:

[[email protected] 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的fcontext政策写入内部在/ 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我可以desifer我需要有什么目录结构。我们来为项目和结账创建一个目录。

[[email protected] /]# mkdir -p /var/www/html/sun 
[[email protected] /]# cd /var/www/html/sun 
[[email protected] sun]# svn co http://server/ . 

让我们来看看,如果我们有合适的fcontext应用:

[[email protected] sun]# cd /var/www/html/sun/public/wp-content 
[[email protected] 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,看看会发生什么:

[[email protected] wp-content]# restorecon -Rv /var/www/html/sun/ 
[[email protected] 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 /太阳和结账。

[[email protected] wp-content]# mkdir -p /var/www/sun 
[[email protected] wp-content]# cd /var/www/sun/ 
[[email protected] sun]# svn co http://server/ . 

让我们来看看,如果我们有合适的fcontext应用:

[[email protected] sun]# cd /var/www/sun/public/wp-content/ 
[[email protected] 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),让我们尝试恢复到默认值:

[[email protected] wp-content]# restorecon -Rv /var/www/sun 
...Output omitted 
[[email protected] 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 

使用的restorecon在/ var/WWW /太阳按预期工作,但是...难题是:

为什么在/ var/www/sun中svn co使用了非现有策略?该策略匹配fcontext但不适合这个位置:

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

除了指数。PHP有differect fcontext,但目录具有相同的:httpd_sys_rw_content_t

回答

2

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

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

在你的榜样,目录在/ var/WWW /阳光/公共继承httpd_sys_content_t在/ var/WWW下来。

通常情况下,一个目录下名为所创建内部在/ var/WWW /阳光/公共也将继承httpd_sys_content_t的wp-content

apache.if有一个文件名过渡规则,它说,任何所谓的目录,将已与创造“的wp-content” httpd_sys_content_t应该改为为httpd_sys_rw_content_t

在创建CentOS的7,安装SELinux的政策-devel的包,你可以看到在定义/usr/share/selinux/devel/include/contrib/apache.if

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

您可以测试理论如下:

[[email protected] /]# mkdir junk 
[[email protected] /]# ls -aldZ junk 
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 junk 
[[email protected] /]# chcon -t httpd_sys_content_t junk 
[[email protected] /]# ls -aldZ junk 
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 junk 
[[email protected] /]# cd junk 
[[email protected] junk]# mkdir wp-otherdir 
[[email protected] junk]# mkdir wp-content 
[[email protected] 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