曲径通幽论坛

标题: 非约束进程(Unconfined Processes) [打印本页]

作者: beyes    时间: 2013-6-21 13:48
标题: 非约束进程(Unconfined Processes)
在《
被约束的进程(Confined Processes)》里谈过约束进程的情况,这里讨论一下其对立面 --- 非约束进程。


非约束进程运行在一个非约束域中,比如 init 这个程序运行非约束的 initrc_t 域里,非约束的内核进程运行在 kernel_t 域中,非约束的 Linux 用户运行在 unconfined_t 域里。运行在非约束域中的进程会退回到使用 DAC 规则。如果一个非约束进程被侵入,SELinux 就不能阻止攻击者从中扩大访问系统资源与数据的可能,当然,DAC 规则自身的限制仍然适用。SELinux 只是一个在 DAC 规则之上的一个增强型安全策略,它不能用来取代 DAC 规则。


下面的例子将描述在处于非约束状况下, Apache HTTP 服务器是如何可以访问原本是被 Samba 服务的数据的(在默认的 SELinux 策略下这会被阻止)。


1. 使用 sestatus 确认一下 SELinux 是否启动,是否运行于 enforcing 规则,是否使用了定向策略(targeted policy) :
[root@groad.net ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   enforcing
Mode from config file:          enforcing
Policy version:                 24
Policy from config file:        targeted
由输出可见,上述条件均已满足。

2. 切换到 root 用户,然后在 /var/www/html 目录下建立一个 index.html 的文件,并且使用 ls 的 -Z 选项来查看该文件的 SELinux 上下文:
[root@groad.net ~]# echo "hello groad.net" > /var/www/html/index.html
[root@groad.net ~]# ls -Z /var/www/html/index.html
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/index.html

3. 用 chcon 命令修改一下 index.html 这个文件的类型为 samba_share_t
[root@groad.net ~]# chcon -t samba_share_t /var/www/html/index.html

[root@groad.net ~]# ls -Z /var/www/html/index.html
-rw-r--r--. root root unconfined_u:object_r:samba_share_t:s0 /var/www/html/index.html
这时用浏览器浏览该文件,可以看到出现拒绝访问的信息:
Forbidden


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


也就是说,运行于 httpd_exec_t 域的 httpd 无权访问 samba_share_t 类型的文件。


5. 接下来,暂时将 httpd 服务停下来:
[root@groad.net ~]# service httpd stop
Stopping httpd:                                            [  OK  ]

6. 接着,我们打算让 httpd 运行在 unconfiged_exec_t 域中,修改方法如下:
[root@groad.net ~]# chcon -t unconfined_exec_t /usr/sbin/httpd
[root@groad.net ~]# ls -Z /usr/sbin/httpd
-rwxr-xr-x. root root system_u:object_r:unconfined_exec_t:s0 /usr/sbin/httpd
修改成功。

7. 再次重启 httpd 服务:
[root@groad.net ~]# service httpd start
Starting httpd:                                            [  OK  ]

8. 使用 ps 命令的 -Z  选项观察已经运行的 httpd 进程的 SELinux 上下文:
[root@groad.net ~]# ps -eZ |grep httpd
unconfined_u:unconfined_r:unconfined_t:s0 10069 ? 00:00:00 httpd
unconfined_u:unconfined_r:unconfined_t:s0 10072 ? 00:00:00 httpd
unconfined_u:unconfined_r:unconfined_t:s0 10073 ? 00:00:00 httpd
unconfined_u:unconfined_r:unconfined_t:s0 10074 ? 00:00:00 httpd
unconfined_u:unconfined_r:unconfined_t:s0 10075 ? 00:00:00 httpd
unconfined_u:unconfined_r:unconfined_t:s0 10076 ? 00:00:00 httpd
unconfined_u:unconfined_r:unconfined_t:s0 10077 ? 00:00:00 httpd
unconfined_u:unconfined_r:unconfined_t:s0 10078 ? 00:00:00 httpd
unconfined_u:unconfined_r:unconfined_t:s0 10079 ? 00:00:00 httpd

9. 再次启动 httpd 服务,然后再在浏览器浏览一下该页面,这时会发现已经可以正常浏览页面。

从上面的测试可以看到,httpd 运行在 httpd_t 域的时候,是不能访问标记有 samba_share_t 类型的文件的;但是将 httpd 的域改为 unconfined_t 后,httpd 就会退回使用 DAC 这一传统的权限机制,于是它可以浏览上述的 index.html 文件。

10. 为了做上面的实验,我们手动改动了 httpd 的 SELinux 上下文。如果我们想对其复原到原来的默认状态,可以使用 restorecon 命令:
[root@groad.net ~]# restorecon -v /usr/sbin/httpd
restorecon reset /usr/sbin/httpd context system_u:object_r:unconfined_exec_t:s0->system_u:object_r:httpd_exec_t:s0
验证一下是否复原成功:
[root@groad.net ~]# ls -Z /usr/sbin/httpd
-rwxr-xr-x. root root system_u:object_r:httpd_exec_t:s0 /usr/sbin/httpd

此时需要重新启动一下 httpd 服务:
[root@groad.net ~]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

再用 ps 命令观察一下进程的上下文:
[root@groad.net ~]# ps -eZ |grep httpd
unconfined_u:system_r:httpd_t:s0 10318 ?       00:00:00 httpd
unconfined_u:system_r:httpd_t:s0 10322 ?       00:00:00 httpd
unconfined_u:system_r:httpd_t:s0 10323 ?       00:00:00 httpd
unconfined_u:system_r:httpd_t:s0 10326 ?       00:00:00 httpd
unconfined_u:system_r:httpd_t:s0 10327 ?       00:00:00 httpd
unconfined_u:system_r:httpd_t:s0 10328 ?       00:00:00 httpd
unconfined_u:system_r:httpd_t:s0 10329 ?       00:00:00 httpd
unconfined_u:system_r:httpd_t:s0 10330 ?       00:00:00 httpd
unconfined_u:system_r:httpd_t:s0 10333 ?       00:00:00 httpd
由上可见,httpd 重新运行在 httpd_t 域中。

最后再次在浏览器中浏览一下 index.html ,你会看到再次被拒绝。




欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2