SELinux是什么?
传统读写文件系统的方式位:DAC,自主访问控制,依据进程的拥有者对文件的权限来决定,缺点如下:1)root权限大概会被截取,具有最高权限控制,从而导致权限完全泄露;2)如果将文件设置为777,那么这会导致该文件会被全部用户访问
SELinux的筹谋控制为:以策略规则订定特定进程读写特定文件,即逼迫访问控制(MAC),每个进程都有自己的运行地区,各个进程只运行在自己的地区内,无法访问其他进程和文件(这和docker的资源管理有异曲同工之妙)
SELinux中分为主体、对象和策略,此处重点先容策略
策略做了什么安全保证?
要素:
- 主体:进程
- 对象:目标资源,一样平常是文件系统
- 策略:会根据某些服务来订定基本的存取安全性政策,政策内还会有详细的规则 rule 来指定不同的服务开放某些资源的存取
对于SELinux,他提供了三种工作模式 :
- Disabled:SELinux 被关闭,使用 DAC 访问控制方式
- Permissive: 宽容模式,SELinux被启用,但安全策略规则并没有被逼迫实行。当安全策略规则应该拒绝访问时,访问仍然被允许。这时会向日志文件发送一条该访问应该被拒绝的消息
- Enforcing: 逼迫模式,SELinux 被启动,并逼迫实行全部的安全策略规则
设置文件如下
- root@iZbp11biajzuozsuyop6jgZ:/# 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 enforcing.
- # disabled - No SELinux policy is loaded.
- SELINUX=permissive
- # SELINUXTYPE= can take one of these two values:
- # default - equivalent to the old strict and targeted policies
- # mls - Multi-Level Security (for military and educational use)
- # src - Custom policy built from source
- SELINUXTYPE=default
- # SETLOCALDEFS= Check local definition changes
- SETLOCALDEFS=0
复制代码 获取当前 SELinux 运行状态
- # sestatus
- SELinux status: enabled
- SELinuxfs mount: /selinux
- Current mode: enforcing
- Mode from config file: enforcing
- Policy version: 21
- Policy from config file: targeted
复制代码 改变 SELinux 运行状态
- # 强制访问控制
- setenforce 1
- # 自主访问控制
- setenforce 0
复制代码 SELinux中默认定义了两个策略来订定规则
- targeted:默认策略,用于限定网络服务
- strict :多级安全掩护策略,代表全部网络服务和访问哀求都要经过 SELinux
在/etc/sysconfig/selinux中对其进行修改
- # cat /etc/sysconfig/selinux
- # 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 - No SELinux policy is loaded.
- SELINUX=disabled
- # SELINUXTYPE= can take one of these two values:
- # targeted - Targeted processes are protected,
- # mls - Multi Level Security protection.
- SELINUXTYPE=strict
复制代码 除此之外,每个文件或者进程都要有一个安全上下文,进程是否可以访问文件或目次,就要其安全上下文是否匹配(是否匹配的规则是通过策略中的规则来订定的)
- [root@study ~]# ls -Z
- -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 accountadd.sh
- -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 accountadd.txt
- -rwxr--r--+ root root unconfined_u:object_r:admin_home_t:s0 acl_test1
- -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 addaccount2.sh
- -rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
- -rw-r--r--. root root system_u:object_r:admin_home_t:s0 initial-setup-ks.cfg
复制代码 安全上下文存在于主体进程中与目标文件资源中,物理位置是放在文件的 inode 中,一样平常分为三个字段
- identify:身份,相当于账户方面的身份辨认,一样平常分为unconfined_u、system_u
- role:通过该字段,可以知道这个资料是属于进程、文件资源照旧代表使用者,object_r代表的是文件或目次等文件资源,system_r代表的是进程
- type:类型字段在文件与进程的定义不雷同,在进程中称为domain
domain 需要与 type 搭配,则该进程才可以或许顺利的读取文件资源,一个进程安全上下文一样平常对应多个文件安全上下文
设置过程(以AppArmor为例)
Ubuntu默认是不支持SELinux的,可以选择安装CentOS
在此处我以AppArmor为例
两者均为MAC访问控制,但是也有一些差异点:AppArmor 的操纵原则是先授予权限,再应用限定。SELinux 则采取更为保守的方式,默认会限定全部应用步伐的访问,仅向提供适当凭证的用户授予访问权限
- sudo apt install apparmor-profiles
- sudo apt install apparmor-utils
- sudo apt install apparmor-profiles-extra
复制代码 查看AppArmor是否开启
- root@iZbp11biajzuozsuyop6jgZ:~# aa-enabled
- Yes
复制代码 AppArmor 通过设置文件来管理各个步伐的权限(这一点和qnx的secpool机制有点像)设置文件的文件名通常是其控制的步伐的完备路径名(比如 /etc/apparmor.d/usr.sbin.nginx 是控制 Nginx 的设置文件)
生效的设置文件的位置是 /etc/apparmor.d,而提供的一些可选设置文件夹位于 /usr/share/apparmor/profiles 里
- root@iZbp11biajzuozsuyop6jgZ:/etc/apparmor.d# ls
- abi force-complain samba-bgqd tunables usr.bin.pidgin usr.sbin.apt-cacher-ng usr.sbin.mdnsd usr.sbin.smbldap-useradd
- abstractions local sbin.dhclient ubuntu_pro_apt_news usr.bin.tcpdump usr.sbin.avahi-daemon usr.sbin.nmbd usr.sbin.traceroute
- apache2.d lsb_release sbin.klogd ubuntu_pro_esm_cache usr.bin.totem usr.sbin.chronyd usr.sbin.nscd
- bin.ping nvidia_modprobe sbin.syslogd usr.bin.irssi usr.bin.totem-previewers usr.sbin.dnsmasq usr.sbin.rsyslogd
- disable php-fpm sbin.syslog-ng usr.bin.man usr.lib.snapd.snap-confine.real usr.sbin.identd usr.sbin.smbd
复制代码 在 AppArmor 中,每一个设置文件都有三种状态:enforce、complain 以及 disable,分别使用 aa-enforce ()、aa-complain () 以及 aa-disable () 来切换对应步伐的设置文件
- enforce:逼迫模式,这种模式下,全部没有被授权的运动都会被禁止而且记载
- complain:抱怨模式,此时未授权运动会被放行,但是也会被记载
- disable:此时对应设置文件不加载,没有对步伐运动进行限定
开启和关闭AppArmor服务
- sudo systemctl enable apparmor # 允许该服务运行
- sudo systemctl disable apparmor # 禁止该服务运行
- sudo systemctl start apparmor.service # 开启和服务
- sudo systemctl stop apparmor.service # 停止服务
- sudo systemctl status apparmor.service # 服务状态
- sudo systemctl reload apparmor.service # 加载配置
复制代码 添加设置文件
- apparmor_parser -r /path/to/profile # 添加入内核中
- apparmor_parser -R /path/to/profile # 从内核中移除
复制代码 同理:QNX的secpol机制
参考官网:secpol官网资料
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |