audit2allow 添加SELinux权限

快速入手必看:

  • 如何确认是否是selinux问题:

可以通过运行 cat /proc/kmsg 或者 cat /sys/fs/pstore/console-ramoops 来获取上次启动时的事件日志。SELinux 日志消息中包含“avc:”字样,因此可使用 grep 轻松找到。

一般如下:

[10104.199705] (0)[433:logd.auditd]type=1400 audit(1609222167.878:20645): avc: denied { read } for comm="com.miui.cit" name="panel_info" dev="sysfs" ino=32487 scontext=u:r:system_app:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0

可以看到permissive=0 代表访问被拒绝,permissive=1代表拥有访问权限。

  • 如何生成selinux rule

只需三步,

第一步,把手机里面的policy文件拉到本地。

第二步,复现问题。

第三步,把log喂给audit2allow命令来帮助我们生成selinux rule。

假如有提示本地环境找不到audit2allow,可以手动根据提示安装或者在Android 目录下寻找。

adb pull /sys/fs/selinux/policy
adb shell dmesg | audit2allow -p policy

这里比较灵活,我们可以直接把dmesg喂给audit2allow也可以直接把logcat喂给audit2allow(adb logcat -b all -d | audit2allow -p policy) ,甚至可以直接cat 或者echo文件给它。比如:

echo "[10104.199705] (0)[433:logd.auditd]type=1400 audit(1609222167.878:20645): avc: denied { read } for comm="com.miui.cit" name="panel_info" dev="sysfs" ino=32487 scontext=u:r:system_app:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0 " | audit2allow -p policy
cat logfile | audit2allow -p policy

最后生成的文件如下:

#============= system_app ==============
allow system_app sysfs:file read;
  • 修复问题:

最后根据生成的文件,在源码目录找到或者新建system_app.te 把“allow system_app sysfs:file read;” 写入到该文件即可。

原理说明(快速入手可跳过)

我们 仍以下面的例子说明,

[10104.199705] (0)[433:logd.auditd]type=1400 audit(1609222167.878:20645): avc: denied { read } for comm="com.miui.cit"  comm="com.miui.cit"  dev="sysfs" ino=32487 scontext=u:r:system_app:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0

其规则可以表述如下

avc: denied { connectto } for comm="com.miui.cit" name="panel_info" dev="sysfs" ino=32487 scontext=u:r:system_app:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0

该输出的解读

上方的 { connectto } 表示执行的操作。根据它和末尾的 tclass (file),您可以大致了解是对什么对象执行什么操作。在此例中,是操作方正在试图读取文件。

scontext (u:r:system_app:s0) 表示发起相应操作的环境,在此例中是 system_app 中运行的某个程序。

tcontext (u:object_r:sysfs:s0) 表示操作目标的环境,在此例中是归 sysfs 所有的某个file。

顶部的 comm="com.miui.cit" 可帮助您了解拒绝事件发生时正在运行的程序。在此示例中,表示小米的cit测试程序。

所以整体来看,这句avc的log就是属于 system_app的“com.miui.cit”(comm="com.miui.cit")需要read属于sysfs的panel_info(comm="com.miui.cit"),但是因为没有权限读取失败。

资料来源:

https://source.android.com/security/selinux/validate

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值