Android11-SElinux-avc权限问题

image

Android SElinux avc: denied 权限问题 解决办法

背景

在 Android 11 修改开机工模的按键灯问题,发现写入 /sys/devices/platform/device_info/key_led 节点时,权限不够,查看logcat 原因如下:
type=1400 audit(0.0:371): avc: denied { write } for name="key_led" dev="sysfs" ino=26274 scontext=u:r:system_app:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0

介绍

安全增强型 Linux(Security-Enhanced Linux)简称 SELinux,它是 Linux 的一个安全子系统。SELinux 主要作用是最大限度地减小系统中服务进程可访问的资源(最小权限原则)。对资源的访问控制分为两类: DAC和MAC

SELinux 工作模式

SElinux 有三种工作模式:

  1. enforcing: 强制模式, 执行SELinux规则, 违反的行为会被阻止
  2. permissive: 宽容模式, 执行SELinux规则, 违反的行不会被阻止
  3. disabled: 关闭SELinux

avc denied

在Android系统开发中, 可能会遇到SELinux的权限不足而引起的各种问题;可以尝试将SELinux工作模式临时改为宽容模式看问题是否解决, 来判定是否是SELinux引起的问题标志性Log:
avc: denied { 操作权限 } for pid=7201 comm="进程名" scontext=u:r:源类型:s0 tcontext=u:r:目标类型:s0 tclass=访问类型 permissive=0

  1. 源类型:授予访问的类型,通常是进程的域类型
  2. 目标类型:客体的类型,它被授权可以访问的类型
  3. 访问类型
  4. 操作权限:表示主体对客体访问时允许的操作类型(也叫做访问向量)。

举例

如上面的错误为例:
type=1400 audit(0.0:371): avc: denied { write } for name="key_led" dev="sysfs" ino=26274 scontext=u:r:system_app:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0

  1. write: 表示没有 write 权限
  2. system_app: system_app中缺少权限
  3. file: file类型的文件
  4. 分析: adb shell setenforce 0。设置SELinux 成为permissive模式。若apk可正常使用,则进行规避此权限问题

修改 SELinux 规则

type=1400 audit(0.0:371): avc: denied { write } for name="key_led" dev="sysfs" ino=26274 scontext=u:r:system_app:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0

节点 寻找点 结果
缺少的权限 denied wirte
谁(哪个进程)缺少权限 scontext=u:r:进程:s0 system_app
对哪个节点缺少权限 tcontext=u:r:节点:s0 sysfs
缺少要访问的对象 tclass=被访问对象 file

技巧:

  • 如果 tcontext 中的节点与 scontext 中的进程名相同,则可以写成 self
  • 因为有时候 avc denied 的 log 不是一次性显示所有问题,要等解决一个权限问题后,才会显示另外 一个权限问题,比如显示缺失某个目录的 read 权限。所以建议是先参考其他可能出现访问权限的问题的有关接口关键字,进行多次添加。
  • 要加入的权限很多时,可以用中括号,比如:allow system_app sysfs:file write;
    由于 scontext 对应的是我们要修改的文件(这里是 system_app ),根据下面的提示:
    type=1400 audit(0.0:371): avc: denied { write } for name="key_led" dev="sysfs" ino=26274 scontext=u:r:system_app:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0
    因此可以在 system.app.te 最后面中添加所对应的权限是:
    //file : system_app.te
    allow system_app sysfs:file write;
    对应 XCheng 目录为:
    t-alps-release-r0.mp1-V5\vendor\xcheng\sepolicy\system_app.te
    对应 MTK 目录为:
    t-alps-release-r0.mp1-V5\device\mediatek\sepolicy\basic\plat_private\system_app.te
    在看一个例子:
  1. type=1400 audit(0.0:41): avc: denied { read } for
  2. name="u:object_r:media_prop:s0"
  3. dev="tmpfs" ino=11511
  4. scontext=u:r:mediacodec:s0
  5. tcontext=u:object_r:media_prop:s0
  6. tclass=file permissive=0
  7. file: mediacodec.te
    allow mediacodec media_prop:file {read};

修改系统 SELinux 模式

临时修改:

adb shell setenforce 0 #设置SELinux 成为permissive模式
adb shell setenforce 1 #设置SELinux 成为enforcing模式
adb shell getenforce #获取SELinux状态(permissive,enforcing,disabled)

修改读取权限

在 \t-alps-release-r0.mp1-V5\device\mediatek\mtxxxx\init.mtxxxx.rc 目录 修改:
chown system system /sys/devices/platform/device_info/key_led
chmod 0666 /sys/devices/platform/device_info/key_led

敏感权限

在修改完上面的问题,如果此权限是敏感权限,编译会报如下错误:
out/target/product/k61v1_32_bsp_1g/obj/FAKE/sepolicy_neverallows_intermediates/sepolicy_neverallows
/bin/bash -c "(ASAN_OPTIONS=detect_leaks=0 out/host/linux-x86/bin/checkpolicy -M -c 30 -o out/target/product/k61v1_32_bsp_1g/obj/FAKE/sepolicy_neverallows_intermediates/sepolicy_neverallows.tmp out/target/product/k61v1_32_bsp_1g/obj/FAKE/sepolicy_neverallows_intermediates/policy.conf ) && (out/host/linux-x86/bin/sepolicy-analyze out/target/product/k61v1_32_bsp_1g/obj/FAKE/sepolicy_neverallows_intermediates/sepolicy_neverallows.tmp neverallow -w -f out/target/product/k61v1_32_bsp_1g/obj/FAKE/sepolicy_neverallows_intermediates/policy_2.conf || ( echo "" 1>&2; echo "sepolicy-analyze failed. This is most likely due to the use" 1>&2; echo "of an expanded attribute in a neverallow assertion. Please fix" 1>&2; echo "the policy." 1>&2; exit 1 ) ) && (touch out/target/product/k61v1_32_bsp_1g/obj/FAKE/sepolicy_neverallows_intermediates/sepolicy_neverallows.tmp ) && (mv out/target/product/k61v1_32_bsp_1g/obj/FAKE/sepolicy_neverallows_intermediates/sepolicy_neverallows.tmp out/target/product/k61v1_32_bsp_1g/obj/FAKE/sepolicy_neverallows_intermediates/sepolicy_neverallows )"
libsepol.report_failure: neverallow on line 517 of system/sepolicy/public/app.te (or line 10326 of policy.conf) violated by allow system_app sysfs:file { write };
大致问题: neverallow on line 517 of system/sepolicy/public/app.te (or line 10326 of policy.conf) violated by allow system_app sysfs:file { write }; 先确认需要访问的节点是否为 system_app ,这个节点属于敏感权限,可以的话请修改访问的目录和文件,缩小 system_app 的范围 解决该问题步骤如下:

  • 确定访问的节点位置,通过源码或者 log 确定到底访问的哪一个具体的节点, 例如 :
    /sys/devices/platform/device_info/key_led
  • 在相应的 te 文件中新声明一个节点名称, 如:
    t-alps-release-r0.mp1-V5\vendor\xcheng\sepolicy\file.te
    type xc_sysfs_key_led, fs_type, sysfs_type;
  • 在file_context中将具体节点绑定新的节点名称, 如:
    t-alps-release-r0.mp1-V5\vendor\xcheng\sepolicy\file_context
    /sys/devices/platform/device_info/key_led u:object_r:xc_sysfs_key_led:s0
  • 增加或修改需要的权限:
    t-alps-release-r0.mp1-V5\vendor\xcheng\sepolicy\system_app.te
    allow system_app xc_sysfs_key_led:file rw_file_perms; or
    allow system_app xc_sysfs_key_led:write;
posted @ 2024-06-06 17:29  白云一片去悠悠  阅读(596)  评论(1)    收藏  举报