目的
为新添加的服务添加Selinux权限并解决拒绝事件
第一步
需要在rc文件中添加服务,以testSer服务为例,首先在init.device.rc
文件中添加
class core表示开机会启动这个服务
service testSer /system/bin/testSer
class core
第二步
为我们新添加的这个服务创建一个新域,创建包含以下内容的文件
device/manufacturer/device-name/sepolicy/testSer/testSer.te:
# testSer service
# 如果服务对应的可执行文件是放在system/bin下面的需要加上coredomain
# 如果是放在vendor/bin目录下的则不需要加
type testSer, domain, coredomain;
type testSer_exec, exec_type, file_type;
init_daemon_domain(testSer)
第三步
为 /system/bin/testSer
添加标签
创建文件device/manufacturer/device-name/sepolicy/testSer/file_contexts:
/system/bin/testSer u:object_r:testSer_exec:s0
第四步
新建mk文件将新添加的目录device/manufacturer/device-name/sepolicy/testSer/
编译到车机
验证标签是否添加成功
构建并刷写启动映像和系统映像,进入到相应的目录下。使用以下代码查看标签是否添加成功
ls -Z | grep testSer
添加标签成功后需要解决拒绝事件
通过命令 adb shell dmesg | grep testSer
会抓取到如下类似的log
*type=1400 audit(1586514832.429:1150): avc: denied { write } for pid=6631 comm="testSer" name="dropbox" dev="mmcblk0p81" ino=895 scontext=u:r:testSer:s0 tcontext=u:object_r:system_data_file:s0 tclass=dir permissive=1
这里只需要注意以下字符串
"scontext=u:r:testSer:s0"
"tcontext=u:object_r:system_data_file:s0"
"tclass=dir"
"denied { write }"
然后在testSer.te
中添加:
allow testSer system_data_file:dir write;
如果相同的scontext和tcontext出现多个denied,例如"denied { open }" 可以使用以下格式:
allow testSer system_data_file:dir {write open};