分享

详解Android Selinux 权限及问题

 宇宙之彬 2020-09-10

发布时间: 2019-03-20 22:28:46 来源: 互联网 作者: Andy.Lee 栏目: Android 点击: 208

本篇文章主要介绍了详解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 换为 qcomalps\device\mediatek\common\sepolicyalps\device\mediatek\<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; //打开seLinuxsetenforce 0; //关闭seLinux

kernel中关闭

# alps\kernel-3.18\arch\arm64\configs\xxx_defconfigCONFIG_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/sepolicymake -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” 可以解决问题

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多