06 Security Architecture IV
06 Security Architecture IV
Architecture IV
Dr.-Ing. Sven Bugiel
• Compartmentalization:
− Isolation & Principle of least privilege
− Android’s UID-based sandboxing
− IPC/ICC as a controlled channel between app
sandboxes
− Zygote as parent process for all app processes
• Android’s middleware services and apps as reference
monitors for system and user resources
− “encapsulating” resources and exposing API via
Binder IPC
• Role of Binder driver: Provide services and apps with UID
of calling processes
− Use UID to enforce permission checks
Sandbox
2
Android Security
Architecture
Need for Defense in Depth
Problem of critical system exploits [126,130]
Source: https://www.statista.com/statistics/271774/share-of-android-platforms-
4
on-mobile-devices-with-android-os/
Problem of critical system exploits [126]
Flow of updates from upstream projects into Flow of updates between participants in
Android the Android ecosystem
(latency between the upstream release and the release of the first (Number edge: Patches shipped in dataset;
Android version containing it in the dataset) Number box: #entities in dataset)
5
Problem of distributing updates [171]
6
Problem of distributing updates [171]
Update latency per carrier: Security announcement vs Android Security Bulletin Update latency for models supported by all four carriers
7
Root providers [130]
• Users might want root access but don’t always have the ability or possibility
to flash a custom ROM with root access
− Entire ecosystem of “soft root providers” established that build and tailor
“one-click root methods” for various devices, usually exploiting known or
zero-day root exploits
− Business model: More and better exploits for more supported devices
• Potential problem: How well do the providers protect their solutions? Can
malware easily use them or adapt their exploits?
Root providers
A: Anti-debug
C: Tamper-detection
N: Code Protection with JNI.
O: Obfuscation
P: Packing
S: String Encryption.
8
Approaches to address the vulnerabilties
9
Approaches to address the vulnerabilties
https://source.android.com/docs/setup/build/rust/building-rust-modules/overview
10
Approaches to address the vulnerabilties
11
Recap: Fundamental security design principles
• Least privilege: Every process and every user of the system should operate
using the least set of privileges necessary to perform their tasks. Compromise
only yields minimum set of privileges.
• Least common mechanism: Minimize the functions shared by different users,
providing mutual security.
• Modularity / Compartmentalization: Modular system architecture and security
mechanisms as isolated, protected modules. Helps protecting individual
modules from tampering.
• Defense-in-depth: Use multiple, overlapping protection approaches, the failure
or circumvention of any individual protection approach will not leave the
system unprotected. No single point of failure.
12
Modularization of Android: Treble
13 https://www.androidauthority.com/project-treble-818225/
https://source.android.com/devices/architecture
Modularization of Android: Treble
14 https://www.androidauthority.com/project-treble-818225/
https://source.android.com/devices/architecture
Modularization of Android: Modular System Components
(Project Mainline)
• Android 10 modularizes some Android system components and enables them to
be updated via Google Play outside of the normal Android release cycle
− New APEX container format introduced for lower-level system modules that
do not fit into standard Android application model
− Examples: Runtime, DNS Resolver, Media Codecs, Network Components,
Permission Controller,...
15 https://source.android.com/devices/architecture/modular-system
Kernel Fragmentation
• Problem: long supply chains for kernel and fragmentation of kernels (as
much as 50% of the code running on a device is out-of-tree code, i.e., not
from upstream Linux or from AOSP common kernels)
Linux kernel from kernel.org
16 https://source.android.com/devices/architecture/kernel/generic-kernel-image
Generic Kernel Images
17 https://source.android.com/devices/architecture/kernel/generic-kernel-image
Hardening the system
18 https://source.android.com/security/index.html
Android Security
Architecture
Mandatory Access Control
with SELinux
Android’s security principle
System Service/App
• Android’s security design relies on the OS to
(UID S)
enforce the application sandbox based on App IPC Application
the app’s UID (UID A) Framework API
Permission check
Privileged code
• Problems:
User space syscalls syscalls
− Misconfigured kernel level access control
Kernel space
− Attacker/App escalates its privileges to a System resource
e.g., files, drivers, etc.
higher privileged UID
Compliant access Non-compliant access
− In particular: Root exploits
Kernel level access control
− Kernel exploits
20
Defense in depth
Gained root
privileges
Linux DAC
SELinux (MAC)
Should still be
Kernel driver
prevented!
21
Mandatory Access Control (MAC)
22
Implementations of MAC
• Label-based MAC
− Subjects and objects labeled with a security context
− Example implementations: SELinux, SMACK (Simplified Mandatory Access
Control Kernel), Windows MIC
• Pathname-based MAC
− Policies based on the path of executables and objects instead of label
− Example implementations: TOMOYO, grsecurity, AppArmor
23
SELinux on Android overview [128,129]
24
Mandatory Access Control Models: Domain-type
enforcement (DTE) model
• Enhanced type enforcement (TE) mechanism
− Deployed in operating systems but also firewalls
− Can support variety of security policies
• Access control decisions based on domain and type
− Subjects associated with a domain and objects with a type
− Policy maps each (domain, type) and (domain, domain) pair to a set of
allowed permissions
− E.g., domains root and user: (root, user)→(create, kill) would allow
processes in the root domain to create and kill processes in the user
domain
− E.g., domain user and type userdata: (user, userdata)→(create, read,
write) would allow a process in the user domain to create, read, and
write an object (e.g., file) with the userdata type
25
Linux Security Modules [127]
26
Original figure from: Frank Meyer “SELinux by Example: Using Security Enhanced Linux: Using Security Enhanced Linux”
LSM Concept
27
SELinux policy concept
• All subjects and objects in the system are labeled with a security context:
user : role : type : security level
• type: most important attribute! Type of objects and subjects (i.e., subject’s
domain) used for domain-type enforcement
• role: only relevant for processes/domains; used to group types (implement
Role-Based Access Control with type enforcement), i.e., which types a
process can reach
• user: to group roles (usually by user, system, root)
• security level: used for Multi-level Security (MLS), defines the sensitivity
(clearance level) and category for implementing the Bell-LaPadula model
28
SELinux policy concept
• All subjects and objects in the system are labeled with a security context:
user : role : type : security level
• Values just strings assigned by the OM
− security interpretation up to the security server
− semantic of OM and Security Server must be in sync!
• Context stored with subject/object:
− File: Usually in extended file system attribute
− Process: Extended task struct in process management of kernel
• Tool support to get security context of subjects/objects
− id, ps -Z, ls -Z
29
Example: Fedora Linux with SELinux
[sven@mbpc05 ~]$ ls –lZ
total 172
drwxr-xr-x. 2 sven sven unconfined_u:object_r:user_home_t:s0 4096 Dec 7 12:52 Desktop
drwxr-xr-x. 2 sven sven unconfined_u:object_r:user_home_t:s0 4096 Dec 7 12:52 Documents
drwxr-xr-x. 2 sven sven unconfined_u:object_r:user_home_t:s0 4096 Dec 7 12:52 Downloads
drwxr-xr-x. 2 sven sven unconfined_u:object_r:audio_home_t:s0 4096 Dec 7 12:52 Music
drwxr-xr-x. 2 sven sven unconfined_u:object_r:user_home_t:s0 4096 Dec 7 12:52 Pictures
drwxr-xr-x. 2 sven sven unconfined_u:object_r:user_home_t:s0 4096 Dec 7 12:52 Public
-rw-r--r--. 1 sven sven unconfined_u:object_r:user_home_t:s0 143331 Dec 9 13:16 sepolicy
[sven@mbpc05 ~]$ id
uid=1000(sven) gid=1000(sven) groups=1000(sven),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-
s0:c0.c1023
31
SELinux Reference Monitor
Reference monitor
32
Short example rule
• The rule states that a process with the type “untrusted_app” can create,
open, and read file objects with the type “sdcard_external”
− If the system ensures that all files stored on the SDcard get the type
“sdcard_external” and that all untrusted programs get the type
“untrusted_app”, then this rule governs how untrusted programs can
operate on files stored on the SDcard independently of the UID
33
SELinux on Android
34
How are subjects and objects labeled
35
General object labeling (extract)
• file_contexts
− initial file contexts for setting the filesystem
− apps and their data stores handled separately in seapp_contexts
• property_contexts
− default contexts for Android property services
− Used by <AOSP>/system/core/init/property_service.cpp and
<AOSP>/system/core/init/init.cpp
• service_contexts
− default contexts for Android services
− Used by
<AOSP>/frameworks/native/cmds/servicemanager/ServiceManager.cpp
• initial_sids and initial_sid_contexts
− Fail-safe default contexts
36
Example Security Contexts
• Files labeled during system build
• File system SELinux security contexts:
drwxrwx--- system cache u:object_r:cache_file:s0 cache
dr-x------ root root u:object_r:rootfs:s0 config
drwxrwx--x system system u:object_r:system_data_file:s0 data
-rw-r--r-- root root u:object_r:rootfs:s0 default.prop
drwxr-xr-x root root u:object_r:device:s0 dev
lrwxrwxrwx root root u:object_r:rootfs:s0 etc -> /system/etc
-rw-r--r-- root root u:object_r:rootfs:s0 file_contexts
-rwxr-x--- root root u:object_r:rootfs:s0 init
dr-xr-xr-x root root u:object_r:proc:s0 proc
drwx------ root root u:object_r:rootfs:s0 root
drwxr-x--- root root u:object_r:rootfs:s0 sbin
lrwxrwxrwx root root u:object_r:rootfs:s0 sdcard ->
/storage/self/primary
-rw-r--r-- root root u:object_r:rootfs:s0 seapp_contexts
-rw-r--r-- root root u:object_r:rootfs:s0 selinux_version
-rw-r--r-- root root u:object_r:rootfs:s0 sepolicy
-rw-r--r-- root root u:object_r:rootfs:s0 service_contexts
drwxr-xr-x root root u:object_r:storage_file:s0 storage
dr-xr-xr-x root root u:object_r:sysfs:s0 sys
drwxr-xr-x root root u:object_r:system_file:s0 system
37
User Space Object Managers (USOM)
38
Original figure from: Frank Meyer “SELinux by Example: Using Security Enhanced Linux: Using Security Enhanced Linux”
libselinux
39
Libselinux on Android
Access to selinuxfs in
kernel controlled by
SELinux policy
40
USOMs on Android
41
Example USOM: ServiceManager
42
app_process handle2 BinderLM
Example USOM: ServiceManager
43
Example USOM: ServiceManager
In libselinux:
Retrieves security
context for a given PID
44
Example USOM: ServiceManager
45
Example USOM: ServiceManager
Only runtime
parameters
46
Check SELinux policy for allow rule for < subject type, object type, object class, operation>
Example USOM: ServiceManager
47
Example USOM: ServiceManager
48
Example USOM: ServiceManager, add KeyStore service
• What are the relevant allow rules for adding this service?
android.security.keystore u:object_r:keystore_service:s0
android.system.keystore2 u:object_r:keystore_service:s0
system/sepolicy/public/te_macros system/sepolicy/public/keystore.te
Last question: Which processes have type keystore? Those can add their
Binder as “android.security.keystore” or “android.system.keystore2” service!
49
Example USOM: ServiceManager, add KeyStore service
• Which processes have type keystore? Those can add their Binder as
“android.security.keystore” or “android.system.keystore2” service!
allow keystore keystore_service: service_manager add
system/sepolicy/public/te_macros system/sepolicy/private/keystore.te
system/sepolicy/private/file_contexts
• Answer:
− Only when the init process executes the /system/bin/keystore{2}
executables the resulting process can register itself as a service in
ServiceManager with the name “android.security.keystore” or
“android.system.keystore2”
− Since no other process than init can execute these executables, no
other process can register itself; init is started by the kernel during
system boot
− neverallow rule prevents any other type than keystore from adding the
service keystore_service
51
Principle of Tranquility
• Principle of tranquility: subjects and objects may not change their security
levels once they have been instantiated
− Violation of tranquility would violate fundamental restrictions
− Raising the classification of an object means that available information
is no longer available
− Lowering the classification means that information previously
considered restricted is now available to all (declassification problem)
• The tranquility principle has two forms:
− Principle of strong tranquility: security levels do not change during the
lifetime of the system
− Principle of weak tranquility: security levels do not change in a way that
violates the rules of a given security policy
− SELinux type transitions allow policy-controlled type changes
52
Zygote USOM: Labeling Android applications
53
SELinux integration into zygote
Zygote Dalvik VM
(UID A, security
(root) context A)
2) Load
App Code
APK
1) Fork() 3) setUID(A)
4) Set SELinux context A
(dynamic domain transition)
5) Give control flow to
54 VM process
Dynamic domain transition
• Permission dyntransition allows a running process to switch its own security context
− Abstractly like the setuid() call
• Very dangerous! Processes can change their type at any point! Possible target domains
for source domain specified in the policy but can more easily break tranquility
• Android: Allowed exclusively to the Kernel domain and zygote
− Kernel must dynamically transition into init domain for init process
− Zygote must dynamically transition into the application domain on app start after
forking
− Remember: Zygote does not “execve() application binaries” (no process hierarchy
as on a normal Linux), but instead “forks and loads” the application code
• Mostly restricted with neverallow rules. For example, in init.te:
# The init domain is only entered via setcon from the kernel domain,
# never via an exec-based transition.
neverallow { domain -kernel} init:process dyntransition;
neverallow domain init:process transition;
neverallow init { file_type fs_type }:file entrypoint;
55
Zygote USOM: Determining the type for an Application
• SE Android introduced new install policy definitions to define the mapping from
package information to security type
− Based on developer signature and AID/UID
− <AOSP>/system/sepolicy/private/mac_permissions.xml
− Define developer signature to “seinfo” mapping
− <AOSP>/system/sepolicy/private/seapp_contexts
− Defines rules to assign type to app based on AID, seinfo value, others
isSystemServer=true domain=system_server_startup
user=system seinfo=platform domain=system_app type=system_app_data_file
user=bluetooth seinfo=platform domain=bluetooth type=bluetooth_data_file
user=nfc seinfo=platform domain=nfc type=nfc_data_file
user=radio seinfo=platform domain=radio type=radio_data_file
user=shell seinfo=platform domain=shell name=com.android.shell type=shell_data_file
user=webview_zygote seinfo=webview_zygote domain=webview_zygote
user=_app domain=untrusted_app type=app_data_file levelFrom=all
− Used by SElinux user-space tools, called from Zygote, in order set the type
of the new app process
56
Example Security Contexts
• Process list showing security contexts:
root@generic_x86:/ $ ps –Z
LABEL USER PID PPID NAME
u:r:init:s0 root 1 0 /init
u:r:kernel:s0 root 2 0 kthreadd
u:r:kernel:s0 root 3 2 ksoftirqd/0
u:r:kernel:s0 root 4 2 kworker/0:0
u:r:kernel:s0 root 5 2 kworker/0:0H
u:r:kernel:s0 root 7 2 kworker/u:0H
u:r:kernel:s0 root 8 2 migration/0
…
u:r:system_server:s0 system 1533 1240 system_server
u:r:sdcardd:s0 media_rw 1629 1176 /system/bin/sdcard
u:r:platform_app:s0:c512,c768 u0_a14 1631 1240 com.android.systemui
u:r:sdcardd:s0 media_rw 1686 1176 /system/bin/sdcard
u:r:radio:s0 radio 1879 1240 com.android.phone
u:r:system_app:s0 system 2300 1240 com.android.settings
u:r:untrusted_app:s0:c512,c768 u0_a21 2339 1240 com.android.calendar
u:r:system_app:s0 system 2421 1240 com.android.keychain
u:r:untrusted_app:s0:c512,c768 u0_a31 2525 1240 com.android.exchange
u:r:platform_app:s0:c512,c768 u0_a50 2548 1240 com.android.messaging
u:r:su:s0 root 2846 1227 /system/bin/sh
57
Example Security Contexts
• Listing other processes in su security context:
root@generic_x86:/ $ id
uid=0(root) gid=0(root)
groups=0(root),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),300
1(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:su:s0
root@generic_x86:/ top
PID PR CPU% S #THR VSS RSS PCY UID Name
1233 0 1% S 11 132904K 10636K fg media /system/bin/mediaserver
1533 0 0% S 73 1393552K 87392K fg system system_server
1875 0 0% S 29 1289024K 45992K fg radio com.android.phone
1228 0 0% S 6 10032K 512K fg root /sbin/adbd
1231 0 0% S 4 9224K 1704K fg radio /system/bin/rild
2824 1 0% R 1 3980K 1108K fg root top
9 0 0% S 1 0K 0K fg root rcu_bh
10 0 0% S 1 0K 0K fg root rcu_sched
11 1 0% S 1 0K 0K fg root migration/1
...
1235 0 0% S 1 7600K 2256K fg keystore /system/bin/keystore
...
2318 0 0% S 17 1271556K 29588K fg u0_a21 com.android.calendar
58
Example Security Contexts
Source: Stephen Smalley. “Protecting the Android TCB with SELinux”. Linux Security Summit’14
60 Source: Stephen Smalley. “SELinux in Android Lollipop and Marshmallow”. Linux Security Summit’15
Protection by SELinux on Android (Examples)
Source: Stephen Smalley. “Protecting the Android TCB with SELinux”. Linux Security Summit’14
61 Source: Stephen Smalley. “SELinux in Android Lollipop and Marshmallow”. Linux Security Summit’15
Protection by SELinux on Android (Case-study)
Source: Stephen Smalley. “The Case for SELinux: Enhanced (SE) ANdroid”. Android Builder Summit’12
62
Problem: Least privilege policy
63
Research insights: ML-based Audit SELinux logs [149]
64
Research insights: Impact of vendor customizations [188]
65
SELinux as Reference Monitor
• Non-bypassable
− Part of the LSM architecture inside the Linux kernel
− Processes are not able to access kernel space resources (files, etc.)
without going through the LSM hooks
• Tamperproof
− Depends on the integrity of the kernel
− Kernel exploits exist
− What is the trusted computing base of SELinux policy enforcement?
− User-space processes enforcing policies (USOMs)
− Most importantly: Linux kernel
66
Problem of critical system exploits [126,130]
68
Root exploits and Permissions
69
Research insights: FlaskDroid [170]
70
Research insights: SEApp
71