Android13 添加SELinux权限 编译的时候出现 neverallow 编译报错
背景:
项目需要,需要通过init.rc启动一个服务,需要添加SELinux权限。
添加SELinux权限的步骤就不在此详细说明,大致如下:
修改system/sepolicy/prebuilts/api/33.0/private/file_contexts和system/sepolicy/private/file_contexts文件,在system/sepolicy/prebuilts/api/33.0/private/和system/sepolicy/private/目录下添加对应的te文件。以下是我修改的内容
heartbeat.te
type heartbeat, domain;
type heartbeat_exec, exec_type, file_type;
init_daemon_domain(heartbeat)
allow heartbeat sysfs:file {open write};
file_contexts
/system/bin/heartbeat u:object_r:heartbeat_exec:s0
编译的时候在Selinux检测阶段出现 neverallow 编译报错
这个报错的原因,就是定义的te文件allow部分的规则和谷歌定义的neverallow的规则发生了冲突,导致编译不过。
根据报错提示信息,在system/sepolicy/public/domain.te第945行,找到对应文件位置
full_treble_only(`
# Do not allow coredomain to access entrypoint for files other
# than system_file_type and postinstall_file
neverallow coredomain {
file_type
-system_file_type
-postinstall_file
}:file entrypoint;
# Do not allow domains other than coredomain to access entrypoint
# for anything but vendor_file_type and init_exec for vendor_init.
neverallow { domain -coredomain } {
file_type
-vendor_file_type
-init_exec
}:file entrypoint;
')
我添加的是main类型,冲突的部分就是neverallow {domain -coredomain},若是core类型,冲突部分应该是neverallow coredomain。
根据网上的解决方案大部分都是在domain上加上“-heartbeat”,编译报错找不到heartbeat类型,反正在android13上是无法解决。
然后看代码,既然“-”屏蔽规则,这里冲突的部分应该就是“file_type”,尝试将“file_type”改为“-file_type”,编译居然通过,虽然这这办法可行,但会导致google的CTS测试失败,不推荐。
看到domain的规则中
翻译是不允许除coredomain之外的域访问除vendor_file_type和vendor_init的init_exec之外的任何内容的入口点,也就是说coredomain之外的域只能访问vendor_file_type和vendor_init的init_exec,白话的意思是vendor_file_type和init_exec不受规则影响。
修改heartbeat.te
type heartbeat, domain;
type heartbeat_exec, exec_type, vendor_file_type, file_type;
init_daemon_domain(heartbeat)
allow heartbeat sysfs:file {open write};
添加一个vendor_file_type,编译通过,烧写到机器后权限也正常,程序正常运行。
至此问题解决,也不影响google认证,虽然原理不是很清楚。还有,我只在Android13上验证过,其他版本不知道是不是可以用此方法。再说一嘴,网上的文章真是千篇一律,感觉都是一个版本抄袭出来,例子都一摸一样的,不知道有没有验证就发出来,希望大家都能把博客写好,给人以便利,给自己以价值。