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

SELinux 入门教程

[日期:2013-04-08] 来源:linuxtoy.org  作者:黑日白月 [字体: ]

Apache SELinux 配置实例

1. 让 Apache 可以访问位于非默认目录下的网站文件

首先,用 semanage fcontext -l | grep '/var/www' 获知默认 /var/www 目录的 SELinux 上下文:

/var/www(/.*)? all files system_u:object_r:httpd_sys_content_t:s0

从中可以看到 Apache 只能访问包含 httpd_sys_content_t 标签的文件。

假设希望 Apache 使用 /srv/www 作为网站文件目录,那么就需要给这个目录下的文件增加 httpd_sys_content_t 标签,分两步实现。

首先为 /srv/www 这个目录下的文件添加默认标签类型:semanage fcontext -a -t httpd_sys_content_t '/srv/www(/.*)?' 然后用新的标签类型标注已有文件:restorecon -Rv /srv/www 之后 Apache 就可以使用该目录下的文件构建网站了。

其中 restorecon 在 SELinux 管理中很常见,起到恢复文件默认标签的作用。比如当从用户主目录下将某个文件复制到 Apache 网站目录下时,Apache 默认是无法访问,因为用户主目录的下的文件标签是 user_home_t。此时就需要 restorecon 将其恢复为可被 Apache 访问的 httpd_sys_content_t 类型:

restorecon -v /srv/www/foo.com/html/file.html

restorecon reset /srv/www/foo.com/html/file.html context unconfined_u:object_r:user_home_t:s0->system_u:object_r:httpd_sys_content_t:s0

2. 让 Apache 侦听非标准端口

默认情况下 Apache 只侦听 80 和 443 两个端口,若是直接指定其侦听 888 端口的话,会在 service httpd restart 的时候报错:

Starting httpd: (13)Permission denied: make_sock: could not bind to address [::]:888

(13)Permission denied: make_sock: could not bind to address 0.0.0.0:888

no listening sockets available, shutting down

Unable to open logs

这个时候,若是在桌面环境下 SELinux 故障排除工具应该已经弹出来报错了。若是在终端下,可以通过查看 /var/log/messages 日志然后用 sealert -l加编号的方式查看,或者直接使用 sealert -b 浏览。无论哪种方式,内容和以下会比较类似:

SELinux is preventing /usr/sbin/httpd from name_bind access on the tcp_socket port 888.

***** Plugin bind_ports (92.2 confidence) suggests *************************

If you want to allow /usr/sbin/httpd to bind to network port 888

Then you need to modify the port type.

Do

# semanage port -a -t PORT_TYPE -p tcp 888

`where PORT_TYPE is one of the following: ntop_port_t, http_cache_port_t, http_port_t.` 

***** Plugin catchall_boolean (7.83 confidence) suggests *******************

If you want to allow system to run with NIS

Then you must tell SELinux about this by enabling the 'allow_ypbind' boolean.

Do

setsebool -P allow_ypbind 1

***** Plugin catchall (1.41 confidence) suggests ***************************

If you believe that httpd should be allowed name_bind access on the port 888 tcp_socket 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

可以看出 SELinux 根据三种不同情况分别给出了对应的解决方法。在这里,第一种情况是我们想要的,于是按照其建议输入:

semanage port -a -t http_port_t -p tcp 888

之后再次启动 Apache 服务就不会有问题了。

这里又可以见到 semanage 这个 SELinux 管理配置工具。它第一个选项代表要更改的类型,然后紧跟所要进行操作。详细内容参考 Man 手册

3. 允许 Apache 访问创建私人网站

若是希望用户可以通过在 ~/public_html/ 放置文件的方式创建自己的个人网站的话,那么需要在 Apache 策略中允许该操作执行。使用:

setsebool httpd_enable_homedirs 1

setsebool 是用来切换由布尔值控制的 SELinux 策略的,当前布尔值策略的状态可以通过 getsebool 来获知。

默认情况下 setsebool 的设置只保留到下一次重启之前,若是想永久生效的话,需要添加 -P 参数,比如:

setsebool -P httpd_enable_homedirs 1

总结

希望通过这一个简短的教程,扫除您对 SELinux 的误解甚至恐惧,个人感觉它并不比 iptables 策略复杂。如果希望您的服务器能有效抵挡 0-day 攻击时,那么 SELinux 或许就是一个值得考虑的缓和方案。

致谢

本文大量参考自 Vincent Danen 发表在 TechRepublic 上的 SELinux 系列教程123。在此向 Vincent Danen 致敬。

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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