由于现做的是MTK平台,源码路径基于MTK, 不过高通大同小异
说明
Android 5.0以后完全引入了 SEAndroid/SELinux 安全机制,这样即使拥有 root 权限或 chmod 777 ,仍然无法再JNI以上访问内核节点。
其实在 Android 4.4 就有限制的启用此安全机制了。后面内容都按照 5.0 以后介绍,4.4 会有些许差异。
SELinux Mode
SELinux 分为两种模式,Android 5.0 后所有进程都使用 enforcing mode。
1 2 |
|
SELinux Policy文件路径
1 2 3 4 5 6 |
|
编译时将以合并的方式将厂家policy追加到Google原生。
Log
没有权限时可以在内核找到如下 log :
1 2 3 4 5 6 7 8 9 10 11 12 |
|
kernel中关闭
1 2 |
|
SELinux Sepolicy中添加权限
修改相应源类型.te文件(基本以源进程名命名),添加如下一行语句:
1 2 3 4 5 6 7 8 9 |
|
通常很少修改Google default 的policy, 推荐更新mediatek 下面的相关的policy.
新建节点
如果是自己新建的节点,需要在 sepolicy 路径下的 file_contexts 文件中做如下添加:
1 2 |
|
Android 5.0 修改的文件为device.te 和 file_contexts.be,而且device/mediatek/common/BoardConfig.mk 中的 BROAD_SEPOLICY_UNION 增加对应的xxxx.te。
编译
1 2 3 4 5 6 |
|
ps添加权限后的neverallowed冲突
编译报错:
libsepol.check_assertion_helper: neverallow on line xxx ofexternal/sepolicy/domain.te ……
原因:
新添加的sepolicy项目违反了domain.te 中规定的的总策略原则。所以该条权限策略不能添加,如果强行添加的话有CTS测试失败的风险。
解决方法:
1.从运行log中找到要访问的目标名称,一般是name字段后的名称
avc: denied { read write } for pid=303 comm="mediaserver" name="tfa9890"dev="tmpfs" ino=3880 scontext=u:r:mediaserver:s0tcontext=u:object_r:device:s0tclass=chr_file permissive=0
2.找到相应的*_contexts文件。
一般有file_contexts, genfs_contexts, property_contexts, service_contexts 等文件
在contexts文件中指定要访问的目标为一个“源类型 ”有权限访问的“目标类型”
如:在file_contexts中添加: /dev/tfa9890 u:object_r:audio_device:s0
举例
添加权限:
在mediaserver.te中添加allow mediaserver device:chr_file { read write open};
编译报错:
libsepol.check_assertion_helper: neverallow on line 258 ofexternal/sepolicy/domain.te (or line 5252 of policy.conf) violated byallow mediaserver device:chr_file { read write open};
违反了domain.te 258的:
neverallow {domain –unconfineddomain –ueventd } device:chr_file { open read write}
运行Log:
avc: denied { read write } for pid=303 comm="mediaserver"name="tfa9890" dev="tmpfs" ino=3880 scontext=u:r:mediaserver:s0 tcontext=u:object_r:device:s0tclass=chr_file permissive=0
修改步骤:
1.目标名称是: tfa9890, 其在系统中的路径是: /dev/tfa9890, 是audio相关的设备文件
2.源类型是mediaserver, 在mediaserver.te 文件中发现其具有 audio_device 目标类型的权限
3.所以在file_contexts 中添加 “/dev/tfa9890 u:object_r:audio_device:s0” 可以解决问题
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家