Android selinux 权限添加

张开发
2026/4/12 0:12:29 15 分钟阅读

分享文章

Android selinux 权限添加
1.问题排查当某个功能不能正常使用且日志有相关的avc denial时首先排查是不是SELinux权限引发的问题通过 setenforce 命令关闭SELinux。关闭SELinux Permissive后 功能正常了说明就是权限问题此时需要增加相关的权限。Permissiver 仅用于问题排查添加权限时需要为 Enforcing 状态。2.日志解析例如[ 29.428184][ T423] type1400 audit(1773301890.624:4): avc: denied { search } for commBootAnimation namedata devdm-51 ino301 scontextu:r:bootanim:s0 tcontextu:object_r:system_data_file:s0:c512,c768 tclassdir permissive0denied { search } 被拒绝的操作这里是搜索相应的还有getattr read open 等等。commBootAnimation触发该操作的进程名。namedata尝试访问的文件/目录名。devdm-51设备映射器表示加密/分区设备不重要。scontextu:r:bootanim:s0源上下文进程的上下文。tcontextu:object_r:system_data_file:s0:c512,c768目标上下文被访问的文件/目录的上下文。tclassdir目标类型相应的还有file lnk_file等等。permissive0enforcing 模式非 permissive。这表明BootAnimation 进程尝试搜索 /data 目录但被 SELinux 阻止了。根本原因是SELinux 策略中bootanim 域缺少对 system_data_file 类型目录的 search 权限。3.添加权限找到相关的策略文件在 device/cix/sky1/sky1_evb/sepolicy 目录下找到 bootanim.te 文件如果没有就创建然后在该文件中添加allow bootanim system_data_file:dir { search };即 allow 源上下文 目标上下文目标类型 { 需要的权限 } 即可。如果不想手动编写策略可以在主机上下载相应的工具 audit2allow 和 selinux 策略文件安装命令如下sudo apt install policycoreutilssudo apt install selinux-policy-default在开发板中收集 avc denialdmesg | grep avc dmesg_avc.loglogcat | grep avc logcat_avc.log将上面的 log 传到主机上然后执行 audit2allow -i dmesg_avc.log audit2allow -i logcat_avc.log亲测不好用不推荐建议手动分析编写或者把日志提交给AI。4.特殊情况4.1 ioctl 权限的添加如果有类似 avc: denied { ioctl } 的权限问题比如avc: denied { ioctl } for commsystem_server path/dev/__properties__ devtmpfs ino123 ioctlcmd0x7701 scontextu:r:system_server:s0 tcontextu:object_r:properties_device:s0除了添加 allow system_server properties_device:file { ioctl }; 之外还需要额外指定具体的 ioctl 命令allowxperm system_server properties_device:file ioctl { 0x7701 };4.2 解决neverallow冲突添加好规则后进行编译可能会造成冲突比如有如下规则avc: denied { read } for commbootanim path/sys/devices/platform/special/special_file scontextu:r:bootanim:s0 tcontextu:object_r:sysfs:s0 tclassfile按照之前的分析我们需要添加 allow bootanim sysfs:file { read }; 但这样后直接违反neverallow造成编译错误。此时需要在 bootanim.te 中定义新类型type bootanim_special_file, fs_type, sysfs_type, file_type;然后在 genfs_contexts 中为目标文件绑定标签genfscon sysfs /devices/platform/special/special_file u:object_r:bootanim_special_file:s0最后在 bootanim.te 中添加规则allow bootanim bootanim_special_file:file { read };这样即规避了neverallow冲突同时也给目标文件增加了相应的访问权限。一般在以下几种情况考虑这种添加方式访问 /sys/ 下特定文件sysfs访问 /proc/ 下特定文件procfs访问 /dev/ 下自定义设备节点访问特定应用程序数据目录直接添加allow规则编译失败提示违反neverallow绑定标签时需要判断目标文件所在的文件系统以选择正确的配置文件| 路径前缀 | 文件系统类型 | 配置文件 | 语法示例 ||----------|--------------|----------|----------|| /sys/ | sysfs | genfs_contexts | genfscon sysfs /devices/... u:object_r:type:s0 || /proc/ | procfs | genfs_contexts | genfscon proc /... u:object_r:type:s0 || /dev/ | devtmpfs | file_contexts | /dev/xxx u:object_r:type:s0 || /data/ | ext4/f2fs | file_contexts | /data/xxx u:object_r:type:s0 || /system/ | ext4/erofs | file_contexts | /system/xxx u:object_r:type:s0 || /vendor/ | ext4/erofs | file_contexts | /vendor/xxx u:object_r:type:s0 |对于虚拟文件系统大部分是 path 为 sys、proc、dev 开头的文件将绑定的语法添加到 genfs_contexts 文件中genfs_contexts不支持通配符和正则表达式需要进行精确匹配。对于普通文件系统大部分是 path 为 data、system、vendor 开头的文件需要将绑定的语法添加到 file_contexts 文件中file_contexts支持通配符和正则表达式。对于其他不确定的文件直接用AI判断。添加之后可在系统中通过命令 ls -alZ 文件路径 来判断该文件的标签是否绑定成功。4.3 unknown type duplicate declaration对于平台定义好的类型比如avc: denied { create } for commapp_process anonclass[userfaultfd] scontextu:r:tradeinmode:s0 tcontextu:object_r:tradeinmode:s0 tclassanon_inode/system/sepolicy/private 已经存在 tradeinmode.te 文件如果按照以前的方式添加编译时会遇到 unknown type 即使添加相应 type 后又会遇到 duplicate declaration。此时我们需要在 device/cix/sky1/sky1_evb/sepolicy 目录下添加 system_ext/private 目录然后添加 tradeinmode.te 文件。在该文件第一行添加 userfaultfd_use(tradeinmode) 然后再添加相应的 allow此外还需要更改device/cix/sky1/sky1_evb/BoardConfig.mk 文件添加下面内容SYSTEM_EXT_PRIVATE_SEPOLICY_DIRS $(CIX_DEVICE_PATH)/sepolicy/system_ext/private这样保证 Vendor 策略和 Platform 策略是两路编译。

更多文章