手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

在CentOS 6.X上折腾SELinux

[日期:2012-09-10] 来源:Linux社区  作者:liupengblog [字体:大 中 小]

[root@CentOS ~]# semanage fcontext -l
SELinux fcontext                                   type               Context

/                                                  directory          system_u:object_r:root_t:s0
/.*                                                all files          system_u:object_r:default_t:s0
/[^/]+                                             regular file       system_u:object_r:etc_runtime_t:s0
/\.autofsck                                        regular file       system_u:object_r:etc_runtime_t:s0
/\.autorelabel                                     regular file       system_u:object_r:etc_runtime_t:s0
/\.journal                                         all files          <>
/\.suspended                                       regular file       system_u:object_r:etc_runtime_t:s0
/a?quota\.(user|group)                             regular file       system_u:object_r:quota_db_t:s0
/afs                                               directory          system_u:object_r:mnt_t:s0
/bin                                               directory          system_u:object_r:bin_t:s0
/bin/.*                                            all files          system_u:object_r:bin_t:s0
/bin/alsaunmute                                    regular file       system_u:object_r:alsa_exec_t:s0
/bin/bash                                          regular file       system_u:object_r:shell_exec_t:s0
...............
增加一个目录的默认security context:

[root@centos ~]# semanage fcontext -a -t public_content_t "/srv/test(/.*)?"
上面这个命令将/srv/test的默认的security context的type设置为public_content_t

改变一个文件的security context:

[root@centos ~]# chcon -t var_t /var/www/html/index.html
[root@centos ~]# ll -Z /var/www/html/index.html
-rw-r--r--. root root unconfined_u:object_r:var_t:s0   /var/www/html/index.html
上面的命令将/var/www/html/index.html的type改为var_t

可以使用restorecon命令将档案恢复为所在目录的默认security context:
先查看一下/var/www的默认context是什么:

[root@centos ~]# semanage fcontext -l | grep /var/www
/var/www(/.*)?                                     all files          system_u:object_r:httpd_sys_content_t:s0
/var/www/[^/]*/cgi-bin(/.*)?                       all files          system_u:object_r:httpd_sys_script_exec_t:s0
/var/www/apcupsd/multimon\.cgi                     regular file       system_u:object_r:httpd_apcupsd_cgi_script_exec_t:s0
/var/www/apcupsd/upsfstats\.cgi                    regular file       system_u:object_r:httpd_apcupsd_cgi_script_exec_t:s0
.............
使用restorecon恢复/var/www的所有档案和目录为默认:

[root@centos ~]# restorecon -Rv /var/www
restorecon reset /var/www/html/index.html context unconfined_u:object_r:var_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
——————————————————————————————
selinux是提供日志文件来记录错误信息的,错误信息记录在/var/log/messages 和 /var/log/setroubleshoot/* 里头,需要重启auditd服务来开启selinux的log服务:

[root@centos ~]# /etc/init.d/auditd restart
查看selinux的错误信息:

[root@centos ~]# cat /var/log/messages | grep setroubleshoot
Aug  9 17:46:47 centos yum[6590]: Installed: setroubleshoot-plugins-3.0.40-1.el6.noarch
Aug  9 17:46:50 centos yum[6590]: Installed: setroubleshoot-server-3.0.47-3.el6_3.i686
Aug  9 17:46:54 centos yum[6590]: Installed: setroubleshoot-3.0.47-3.el6_3.i686
Aug  9 17:58:57 centos 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 c7a436a1-a114-4659-91a9-4155b1003dd7
Aug  9 17:58:58 centos 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 c7a436a1-a114-4659-91a9-4155b1003dd7
Aug  9 18:00:35 centos 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 c7a436a1-a114-4659-91a9-4155b1003dd7
Aug  9 18:00:36 centos 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 c7a436a1-a114-4659-91a9-4155b1003dd7
运行sealert查看具体解决方法:

[root@centos ~]# sealert -l c7a436a1-a114-4659-91a9-4155b1003dd7
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

*****  Plugin catchall (1.49 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

上面的信息已经给出了具体解决方法,就是使用restorecon将index.html恢复为默认。

——————————————————————————————
下面用httpd这个www服务简单实验一下:
没装httpd的就先装吧。。

[root@centos ~]# yum install httpd
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
 * base: mirrors.ta139.com
 * extras: mirrors.ta139.com
 * updates: mirrors.ta139.com
Setting up Install Process
Package httpd-2.2.15-15.el6.centos.1.i686 already installed and latest version
Nothing to do
启动httpd service:

[root@centos ~]# /etc/init.d/httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
查看系统是否启动了80端口监听:

[root@centos ~]# netstat -tupln | grep httpd
tcp        0      0 :::80                       :::*                        LISTEN      9587/httpd    
在root的家目录创建index.html文件:

[root@centos ~]# echo "Test for selinux" > index.html
查看它的context:

[root@centos ~]# ls -Z index.html
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 index.html
可以看到它的type为admin_home_t,是继承自/root这个目录的security context

把它copy到/var/www/html中:

[root@centos ~]# cp -a index.html  /var/www/html/
注意,我们使用的是-a这个option来copy,也就是我们保留了源文件的security context:

[root@centos ~]# ll -Z /var/www/html/index.html
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /var/www/html/index.html
使用links访问一下这个page:

[root@centos ~]# links http://localhost/index.html -dump
                                   Forbidden

   You don't have permission to access /index.html on this server.

   --------------------------------------------------------------------------

    Apache/2.2.15 (CentOS) Server at localhost Port 80
意料之中,出现permission deny了,这个时候是可以直接去看/var/log/messages来解决问题的。

我们这里先查看一下进程httpd的context:

[root@centos ~]# ps aux -Z | grep httpd
unconfined_u:system_r:httpd_t:s0 apache   9590  0.0  0.2  11804  2852 ?        S    10:38   0:00 /usr/sbin/httpd
再查看一下有没有允许httpd访问type为admin_home_t的文件:

[root@centos ~]# sesearch --all | grep "allow httpd_t admin_home_t"
ERROR: Cannot get avrules: Neverallow rules requested but not available
查询结果显示,没有这个rule,所以httpd进程被block。

查看有没有允许httpd访问type为httpd_sys_content_t的文件:

root@centos ~]# sesearch --all | grep "allow httpd_t httpd_sys_content_t"
ERROR: Cannot get avrules: Neverallow rules requested but not available
   allow httpd_t httpd_sys_content_t : file { ioctl read getattr lock open } ;
   allow httpd_t httpd_sys_content_t : dir { ioctl read getattr lock search open } ;
   allow httpd_t httpd_sys_content_t : lnk_file { read getattr } ;
   allow httpd_t httpd_sys_content_t : file { ioctl read getattr lock open } ;
   allow httpd_t httpd_sys_content_t : dir { ioctl read getattr lock search open } ;
   allow httpd_t httpd_sys_content_t : dir { ioctl read write getattr lock add_name remove_name search open } ;
   allow httpd_t httpd_sys_content_t : lnk_file { read getattr } ;
好了,恢复/var/www/html/index.html吧:

[root@centos ~]# restorecon -Rv /var/www/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
在访问一次:

[root@centos ~]# links http://localhost/index.html -dump
   Test for selinux
OK了。

linux
相关资讯       SELinux 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款