Sandbox (Seatbelt)
App Sandbox là mandatory access control framework giới hạn mỗi process chỉ được truy cập tài nguyên được phép. Sandbox escape là bước bắt buộc trong hầu hết exploit chains từ app context.
Tại Sao Cần Học
- App Store apps bị sandbox → exploit từ app phải escape sandbox trước khi tấn công kernel
- System daemons cũng bị sandbox nhưng với profiles khác nhau (rộng hơn)
- Hiểu sandbox = biết process nào có thể truy cập attack surface nào (IOKit, Mach ports, …)
- Sandbox escape CVEs là thành phần quan trọng trong exploit chains thương mại (NSO, Predator, …)
Kiến Trúc Sandbox
Các Thành Phần
Userspace Kernel
┌──────────────┐ ┌─────────────────────┐
│ App Process │ ─── syscall ───→ │ Sandbox.kext │
│ (sandboxed) │ │ ├── Check policy │
│ │ │ ├── Allow/Deny │
│ │ │ └── Log violation │
└──────────────┘ └─────────────────────┘
│
┌───────┴───────┐
│ Compiled SBPL │
│ (sandbox │
│ profile │
│ bytecode) │
└───────────────┘
Sandbox Profile Language (SBPL)
Sandbox profiles được viết bằng Scheme-like language:
;; Mặc định deny tất cả
(version 1)
(deny default)
;; Cho phép đọc file trong container
(allow file-read*
(subpath (param "CONTAINER_PATH")))
;; Cho phép ghi vào tmp directory
(allow file-write*
(subpath (param "TMPDIR")))
;; Cho phép network outbound
(allow network-outbound
(remote tcp "*:80")
(remote tcp "*:443"))
;; Cho phép lookup Mach services cụ thể
(allow mach-lookup
(global-name "com.apple.frontboard.systemappservices"))
;; Cho phép mở IOKit UserClient cụ thể
(allow iokit-open
(iokit-user-client-class "IOSurfaceRootUserClient"))
;; Deny với logging (để debug)
(deny file-write*
(with report)
(subpath "/private/var/db"))
Profile Types
| Type | Áp dụng cho | Restrictiveness |
|---|---|---|
Container (container) |
App Store apps | Rất chặt: chỉ truy cập container, limited IPC |
| System profiles | System daemons | Tùy daemon: một số rất rộng |
| No sandbox | Kernel, launchd | Không bị giới hạn |
Operations Bị Kiểm Soát
| Category | Operations | Ví dụ |
|---|---|---|
| file | read, write, ioctl, mount | File system access |
| network | inbound, outbound, bind | Socket operations |
| mach | lookup, register, priv-host-port | Mach port operations |
| iokit | open, set-properties, get-properties | IOKit driver access |
| process | exec, fork, signal | Process operations |
| sysctl | read, write | Kernel parameter access |
| ipc-posix-shm | read, write, create | Shared memory |
| nvram | get, set, delete | NVRAM access |
Sandbox Internals
Kernel Implementation
Sandbox.kext hook vào MAC (Mandatory Access Control) framework của XNU:
- Mỗi syscall đi qua MAC policy check
sandbox_check()function trong kernel evaluate compiled profile- Profile compiled thành bytecode (binary format, không phải text SBPL)
Sandbox Container
Mỗi sandboxed app có:
/var/mobile/Containers/
├── Bundle/{UUID}/ ← App bundle (read-only)
│ └── MyApp.app/
├── Data/{UUID}/ ← App data (read-write)
│ ├── Documents/
│ ├── Library/
│ │ ├── Caches/
│ │ └── Preferences/
│ └── tmp/
└── Shared/AppGroup/{GROUP_UUID}/ ← Shared data (app groups)
Extension Points
Sandbox profile có thể:
(param "KEY")— nhận parameters lúc apply(extension "com.apple.app-sandbox.read" "/path")— dynamic extensions granted at runtime- System services (e.g., Photos, Contacts) grant extensions qua XPC
Sandbox Escape Techniques
1. IPC Service Exploitation
Phổ biến nhất. Sandboxed app giao tiếp với unsandboxed (hoặc less-sandboxed) daemon qua:
- XPC services: serialize/deserialize bugs, logic errors
- Mach messages: type confusion, memory corruption trong message handlers
- IOKit: driver bugs reachable từ sandbox
App (sandboxed) → XPC → mediaserverd (less sandbox) → IOKit → kernel
↑
Sandbox escape ở đây
Ví dụ thực tế:
mediaplaybackd— accessible từ WebContent sandboxGPU process— accessible từ WebContent sandboxCommCenter— telephony service
2. NSPredicate / NSExpression Injection
- Nhiều system services accept NSPredicate từ sandboxed clients
- NSPredicate có thể evaluate arbitrary expressions
- Bug class phát hiện bởi Trellix: hầu hết NSPredicateVisitor implementations có thể bị bypass
- Cho phép execute code trong context của target service (ngoài sandbox)
3. File System Confusion
- Symlink/hardlink attacks: trick privileged process đọc/ghi file ngoài sandbox
- Container boundary violations
4. Mach Port Theft / Injection
- Exploit Mach port lifecycle bugs để gain send right tới privileged port
mach_port_insert_rightvào target process
5. Logic Bugs Trong Sandbox Profile
- Sandbox profiles đôi khi allow quá rộng
- System profiles có thể có exceptions bị lợi dụng
Phân Tích Sandbox Profile
Sử dụng sandblaster
# sandblaster — extract và decompile sandbox profiles
git clone https://github.com/nicklasb/sandblaster
python3 sandblaster/reverse_sandbox.py \
-r sandbox_profiles_dir \
-o output_dir \
-t profile_name
Đọc compiled profiles trên device
# Profiles nằm trong:
/System/Library/Sandbox/Profiles/ # System daemon profiles
/usr/share/sandbox/ # Additional profiles
# App sandbox profile:
# Compiled vào sandbox.kext, không phải file riêng
# Dùng sandblaster để extract từ kernelcache
Profile quan trọng cần đọc
container— App Store app sandbox (chặt nhất)com.apple.WebKit.WebContent— WebContent process (Safari renderer)com.apple.mediaserverd— Media daemon (target phổ biến cho sandbox escape)
Tài Nguyên
- 8kSec — Reading iOS Sandbox Profiles
- Redfox Security — iOS Sandboxing Challenges
- Apple — Security of Runtime Process
- SSD Disclosure — 3 Sandbox Escape CVEs
- sandblaster on GitHub
Bài Tập
- Dump entitlements của 10 system daemons khác nhau, phân loại mức privilege
- Extract sandbox profile của
com.apple.WebKit.WebContent, liệt kê mọi IOKit UserClients được phép - Viết custom SBPL profile deny network nhưng allow file read/write, test trên macOS (
sandbox-exec) - Identify sandbox-reachable services: từ container profile, list mọi
mach-lookupglobal-names - Nghiên cứu 1 sandbox escape CVE: tìm write-up, trace attack path từ sandboxed app đến unsandboxed code