【Android】Selinux权限问题记录

目的

为新添加的服务添加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};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值