Android SELinux 权限相关问题

问题

再做OTA的时候,需要zip包放在data/ota_package下,再操作的时候,报错如下

avc: denied { write } for path="/data/ota_package/update.zip" dev="dm-9" ino=11969 scontext=u:r:system_app:s0 tcontext=u:object_r:ota_package_file:s0 tclass=file permissive=0

提取关键字

scontext: system_app

tcontext: ota_package_file

tclass: file

denied: write
翻译过来就是 system_app 对于ota_package_file 目录缺少write 权限
我们只需要android/device/qcom/sepolicy_vndr/generic/vendor/common/目录下 的system_app.te添加如下:

allow system_app apk_data_file:dir write;
allow system_app ota_package_file:dir { search write add_name create};
allow system_app ota_package_file:file { getattr read open map create write};

然后在Android 目录下执行make selinux_policy -j96

验证

make selinux_policy产生的编译文件会输出到out/target/product/XXXX/system/etc/selinux 和 out/target/product/XXXX/vendor/etc/selinux目录。
adb push out/target/product/XXXX/system/etc/selinux 和 out/target/product/XXXX/vendor/etc/selinux到设备的/system/etc/和/vendor/etc/目录,重启设备即可生效。

SELinux 权限不起作用

正常情况如上就可以,当然下面就是不一般的问题,在SystemUI里面操作文件节点写值,报错如下:

   systemui: type=1400 audit(0.0:102): avc: denied { write } for name="commit" dev="sysfs" ino=48206 scontext=u:r:platform_app:s0:c512,c768 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0 app=com.android.systemui

按照上面的经验,我们需要再platform_app.te里添加如下:
allow platform_app sysfs:file write
但是这样会有两个问题:

  • 一个是权限放大,这样的话所有的sysfs文件都被赋予了write权限,
  • 二是这样会导致never allow 报错,编译时会提示。所以不推荐这种方法
正确姿势
  • 在device/qcom/sepolicy_vndr / generic/vendor/common/file.te文件里声明如下
type sys_bus_devices_commit, sysfs_type, fs_type;
  • 在device/qcom/sepolicy_vndr / generic/vendor/common/file_contexts
/sys/bus/i2c/devices/0-0006/commit                                                         u:object_r:sys_bus_devices_commit:s0
/sys/devices/platform/soc/4a84000.i2c/i2c-0/0-0006/commit                                  u:object_r:sys_bus_devices_commit:s0
  • 最后在 platform_app.te追加如下
allow platform_app sys_bus_devices_commit:file {getattr read open write};
allow platform_app sys_bus_devices_commit:lnk_file {getattr read open write};

验证方法如上述,可是,死活就是不成功,错误一点都没变,说明添加的一直未生效,最后生生就在这卡了一天,增量编,整编都试了,最后问题出在
android/system/sepolicy/private/mls


mlsconstrain { file lnk_file sock_file chr_file blk_file } { write setattr append unlink link rename }
             (t2 == app_data_file or t2 == privapp_data_file or t2 == appdomain_tmpfs or l1 eq l2 or t1 == mlstrustedsubject or t2 == mlstrustedobject);

上一条的限制就导致了,即使在 platform_app.te 文件中添加的语句验证没有问题,但是这条限制没有通过,权限问题同样没有解决。
最终在file.te里添加如下:

type sys_bus_devices_commit, sysfs_type, fs_type, mlstrustedobject;

重新编译,验证,没问题了,
事实就是如此,真是竟无语凝噎
参考:https://blog.csdn.net/it_rensheng/article/details/121691982

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值