CentOS 系统中 Apache 的 SELinux 设置
引入
SELinux是Linux的扩展功能,用于设置每个进程对于哪些文件或目录可以进行什么样的操作(读取或写入)。
中国的本地化解释如下:
有两种策略来管理网络相关的守护程序(服务),分别是targeted策略和strict策略,其中strict策略充分利用了SELinux的功能。本节的解释是针对使用targeted策略的情况。通过查看sestatus命令的执行结果中的Policy from config file:项目,可以知道当前设置了哪种策略。
如果SELinux被禁用了的话
如果sestatus命令的执行结果显示SELinux状态为disabled,则表示SELinux已被禁用。
编辑配置文件,将其暂时更改为宽容模式。宽容模式是一种仅记录访问日志而不阻断访问的模式,即使存在策略违规情况。1
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - SELinux is fully disabled.
SELINUX=permissive
# ↑この項目を、disabledからpermissiveに変更する
# SELINUXTYPE= type of policy in use. Possible values are:
# targeted - Only targeted network daemons are protected.
# strict - Full SELinux protection.
SELINUXTYPE=targeted
在SELinux中,我们会给进程和文件加上一个名为SELinux安全上下文的标签,通过判断标签的类型来确定是否具有访问权限。然而,在SELinux禁用的情况下,标签会被删除,因此需要重新构建标签。你可以创建一个名为”.autorelabel”的空文件在根目录下,这样下次启动时将会进行标签的重新构建。
# 再起動時にラベルの再構築を行う
sudo touch /.autorelabel
# 再起動
sudo reboot
由于重新构建标签的操作系统启动需要一定时间,因此在实际运行环境中,请谨慎操作。
来自Web服务器进程的访问权限
只需在/etc/selinux/targeted/contexts/files/file_contexts文件中查找哪个标签适用于哪个位置。 这个文件已经包含了与Web服务器相关的标签设置,所以只需对Apache的文档根目录等位置执行restorecon命令即可。
# /var/wwwのラベルの貼り直し
sudo restorecon -R /var/www
如果你以前一直在运行Web服务器,并且将会话文件保存在/tmp目录以外的特定位置,那么我会协助你清理这些文件。
從違反日誌中制定新的規則
基本上来说,我认为这样做不会违反Apache的政策。由于在我的环境中发生了政策违反,我定义了新规则。
违反策略的日志可以通过执行”sudo grep avc: /var/log/messages” 或者 “sudo ausearch –message AVC” 在启动审计状态下进行确认。
首先,我们要确保能够使用audit2allow命令将日志转换为规则。
# audit2allowコマンドが含まれるパッケージを調べる
yum --quiet provides */audit2allow
# policycoreutils-python-2.0.83-19.30.el6.x86_64 : SELinux policy core python utilities
# Repo : base
# Matched from:
# Filename : /usr/bin/audit2allow
# 上で返った情報先頭のパッケージ名を指定してインストール
sudo yum install policycoreutils-python
如果启动了审核并且运行了sudo audit2allow –module=httpd –dmesg命令,那么如果还要执行sudo audit2allow –module=httpd –all命令,则会输出与已记录的策略违反相对应的访问权限规则。3 4
在我的环境中,得到了以下的输出结果。
module httpd 1.0;
require {
type httpd_sys_script_t;
type httpd_t;
class process signull;
}
#============= httpd_t ==============
allow httpd_t httpd_sys_script_t:process signull;
这是一个让标记为httpd_t的进程允许发送信号给标记为httpd_sys_script_t的进程的规则。
如果确认显示的规则没有问题,可以按以下步骤安装规则。
# ルールが定義されたモジュールを作成
# ※Auditが起動していた状態なら、--dmesg → --all
sudo audit2allow --module-package=httpd --dmesg
# 作成されたモジュールをインストール
sudo semodule --install=httpd.pp
手動定义新的规则
如果使用虚拟机软件如VirtualBox或VMware来搭建开发环境,是无法更改共享目录的标签的。如果将这些目录放置在虚拟机中的/var/www目录下,则需要定义规则以允许Apache访问这些目录。
首先,创建类似以下的强制类型安全策略文件。该文件的规则定义是通过audit2allow命令将我的开发环境中的策略违规日志转换而来的。它允许httpd_t访问vmblock_t。
module httpd 1.0;
require {
type httpd_t;
type vmblock_t;
class dir { getattr search read open };
class file { getattr read open };
}
#============= httpd_t ==============
allow httpd_t vmblock_t:dir { getattr search read open };
allow httpd_t vmblock_t:file { getattr read open };
如果您正在使用执行文件操作的CGI脚本,您需要允许写入和执行,如下所示。
module httpd 1.0;
require {
type httpd_t;
type vmblock_t;
class dir { getattr search read open write remove_name add_name };
class file { getattr read open write lock execute execute_no_trans ioctl append setattr create unlink };
}
#============= httpd_t ==============
allow httpd_t vmblock_t:dir { getattr search read open write remove_name add_name };
allow httpd_t vmblock_t:file { getattr read open write lock execute execute_no_trans ioctl append setattr create unlink };
将此规则分为两个阶段进行编译和安装,如下:5 6
# 中間ファイルの作成
checkmodule -m -M -o httpd.mod httpd.te
# 中間ファイルをコンパイル
semodule_package --outfile httpd.pp --module httpd.mod
# インストール
sudo semodule --install=httpd.pp
如果SELinux在宽容模式下运行
如前所述,permissive模式只会记录日志而不会阻断访问,因此我们会切换到实际阻断访问的enforcing模式。
如果在sestatus命令的执行结果中显示“配置文件中模式:宽容”,则表示操作系统在启动时会进入宽容模式。您应编辑设置文件,以确保在启动时进入强制执行模式。
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - SELinux is fully disabled.
SELINUX=enforcing
# ↑この項目を、permissiveからenforcingに変更する
# SELINUXTYPE= type of policy in use. Possible values are:
# targeted - Only targeted network daemons are protected.
# strict - Full SELinux protection.
SELINUXTYPE=targeted
如果运行getenforce命令显示为Permissive,可以执行以下命令来更改操作模式。1
# SELinuxをenforcingモードに(OS終了時まで)
sudo setenforce 1
只需指定0而非1,即可将模式再次更改为宽松模式。
【补充】如果使用mod_ssl
需要正确地对用于设置公开密钥文件的SSLCertificateFile指令,用于设置秘密密钥文件的SSLCertificateKeyFile指令,以及用于设置密码短语的SSLPassPhraseDialog指令的程序文件进行标记。
如果使用了builtin作为SSLPassPhraseDialog的选项。
需要在公钥文件和私钥文件中都附加httpd_config_t标签。
如果两个文件都放置在/etc/httpd目录下,那么只需要执行sudo restorecon -R /etc/httpd命令即可。
如果SSLPassPhraseDialog的exec参数为”{程序文件路径}”
需要在公钥文件上附加httpd_config_t,以及在输出密钥文件和密码短语的程序文件上附加httpd_sys_script_exec_t。似乎还需要安装以下规则。
module httpd 1.0;
require {
type user_devpts_t;
type httpd_sys_script_t;
class capability { dac_override };
class chr_file { read write };
}
#============= httpd_sys_script_t ==============
allow httpd_sys_script_t self:capability { dac_override };
allow httpd_sys_script_t user_devpts_t:chr_file { read write };
使用semanage命令可以永久更改标签。
sudo semanage fcontext --add --type httpd_sys_script_exec_t /usr/local/apache/sbin/pp-filter
sudo restorecon /usr/local/apache/sbin/pp-filter
【补充说明】如果使用了套接字通信
为了从像PHP这样的程序中进行Whois搜索,需要进行套接字通信,但SELinux默认情况下禁止此操作。
执行”sudo setsebool -P httpd_can_network_connect on”并持久地进行许可。7
【补充】如果使用数据库的情况下
要从PHP等程序连接到数据库,需要遵守以下规则。
module httpd 1.0;
require {
type init_t;
class unix_stream_socket connectto;
}
#============= httpd_t ==============
allow httpd_t init_t:unix_stream_socket connectto;
请参考该页面。
SELinux - PukiWiki
这么简单!SELinux的标签重新添加 | hiro345
使用SELinux加强安全的解决方案第三部分 – 基本操作和策略文件配置(3/3) – ITmedia企业
EZ-NET:为SELinux添加新的许可设置(CentOS 5.4):SELinux的用法
自制SELinux策略模块 – わんこいん
与SELinux搏斗 第二部分 jumble/网络博客
[php] 使用php的fsockopen函数时出现Permission denied问题 | 迷途男子!!