android设置selinux权限

前言

在底层驱动写了一个节点,应用在写入的时候,发现没有selinux权限,因此记录一下

一、添加节点的selinux权限步骤

1、在device/qcom/sepolicy/common/file.te文件中添加变量

创建一个新的变量

#tp file type for sysfs access
type sysfs_tp, sysfs_type, fs_type;

2、在device/qcom/sepolicy/common/file_contexts文件中,将创建的节点声明为第一步的变量

注:此处的节点路径,不能是软链接路径。

/sys/class/tp/cmcp_test                       u:object_r:sysfs_tp:s0

3、编译,应用在调用该节点后,会有如下类似的报错

<36>[13393.013691] c2   420 [logd.auditd] type=1400 audit(1542296164.999:120): avc: denied { write } for pid=4116 comm="sh" name="cmcp_test" dev="sysfs_tp" ino=30217 scontext=u:r:system_app:s0 tcontext=u:object_r:sysfs_tp:s0 tclass=file permissive=0

表示没有该节点的写入权限

4 添加对应规则

4.1手动添加

添加公式为:allow + scontext + tcontext + “:” + “tclass” + permission

根据公式将该代码加入到对于的te文件。例如;

allow system_app sysfs_tp:file write
4.2 自动添加
  • 1 提取所有的avc LOG. 如 adb shell “cat /proc/kmsg | grep avc” > avc_log.txt
  • 2 使用 audit2allow tool 直接生成policy.
  • 3 audit2allow -i avc_log.txt 即可自动输出生成的policy。第一行表示为system_app.te文件,第二行为要添加的代码
  • 有时候使用audit2allow -i avc_log.txt命令会报其他错误,因此可以增加 -p参数,意思是使用指定的policy文件来解析,一般来说这个文件路径是out/target/product/xxx/recovery/root/sepolicy
audit2allow -i avc_log.txt -p out/target/product/xxx/recovery/root/sepolicy
#============= system_app ==============
allow system_app sysfs_tp:file write
  • 4 将对应的policy 添加到selinux policy 规则中

二、小结

当出现<36>[13393.013691] c2 420 [logd.auditd] type=1400 audit(1542296164.999:120): avc: denied { write } for pid=4116 comm="sh" name="cmcp_test" dev="sysfs_tp" ino=30217 scontext=u:r:system_app:s0 tcontext=u:object_r:sysfs_tp:s0 tclass=file permissive=0该报错,就是缺少linux权限,可以手动添加。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值