init启动Native Service时出现Service xxxx needs a SELinux domain defined;

来源:互联网 发布:钢结构图纸及数据图片 编辑:程序博客网 时间:2024/06/02 07:53
在android 5.0 后, 默认启用了Enforcing SELinux. 有很多同仁经常会从kernel log 中看到这样
的警告.
"[1:init]init: Warning! Service xxxx needs a SELinux domain defined; please fix!"
其原因是因为Google 要求init 启动service 时,都要进行SELinux domain 切换,即从init
domain 切换到另外的domain. 这个是从安全方面考虑的, 默认init domain 的SELinux 权限很大,
可以做很多危险行为,比如mount image, kill process 等等. 如果普通service 使用 init
domain, 就增大了可攻击的安全面.
Google 在CTS 中有对这一项进行检查, CTS fail of android.security.cts.SELinuxDomainTest
# testInitDomain
通常情况下,如果init 启动的是一个可快速执行完的oneshot 的service, 即非deamon 程序, “一
闪而过” 的话,可以不进行domain 切换. 此类CTS 检测不到. 如果init 启动的是常驻内存的
deamon service, 那么一定要进行domain 切换.(L0/L1 版版本)
但在M版本上,Google 增强了这块的约束,通过使用neverallow init { file_type fs_type}:file
execure_no_trans;严格限制了init 启动service 都必须进行domain 切换,否则service 无法启动
!!!
下面是一个demo, 方便大家参考.
定义一个init 启动的service, demo_service, 对应的执行档是/system/bin/demo.
(1). 创建一个demo.te 在/device/mediatke/common/sepolicy 目录下, 然后在
/device/mediatke/common/BoardConfig.mk 的BOARD_SEPOLICY_UNION 宏中新增 demo.te (注意: M
版本后取消了BOARD_SEPOLICY_UNION 宏,不需要再修改了,添加了文件即可)
(2). 定义demo 类型,init 启动service 时类型转换, demo.te 中
type demo, domain;
type demo_exec, exec_type, file_type;
init_daemon_domain(demo)
(3). 绑定执行档 file_context 类型
/system/bin/demo u:object_r:demo_exec:s0
(4). 根据demo 需要访问的文件以及设备, 定义其它的权限在demo.te 中.
如果大家需要更加深入理解SELinux ,可以参考:
mtk Online SELinux Topic, 没有比这更加完整的文档了.
http://online.mediatek.com/_Layouts/15/mol/topic/ext/Topic.aspx?id=158
阅读全文
0 0
原创粉丝点击