SELinux权限问题分析与修改
SELinux是Google从android 5.0开始,强制引入的一套非常严格的权限管理机制,主要用于增强系统的安全性
通常进行文件/分区读写时,就可能存在缺少SELinux权限,而导致读取和写入值失败。首先时如何确认时否是SElinux权限引起的问题:
一、通过log分析:
通过查看手机中的log,过滤出关键信息(关键字avc: denied):
比如这一条log:
M000001 01-01 00:02:15.109 223 223 W init : type=1400 audit(0.0:15): avc: denied { write } for name=“misc” dev=“mmcblk0p37” ino=23 scontext=u:r:vendor_init:s0 tcontext=u:object_r:system_data_file:s0 tclass=dir permissive=0
1、这句log中给出的信息:
1)denied { write } :缺少写的权限
2)scontext=u:r:vendor_init:文件:vendor_init.te
u:object_r:system_data_file:s0 ----> user:role:type:sensitivity //用户(u):角色(system_data):类型(文件):权限(s0)
根据信息在对应文件 vendor_init.te 给对应的权限:allow vendor_init system_data_file:dir { write }
2、再如这个log:
M000001 01-01 00:02:14.979 1 1 W init : type=1400 audit(0.0:7): avc: denied { getattr } for path="/data/nativetest" dev=“mmcblk0p37” ino=7 scontext=u:r:init:s0 tcontext=u:object_r:nativetest_data_file:s0 tclass=dir permissive=0 b/77873135
根据信息在对应文件:init.te给对应的权限:allow init nativetest_data_file:dir { getattr }
对应文件的路径通常都是在:devices/sprd/mpool/sepolicy/vendor/domain.te 或者 device/sprd/sharlk/common/sepolicy/domain.te
(给权限我们采取需要什么权限就给哪些,不要多给的原则)
对应文件在项目的路径: device/sprd/sharlk/common/sepolicy/init.te(具体根据项目来)
给了SELinux权限时,可能会报错(neverallow ):
libsepol.report_failure: neverallow on line 31 of system/sepolicy/private/domain.te (or line 26746 of policy.conf) violated by allow system_app sysfs:file { read write create setattr open };
libsepol.report_failure: neverallow on line 507 of system/sepolicy/public/app.te (or line 8383 of policy.conf) violated by allow system_app sysfs:file { write };
libsepol.check_assertions: 2 neverallow failures occurred
这时可以根据错误去修改:
修改system/sepolicy/private/domain.te中的内容,增加-system_app
# /sys
neverallow {
coredomain
-init
-ueventd
-vold
-system_app //排除system_app
} sysfs:file no_rw_file_perms;
这样去改的话,就可能会对GMS造成fail了,你需要自己去评估/测试是否要过GMS测试,以及别的规避方法。
GMS需要测试的case:(有点多,但国内可能不需要过GMS)
调节Selinux权限得一个一个给,需要长时间的调试。
二、通过直接给SElinux权限确认:
在userdebug模式下,直接给权限,查看问题是否得到解决,如果给了SElinux权限,问题未复现,那基本可以确定是权限问题导致的了。
注:在USER 版本中无法设置SELinux权限
adb shell getenforce //显示SELinux权限,一般默认的为:Enforcing
adb shell setenforce 0 //给SELinux权限,Permissive
adb shell setenforce 1 //恢复到原来的状态