Android selinux 解决实例

博客围绕Android新增设备/dev/video8应用访问时出现selinux权限问题展开。初始尝试修改已定义设备权限遇neverallow错误,后单独定义设备仍有write权限问题。经分析是MLS相关问题,最终通过给新建的video_usb_device设备类型增加mlstrustedobject解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题如下,新增一个 设备 /dev/video8, 在 应用访问时 log 中 报出 avc: denied, 是 selinux 问题, 初始的 log 如下:

06-17 20:23:09.244 19650 19650 I pool-4-thread-1: type=1400 audit(0.0:530): avc: denied { read } for name="video8" dev="tmpfs" ino=224395 scontext=u:r:untrusted_app_25:s0:c512,c768 tcontext=u:object_r:video_device:s0 tclass=chr_file permissive=1
06-17 20:23:09.244 19650 19650 I pool-4-thread-1: type=1400 audit(0.0:532): avc: denied { ioctl } for path="/dev/video8" dev="tmpfs" ino=224395 ioctlcmd=5600 scontext=u:r:untrusted_app_25:s0:c512,c768 tcontext=u:object_r:video_device:s0 tclass=chr_file permissive=1

 

解决方法:

scontext=u:r:untrusted_app_25:s0:c512,c768

tcontext=u:object_r:video_device:s0

tclass=chr_file

根据这个信息,一般的做法是 在 untrusted_app_25.te 中增加  allow untrusted_app_25 video_device:chr_file { read ioctl }; 现在 android 也自带了一个 命令 audio2allow -i error.txt (把avc: denied 的log 拷贝出来存成 error.txt),就可以生成需要增加的语句。但是这样修改后发现, 编译时包 neverallow, 是因为 video_device 是已经定义过的设备, 修改已经定义的设备的权限就可能包 neverallow。

libsepol.report_failure: neverallow on line 384 of system/sepolicy/public/app.te (or line 8744 of policy.conf) violated by allow untrusted_app_25 video_device:chr_file { read };
libsepol.check_assertions: 1 neverallow failures occurred

 

那么,我们怎么修改呢, 可以单独定义一个设备,在 file_contexts 中定义

/dev/video8    u:object_r:video_usb_device:s0

在 device.te 中定义类型(dev_type 类型的貌似需要以 device 结尾,一开始我定义的 video_device_usb, 编译报错):

type video_usb_device, dev_type;

然后在 untrusted_app_25.te 中 增加:

allow untrusted_app_25 video_usb_device:chr_file rw_file_perms;

理论上 加了 rw_file_perms,这个权限,已经包含了 读 写的 全部权限, 但是 实际情况是 还是 有 avc: denied

06-18 11:48:14.459 8709 8709 W com.cloudminds.roboticservice: type=1400 audit(0.0:286): avc: denied { write } for comm=43616D657261205375726661636554 name="video8" dev="tmpfs" ino=88535 scontext=u:r:untrusted_app_25:s0:c512,c768 tcontext=u:object_r:video_usb_device:s0 tclass=chr_file permissive=0
06-18 11:48:14.979 8709 8709 W com.cloudminds.roboticservice: type=1400 audit(0.0:288): avc: denied { write } for comm=43616D657261205375726661636554 name="video8" dev="tmpfs" ino=88535 scontext=u:r:untrusted_app_25:s0:c512,c768 tcontext=u:object_r:video_usb_device:s0 tclass=chr_file permissive=0
06-18 11:48:15.489 8709 8709 W com.cloudminds.roboticservice: type=1400 audit(0.0:289): avc: denied { write } for comm=43616D657261205375726661636554 name="video8" dev="tmpfs" ino=88535 scontext=u:r:untrusted_app_25:s0:c512,c768 tcontext=u:object_r:video_usb_device:s0 tclass=chr_file permissive=0

这个 write 权限还是有问题, 看到 红色的标记了吗,正常的情况是没有 这两个 c512, c768的。seAndroid 主要采用了两种强制访问的方法: TE  MLS。 我们经常接触到的都是 TE。 这个恰好是 MLS 相关的,这个部分比较难理解,详细的可以读下面连接的文档

https://blog.youkuaiyun.com/l173864930/article/details/17194899。 我用到的只参考了下面的部分:

在SEAndroid中共定义了三个拥有巨大权限的attribute分别是mlstrustedsubject、mlstrustedobject、unconfineddomain,被分类到mlstrustedsubject的type在充当主体domain是可以越过MLS检查,被分类到mlstrustedobject的type在充当客体时可以越过MLS检查,被分到unconfineddomain的type则拥有所有权限可对客体进行任意操作。
在SEAndroid中被分在mlstrustedsubject attribute中的type有adbd、debuggerd、drmserver、init、installd、kernel、mediaserver、netd、surfaceflinger、su、system、vold、zygote。
被分在mlstrustedobject attribute中的type有alarm_device、ashmem_device、binder_device、log_device、mtp_device、nv_device、powervr_device、ptmx_device、null_device、cgroup、sysfs、sysfs_writable、sysfs_writable、sysfs_writable、debugfs、apk_data_file、cache_file、dnsproxyd_socket。
被分在unconfineddomain的type有init、kernel、su。

 

按照上面的说明,我可以把 untrusted_app_25 类型定义的时候加上 mlstrustedsubject, 但是增加后 编译报错,因为 untrusted_app_25 是已经定义好的类型, 再定义会冲突。 那么只能修改 video_usb_device 设备的类型了,这个是我们新建的,可以定义, 在 类型定义中,我增加了 mlstrustedobject, 问题解决。

type video_usb_device, dev_type, mlstrustedobject;

 

转载于:https://www.cnblogs.com/mojl-cnblogs/p/11053174.html

### 如何在RK3588设备上运行Android 13时禁用SELinux 为了在RK3588设备上运行Android 13并成功禁用SELinux,可以采取以下方法: #### 修改init.rc文件 通过编辑`init.rc`文件来设置SELinux模式为permissive而非enforcing。具体操作是在`init.rc`中添加或修改如下行: ```bash setprop selinux.plat_sepolicy_version 30.0 set enforcing 0 ``` 这一步骤确保了SELinux不会强制执行其安全策略[^1]。 #### 编辑Zygote源码 如果仅更改`init.rc`不足以完全解决问题,则可能还需要调整Zygote的相关实现。可以通过修改`core/jni/com_android_internal_os_Zygote.cpp`以及`cmds/app_process/app_main.cpp`中的逻辑来解决无法正确设置UID的问题。此过程涉及深入理解Zygote进程初始化阶段的行为及其与SELinux交互的方式[^2]。 #### 调整ADB守护程序行为 对于某些特定场景下(例如调试环境中),还需注意调整ADB服务的行为以适应新的SELinux状态。可以在`system/core/adb/daemon/main.cpp`里找到函数定义部分,并将其返回值改为固定形式从而避免因SELinux引起的能力集丢弃问题: ```cpp static bool should_drop_capabilities_bounding_set() { return false; } ``` 上述改动使得即使处于permissive模式下的SELinux也不会影响到ADB功能正常运作[^3]。 #### 关于工具链配置注意事项 无论采用何种方式构建目标平台镜像,在实际开发过程中都应保持一致性的编译环境设定。即忽略掉不同版本之间可能存在差异性因素干扰最终效果评估标准——只要确认host等于arm-linux即可满足基本需求[^4]。 综上所述,以上措施能够有效帮助开发者实现在基于RK3588硬件架构之上部署支持permissive SELinux特性的Android 13操作系统实例的目标。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值