源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

详解Android Selinux 权限及问题

  • 时间:2022-04-01 18:31 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:详解Android Selinux 权限及问题
由于现做的是MTK平台,源码路径基于MTK, 不过高通大同小异 [b]说明[/b] Android 5.0以后完全引入了 SEAndroid/SELinux 安全机制,这样即使拥有 root 权限或 chmod 777 ,仍然无法再JNI以上访问内核节点。 其实在 Android 4.4 就有限制的启用此安全机制了。后面内容都按照 5.0 以后介绍,4.4 会有些许差异。 [b]SELinux Mode[/b] SELinux 分为两种模式,Android 5.0 后所有进程都使用 enforcing mode。
enforcing mode: 限制访问
permissive mode: 只审查权限,不限制
SELinux Policy文件路径
# Google 原生目录 
external/sepolicy

# 厂家目录,高通将 mediatek 换为 qcom
alps\device\mediatek\common\sepolicy
alps\device\mediatek\<platform>\sepolicy
编译时将以合并的方式将厂家policy追加到Google原生。 [b]Log[/b] 没有权限时可以在内核找到如下 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
[b]kernel中关闭[/b]
# alps\kernel-3.18\arch\arm64\configs\xxx_defconfig
CONFIG_SECURITY_SELINUX=y // 屏蔽此配置项
[b]SELinux Sepolicy中添加权限[/b] 修改相应源类型.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. [b]新建节点[/b] 如果是自己新建的节点,需要在 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。 [b]编译[/b]
# 模块编译
mmm external/sepolicy
make -j24 ramdisk-nodeps & make -j24 bootimage-nodeps

# 整编
make -j24
[b]ps添加权限后的neverallowed冲突[/b] 编译报错: 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” 可以解决问题 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部