zoukankan      html  css  js  c++  java
  • [Android L]SEAndroid增强Androd安全性背景概要及带来的影响

    1  SEAndroid背景

     

          Android对于操作系统安全性方面的增强一直沿用Linux内核所提供的MAC强制访问控制套件SELinux,对权限进行了更为深度的管理,有效地控制着进程对资源的访问。2012年才问世的SE Android将SELinux移植到Android平台上,以降低恶意应用程序攻击带来的损害,提供Android系统的防御能力。

         SE Android(Secutity-Enhanced Android)是Android与SE Linux的结合,由美国NSA在2012年推出的Android操作系统的安全强化套件,以支持在Android平台上使用SE Linux。SE Android 将原来应用在Linux操作系统上的MAC强制访问控制套件SELinux移植到Android平台上,其目的在于降低恶意应用程序攻击带来的损害。然而SE Android的范畴并不局限于SELinux,它通过强化Android操作系统对应用程序的访问控制,增强应用程序之间的隔离效果,确保每个应用程序之间的独立运作,建立类似于沙盒的隔离效果,从而阻止恶意应用程序对系统或其他应用程序的攻击。
          Android是建立在标准的Linux Kernel 基础上, 自然也可以开启SELinux, 通常在通用移动平台上, 很少开启这样的安全服务, Google 为了进一步增强Android 的安全性, 经过长期的准备,目前已经在Android 5.0(L) 上完整的开启SELinux, 并对SELinux 进行深入整合形成了SEAndroid.
     
     

    2 SEAndroid安全策略概述

     
         SE Android的策略源码位置在external/sepolicy,其中包含用来生成SELinux内核策略文件的源代码。以下是Android 5.1的策略文件结构树:
    external/sepolicy
     |.
    ├── access_vectors
    ├── adbd.te
    ├── Android.mk
    ├── app.te
    ├── attributes
    ├── binderservicedomain.te
    ├── bluetooth.te
    ├── bootanim.te
    ├── clatd.te
    ├── debuggerd.te
    ├── device.te
    ├── dex2oat.te
    ├── dhcp.te
    ├── dnsmasq.te
    ├── domain.te
    ├── drmserver.te
    ├── dumpstate.te
    ├── file_contexts
    ├── file.te
    ├── fs_use
    ├── genfs_contexts
    ├── global_macros
    ├── gpsd.te
    ├── hci_attach.te
    ├── healthd.te
    ├── hostapd.te
    ├── initial_sid_contexts
    ├── initial_sids
    ├── init_shell.te
    ├── init.te
    ├── inputflinger.te
    ├── installd.te
    ├── install_recovery.te
    ├── isolated_app.te
    ├── kernel.te
    ├── keys.conf
    ├── keystore.te
    ├── lmkd.te
    ├── logd.te
    ├── mac_permissions.xml
    ├── mdnsd.te
    ├── mediaserver.te
    ├── mls
    ├── mls_macros
    ├── mtp.te
    ├── netd.te
    ├── net.te
    ├── nfc.te
    ├── NOTICE
    ├── platform_app.te
    ├── policy_capabilities
    ├── port_contexts
    ├── ppp.te
    ├── property_contexts
    ├── property.te
    ├── racoon.te
    ├── radio.te
    ├── README
    ├── recovery.te
    ├── rild.te
    ├── roles
    ├── runas.te
    ├── sdcardd.te
    ├── seapp_contexts
    ├── security_classes
    ├── selinux-network.sh
    ├── service_contexts
    ├── servicemanager.te
    ├── service.te
    ├── shared_relro.te
    ├── shell.te
    ├── surfaceflinger.te
    ├── su.te
    ├── system_app.te
    ├── system_server.te
    ├── tee.te
    ├── te_macros
    ├── tools
    │   ├── Android.mk
    │   ├── checkfc.c
    │   ├── check_seapp.c
    │   ├── insertkeys.py
    │   ├── post_process_mac_perms
    │   ├── README
    │   ├── sepolicy-analyze
    │   │   ├── Android.mk
    │   │   ├── dups.c
    │   │   ├── dups.h
    │   │   ├── neverallow.c
    │   │   ├── neverallow.h
    │   │   ├── perm.c
    │   │   ├── perm.h
    │   │   ├── README
    │   │   ├── sepolicy-analyze.c
    │   │   ├── typecmp.c
    │   │   ├── typecmp.h
    │   │   ├── utils.c
    │   │   └── utils.h
    │   └── sepolicy-check.c
    ├── ueventd.te
    ├── unconfined.te
    ├── uncrypt.te
    ├── untrusted_app.te
    ├── users
    ├── vdc.te
    ├── vold.te
    ├── watchdogd.te
    ├── wpa.te
    └── zygote.te
     
    SELinux内核策略文件包含file_contexts配置文件、genfs_contexts配置文件、property_contexts配置文件、seapp_contexts配置文件和mac_permissions.xml配置文件,SE Android项目的开发人员仍然在对Android系统安全进行深入的研究,因此这些策略配置也处在随时变化中。其中genfs_contexts配置文件、property_contexts配置文件和seapp_contexts配置文件是专门为SE Android系统创建的,因此不属于传统SELinux策略
         这些策略文件是在Android系统编译过程中产生并且被添加到ramdisk镜像当中,因此可以保证这些策略在系统启动的初期,映射系统分区之前,最先被加载。一旦数据分区被加载后,可以将策略文件放置在/data/system目录下并且将selinux.reload_policy属性置为1(使用setprop selinux.reload_policy 1 命令),之后重新加载/data/system目录下的策略文件,这种设置将会触发系统的init进程重新加载策略,同时重新启动ueventd和installd进程以保证它们可以重新加载与这两个服务相关的策略。需要注意的是,对于内核策略,需要在主机的编译环境中,重新编译sepolicy(make policy),并且更新到/data/system中,如果希望每次设备启动时都会自动加载/data/system下的策略文件,需要将setprop命令添加到init.rc(system/core/rootdir/init.rc)中。
     

    2.1 seapp_contexts配置文件

     
    Android L(5.1) seapp_contexts文件内容
     1 # Input selectors:
     2 #     isSystemServer (boolean)
     3 #     user (string)
     4 #     seinfo (string)
     5 #     name (string)
     6 #     path (string)
     7 #     sebool (string)
     8 # isSystemServer=true can only be used once.
     9 # An unspecified isSystemServer defaults to false.
    10 # An unspecified string selector will match any value.
    11 # A user string selector that ends in * will perform a prefix match.
    12 # user=_app will match any regular app UID.
    13 # user=_isolated will match any isolated service UID.
    14 # All specified input selectors in an entry must match (i.e. logical AND).
    15 # Matching is case-insensitive.
    16 #
    17 # Precedence rules:
    18 #        (1) isSystemServer=true before isSystemServer=false.
    19 #       (2) Specified user= string before unspecified user= string.
    20 #       (3) Fixed user= string before user= prefix (i.e. ending in *).
    21 #       (4) Longer user= prefix before shorter user= prefix.
    22 #       (5) Specified seinfo= string before unspecified seinfo= string.
    23 #       (6) Specified name= string before unspecified name= string.
    24 #       (7) Specified path= string before unspecified path= string.
    25 #       (8) Specified sebool= string before unspecified sebool= string.
    26 #
    27 # Outputs:
    28 #     domain (string)
    29 #     type (string)
    30 #     levelFrom (string; one of none, all, app, or user)
    31 #     level (string)
    32 # Only entries that specify domain= will be used for app process labeling.
    33 # Only entries that specify type= will be used for app directory labeling.
    34 # levelFrom=user is only supported for _app or _isolated UIDs.
    35 # levelFrom=app or levelFrom=all is only supported for _app UIDs.
    36 # level may be used to specify a fixed level for any UID.
    37 #
    38 isSystemServer=true domain=system_server
    39 user=system domain=system_app type=system_app_data_file
    40 user=bluetooth domain=bluetooth type=bluetooth_data_file
    41 user=nfc domain=nfc type=nfc_data_file
    42 user=radio domain=radio type=radio_data_file
    43 user=shared_relro domain=shared_relro
    44 user=shell domain=shell type=shell_data_file
    45 user=_isolated domain=isolated_app
    46 user=_app seinfo=platform domain=platform_app type=app_data_file
    47 user=_app domain=untrusted_app type=app_data_file
    48 #user=_app seinfo=lenovoapp domain=platform_app type=app_data_file
    49 user=_app seinfo=lenovordvr domain=platform_app type=app_data_file
    50 user=_app seinfo=lenovordvm domain=platform_app type=app_data_file
    51 user=_app seinfo=lenovordvs domain=platform_app type=app_data_file
    52 user=_app seinfo=lenovordvp domain=platform_app type=app_data_file
    53 user=_app seinfo=lenovopadr domain=platform_app type=app_data_file
    54 user=_app seinfo=lenovopadm domain=platform_app type=app_data_file
    55 user=_app seinfo=lenovopads domain=platform_app type=app_data_file
    56 user=_app seinfo=lenovopadp domain=platform_app type=app_data_file
    seapp_contexts文件用来标记应用程序的进程和应用程序包所在的目录。该文件的源位置在external/sepolicy目录下,下面是可以再其中设置的选项(输入)
    1).isSystemServer :布尔值,匹配系统级服务程序,在文件中只能被定义为真(true)一次,默认值为假(false)
    2).user:字符串,匹配应用程序的用户,若为空或没有定义为任意用户,以*结尾的字符串将进行前缀匹配。user=_app将匹配任何一般应用UID,user=_isolated将匹配任意被隔离的服务UID
    3).seinfo:字符串,匹配SELinux控制类型
    4).name:字符串,匹配应用名称,如com.android.deskclock
    5).sebool:字符串,匹配布尔值,该字符串定义的布尔值为真时匹配
     
    SELinux会通过该定义文件为匹配的应用程序找到对应的结果(输出)
    1).domain:字符串,程序所属于域
    2).type:字符串,程序所属类型
    3).levelFromUid:布尔值,是否根据UID设置程序级别,当前只针对应用程序的UID。
    4).level:字符串,应用程序的级别
     
    根据这个结果,SELinux会为应用程序进程以及目录分配相应的权限。
     

    2.2 property_contexts配置文件

     
    Android L(5.1)property_contexts文件内容
     
     1 ##########################
     2 # property service keys
     3 #
     4 #
     5 net.rmnet               u:object_r:net_radio_prop:s0
     6 net.gprs                u:object_r:net_radio_prop:s0
     7 net.ppp                 u:object_r:net_radio_prop:s0
     8 net.qmi                 u:object_r:net_radio_prop:s0
     9 net.lte                 u:object_r:net_radio_prop:s0
    10 net.cdma                u:object_r:net_radio_prop:s0
    11 net.dns                 u:object_r:net_radio_prop:s0
    12 sys.usb.config          u:object_r:system_radio_prop:s0
    13 ril.                    u:object_r:radio_prop:s0
    14 gsm.                    u:object_r:radio_prop:s0
    15 persist.radio           u:object_r:radio_prop:s0
    16 
    17 net.                    u:object_r:system_prop:s0
    18 dev.                    u:object_r:system_prop:s0
    19 runtime.                u:object_r:system_prop:s0
    20 hw.                     u:object_r:system_prop:s0
    21 sys.                    u:object_r:system_prop:s0
    22 sys.powerctl            u:object_r:powerctl_prop:s0
    23 service.                u:object_r:system_prop:s0
    24 wlan.                   u:object_r:system_prop:s0
    25 dhcp.                   u:object_r:dhcp_prop:s0
    26 dhcp.bt-pan.result      u:object_r:pan_result_prop:s0
    27 bluetooth.              u:object_r:bluetooth_prop:s0
    28 
    29 debug.                  u:object_r:debug_prop:s0
    30 debug.db.               u:object_r:debuggerd_prop:s0
    31 log.                    u:object_r:shell_prop:s0
    32 service.adb.root        u:object_r:shell_prop:s0
    33 service.adb.tcp.port    u:object_r:shell_prop:s0
    34 
    35 persist.audio.          u:object_r:audio_prop:s0
    36 persist.logd.           u:object_r:logd_prop:s0
    37 persist.sys.            u:object_r:system_prop:s0
    38 persist.service.        u:object_r:system_prop:s0
    39 persist.service.bdroid. u:object_r:bluetooth_prop:s0
    40 persist.security.       u:object_r:system_prop:s0
    41 
    42 # selinux non-persistent properties
    43 selinux.                u:object_r:security_prop:s0
    44 
    45 # default property context
    46 *                       u:object_r:default_prop:s0
    47 
    48 # data partition encryption properties
    49 vold.                   u:object_r:vold_prop:s0
    50 crypto.                 u:object_r:vold_prop:s0
    51 
    52 # ro.build.fingerprint is either set in /system/build.prop, or is
    53 # set at runtime by system_server.
    54 build.fingerprint       u:object_r:fingerprint_prop:s0
    55 
    56 # ctl properties
    57 ctl.bootanim            u:object_r:ctl_bootanim_prop:s0
    58 ctl.dumpstate           u:object_r:ctl_dumpstate_prop:s0
    59 ctl.fuse_               u:object_r:ctl_fuse_prop:s0
    60 ctl.mdnsd               u:object_r:ctl_mdnsd_prop:s0
    61 ctl.ril-daemon          u:object_r:ctl_rildaemon_prop:s0
    62 ctl.bugreport           u:object_r:ctl_bugreport_prop:s0
    63 ctl.dhcpcd_bt-pan       u:object_r:ctl_dhcp_pan_prop:s0
    64 ctl.                    u:object_r:ctl_default_prop:s0
    65 
    66 # NFC properties
    67 nfc.                    u:object_r:nfc_prop:s0
    68 # DOLBY_START
    69 dolby.audio             u:object_r:audio_prop:s0
    70 dolby.                  u:object_r:system_prop:s0
    71 # DOLBY_END
     
          property_contexts配置文件为权限检查定义了Android系统各属性间的安全关联。该文件为系统中的每一种服务类型定义了不同的属性,包括用户(user)、角色(role)、属性(property)和级别(level)。一种应用程序在调用某一服务资源时,系统将会根据这些属性检查是否有权限使用这些资源。
         以下是目前各项属性可用的值。
         1) 用户:u,系统默认,唯一值
         2) 角色:object_r ,系统默认,唯一值
         3) 属性:默认属性是default_prop,其他属性分别是:见上,如net_radio_prop、system_radio_prop、shell_prop ...
         4) 级别:s0 ,系统默认,唯一值
     

    3 SELinux在Android上的更新过程

     
    如下图所描述:
    SELinux 在Android 的更新过程
    1) KK 4.4 针对netd, installd, zygote, vold 四个原本具有root 权限的process, 以及它们fork 出的子进程启用Enforce 模式.
    2) L 版本普遍性开启SELinux Enforce mode.
    3) Permissive 模式,只打印audit 异常LOG,不拒绝请求, Enforce 模式,即打印audit 异常LOG, 也拒绝请求
     

    4 SELinux给Android带来了哪些影响

     
       1) 严格限制了ROOT 权限, 以往ROOT "无法无天" 的情况将得到极大的改善.
       2) 通过SELinux保护, 降低系统关键进程受攻击的风险, 普通进程将没有权限直接连接到系统关键进程.
       3) 进一步强化APP的沙箱机制, 确保APP难以做出异常行为或者攻击行为.
       4) 将改变APP一旦安装, 权限就已经顶死的历史, APP权限动态调整将成为可能.

     
     参考文献
    1) Android安全机制解析与应用实践(吴倩/赵晨啸)
    2) MTK-SELinux问题快速分析
     
    转自:http://blog.csdn.net/yelangjueqi/article/details/46756341
     
     
  • 相关阅读:
    字符串
    zval结构体
    需要优化代码的leetcode
    删除字符串中的字符
    python 目录
    文件
    awk 复习
    链表和数组的说法
    在linux服务器新添加硬盘,如何识别、挂载。
    Linux 的 date 日期的使用
  • 原文地址:https://www.cnblogs.com/l2rf/p/4960572.html
Copyright © 2011-2022 走看看