Android安全模型简介

从技术架构角度来看,Android安全模型基于强健的Linux操作系统内核安全性,通过进程沙箱机制隔离进程资源,并且辅以独特的内存管理技术与安全高效的进程间通信机制,适应嵌入式移动端处理器性能与内存容量的限制。在应用层面,使用显式定义且经用户授权的应用权限控制机制等,系统化地规范并强制各类应用程序的行为准则与权限许可;引入应用程序签名机制定义应用程序之间的信任关系与资源共享的权限。Android应用程序基于Android特有的应用框架(Framework),由Java语言编写,运行于Dalvik Java虚拟机。同时,部分底层应用仍可由C/C++语言设计实现,以原生库形式直接运行于操作系统的用户空间。应用程序及其Dalvik虚拟机运行环境都被限制在“进程沙箱”的隔离环境下,自行拥有专用的文件系统区域,独享私有数据。

1 总述

众所周知,Android底层是基于Linux内核,因此Linux内核向Android提供了一系列安全机制。比如基于用户的权限模型、进程隔离机制、进程IPC安全机制,除此之外还裁剪了Linux内核中非必要和有潜在风险的功能。最终,Android安全模型主要提供以下几种安全机制:

1.1 进程沙箱隔离机制

如下图所示,每个Android应用程序在安装时被PackageManagerService赋予独特的用户标识(UID),并永久保持,除非用户卸载掉该应用。应用程序及其运行的Dalvik虚拟机运行于独立的Linux进程空间,UID不同的应用程序完全隔离。通过这一措施阻止不同应用之间的相互访问。

sandbox.png

图1  sandbox模型

1.2 应用程序签名机制

应用程序包(.apk文件)必须被开发者数字签名;第一,可以根据签名文件保证应用完整性。第二,同一开发者可指定不同的应用程序制定相同的UID。通过配置shareduserid,拥有同一个UID的多个APK可以配置成运行在同一个进程中。所以默认就是可以互相访问任意数据。也可以配置成运行成不同的进程,同时可以访问其他APK的数据目录下的数据库和文件。就像访问本程序的数据一样。

shareduserid.png

图2  shareduserid机制

1.3 权限定义与授权机制

Android系统通过权限限制应用程序所能够使用的功能。应用程序需要显式声明权限,名称,权限组与保护级别。不同的级别要求应用程序行使此权限时的认证方式不同:Normal级申请即可用;Dangerous级需在安装时或者赋予权限前,会弹出对话框,显式请求权限,由用户确认才可用;Signature与Signatureorsystem则必须是系统用户才可用。总的来说就是通过GID来控制应用程序所能够使用的权限,比如应用程序声明了网络权限,那么会在安装或者使用时,系统会给应用程序分配额外的GID。

在Android中,所有权限的定义都在:system/core/include/private/android_filesystem_config.h。在这个头文件中定义了Android系统的一些用户,包含root用户,system用户,shell用户所对应的值等等。

/* This is the master Users and Groups config for the platform.
 * DO NOT EVER RENUMBER
 */

#define AID_ROOT             0  /* traditional unix root user */

#define AID_SYSTEM        1000  /* system server */

#define AID_RADIO         1001  /* telephony subsystem, RIL */
#define AID_BLUETOOTH     1002  /* bluetooth subsystem */
#define AID_GRAPHICS      1003  /* graphics devices */
#define AID_INPUT         1004  /* input devices */
#define AID_AUDIO         1005  /* audio devices */
#define AID_CAMERA        1006  /* camera devices */
#define AID_LOG           1007  /* log devices */
#define AID_COMPASS       1008  /* compass device */
#define AID_MOUNT         1009  /* mountd socket */
#define AID_WIFI          1010  /* wifi subsystem */
#define AID_ADB           1011  /* android debug bridge (adbd) */
#define AID_INSTALL       1012  /* group for installing packages */
#define AID_MEDIA         1013  /* mediaserver process */
#define AID_DHCP          1014  /* dhcp client */
#define AID_SDCARD_RW     1015  /* external storage write access */
#define AID_VPN           1016  /* vpn system */
#define AID_KEYSTORE      1017  /* keystore subsystem */
#define AID_USB           1018  /* USB devices */
#define AID_DRM           1019  /* DRM server */
#define AID_MDNSR         1020  /* MulticastDNSResponder (service discovery) */
#define AID_GPS           1021  /* GPS daemon */
#define AID_UNUSED1       1022  /* deprecated, DO NOT USE */
#define AID_MEDIA_RW      1023  /* internal media storage write access */
#define AID_MTP           1024  /* MTP USB driver access */
#define AID_UNUSED2       1025  /* deprecated, DO NOT USE */
#define AID_DRMRPC        1026  /* group for drm rpc */
#define AID_NFC           1027  /* nfc subsystem */
#define AID_SDCARD_R      1028  /* external storage read access */
#define AID_CLAT          1029  /* clat part of nat464 */
#define AID_LOOP_RADIO    1030  /* loop radio devices */
#define AID_MEDIA_DRM     1031  /* MediaDrm plugins */
#define AID_PACKAGE_INFO  1032  /* access to installed package details */
#define AID_SDCARD_PICS   1033  /* external storage photos access */
#define AID_SDCARD_AV     1034  /* external storage audio/video access */
#define AID_SDCARD_ALL    1035  /* access all users external storage */
#define AID_LOGD          1036  /* log daemon */
#define AID_SHARED_RELRO  1037  /* creator of shared GNU RELRO files */
#define AID_DBUS          1038  /* dbus-daemon IPC broker process */
#define AID_TLSDATE       1039  /* tlsdate unprivileged user */
#define AID_MEDIA_EX      1040  /* mediaextractor process */
#define AID_AUDIOSERVER   1041  /* audioserver process */
#define AID_METRICS_COLL  1042  /* metrics_collector process */
#define AID_METRICSD      1043  /* metricsd process */
#define AID_WEBSERV       1044  /* webservd process */
#define AID_DEBUGGERD     1045  /* debuggerd unprivileged user */
#define AID_MEDIA_CODEC   1046  /* mediacodec process */
#define AID_CAMERASERVER  1047  /* cameraserver process */
#define AID_FIREWALL      1048  /* firewalld process */
#define AID_TRUNKS        1049  /* trunksd process (TPM daemon) */
#define AID_NVRAM         1050  /* Access-controlled NVRAM */
#define AID_DNS           1051  /* DNS resolution daemon (system: netd) */
#define AID_DNS_TETHER    1052  /* DNS resolution daemon (tether: dnsmasq) */
/* Changes to this file must be made in AOSP, *not* in internal branches. */

#define AID_SHELL         2000  /* adb and debug shell user */
#define AID_CACHE         2001  /* cache access */
#define AID_DIAG          2002  /* access to diagnostic resources */

/* The range 2900-2999 is reserved for OEM, and must never be
 * used here */
#define AID_OEM_RESERVED_START 2900
#define AID_OEM_RESERVED_END   2999

/* The 3000 series are intended for use as supplemental group id's only.
 * They indicate special Android capabilities that the kernel is aware of. */
#define AID_NET_BT_ADMIN  3001  /* bluetooth: create any socket */
#define AID_NET_BT        3002  /* bluetooth: create sco, rfcomm or l2cap sockets */
#define AID_INET          3003  /* can create AF_INET and AF_INET6 sockets */
#define AID_NET_RAW       3004  /* can create raw INET sockets */
#define AID_NET_ADMIN     3005  /* can configure interfaces and routing tables. */
#define AID_NET_BW_STATS  3006  /* read bandwidth statistics */
#define AID_NET_BW_ACCT   3007  /* change bandwidth statistics accounting */
#define AID_NET_BT_STACK  3008  /* bluetooth: access config files */
#define AID_READPROC      3009  /* Allow /proc read access */
#define AID_WAKELOCK      3010  /* Allow system wakelock read/write access */

/* The range 5000-5999 is also reserved for OEM, and must never be used here. */
#define AID_OEM_RESERVED_2_START 5000
#define AID_OEM_RESERVED_2_END   5999

#define AID_EVERYBODY     9997  /* shared between all apps in the same profile */
#define AID_MISC          9998  /* access to misc storage */
#define AID_NOBODY        9999

#define AID_APP          10000  /* first app user */

#define AID_ISOLATED_START 99000 /* start of uids for fully isolated sandboxed processes */
#define AID_ISOLATED_END   99999 /* end of uids for fully isolated sandboxed processes */

#define AID_USER        100000  /* offset for uid ranges for each user */

#define AID_SHARED_GID_START 50000 /* start of gids for apps in each user to share */
#define AID_SHARED_GID_END   59999 /* start of gids for apps in each user to share */

1.4 进程通信机制

Android中使用了一种名为Binder的全新IPC机制,Binder进程通信机制提供基于共享内存的高效进程通信;Binder基于Client-Server模型,提供类似COM与CORBA的轻量级远程进程调用(RPC);通过接口描述语言(AIDL)定义接口与交换数据的类型,确保进程间通信的数据不会溢出越界,污染进程空间。另外在Binder通信过程中通过调用Binder.getCallingUID()就能取得对方UID来检验它的身分。

1.5 访问控制机制

在SELinux出现之前,Linux上的安全模型叫DAC,全称是Discretionary Access Control,翻译为自主访问控制。DAC的核心思想很简单,其原理就是:进程理论上所拥有的权限与执行它的用户的权限相同。比如,以root用户启动Browser,那么Browser就有root用户的权限,在Linux系统上能干任何事情。由于DAC的管理太过宽松,所以NSA设计了一种新的安全模型,叫MAC(Mandatory Access Control),翻译为强制访问控制。由于Android系统有着独特的用户空间运行时,因此SELinux不能完全适用于Android系统。为此NSA针对Android系统,在SELinux基础上开发了SEAndroid。MAC和DAC一起构成了Android中的访问控制机制。

mac.png

图3  访问控制检查流程