国产平台之T507 开发板Android 安全策略漫谈

发布时间:2021-12-3 14:33    发布者:哦哈哟
关键词: 飞凌嵌入式
[color=rgb(51, 51, 51) !important]硬件平台:飞凌嵌入式 OKT507-C开发板
[color=rgb(51, 51, 51) !important]操作系统:Android10.0


[color=rgb(51, 51, 51) !important]飞凌嵌入式 T507 开发板 Android系统版本为Android10.0,默认开启了SELinux。基于MAC访问控制模型的SElinux,可以更好地保护我们的Android系统, 比如限制系统服务的访问权限、控制应用对数据和系统日志的访问等措施,这样就降低了恶意软件的影响,并且可以防止因代码存在的缺陷而产生的对系统安全的影响。
[color=rgb(51, 51, 51) !important]从系统安全方面考虑,SELinux是保护神,但是从软件开发方面,SELinux就是一道牵绊,这是一把双刃剑。

[color=rgb(51, 51, 51) !important]比如我们开发应用或者增加系统服务的某些权限的时候,我们必须遵循SELinux的规则,给我们的应用设置对应的安全策略,否则我们的应用就不具备访问数据或者设备的权限。下面我们MAC访问控制模型开始,简单的梳理一下飞凌嵌入式 T507 开发板 Android的安全策略,以及自定义飞凌嵌入式 T507 开发板Android安全策略的方法。
访问控制模型DAC,MAC
[color=rgb(51, 51, 51) !important]访问控制是指控制对计算机或者网络中某个资源的访问。没有它,所有人都可以访问任何资源。有了访问控制,用户在获取实际访问资源或进行操作之前,必须通过识别、验证、授权。
[color=rgb(51, 51, 51) !important]自主访问控制(DAC: Discretionary Access Control)系统识别用户,根据被操作对象的权限的设置,来决定该用户对其拥有的操作权限,read、write、exec。拥有这个对象权限的用户,又可以将该权限分配给其他用户,此谓之“Discretionary”。缺陷就是对权限控制比较分散,不便于管理,比如无法简单地将一组文件设置统一的权限开放给指定的一群用户。
[color=rgb(51, 51, 51) !important]强制访问控制(MAC: Mandatory Access Control)MAC是为了弥补DAC权限控制过于分散的问题而诞生的。在MAC这种模型里,管理员管理访问控制。管理员制定策略,用户不能改变它。策略定义了哪个主体能访问哪个对象。这种访问控制模型可以增加安全级别,因为它基于策略,任何没有被显式授权的操作都不能执行。MAC被开发和实现在最重视保密的系统中,如军事系统。主体获得清楚的标记,对象得到分类标记,或称安全级别。
基于MAC的SElinux
[color=rgb(51, 51, 51) !important]参考链接:https://source.android.google.cn/security/selinux
[color=rgb(51, 51, 51) !important]软件通常情况下必须以 Root 用户帐号的身份运行,才能向原始块设备写入数据。在基于 DAC 的传统 Linux 环境中,如果 Root 用户遭到入侵,攻击者便可以利用该用户身份向每个原始块设备写入数据。从 Android 4.3 起,SELinux 开始为传统的自主访问控制 (DAC) 环境提供强制访问控制 (MAC) 保护功能。作为 Android 安全模型的一部分,Android 使用安全增强型 Linux (SELinux) 对所有进程强制执行强制访问控制 (MAC),甚至包括以 Root/超级用户权限运行的进程(Linux 功能)。例如,可以使用 SELinux 为这些设备添加标签,以便被分配了 Root 权限的进程只能向相关政策中指定的设备写入数据。这样一来,该进程便无法重写特定原始块设备之外的数据和系统设置。借助 SELinux,Android 可以更好地保护和限制系统服务、控制对应用数据和系统日志的访问、降低恶意软件的影响,并保护用户免遭移动设备上的代码可能存在的缺陷的影响。

[color=rgb(51, 51, 51) !important]飞凌嵌入式 T507 开发板Android系统版本为Android10,SELinux默认开启,即使获得了该系统的root权限,也只能向相关策略中指定的设备写入数据,从而更好地保护和限制系统服务,保障系统和数据的安全。
标签、规则和域
[color=rgb(51, 51, 51) !important]SELinux 依靠标签来匹配操作和策略。标签用于决定允许的事项。套接字、文件和进程在 SELinux 中都有标签。SELinux 在做决定时需参照两点:一是为这些对象分配的标签,二是定义这些对象如何交互的策略。
[color=rgb(51, 51, 51) !important]在 SELinux 中,标签采用以下形式:user:role:type:mls_level,其中 type 是访问决定的主要组成部分,可通过构成标签的其他组成部分进行修改。对象会映射到类,对每个类的不同访问类型由权限表示。
[color=rgb(51, 51, 51) !important]策略规则采用以下形式:allow domains types:classes permissions;,其中:

[color=rgb(51, 51, 51) !important]Domain - 一个进程或一组进程的标签。也称为域类型,因为它只是指进程的类型。
[color=rgb(51, 51, 51) !important]Type - 一个对象(例如,文件、套接字)或一组对象的标签。
[color=rgb(51, 51, 51) !important]Class - 要访问的对象(例如,文件、套接字)的类型。Permission - 要执行的操作(例如,读取、写入)。
策略配置源文件
[color=rgb(51, 51, 51) !important]1、external/sepolicy
[color=rgb(51, 51, 51) !important]这是独立于设备的配置,一般不能针对设备进行修改

2、device///sepolicy
[color=rgb(51, 51, 51) !important]这是特定于设备的配置,基于 BOARD_SEPOLICY_* 变量来选择对应平台的策略配置。

[color=rgb(51, 51, 51) !important]以飞凌嵌入式 T507 开发板为例,T507策略文件的路径如下:
[color=rgb(51, 51, 51) !important]OKT507-android-source/android$ ls device/softwinner/common/sepolicy/private vendor
Type Enforcement (TE) 配置文件
[color=rgb(51, 51, 51) !important].te 文件中保存了对应对象的域和类型定义、规则。通常每个域一个 .te 文件,例如installd.te。在 device.te、file.te 中声明了设备和文件类型。在某些文件(例如domain.te、app.te)中则存储着共享规则。

[color=rgb(51, 51, 51) !important]以飞凌嵌入式 T507 开发板为例,T507 system_app的TE文件的路径如下:
[color=rgb(51, 51, 51) !important]device/softwinner/common/sepolicy/vendor/system_app.te
标签配置文件
[color=rgb(51, 51, 51) !important]1、file_contexts:文件安全上下文
[color=rgb(51, 51, 51) !important]2、property_contexts:属性安全上下文

[color=rgb(51, 51, 51) !important]以飞凌嵌入式 T507 开发板为例,T507 安全上下文文件路径如下:
[color=rgb(51, 51, 51) !important]device/softwinner/common/sepolicy/vendor/property_contexts
[color=rgb(51, 51, 51) !important]device/softwinner/common/sepolicy/vendor/file_contexts
SEAndroid app分类
[color=rgb(51, 51, 51) !important]SELinux(或SEAndroid)将app划分为主要三种类型(根据user不同,也有其他的domain类型):
[color=rgb(51, 51, 51) !important]1)untrusted_app 第三方app,没有Android平台签名,没有system权限
[color=rgb(51, 51, 51) !important]2)platform_app 有android平台签名,没有system权限
[color=rgb(51, 51, 51) !important]3)system_app 有android平台签名和system权限
[color=rgb(51, 51, 51) !important]从上面划分,权限等级,理论上:untrusted_app < platform_app < system_app
APP的domain和type
[color=rgb(51, 51, 51) !important]查看seapp_contexts文件,APP的domain和type由user和seinfo两个参数决定
[color=rgb(51, 51, 51) !important]system/sepolicy/private/seapp_contexts
[color=rgb(51, 51, 51) !important]isSystemServer=true domain=system_server_startup
[color=rgb(51, 51, 51) !important]user=_app seinfo=platform name=com.android.traceur domain=traceur_app type=app_data_file levelFrom=all
[color=rgb(51, 51, 51) !important]user=system seinfo=platform domain=system_app type=system_app_data_file
[color=rgb(51, 51, 51) !important]user=bluetooth seinfo=platform domain=bluetooth type=bluetooth_data_file
[color=rgb(51, 51, 51) !important]user=network_stack seinfo=network_stack domain=network_stack levelFrom=all
[color=rgb(51, 51, 51) !important]type=radio_data_file
[color=rgb(51, 51, 51) !important]user=nfc seinfo=platform domain=nfc type=nfc_data_file
[color=rgb(51, 51, 51) !important]user=secure_element seinfo=platform domain=secure_element levelFrom=all
[color=rgb(51, 51, 51) !important]user=radio seinfo=platform domain=radio type=radio_data_file
[color=rgb(51, 51, 51) !important]user=shared_relro domain=shared_relro
[color=rgb(51, 51, 51) !important]user=shell seinfo=platform domain=shell name=com.android.shell type=shell_data_file
[color=rgb(51, 51, 51) !important]user=webview_zygote seinfo=webview_zygote domain=webview_zygote
[color=rgb(51, 51, 51) !important]user=_isolated domain=isolated_app levelFrom=al
[color=rgb(51, 51, 51) !important]luser=_app seinfo=app_zygote domain=app_zygote levelFrom=all
[color=rgb(51, 51, 51) !important]user=_app seinfo=media domain=mediaprovider name=android.process.media type=app_data_file
[color=rgb(51, 51, 51) !important]levelFrom=user
[color=rgb(51, 51, 51) !important]user=_app seinfo=platform domain=platform_app type=app_data_file levelFrom=user
[color=rgb(51, 51, 51) !important]user=_app isEphemeralApp=true domain=ephemeral_app type=app_data_file levelFrom=all
[color=rgb(51, 51, 51) !important]user=_app isPrivApp=true domain=priv_app type=privapp_data_file levelFrom=user
[color=rgb(51, 51, 51) !important]user=_app minTargetSdkVersion=29 domain=untrusted_app type=app_data_file levelFrom=all
[color=rgb(51, 51, 51) !important]user=_app minTargetSdkVersion=28 domain=untrusted_app_27 type=app_data_file levelFrom=all
[color=rgb(51, 51, 51) !important]user=_app minTargetSdkVersion=26 domain=untrusted_app_27 type=app_data_file
[color=rgb(51, 51, 51) !important]levelFrom=user
[color=rgb(51, 51, 51) !important]user=_app domain=untrusted_app_25 type=app_data_file levelFrom=user
[color=rgb(51, 51, 51) !important]user=_app minTargetSdkVersion=28 fromRunAs=true domain=runas_app levelFrom=all
[color=rgb(51, 51, 51) !important]user=_app fromRunAs=true domain=runas_app levelFrom=user
user
[color=rgb(51, 51, 51) !important]参考链接:https://blog.csdn.net/huilin9960/article/details/81530568
[color=rgb(51, 51, 51) !important]user可以理解为UID。android的UID和linux的UID根本是两回事,Linux的UID是用于针对多用户操作系统中用于区分用户的,而Android中的UID是用于系统进行权限管理的。参考链接中的文章对于uid的产生讲的很清楚。
seinfo
[color=rgb(51, 51, 51) !important]不同签名会创建对应的selinux上下文。
[color=rgb(51, 51, 51) !important]Android.mk
[color=rgb(51, 51, 51) !important]LOCAL_CERTIFICATE := platform
[color=rgb(51, 51, 51) !important]有platform签名,所以seinfo是platform。
LOCAL_CERTIFICATE作用
[color=rgb(51, 51, 51) !important]参考文档https://blog.csdn.net/hnlgzb/article/details/107823874
[color=rgb(51, 51, 51) !important]可以查看Android源码build/target/product/security/ 目录下提供的默认签名文件,不同平台可能会有差异:飞凌嵌入式 T507 开发板提供了media、networkstack、platform、shared、testkey、verity六种不同权限的签名文件。
[color=rgb(51, 51, 51) !important]以飞凌嵌入式 T507 开发板为例,查看当前运行的应用信息:
[color=rgb(51, 51, 51) !important]console:/ # ps -Z
[color=rgb(51, 51, 51) !important]u:r:system_app:s0 system 15712 1861 1050628 110868 SyS_epoll_wait
[color=rgb(51, 51, 51) !important]0 S forlinx.example.app
[color=rgb(51, 51, 51) !important]u:r:untrusted_app_27:s0:c512,c768 u0_a62 30259 1861 1052120 114132 SyS_epoll_wait
[color=rgb(51, 51, 51) !important]0 S com.forlinx.changelogo
[color=rgb(51, 51, 51) !important]当前运行的两个APP,forlinx.example.app的UID(user)是system,拥有platform签名,它的domain和type就是system_app。

[color=rgb(51, 51, 51) !important]com.forlinx.changelogo没有设置UID使用的默认设置,其UID为u0_a62,并且没有设置签名文件,它的domain和type就是untrusted_app。
T507自定义安全策略
[color=rgb(51, 51, 51) !important]以上面两个运行的app来说,我们为这两个APP添加额外的权限,对应的TE配置文件分别就是system_app.te、untrusted_app.te,对应路径为:
[color=rgb(51, 51, 51) !important]device/softwinner/common/sepolicy/vendor/system_app.te
[color=rgb(51, 51, 51) !important]device/softwinner/common/sepolicy/vendor/untrusted_app.te

[color=rgb(51, 51, 51) !important]以forlinx.example.app为例,我们为其添加can设备的执行权限:

[color=rgb(51, 51, 51) !important]OKT507-android-source/android$ vi device/softwinner/common/sepolicy/vendor/system_app.te
[color=rgb(51, 51, 51) !important]...
[color=rgb(51, 51, 51) !important]allow system_app vendor_shell_exec:file { getattr open read execute execute_no_trans };
[color=rgb(51, 51, 51) !important]allow system_app shell_exec:file { getattr open read execute execute_no_trans };
[color=rgb(51, 51, 51) !important]allow system_app shell:file { getattr open read execute execute_no_trans };
[color=rgb(51, 51, 51) !important]...
[color=rgb(51, 51, 51) !important]以策略规则配置形式(allow domains types:classes permissions)
[color=rgb(51, 51, 51) !important]分析:domains:system_app
[color=rgb(51, 51, 51) !important]types:vendor_shell_exec
[color=rgb(51, 51, 51) !important]classes:file
[color=rgb(51, 51, 51) !important]permissions:getattr open read execute execute_no_trans
neverallow failures
[color=rgb(51, 51, 51) !important]有时我们增加的权限,系统默认的配置是不允许的,比如我们上面给forlinx.example.app增加的执行脚本的权限,报错如下:

[color=rgb(51, 51, 51) !important]libsepol.report_failure: neverallow on line 9 of system/sepolicy/private/system_app.te
[color=rgb(51, 51, 51) !important](or line 41463 of policy.conf) violated by allow system_app shell:file { read open };
[color=rgb(51, 51, 51) !important]libsepol.report_failure: neverallow on line 22 of system/sepolicy/private/shell.te
[color=rgb(51, 51, 51) !important](or line 40025 of policy.conf) violated by allow system_app shell:file { read open };
[color=rgb(51, 51, 51) !important]libsepol.check_assertions: 2 neverallow failures occurred

[color=rgb(51, 51, 51) !important]系统默认的安全策略的路径为system/sepolicy/,根据报错的提示,我们可以修改默认的配置,修改system/sepolicy/private/system_app.te和system/sepolicy/private/shell.te,从而完成权限的赋予。

[color=rgb(51, 51, 51) !important]以上就是Android 安全策略的脉络,以及飞凌嵌入式 T507 开发板Android系统下自定义安全策略的方法了。
[color=rgb(51, 51, 51) !important]原文链接:https://www.forlinx.com/article_view_792.html

本文地址:https://www.eechina.com/thread-779940-1-1.html     【打印本页】

本站部分文章为转载或网友发布,目的在于传递和分享信息,并不代表本网赞同其观点和对其真实性负责;文章版权归原作者及原出处所有,如涉及作品内容、版权和其它问题,我们将根据著作权人的要求,第一时间更正或删除。
您需要登录后才可以发表评论 登录 | 立即注册

厂商推荐

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表