22-Hexacon-Hara-Kirin Dissecting The Privileged Components of Huawei Mobile Devices
22-Hexacon-Hara-Kirin Dissecting The Privileged Components of Huawei Mobile Devices
22-Hexacon-Hara-Kirin Dissecting The Privileged Components of Huawei Mobile Devices
About Us
Impalabs – @the_impalabs
Website – https://impalabs.com
Blog – https://blog.impalabs.com
Outline
Introduction
Introduction
Bootchain
Bootchain
Hypervisor
Hypervisor
Secure
SecureMonitor
Monitor
Secure
SecureKernel
Kernel
Trusted
TrustedOS
OS
Trusted
TrustedApplications
Applications
Conclusion
Conclusion
Introduction
Android Device Architecture
Kernel-Based Security
# CPU virtualization
NORMAL WORLD
• Traditionally used to execute multiple operating
KERNEL
# ARM virtualization extensions
•
HVC
•
HYPERVISOR
Memory access restrictions
• Exceptions interception
Secure World
PROC PROC PROC APP APP APP
Secure Monitor
HVC
•
SMC SMC
•
verifies & executes verifies & executes
#
LOADER #N
OEMs implement additional features
• Anti-rollback mechanism
# Security mechanisms
BOOTROM
• Secure boot: prevents replacing or modifying
•
(Cortex-M)
Bootloader lock: prevents reflashing the
# Bootstrapping challenges
#
(Cortex-A)
Getting control over the boot chain
vulnerability first
Boot Chain
First Research Device
not verified
FASTBOOT
crypto engine
BOOTROM
xmodem bug
• Fastboot is split into a privileged
• BootROM code execution can be achieved from this arbitrary write primitive
Dump Xloader using the Flash Patch and Breakpoint unit of the LPMCU
# Huawei “fixed” the BootROM bugs by burning a fuse to disable the USB recovery mode
Boot Chain
Continuation of Execution
Steps Steps
BootROM Xloader
Patches Patches
Qualcomm’s Snapdragon
CODE CODE CODE CODE
DATA DATA DATA DATA
Kernel
•
RO RO
Module
modules code segment can be executed at EL1
DATA DATA DATA DATA DATA DATA DATA DATA
secure npu, modem, etc.) inaccessible to EL0 and EL1 PAGE Executable PAGE Read-Only
•
MEMORY PHYSICAL MEMORY MEMORY
TRANSLATION
MEMORY
TRANSLATION
MEMORY
•
TABLES TABLES
•
TABLES
63 62 59 58 55 54 53 52 51 50 48 47 12 11 10 9 8 7 6 5 2 1 0
RES PBHA IGNORED XN Cont DBM RES0 OA FnXS AF SH S2AP MemAttr Type
1 0 Not executable at EL0 or EL1 1 RES0 Not executable at EL0 or EL1 10 Write-only
# Initial processing
e.g. descriptors with the contiguous bit set actually point to contiguous memory
By default, kernel pages are set non executable at EL1 and non accessible at EL0
# Changes monitoring
• A write to a stage 1 descriptor or a translation fault during a page table walk raises an exception
Handled by the hypervisor to ensure modifications are permitted and update stage 2 accordingly
Security Hypervisor
Software Attributes
63 62 59 58 55 54 53 52 51 50 48 47 12 11 10 9 8 7 6 5 2 1 0
RES PBHA IGNORED XN Cont DBM RES0 OA FnXS AF SH S2AP MemAttr Type
#
Attrs Description
Hypervisor Software Attributes
0b0000 Unmarked
0b1000 OS Read-Only
• Used to prevent disallowed changes to protected memory
0b1001 OS Module Read-Only
• Analysis can be augmented with information # Comparing the security hypervisors of different
data_ptr
head_size data_size
HEADER DATA
H E L L O _ W O R L D \n
curr_off
# CVE-2021-39979
• We can write log strings at any virtual address that is mapped into the hypervisor
Security Hypervisor
Exploitation
data_ptr = target
character
• The allocation function assumes the offset value is void malloc(uint64_t size) {
•
}
Right after the heap is a kernel-accessible region
Security Hypervisor
Exploitation
Global Variables
remaining 0x005BA400
0x12F14C00
heap_ptr Heap Region
0x134CF000
Security Hypervisor
Exploitation
Global Variables
S2 Page Table
S2 Page Table
S2 Page Table
S2 Page Table
S2 Page Table
S2 Page Table
S2 Page Table
S2 Page Table
S2 Page Table
heap_ptr
0x134CF000
Security Hypervisor
Exploitation
Global Variables
•
Heap Region
Step 2: Use the constrained write primitive to
S2 Page Table
S2 Page Table
S2 Page Table
S2 Page Table
S2 Page Table
S2 Page Table
S2 Page Table
S2 Page Table
heap_ptr 0x134CF000
Security Hypervisor
Exploitation
Global Variables
•
Heap Region
Step 2: Use the constrained write primitive to
•
S2 Page Table
S2 Page Table
S2 Page Table
S2 Page Table
S2 Page Table
S2 Page Table
0x134CF000
HVC Handler
mov x1, #8
mov x0, x8
mov x1, #8
mov x0, x8
Java Native
TA TA TA TA TA
Applications Processes
/dev/binder
ITeecService
libc libgm libtee libvendor
tee_auth_daemon
IPC
/dev/hwbinder
ILibteecGlobal
GTask
libteec@3.0-service
Perm
Platdrv RPMB SSA TUI
Serv
libteec_vendor
@tc_ns_socket
Shared IPC
libteec_vendor
Non-Secure
Memory
SVC
/dev/tc_ns_client
TEEK
Client API
Kernel Driver Secure Kernel
TEE-OS
SMC Handlers
Dispatcher
Forward to custom TEE OS Handler
TrustZone
Normal World Overview
ITeecService
•
libc libgm libtee libvendor
/dev/hwbinder
GTask
Serv
RPMB SSA TUI
libteec_vendor
Shared IPC
•
teecd
Memory
•
Client API
through an SMC
SMC SECURE MONITOR SMC
Java Native
TA TA TA TA TA
Applications Processes
/dev/binder
ITeecService
libc libgm libtee libvendor
# Secure Monitor
tee_auth_daemon
IPC
/dev/hwbinder
•
ILibteecGlobal
the trusted OS
Perm
Platdrv RPMB SSA TUI
Serv
#
libteec_vendor
Trusted OS @tc_ns_socket
Shared IPC
teecd
Non-Secure
TEE-OS
SMC Handlers
Dispatcher
Forward to custom TEE OS Handler
Secure Monitor
Secure Monitor
Introduction
ITeecService
libc libgm libtee libvendor
IPC
ILibteecGlobal
•
GTask
Bridge between the Normal and Secure libteec@3.0-service
Worlds Perm
Platdrv RPMB SSA TUI
Serv
libteec_vendor
the trusted OS
Shared IPC
teecd
Non-Secure
Memory
#
hmsysmgr hmfilemgr
Huawei’s implementation based on the ARM
FS MISC RPMB SOCKET
Client API
Kernel Driver Secure Kernel
# CVE-2021-39994
• A response from the SE uses the user_data structure where the user uint32_t unkn;
uint32_t addr;
The address of user_data, that contains the response metadata
•
uint32_t size;
• Body Leveluser_data.size))
user_data.size = min(size, user_size);
•
Five
Information about the SE’s response is thus written at a user-
if (check(user_data.addr,
controlled address
memcpy(user_data.addr, addr, user_data.size);
response metadata
#
Global Variables
Step 1: Use the response metadata to disable the check on the
cma_addr 0x40000000
user_data.addr
• Data isn’t controlled either, but gives us more options
Secure Monitor
Exploitation
response metadata
#
Global Variables
Step 1: Use the response metadata to disable the check on the
cma_addr 0x40000000
0xC
user_data.addr
• Data isn’t controlled either, but gives us more options
Secure Monitor
Exploitation
#
Global Variables
Step 1: Use the response metadata to disable the check on the
cma_addr 0xC
user_data.addr
•
Handler #1
Data isn’t controlled either, but gives us more options
...
BLR X2
Secure Monitor
Exploitation
#
Global Variables
Step 1: Use the response metadata to disable the check on the
cma_addr 0xC
user_data.addr
•
Handler #1
Data isn’t controlled either, but gives us more options
•
response data
...
BLR X2
Secure Monitor
Exploitation
#
Global Variables
Step 1: Use the response metadata to disable the check on the
cma_addr 0xC
user_data.addr
•
Handler #1
Data isn’t controlled either, but gives us more options
•
response data
# Step 3: Call a write gadget to create stable read and write 0x14204A7C
LDR
CBNZ
X2,
X2,
[X2,#0xB8]
loc_14204AA0
primitives
...
BLR X2
RET
Secure Monitor
Exploitation
#
Global Variables
Step 1: Use the response metadata to disable the check on the
cma_addr 0xC
user_data.addr
•
Handler #1
Data isn’t controlled either, but gives us more options
# Step 3: Call a write gadget to create stable read and write 0x14204A7C
LDR
CBNZ
X2,
X2,
[X2,#0xB8]
loc_14204AA0
primitives
...
BLR X2
RET
Secure Monitor
Exploitation
#
Global Variables
Step 1: Use the response metadata to disable the check on the
cma_addr 0xC
user_data.addr
• Data isn’t controlled either, but gives us more options
Arbitrary Read
... ...
primitives
Secure Monitor
Exploitation
#
Global Variables
Step 1: Use the response metadata to disable the check on the
cma_addr 0xC
user_data.addr
• Data isn’t controlled either, but gives us more options
•
page tables
Arbitrary Read
... ...
primitives
#
Global Variables
Step 1: Use the response metadata to disable the check on the
cma_addr 0xC
user_data.addr
• Data isn’t controlled either, but gives us more options
• Change an existing function pointer to an interesting gadget Arbitrary Write 0x14205E74 0x14001000 0x14002000 RX
primitives
#
Global Variables
Step 1: Use the response metadata to disable the check on the
cma_addr 0xC
user_data.addr
• Data isn’t controlled either, but gives us more options
• Change an existing function pointer to an interesting gadget Arbitrary Write 0x14205E74 0x14001000 0x14001000 RX
# Step 3: Call a write gadget to create stable read and write 0x15000000 0x14000000 RW
#
0x15002000 0x14002000
SECURE WORLD
architecture
• Processes (S-EL0)
Platdrv
Perm
RPMB SSA TUI
Serv
SYSTEM
DRV_TIMER
MANAGER
FILE
TEESMCMGR
MANAGER
TEEOS
TEEOS CPIO Archive
TASKS
SECURE WORLD
as:
•
libc libgm libtee libvendor
• Inter-process communication
GTask
• Process scheduling
Perm
•
Platdrv RPMB SSA TUI
Serv
# Huawei Implementation
• IRQCTRL / IRQHDLR
• Most likely inspired by seL4
• VSRoot
• Capabilities system described in a patent filed
in 2019
• Timer
• TEESMC
• All system resources are associated with a
capability • etc.
node)
PROCESS A HMSYSMGR
Grant
CNODE CNODE
... ...
Grant
Main thread
THREAD THREAD
CNODE CNODE
... ...
Grant
VSROOT CHANNEL
hmsysmgr
communications
CHANNEL
Grant Grant
Managers
Overview
SECURE WORLD
TA TA TA TA TA
# Managers
IPC
Secure Kernel
Managers
File & System Managers
TAFS etc.
SECURE WORLD
systems
#
libc libgm libtee libvendor
•
IPC
• Session management
SECURE WORLD
# DRV_TIMER # RPMB
TA TA TA TA TA
•
libc libgm libtee
Uses a normal world agent
libvendor
# GATEKEEPER
• Gatekeeper implementation # SSA IPC
•
GTask
Uses a normal world agent
• Keymaster implementation
•
Serv
glue between GlobalPlatform
• Permissions system for RPMB,
and OS-level APIs
SSA and TUI
IPC
# PLATDRV # TUI
•
hmsysmgr hmfilemgr
Trusted User Interface
• Platform drivers
implementation
• Interrupts, crypto engine, secure
Secure Kernel
Tasks & Drivers
Security
•
Heap buffer overflows
Permissions system
•
also be added dynamically
Only specific tasks can reach the
SECURE WORLD
• Manage the global state • Manage the per-CA state • Handles a request coming from a CA
Create
Destroy
Trusted Applications
Authentication
# Chain of trust
•
•
NX
Secure functions (e.g. memcpy_s)
• RelRO
• Parameter buffers are copied to
•
•
ASLR
Robust and generic Parcel-based
•
The TA base address was written at a
Output buffer sizes can only be
fixed address by the loader
reduced
Only works for the ELF sections,
#
•
The attack surface mostly boils down
Stack & heap buffer overflows
to the command handlers
• Information leaks
# HWPSIRT-2021-63568
struct {
size_t size;
uint32_t a;
uint32_t b;
# HWPSIRT-2021-80349
} value;
} TEE_Param;
uint32_t commandID,
TEE_Param params[4]
SFS
# Arbitrary read
SFS
# Arbitrary read
SFS
# Arbitrary read
key_len 0x42
... ...
if (keyblob->magic == 0x534554
memcpy_s(
params[1].memref.buffer,
params[1].memref.size,
keyblob + keyblob->key_off,
keyblob->key_len);
}
Trusted Applications
Exploitation of HW_KEYMASTER
SFS
# Arbitrary read
key_len 0x42
... ...
Keyblob opened
heap
if (keyblob->magic == 0x534554
params[1].memref.buffer,
Heap
params[1].memref.size,
keyblob + keyblob->key_off,
keyblob->key_len);
}
Trusted Applications
Exploitation of HW_KEYMASTER
SFS
# Arbitrary read
Heap
key_off
key_len
Trusted Applications
Exploitation of HW_KEYMASTER
# Arbitrary read
# Arbitrary write
... ...
# All vulnerabilities were reported to Huawei Bug Bounty Program and fixed in updates released prior to this presentation
• Defense-in-depth measures
• But teams with the resources to break the encryption layer might be less likely to share their findings
• https://blog.impalabs.com
Thank you!