init 启动 Native Service 时出现Service xxxx needs a SELinux domain defined; please fix 警告的说明

来源:互联网 发布:人体数据 编辑:程序博客网 时间:2024/06/05 14:53

[Description]
init 启动 Native Service 时出现Service xxxx needs a SELinux domain defined; please fix 警告的说明
 
[Keyword]
SELinux, Native Service, init, domain

[Android Version]
Version >= android 5.0
 
[Solution]
在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 中.

 

针对特别执行shell 的service , 需要特别处理, 因为/system/bin/sh 已经被定义成了shell_exec 的label. 而有很多这样的service 依赖于sh, 于是需要使用seclabel 操作.

(1). 在你的service 定义中写入 seclabel u:r:demo:s0

(2). 创建你的xxxx_service.te 文件, 和前面类似

type demo, domain;

domain_trans(init, shell_exec, demo)

(3). 根据demo 需要访问的文件以及设备,  定义其它的权限在demo.te 中.

0 0