详解Android Selinux 权限及问题

时间:2022-07-28
本文章向大家介绍详解Android Selinux 权限及问题,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

由于现做的是MTK平台,源码路径基于MTK, 不过高通大同小异

说明

Android 5.0以后完全引入了 SEAndroid/SELinux 安全机制,这样即使拥有 root 权限或 chmod 777 ,仍然无法再JNI以上访问内核节点。

其实在 Android 4.4 就有限制的启用此安全机制了。后面内容都按照 5.0 以后介绍,4.4 会有些许差异。

SELinux Mode

SELinux 分为两种模式,Android 5.0 后所有进程都使用 enforcing mode。

enforcing mode: 限制访问
permissive mode: 只审查权限,不限制

SELinux Policy文件路径

# Google 原生目录 
external/sepolicy

# 厂家目录,高通将 mediatek 换为 qcom
alpsdevicemediatekcommonsepolicy
alpsdevicemediatek<platform sepolicy

编译时将以合并的方式将厂家policy追加到Google原生。

Log

没有权限时可以在内核找到如下 log :

# avc: denied { 操作权限 } for pid=7201 comm=“进程名” scontext=u:r:源类型:s0 tcontext=u:r:目标类型:s0 tclass=访问类型 permissive=0

avc: denied {getattr read} for pid=7201 comm="xxx.xxx" scontext=u:r:system_app:s0 tcontext=u:r:shell_data_file:s0 tclass=dir permissive=0
``` 
## 权限修改
主要有三种方式,前两种只能用来测试,第三种是推荐的正式处理方式。
### adb在线修改seLinux
```bash
# Enforcing - 表示已打开 ,Permissive - 表示已关闭
getenforce;  //获取当前seLinux状态
setenforce 1; //打开seLinux
setenforce 0; //关闭seLinux

kernel中关闭

# alpskernel-3.18archarm64configsxxx_defconfig
CONFIG_SECURITY_SELINUX=y // 屏蔽此配置项

SELinux Sepolicy中添加权限

修改相应源类型.te文件(基本以源进程名命名),添加如下一行语句:

# 格式
allow 源类型 目标类型:访问类型 {操作权限}; // 注意分号

# 实例,具体写法参考源码
allow system_app shell_data_file:dir{getattr read write};
allow mediaserver tfa9897_device:chr_file { open read write }; 
allow system_server tfa9897_device:chr_file rw_file_perms; 

chr_file - 字符设备 file - 普通文件 dir - 目录

通常很少修改Google default 的policy, 推荐更新mediatek 下面的相关的policy.

新建节点

如果是自己新建的节点,需要在 sepolicy 路径下的 file_contexts 文件中做如下添加:

# 参考已有的格式
/dev/goodix_fp     u:object_r:goodixfp_device:s0

Android 5.0 修改的文件为device.te 和 file_contexts.be,而且device/mediatek/common/BoardConfig.mk 中的 BROAD_SEPOLICY_UNION 增加对应的xxxx.te。

编译

# 模块编译
mmm external/sepolicy
make -j24 ramdisk-nodeps & make -j24 bootimage-nodeps

# 整编
make -j24

ps添加权限后的neverallowed冲突

编译报错:

libsepol.check_assertion_helper: neverallow on line xxx ofexternal/sepolicy/domain.te ……

原因:

新添加的sepolicy项目违反了domain.te 中规定的的总策略原则。所以该条权限策略不能添加,如果强行添加的话有CTS测试失败的风险。

解决方法:

1.从运行log中找到要访问的目标名称,一般是name字段后的名称 avc: denied { read write } for pid=303 comm=”mediaserver” name=”tfa9890″dev=”tmpfs” ino=3880 scontext=u:r:mediaserver:s0tcontext=u:object_r:device:s0tclass=chr_file permissive=0

2.找到相应的*_contexts文件。

一般有file_contexts, genfs_contexts, property_contexts, service_contexts 等文件

在contexts文件中指定要访问的目标为一个“源类型 ”有权限访问的“目标类型”

如:在file_contexts中添加: /dev/tfa9890 u:object_r:audio_device:s0

举例

添加权限:

在mediaserver.te中添加allow mediaserver device:chr_file { read write open};

编译报错: libsepol.check_assertion_helper: neverallow on line 258 ofexternal/sepolicy/domain.te (or line 5252 of policy.conf) violated byallow mediaserver device:chr_file { read write open};

违反了domain.te 258的: neverallow {domain –unconfineddomain –ueventd } device:chr_file { open read write}

运行Log: avc: denied { read write } for pid=303 comm=”mediaserver”name=”tfa9890″ dev=”tmpfs” ino=3880 scontext=u:r:mediaserver:s0 tcontext=u:object_r:device:s0tclass=chr_file permissive=0

修改步骤:

1.目标名称是: tfa9890, 其在系统中的路径是: /dev/tfa9890, 是audio相关的设备文件 2.源类型是mediaserver, 在mediaserver.te 文件中发现其具有 audio_device 目标类型的权限 3.所以在file_contexts 中添加 “/dev/tfa9890 u:object_r:audio_device:s0” 可以解决问题

以上就是本文的全部内容,希望对大家的学习有所帮助。