SELinux

SELinux

访问控制

DAC

  • DAC:Discretionary Access Control,自主访问控制
  • 依据进程的所有者与文件资源的rwx权限来决定有无访问权限
  • DAC针对用户,对用户进行访问控制
  • 缺点:
    • root有最高权限无法限制
    • r,w,x权限划分太宽泛,无法针对不同的进程实现限制

MAC

  • MAC:Mandatory Access Control,强制访问控制
  • 依据策略规则决定进程可以访问哪些文件
  • MAC针进程,对进程进行访问控制
  • 优点:
    • 即使是root用户,在使用不同进程时,所能取得的权限并不一定是root,需要看当时进程的设置而定
    • 即使不小心httpd被取得了控制权,也无权浏览/etc/shadow等重要的文档

SELinux介绍

  • SELinux:Security-Enhanced Linux)安全增强型linux,是美国国家安全局(NSA)开发,用于实现强制访问控制(MAC)
  • selinux用于限制进程对系统中文件及目录的访问
  • 被设计成内核模块包含到linux内核中
  • SELinux提供一些默认的策略(Policy), 并在该策略内提供多个规则(rule),让用户可以选择是否启用该控制规则
  • selinux策略模式
[root@server1 ~]# cat /etc/selinux/config 

# 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 disabled.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 
模式中文说明
enforcing强制模式违反SELinux规则的行为将被阻止并记录到日志中
permissive宽容模式违反SELinux规则的行为只会记录到日志中,一般为调试用
disabled关闭模式关闭SELinux

SELinux使用

  • 基本使用
配置文件
[root@server1 ~]# cat /etc/selinux/config 

# 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 disabled.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

开启selinux
[root@server1 ~]# sed -i 's/SELINUX=disabled/SELINUX=enforing/' /etc/selinux/config 

重启系统(不然selinux不生效)
[root@server1 ~]# reboot

查看selinux的模式
[root@server1 ~]# getenforce 
Permissive

命令行设置selinux的模式(0为permissive;1为enforing)
[root@server1 ~]# setenforce 1
[root@server1 ~]# getenforce 
Enforcing

查看文件selinux规则
[root@server1 ~]# touch file1
[root@server1 ~]# ll -Z file1 
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file1

查看进程selinux规则
[root@server1 ~]# ps -efZ |grep httpd
system_u:system_r:httpd_t:s0    root       1565      1  0 14:58 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache     1567   1565  0 14:58 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache     1568   1565  0 14:58 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache     1569   1565  0 14:58 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache     1570   1565  0 14:58 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
  • 当开启selinux时,网卡中配置的域名解析会失效,需要在/etc/resolv.conf文件中配置
[root@server1 ~]# ping www.baidu.com
ping: www.baidu.com: 未知的名称或服务
[root@server1 ~]# vim /etc/resolv.conf 
[root@server1 ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
nameserver 114.114.114.114
[root@server1 ~]# ping www.baidu.com
PING www.a.shifen.com (112.80.248.75) 56(84) bytes of data.
64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=1 ttl=128 time=9.42 ms
64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=2 ttl=128 time=9.50 ms
^C
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 9.421/9.463/9.506/0.106 ms
  • 当开启selinux时,修改http的端口,会导致不能重启httpd服务
端口改为8090
[root@server1 ~]# vim /etc/httpd/conf/httpd.conf 
[root@server1 ~]# cat /etc/httpd/conf/httpd.conf |grep Listen |grep -v '^#'
Listen 8090
[root@server1 httpd]# systemctl restart httpd.service 
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.

重新改回80
[root@server1 ~]# cat /etc/httpd/conf/httpd.conf |grep Listen |grep -v '^#'
Listen 80
[root@server1 ~]# systemctl restart httpd.service
[root@server1 ~]# 
  • 基于selinux有很多坑,建议将selinux永久关闭
[root@server1 ~]# setenforce 0
[root@server1 ~]# sed -i 's/SELINUX=enforing/SELINUX=disabled/' /etc/selinux/config 
05-23
### SELinux 配置概述 SELinux(Security-Enhanced Linux)是一种强制访问控制系统,旨在通过更精细的权限管理来提高系统的安全性。它不仅依赖于传统的 DAC(Discretionary Access Control),还引入了 MAC(Mandatory Access Control)。这种双重验证机制确保只有经过明确授权的操作才能被执行。 #### SELinux 的三种模式 SELinux 支持三种运行模式: 1. **Enforcing**:这是默认模式,在此模式下,SELinux 执行所有的安全策略并记录任何被拒绝的动作。 2. **Permissive**:在此模式下,SELinux 不会阻止任何操作,但它会记录违反策略的行为以便调试和分析。 3. **Disabled**:完全禁用 SELinux 功能[^1]。 可以通过 `getenforce` 命令查看当前 SELinux 运行模式,并使用 `setenforce` 切换模式(仅限临时更改)。永久修改需编辑 `/etc/selinux/config` 文件中的 `SELINUX=enforcing|permissive|disabled` 参数[^3]。 --- ### SELinux 配置方法 #### 1. 查看 SELinux 当前状态 要检查 SELinux 是否启用以及其当前模式,可执行以下命令: ```bash sestatus ``` #### 2. 修改文件或目录的安全上下文 SELinux 使用安全上下文标记文件、目录和其他对象。如果某些应用程序无法正常工作,可能是因为它们缺少正确的安全上下文。可以使用 `ls -Z` 查看文件的安全上下文,并使用 `chcon` 或 `restorecon` 更改这些上下文。 例如,恢复某个目录的标准安全上下文: ```bash restorecon -R /var/www/html/ ``` #### 3. 创建自定义策略模块 对于复杂场景下的需求,可能需要创建自定义策略模块。以下是基本流程: 1. 记录审计日志中因 SELinux 而受阻的操作: ```bash grep AVC /var/log/audit/audit.log | audit2allow -m mymodule > mymodule.te ``` 2. 编译生成的 `.te` 文件为二进制模块: ```bash checkmodule -M -m -o mymodule.mod mymodule.te semodule_package -o mymodule.pp -m mymodule.mod ``` 3. 加载新编写的策略模块至系统中: ```bash semodule -i mymodule.pp ``` 以上过程能够有效扩展 SELinux 默认行为以适应特定业务环境的要求[^1]。 --- ### 常见问题及解决方案 #### 问题一:Web 服务无法读取指定路径的数据 原因可能是目标数据未分配给 web 应用所需的安全上下文标签。尝试重新设定相关联的内容属性即可解决问题: ```bash chcon -t httpd_sys_content_t /path/to/data ``` 或者利用工具自动修复整个树形结构内的所有项目: ```bash restorecon -Rv /path/to/data ``` #### 问题二:数据库客户端连接失败 当 MySQL 数据库或其他类似的后台服务遇到认证错误时,应核查是否由于 SELinux 导致通信障碍。调整布尔值参数或许能缓解此类状况: ```bash setsebool -P allow_httpd_mod_auth_pam on ``` #### 问题三:邮件传输代理 (MTA) 出现异常 Postfix 等 MTA 工具可能会因为缺乏适当许可而崩溃。确认是否有足够的权利去处理队列里的消息包件: ```bash semanage permissive -a postfix_master_t ``` 每种情况都需要具体分析对应的 AVCS 条目,从而采取针对性措施加以修正[^2]。 --- ### 结论 通过对 SELinux 正确配置与维护,可以在保障灵活性的同时极大提升操作系统层面防护水平。尽管初期学习曲线较陡峭,但从长远来看收益显著。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值