SELinux 介绍
SELinux 概述
文件权限控制了哪些用户或用户组可以访问哪些特定文件,但未限定用户访问文件的方式。
例如:对于文件的写入权限而言, 结构化数据文件是否应当设计为只能使用特定的程序写入,但其他编辑器仍可以打开和修改此文件,而这可能会导致损坏。文件权限仅仅是控制谁可以读取、写入或运行文件,无法控制如何使用文件。
Security Enhanced Linux (SELinux)是一个额外的系统安全层,主要目标是防止已遭泄露的系统服务访问用户数据。
例如:恶意人员成功入侵了 Web服务器进程,获得进程的权限,也就是apache用户和apache组的权限。该用户和组对文档根目录/var/www/html具有读取权限,还可以访问/tmp和/var/tmp,以及全局可写的其他任何文件和目录。
SELinux 强制执行一组访问规则,明确定义进程和资源之间允许的操作。每个进程、文件、目录和端口都具有专门的安全标签, 称为SELinux上下文。上下文是一个名称, SELinux策略使用它来确定某个进程能否访问文件、目录或端口。除非显式规则授予访问权限,否则,在默认情况下,策略不允许任何交互。
SELinux 标签有多个内容:用户,角色,类型和敏感度。rhel中默认策略targeted,基于类型部分。类型部分通常以_t结尾。
示例:
- web 服务器守护进程类型是 httpd_exec_t
- web 服务器的网页类型是 httpd_sys_content_t
- web 服务器的端口类型是 http_port_t
- /tmp 标签 tmp_t
基本原理
- SELinux默认策略允许apache进程访问在/var/www/html文件夹下的文件和文件夹,以及其他一些具有httpd_sys_content_t上下文的文件夹,禁止访问具有其他不匹配标签的目录。
- 在 SELinux 的保护下,apache进程不允许访问/tmp和/var/tmp目录。因为/tmp、/var/tmp目录的标签是tmp_t,与默认策略不匹配。
命令选项 -Z
许多处理文件的命令都一个-Z 选项,用于显示和设置SELinux上下文。
[root@server ~]# ps -C sshd -Z
LABEL PID TTY TIME CMD
system_u:system_r:sshd_t:s0-s0:c0.c1023 951 ? 00:00:00 sshd
system_u:system_r:sshd_t:s0-s0:c0.c1023 1261 ? 00:00:00 sshd
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 1276 ? 00:00:00 sshd
system_u:system_r:sshd_t:s0-s0:c0.c1023 2455 ? 00:00:00 sshd
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 2459 ? 00:00:00 sshd[root@server ~]# ls -Z /home
unconfined_u:object_r:user_home_dir_t:s0 furongwang# 其他命令中 -Z 选项
# cp -Z set SELinux security context of destination file to default type
# mkdir -Z set SELinux security context of each created directory to the default type
SELinux 模式
- enforcing 模式: SELinux强制执行访问控制规则。 在强制模式下,如果规则没有明确允许访问,则拒绝访问,并写入日志。此模式是默认模式。
- Permissive 模式:SELinux 加载策略并处于活动状态,但不强制执行访问控制规则,⽽是记录访问违规。此模式有助于对应用和规则进行测试和故障排除。
- disable 模式:完全关闭 SELinux。不拒绝任何SELinux违规,不予记录。
查看当前模式
[root@server ~]# getenforce
更改当前模式
[root@server ~]# setenforce
usage: setenforce [ Enforcing | Permissive | 1 | 0 ]# 强制模式临时切换为允许模式
[root@server ~]# setenforce 0
# 或者
[root@server ~]# setenforce Permissive# 允许模式临时切换为强制模式
[root@server ~]# setenforce 1
# 或者
[root@server ~]# setenforce Enforcing
模式切换注意事项:
- 强制模式和允许模式之间变更,不需要重启系统。
- 从强制模式或者允许模式变更到禁用模式,或者从禁用模式变更到强制模式或者允许模式都需要重启系统。
更改 SELinux 默认模式
[root@server ~]# vim /etc/selinux/config
SELINUX=enforcing
SELINUXTYPE=targeted
在启动时通过将向内核传递参数来设置SELinux 模式:
- enforcing=1,设置强制模式。
- enforcing=0,设置许可模式。
- selinux=0,来彻底禁用 SELinux。
- selinux=1,启用 SELinux(当设置enforcing参数是,该参数可以省略)。
实验: selinux模式为enforcing情况下,破解root密码。破解完成后不执行 touch /.autorelabel命令。
重启测试:使用正确的密码登录。结果:登录失败。原因:用户验证程序无法访问/etc/shadow,因为破解密码过程中/etc/shadow文件标签被破坏了。
处理方法:系统启动时,内核添加参数 selinux=1 enforcing=0
控制文件 SELinux 上下文
新文件默认 SELinux 上下文
新文件通常从父目录继承其SELinux上下文,从而确保它们具有适当的上下文。
[root@server ~]# touch /tmp/ruike1
[root@server ~]# ls -dZ /tmp /tmp/ruike1
drwxrwxrwt. root root system_u:object_r:tmp_t:s0 /tmp
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 /tmp/ruike1[root@server ~]# cp /tmp/ruike1 /root/ruike5
[root@server ~]# ls -dZ /tmp /tmp/ruike1 /root /root/ruike5
dr-xr-x---. root root system_u:object_r:admin_home_t:s0 /root
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /root/ruike5
drwxrwxrwt. root root system_u:object_r:tmp_t:s0 /tmp
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 /tmp/ruike1
以下两种情况,文件的 SELinux 上下文保持不变:
- cp -a 命令复制
- 移动文件
[root@server ~]# cp -a /tmp/ruike1 /root/ruike1-a
[root@server ~]# ls -dZ /tmp /tmp/ruike1 /root /root/ruike1*
dr-xr-x---. root root system_u:object_r:admin_home_t:s0 /root
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 /root/ruike1-a
drwxrwxrwt. root root system_u:object_r:tmp_t:s0 /tmp
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 /tmp/ruike1[root@server ~]# mv /tmp/furongwang /root/furongwang-mv
[root@server ~ ]# ls -1dZ /tmp /root /root/furongwang*system_u:object_r:admin_home_t:s0 /root
unconfined_u:object_r:admin_home_t:s0 /root/furongwang1unconfined_u:object_r:user_tmp_t:s0 /root/furongwang-aunconfined_u:object_r:user_tmp_t:s0 /root/furongwang-mvsystem_u:object_r:tmp_t:s0 /tmp
设置文件 SELinux 上下文
设置文件上下文命令
-
chcon 命令,直接更改文件SELinux上下文。但是,它不会将上下文更改保存到 SELinux 上下文数据库中。系统下一次对所有文件进行 relabel 操作时,将导致该上下文恢复。它对于测试和实验很有用。
-
restorecon 命令,根据 SELinux上下文数据库中规则,恢复文件SELinux上下文。原先通过 chcon命令所做的更改,将失效。
示例:
[root@server ~]# mkdir /www
[root@server ~]# ls -Zd /www
unconfined_u:object_r:default_t:s0 /www[root@server ~]# chcon -t httpd_sys_content_t /www
[root@server ~]# ls -Zd /www
unconfined_u:object_r:httpd_sys_content_t:s0 /www[root@server ~]# restorecon -v /www
Relabeled /www from unconfined_u:object_r:httpd_sys_content_t:s0 to unconfined_u:object_r:default_t:s0
[root@server ~]# ls -Zd /www
unconfined_u:object_r:default_t:s0 /www
semanage fcontext 命令用于管理SELinux上下文数据库中规则,restorecon 命令根据这些规则恢复文件上下文。semanage fcontext命令使用扩展的正则表达式指定路径和文件名。比较常见的扩展正则表达式 (/.*)? ,表示随意匹配/后面接任意数量字符,递归匹配文件夹下的子文件和子文件夹。
示例:
[root@server ~]# echo Hello World > /www/index.html
[root@server ~]# ls -Zd /www/index.html
unconfined_u:object_r:default_t:s0 /www/index.html# 安装 semanage 工具
[root@server ~]# yum install -y policycoreutils-python# 添加默认规则
[root@server ~]# semanage fcontext -a -t httpd_sys_content_t '/www(/.*)?'
[root@server ~]# semanage fcontext -l |grep '^/www(/.*)?'
/www(/.*)? all files system_u:object_r:httpd_sys_content_t:s0 # 只查看非默认策略
[root@server ~]# semanage fcontext -lC
SELinux fcontext type Context
/www(/.*)? all files system_u:object_r:httpd_sys_content_t:s0# 恢复文件上下文
[root@server ~]# restorecon -Rv /www/
Relabeled /www from unconfined_u:object_r:default_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /www/index.html from unconfined_u:object_r:default_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
[root@server ~]# ls -1Zd /www/ /www/index.html
unconfined_u:object_r:httpd_sys_content_t:s0 /www/
unconfined_u:object_r:httpd_sys_content_t:s0 /www/index.html# 删除默认规则
[root@server ~]# semanage fcontext -d -t httpd_sys_content_t '/www(/.*)?'# 删除目录
[root@server ~]# rm -fr /www
示例:配置 web 站点主目录
# 准备目录
[root@server ~]# mkdir /www
[root@server ~]# echo woshichuantongpai > /www/index.html
[root@server ~]# semanage fcontext -a -t httpd_sys_content_t '/www(/.*)?'
[root@server ~]# restorecon -Rv /www/# 安装 httpd
[root@server ~]# yum install -y httpd
[root@server ~]# systemctl enable httpd --now
[root@server ~]# vim /etc/httpd/conf/httpd.conf
# 注释122 行DocumentRoot,在122行后,添加如下内容:
# DocumentRoot "/var/www/html"
DocumentRoot "/www"
<Directory "/www">AllowOverride None# Allow open access:Require all granted
</Directory>
# 重启服务
[root@server ~]# systemctl restart httpd# 访问验证
[root@server ~]# curl http://server.furongwang.cloud/
woshichuantongpai
控制端口 SELinux 上下文
端口 SELinux 上下文
SELinux还可以对端口标记。 当某个进程希望侦听端口时, SELinux将检查是否允许与该进程相关联的标签绑定该端口标签。 这可以阻止恶意服务控制本应由其他网络服务使用的端口。
在 targeted 策略中:
- 端口 22/TCP ,具有标签 ssh_port_t。
- 端口 80/TCP 和 443/TCP ,具有标签 http_port_t。
# 查看端口标记
[root@server ~]# semanage port -l | grep http_port_t
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988# 添加端口标记
[root@server ~]# semanage port -a -t http_port_t -p tcp 18020
[root@server ~]# semanage port -l|grep 18020
http_port_t tcp 18020, 80, 81, 443, 488, 8008, 8009, 8443, 9000# 修改网络端口标记
[root@server ~]# semanage port -m -t ssh_port_t -p tcp 18020
[root@server ~]# semanage port -l | grep 18020
ssh_port_t tcp 18020, 22# 删除端口标记
[root@server ~]# semanage port -d -t ssh_port_t -p tcp 18020
[root@server ~]# semanage port -l | grep 18020
示例:配置 web 站点监听端口
[root@server ~]# vim /etc/httpd/conf/httpd.conf
# 修改原先的 Listen 80 为 Listen 18020
Listen 18020# 重启服务
[root@server ~]# systemctl restart httpd
Job for httpd.service failed because the control process exited with error code.
See "systemctl status httpd.service" and "journalctl -xe" for details[root@server ~]# systemctl status httpd.service |cat |grep Per
......
Nov 29 11:55:26 server.furongwang.cloud httpd[3939]: (13)Permission denied: AH00072: make_sock: could not bind to address 0.0.0.0:18020
......# 配置端口
[root@server ~]# semanage port -a -t http_port_t -p tcp 18020
[root@server ~]# semanage port -l | grep 18020
http_port_t tcp 18020, 80, 81, 443, 488, 8008, 8009, 8443, 9000# 只查看改动标签
[root@server ~]# semanage port -lC
SELinux Port Type Proto Port Number
http_port_t tcp 18020# 重启服务并访问
[root@server ~]# systemctl restart httpd# 访问验证
[root@server ~]# curl http://server.furongwang.cloud:18020/
Hello World
控制 SELinux 布尔值
SELinux 布尔值
SELinux 布尔值是可更改SELinux策略行为的开关,可以启用和禁用。
# 查看SELinux布尔值
[root@server ~]# getsebool -a |grep httpd_enable_homedirs
httpd_enable_homedirs --> off
[root@server ~]# getsebool httpd_enable_homedirs
httpd_enable_homedirs --> off# 设置SELinux布尔值
[root@server ~]# setsebool
Usage: setsebool [ -NPV ] boolean value | bool1=val1 bool2=val2...
# 启用value为1或者on或者true
# 禁用value为0或者off或者false[root@server ~]# setsebool httpd_enable_homedirs=1或者on或者true
[root@server ~]# setsebool httpd_enable_homedirs=0或者off或者false# 持久化设置布尔值,使用-P选项修改将写入SELinux数据库。
[root@server ~]# setsebool httpd_enable_homedirs=1
[root@server ~]# setsebool -P httpd_enable_homedirs=1
还可以semanage boolean 命令管理 SELinux 布尔值。
[root@server ~]# semanage boolean
usage: semanage boolean [-h] [-n] [-N] [-S STORE] [ --extract | --deleteall | --list -C | --modify ( --on | --off ) boolean ]# 查看 SELinux 布尔值
[root@server ~]# semanage boolean -l
# 只查看非默认的 SELinux 布尔值
[root@server ~]# semanage boolean -l -C# 设置 SELinux 布尔值
[root@server ~]# semanage boolean -m -1|--on use_nfs_home_dirs
[root@server ~]# semanage boolean -m -0|--off use_nfs_home_dirs
# 相当于setsebool -P# 删除原先自定义的 SELinux 布尔值
[root@server ~]# semanage boolean -D
示例:允许 httpd 访问用户家目录
[root@server ~]# vim /etc/httpd/conf.d/userdir.conf
# 修改以下两个参数UserDir enabledUserDir public_html
# 重启服务
[root@server ~]# systemctl restart httpd# 设置权限
[furongwang@server ~]$ mkdir public_html
[furongwang@server ~]$ echo "Welcome to litang" > public_html/index.html
[root@server ~]# ll -d /home/furongwang
drwx-----x. 2 furongwang furongwang 83 8月 1 16:20 /home/furongwan
[furongwang@server ~]$ chmod o+x /home/furongwang/# 未启用布尔值访问
[root@server ~]# curl http://server.furongwang.cloud:18020/~furongwang/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>
</body></html># 启用布尔值访问
[root@server ~]# setsebool httpd_enable_homedirs=1
[root@server ~]# curl http://server.furongwang.cloud:18020/~furongwang/
Welcome to litang
SELinux 问题处理思路
- 确定是否是 SELinux 引起的。
- 在做调整前,首先考虑 SELinux 是否正确的阻止了访问。例如web服务器尝试访问/home目录,如果web的内容不是用户发布的,那么发生阻止信号是正确的阻止。如果这个访问是被允许的,参考下面步骤。
- 最常见的 SELinux 问题是不正确的文件上下文。当文件是另外一个地方创建的,然后移动到新的地方,而新的地方需要新的上下文。在大多数情况,执行restorecon可以修复这个问题。这种修复问题方式对系统其他部分安全影响非常小。
- 另外一个问题可能是布尔值设置不当导致拒绝访问。例如ftpd_anon_write布尔值控制匿名ftp用户是否可以上传文件。如果如需匿名用户上传文件,需要打开这个布尔值。调整布尔值需要小心,因为对系统影响范围比较大。
- 还有可能是 端口上下文不匹配。
- 还有可能是 SELinux bug。