SEAndroid
1 SEAndroid介绍
SEAndroid主要通过标签(Label)中的类型(TYPE)来定义安全策略, SEAndroid会给文件,进程等上下文贴上标签,这些标签称为Security Context。
查看进程上下文标签
ps -Z
查看文件,设备节点等上下文标签
ls -Z
配置selinux
如有需要添加selinux目录,在BoardConfig.mk中添加新的selinux目录
// 厂商客制化相关
BOARD_SEPOLICY_DIRS ?= \
device/xxx/common/sepolicy
selinux的配置规则:将设备节点,文件,可执行程序等资源贴上标签分类
selinux策略文件 | 描述 |
---|---|
mac_permissions.xml | App进程 |
seapp_contexts | App数据文件 |
file_contexts | 系统文件 |
property_contexts | 系统属性 |
genfs_contexts | 虚拟文件 |
service_contexts | 系统服务 |
hwservice_contexts | 硬件服务 |
device.te,file.te,property.te | 标签 |
进程*.te | 进程相关 |
*contexts文件
// 给/dev/lightsensor节点贴上sensor_device标签
/dev/lightsensor u:object_r:sensor_device:s0
// 给/system/bin/abc可执行程序贴上abc_exec标签
/system/bin/abc u:object_r:abc_exec:s0
// 给/vendor/lib(64)/的所有文件贴上vendor_file标签
/vendor/lib(64)?/(/.*)? u:object_r:vendor_file:s0
mac_permissions.xml
<?xml version="1.0" encoding="utf-8"?>
<policy>
<!-- google apps key -->
<signer signature="@GOOGLE" >
<seinfo value="google" />
</signer>
<!-- rockchip-platform apps key -->
<signer signature="@PLATFORM" >
<seinfo value="platform" />
</signer>
</policy>
2 TE介绍
属性声明语法:
• attribute 属性名称;
// 声名system_file_type属性
attribute system_file_type;
// 声名vendor_file_type属性
attribute vendor_file_type;
typeattribute语句语法:
• typeattribute 类型名 属性名;
// 把vendor_block_device添加到super_block_device_type类型集合中
typeattribute vendor_block_device super_block_device_type;
类型声明的语法:
• type 类型名称 [alias 别名集] [,属性集];
// 定义了一个名为shell的type
// 和一个名为domain的属性(attribute)关联,既shell属于domain属性集
type shell, domain;
// 定义了一个名为labeledfs的type
// 和一个名为fs_type的属性(attribute)关联,既labeledfs属于fs_type属性集
type labeledfs, fs_type;
SEAndroid策略的基本格式:
- domains : 用于标识单一进程或一系列进程(scontext)
- type: 标识客体(tcontext)或一系列客体(tcontext)
- classes :type资源的具体的类型
- permisions : 被允许的操作
allow domains type:classes permisions;
允许vendor_init进程对media_rw_data_file类型的目录下有创建目录的权限
allow vendor_init media_rw_data_file:dir create;
scontext指主体,tcontext指客体
scontext=u:r:vendor_init:s0 tcontext=u:object_r:media_rw_data_file:s0 tclass=dir permissive=0
3 example
// 定义hello_test类型,hello_test属于domain属性集
type hello_test, domain;
// 定义hello_test_exec类型
// hello_test_exec类型关联exec_type, vendor_file_type, file_type这三种属性集
type hello_test_exec, exec_type, vendor_file_type, file_type;
// 设置init从hello_test_exec转换到hello_test域
init_daemon_domain(hello_test)
// 允许hello_test读,写,调用Binder IPC
vndbinder_use(hello_test)
// 允许hello_test读,写,调用HW Binder IPC
hwbinder_use(hello_test)
// 允许hello_test将hello_test_hwservice添加到hwservice中
add_hwservice(hello_test, hello_test_hwservice)
// 将hello_test添加到hal_allocator客户端的进程域
hal_client_domain(hello_test, hal_allocator)
// 将hello_test添加到hal_graphics_allocator客户端的进程域
hal_client_domain(hello_test, hal_graphics_allocator)
// 允许hello_test进程对hwservicemanager_prop的属性具有获取权限
get_prop(hello_test, hwservicemanager_prop)
// 允许hello_test进程对vendor_platform_prop的属性具有获取权限
get_prop(hello_test, vendor_platform_prop)
// 允许hello_test进程对hello_device类型的字符设备具有读写权限
allow hello_test hello_device:chr_file rw_file_perms;
4 DEBUG
查看selinux状态
getenforce
设置selinux状态
- Enforcing: SELinux处于开启状态,会阻止进程违反SELinux策略访问资源的行为
- Permissive: SELinux关闭,不会阻止进程违反SELinux策略访问资源的行为
setenforce [ Enforcing | Permissive | 1 | 0 ]
查看avc log
dmesg | grep avc
mediacodec进程对default_prop类型的的文件没有读权限
avc: denied { read } for comm="android.hardwar" name="u:object_r:default_prop:s0" dev="tmpfs"
ino=2573 scontext=u:r:mediacodec:s0 tcontext=u:object_r:default_prop:s0 tclass=file permissive=0
模块编译selinux:
make sepolicy
Android8.0后台,替换修改的策略文件
adb push out/tartget/product/XXX/system/etc/selinux/ system/etc/selinux/
adb push out/tartget/product/XXX/vendor/etc/selinux/ vendor/etc/selinux/
/system/etc/selinux:
plat_and_mapping_sepolicy.cil.sha256
plat_file_contexts
plat_hwservice_contexts
plat_mac_permissions.xml
plat_property_contexts
plat_seapp_contexts
plat_sepolicy.cil
plat_service_contexts
/vendor/etc/selinux:
nonplat_file_contexts
nonplat_property_contexts
nonplat_service_contexts
precompiled_sepolicy.plat_and_mapping.sha256
nonplat_hwservice_contexts
nonplat_seapp_contexts
plat_sepolicy_vers.txt
vndservice_contexts
nonplat_mac_permissions.xml
nonplat_sepolicy.cil
precompiled_sepolicy
audit2allow
将avc权限log转成te语句(使用audit2allow工具或者手动添加)
audit2allow在external\selinux\prebuilts\bin目录下
执行audit2allow 之前需要source lunch
./audit2allow -i avc.txt > avc.te
5 博客推荐
https://source.android.com/security/selinux/images/SELinux_Treble.pdf
https://segmentfault.com/a/1190000012780150
http://blog.csdn.net/luoshengyang/article/details/35392905
https://blog.csdn.net/myarrow/article/details/10105961
https://blog.csdn.net/innost/article/details/19299937
https://blog.csdn.net/diaoxuesong/article/details/104572033