前言

在底层驱动写了一个节点,应用在写入的时候,发现没有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权限,可以手动添加。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐