安全增强型 Linux(Security-Enhanced Linux)简称 SELinux,它是 Linux 的一个安全子系统。SELinux 主要作用是最大限度地减小系统中服务进程可访问的资源(最小权限原则)。对资源的访问控制分为两类: DAC和MAC。
DAC: 在未使用SELinux的系统上, 对资源的访问是通过权限位来确定, 比如一个文件对所属用户是否有读、写、执行权限, 其他用户的访问可由所属用户进行配置. 这种由所属用户自己决定是否将资源的访问权或部分访问权授予其他用户,这种控制方式是自主的,即自主访问控制(Discretionary Access Control, DAC)
MAC: 在使用了 SELinux 的系统上,对资源的访问除了通过权限位判定外,还需要判断每一类进程是否拥有对某一类资源的访问权限。这种方式对资源的访问控制, 称之为强制访问控制(Mandatory Access Control, MAC).只给每个进程开放所需要的资源, 将权限开放到最小, 当进程出现漏洞时也只会影响到该进程所涉及的资源, 这大大提升了安全性
SEAndroid 分为进程权限与文件权限,进程权限可对应有多个文件权限,selinux 有三种模式:enforcing、permissive、disable
一、查看命令
通过getenforce 可看到当前系统使用何种模式
msm8953_64:/ # getenforce
Permissive
msm8953_64:/ #
enforcing # 强制模式,代表SELinux运行中,且已经正确开始限制domain/type,将受限主体进入规则比对、安全上下文比对的流程,若失败,就直接阻止主题进程的读写 操作,并且将他记录下来。如果通通没问题,才会进入到DAC(rwx)权限判断
permissive # 宽容模式,代表SELinux运行中,不过仅会有警告信息并不会实际限制domain/type的读写
disabled # 关闭,SELinux并没有实际运行
1. 查看文件权限:ls -Z xxx
msm8953_64:/ # ls -Z
u:object_r:cgroup:s0 acct u:object_r:rootfs:s0 init.usb.rc
u:object_r:apex_mnt_dir:s0 apex u:object_r:rootfs:s0 init.zygote32.rc
u:object_r:rootfs:s0 bin u:object_r:rootfs:s0 init.zygote64_32.rc
...
msm8953_64:/ #
2. 查看进程权限:ps -AZ
u:r:kernel:s0 root 613 2 0 0 worker_th+ 0 S [kworker/2:1H]
u:r:rmt_storage:s0 nobody 616 1 110688 3104 poll_sche+ 0 S rmt_storage
u:r:rfs_access:s0 vendor_rfs 619 1 38840 2668 poll_sche+ 0 S tftp_server
u:r:vendor_per_mgr:s0 system 638 1 166128 4104 do_sigtim+ 0 S pm-service
u:r:ashmemd:s0 nobody 641 1 35984 3076 binder_io+ 0 S ashmemd
u:r:audioserver:s0 audioserver 644 1 2524648 24696 binder_io+ 0 S audioserver
u:r:shell:s0 shell 700 1 33600 2300 wait_woken 0 S sh
二、权限分类介绍
selinux 分为文件权限与进程权限。皆遵守SELinux 规范
用户:角色:类别:级别
user:role:type[:range]
万能公式:allow : {<avc: denied>}
如: denied { read open } for path=“/system/bin/sh” dev=“mmcblk0p29” ino=822 scontext=u:r:su:s0 tcontext=u:object_r:shell_exec:s0 tclass=file permissive=1
在对应的su.te文件中增加以下指令(测试指令,尽量不要放大su的权限,可能导致CTS失败,尽量修改device目录下面的te文件,device会overlay)
allow su shell_exec:file { read open}
可使用audit2allow 工具生成对应的权限:
#avc-audit.log中保存文本:denied { read open } for path="/system/bin/sh" dev="mmcblk0p29" ino=822 scontext=u:r:su:s0 tcontext=u:object_r:shell_exec:s0 tclass=file permissive=1
#selinux_avc.log 为输出的权限
audit2allow -i avc-audit.log > selinux_avc.log
1、文件权限
如init.rc的权限:u:object_r:rootfs:s0 init.rc
u:用户 Android中只有"u"
r:角色 Android中只有"object_r"
rootfs:类别比较多,也可自定义。参考system/sepolicy
s0:级别 Android中只有"s0"
2、进程权限
u:r:system_server:s0 system 1707 911 7758100 169992 SyS_epoll_wait 0 S system_server
u:用户 Android中只有"u"
r:角色 Android中只有"r"
system_server:type,表示该进程所属的 type 为system_server
s0:级别 Android中只有"s0"
三、强制访问控制:TE
TE即Type Enforcement。是 SEAndroid 中的最主要的安全手段,MAC 的管控策略其实也就是 Type Enforcement Access Control(TE 强制访问控制)。所有关于TE的强制访问规则都被定义在了后缀为te的文件中,如system_server进程权限可在system_server.te中增加对应的权限。
allow system_server zygote_tmpfs:file read;
在 te 文件中定义了安全策略中最基本的参量 type,同时将具有共性的 type 归在一起构成一个称为 attribute 的集合,policy 的规则执行就能以 attribute 作为执行对象。
| attribute定义 | 介绍 |
|---|---|
| domain | 所有的进程 type |
| fs_type | 所有属于文件系统的 type,例如 sys 目录下创建的节点文件 |
1)自定义类型权限
**STEP1:**file_contexts 中添加 type
/data/misc_de/[0-9]+/storaged(/.*)? u:object_r:storaged_data_file:s0
/data/misc_ce/[0-9]+/storaged(/.*)? u:object_r:storaged_data_file:s0
**STEP2:**file.te 中对新建 type 进行关联
# /data/misc/storaged
type storaged_data_file, file_type, data_file_type, core_data_file_type;
**STEP3:**修改相关 te allow 权限
allow system_server storaged_data_file:file rw_file_perms;
allow system_server storaged_data_file:dir { relabelto search };
系统的 file_context 加载会在 init 的时候进行,所以这个目录文件创建一定要在init加载file_context 之前,否则 SELinux 的 context 会因为找不到文件或目录就过滤新建的规则了。如果在init之后创建的文件需要重新加载SELinux Context,命令如下:
restorecon /data/misc_ce/custom.config
170

被折叠的 条评论
为什么被折叠?



