Jonathan Levin - *OS Internals Volume III: Security & Insecurity - Detailed index
Book revision: 01/10/2017


Part I: Defensive Techniques and Technologies
The missing documentation for Apple's proprietary security mechanisms

Chapter I: Authentication
1
Password files (*OS)
2
SetUID and SetGID (MacOS)
3
The Pluggable Authentication Module (MacOS)
4
- Function classes
5
- Control Flags
5
- Experiment: Tinkering with PAM configuration files
7
opendirectoryd (MacOS)
8
- Maintaining permissions
9
- The data stores
9
- Experiment: Manipulating local users using dscl(1)
10
- Experiment: Manipulating local users using dscl(1) (cont.)
11
Communicating with clients
12
- Experiment: Domonstrating XPC behind the scenes of getXX APIs
13
- com.apple.opendirectoryd.membership
14
- com.apple.opendirectoryd.api
15
- com.apple.opendirectoryd.rpc
15
The LocalAuthentication Framework
16
- coreauthd
17
- XPC protocol
17
- Entitlements
17
Apple IDs
18
- AppleIDAuthAgent
18
External Accounts
19
- External Profiles
19
References
20

Chapter 2: Auditing (MacOS)
21
Design
22
- A little history
22
- Auditing Concepts (a refresher)
22
- Experiment: Tweaking and viewing auditing in real time
24
Audit Sessions
25
Implementation
26
- Figure 2-12: The implementation of auditing in the MacOS kernel
27
- auditd
28
System call interface
30
- audit(#350)
30
- audition(#351)
31
- [get/set]auid (#353, #354)
31
- [get/set]audit_addr (#357, #358)
31
- auditctl (#359)
31
OpenBSM APIs
32
- Querying the policy
32
- Reading Audit Records
32
- Writing Audit Records
33
Auditing Considerations
34
References
34

Chapter 3: Authorization (KAuth)
35
Design
36
Implementation
37
- KAuth Scopes
37
- KAUTH_SCOPE_GENERIC
38
- KAUTH_SCOPE_PROCESS
38
- KAUTH_SCOPE_FILEOP
39
- KAUTH_SCOPE_VNODE
39
- Authorizing vnode operations
41
KAuth Identity Resolvers (MacOS)
42
- Experiment: Exploring the identitysvc() system call
43
Debugging KAuth
44
References
44

Chapter 4: Mandatory Access Control Framework
45
Background
46
- Nomenclature
46
- Experiment: Finding MAC Policy modules in MacOS and *OS
48
MACF Policies
49
- Experiment: Figuring out policy operations from a disassembly
52
Setting up MACF
53
MACF Callouts
54
- expose_task (MacOS 10.11)
57
- priv_check
58
MACF System Calls
59
Final Notes
60
References
60

Chapter 5: Code Signing
61
The Code Signature Format
62
- LC_CODE_SIGNATURE and the SuperBlob
62
- Experiment: Code Signature Blobs
63
- The Code Directory Blob
64
- Code Page Slots
64
Experiment: Viewing Code Signatures
65
- Special Slots
67
- Experiment: Demonstrating the special signature slots
68
- Ad-Hoc Signatures
68
- Experiment: Generating (self-signed) code signature
69
- Code Signing Flags
70
Code Signature Requirements
71
- The Requirements Grammar
71
- Encoding requirements
72
- Experiment: Examining requirement blobs
73
- Requirement validation
74
Entitlements
75
Code Signature Enforcement
77
- Exceptions
79
- Debugging
80
- Code Signing Weaknesses
81
- Jekyll Apps
81
- Bait-and-Switch inode reuse (< iOS 9)
81
- Locked memory
81
- Lack of validation on __DATA sections and writable memory
82
- Exploiting kernel bugs
82
Code Signing APIs
83
- System Calls
83
- Framework-Level Wrappers
84
- Experiment: Locating entitlement producing daemons
85
- sysctl
86
- DTrace probes (MacOS)
86
References
87

Chapter 6: Software Restrictions (MacOS)
89
Authorizations
90
- The authorization database
90
- Experiment: Examining the authorization database
91
authd
92
- Protocol
92
- Experiment: Executing with privileges
93
GateKeeper (MacOS)
94
- Precursor: Quarantine
94
- Experiment: Displaying the quarantine attributes of a file
95
libquarantine
96
Quarantine.kext
97
- User mode interface
97
Quarantine in action
98
- CoreServicesUIAgent
98
syspolicyd
100
- Experiment: Making sense of the policy database
101
- MacOS 13: Secure Kernel Extension Loading
102
- XPC protocol
102
- spctl(8)
103
App Translocation
104
- Testing translocation
104
- Experiment: Behind the scenes of Path Translocation
105
- Experiment: Behind the scenes of Path Translocation (cont.)
106
Managed Clients (MacOS)
107
- parentalcontrolsd
108
- mdmclient
109
- Startup
109
- Arguments
110
- Entitlements
110
ManagedClient
111
- Mach Messages
111
- Plugins
112
- ManagedClientAgent
112
- Entitlements
112
- APIs
113
- Managed Preferences
113
- Managed Apps
113
- mcxalr.kext
114
- sysctl MIBs
114
- Plugins
115
References
115

Chapter 7: AppleMobileFileIntegrity
117
AppleMobileFileIntegrity.kext
118
- Initialization
118
- boot-args
118
The MACF Policy
120
- proc_check_cpumon (*OS)
121
- proc_check_inherit_ipc_ports
121
- proc_check_get_task
122
- proc_check_map_anon (*OS)
123
- file_check_mmap
124
- proc_check_library_validation
125
- proc_check_mprotect (*OS)
126
- proc_check_run_cs_invalid (*OS)
126
- vnode_check_exec (*OS)
127
- vnode_check_signature
128
- cred_label_update_execve
129
- Exception Handling hooks (MacOS 12+)
130
- Kernel APIs
131
amfid
132
- Daemon-Kext communication
132
- Experiment: Inspecting amfid Mach messages
134
- MIG subsystem 1000
135
Provisioning Profiles
139
- Experiment: Examining provisioning profiles
141
- libmis.dylib
142
- The UDP functions
143
- Profile/UDP "databases"
143
- misagent
144
- online-auth-agent
145
The AMFI Trust Caches
147
The AMFI User Client
148
Final Notes
148
References
148
Chapter 8: The SandBox
149
The Evolution of the Sandbox
150
App Sandbox (MacOS)
151
- (semi)-Voluntary confinement
152
- Experiment: Toying with the App Sandbox
153
- Diagnosing and controlling the App Sandbox
154
Mobile Containers (*OS)
155
Sandbox Profiles
157
- Sandbox profile language
157
- Experiment: Exploring sandbox profiles with sandbox-exec
158
- Sandbox operations
159
- Table 8-9: Sandbox operations (as of v592)
161
- Compiling profiles
162
- Exploring: Steps to decompile a sandbox profile
163
- Extensions
164
- Experiment: Reversing the Sandbox extension token format
166
User mode APIs
167
- sandbox_check
167
- sandbox_[un]suspend
167
- sandbox tracing (460+)
168
- Inspection (460+)
168
- User state items (570+)
168
mac_syscall
169
Sandbox.kext
170
- Flow
170
- hook_policy_init
171
- hook_policy_initbsd
172
- hook_policy_syscall
173
- The Sandbox MACF Hooks
173
- Experiment: Reversing a sandbox hook implementation
174
- Experiment: Reversing a sandbox hook implementation (cont.)
175
- Handling process execution
176
Profile Evaluation
178
Sandboxd (MacOS)
179
- Daemon-Kext Implementation
180
References
180

Chapter 9: System Integrity Protection (MacOS)
181
Design
182
Implementation
183
- Filesystem protections
184
- Debugging protections
184
- Entitlements
185
- Entitlement/Disablement
186
APIs
188
- csrctl (#483)
188
- rootless_* APIs
189
References
189

Chapter 10: Privacy
191
Transparency, Consent and Control
192
- The TCC daemon(s)
192
- Protected Information
192
- The TCC Database(s)
193
- Experiment: Examining the TCC database
194
- Prompting for access
195
- XPC API
195
- TCCAccess* APIs
196
- Experiment: Exploring tccd's XPC interface
197
- Entitlements
198
- Debugging Options
198
Unique Device Identifiers
199
Differential Privacy (MacOS 12/iOS 10)
201
References
202

Chapter 11: Data Protection
203
Volume-level Encryption (MacOS)
204
- Mounting Encrypted Volumes
206
- corestorage daemons
207
- CSDFE* APIs
209
File-level Encryption (*OS)
210
- com.apple.system.cprotect and protection classes
210
- Experiment: Viewing data protection classes
212
- Effaceable Storage
213
- Device Lock/Unlock
214
mobile_obliterator
215
- Obliteration
216
- Entitlements
216
Keybag
218
- KeyBagd
219
- Experiment: Reversing the keybagd XPC interface
220
The AppleKeyStore.kext
221
- Entitlements
222
- Hardware backing
222
Keychains
223
- System Keychain
223
- The Login keychain
223
- The iOS Keychain
224
- Programmatic API
224
- KeyChain Structure
225
- Experiment: Inspecting KeyChain internals
227
Final Notes
228
References
228

Part II: Vulnerabilities and Exploitation
E pur si rompe
A detailed exploration of bugs and their exploits

Chapter 12: MacOS Vulnerabilities
231
10.1: The ntpd remote root (CVE-2014-9295)
232
10.2: The rootpipe privilege escalation (CVE-2015-1130)
234
10.3: Racing Kextd (CVE-2015-3708)
236
10.4: DYLD_PRINT_TO_FILE privilege escalation (CVE-2015-3760)
238
10.5: DYD_ROOT_PATH privilege escalation
240
11.0: tpwn privilege escalation and/or SIP neutering
242
11.3: "Mach Race" local privilege escalation (CVE-2016-1757)
244
- Apple Fix
245
11.4: LokiHardt'S Trifecta (CVE-2016-1796,1797,1806)
246
- Arbitrary Code Execution (CVE-2016-1796)
246
- Sandbox Escape (CVE-2016-1797)
248
- SubmitDiagInfo (CVE-2016-1806)
248
- Getting root
249
- Apple Fixes
250
Final Notes
251
References
252

Chapter 13: Jailbreaking
253
Mythbusting
254
Terminology
255
The jailbreaking process
257
- Running arbitrary (unsigned) code
257
- Getting on the device
257
- Bypassing code signing
258
- Escaping the confines of the Application Sandbox
258
- Elevating Privileges
259
- Reading and Writing Kernel Memory
259
Kernel Patches
261
- MACF sysctl patches
261
- setreuid
263
- TFP0
264
- Kernel pmap
265
- boot-args
266
- Sandbox
267
- AMFI
268
- Root filesystem remount
268
Kernel Patch Protection
269
- Implementation
270
- Experiment: Inspecting KPP with joker and jtool
271
- Experiment: Inspecting KPP with joker and jtool (cont.)
272
- Entry points
273
- Cryptographic algorithm
274
- iOS 10 kernel changes
275
- KTRR (iPhone 7 and later)
275
Evolution of iOS Jailbreaks
279
References
280

Chapter 14: Evasi0n
281
The Loader
282
- Initial contact
283
- Shebang Shenanigans
283
- Picking lockdownd
284
- Pièce de Résistance - Code Signing
285
- Segment overlap
286
- Persistence through /etc/launchd.conf
287
The Untether
288
Kernel-mode exploits
290
- Kernel Memory Layout: I - Zone ("heap") Layout
290
- Kernel Code Execution: IOUSBDeviceFamily's stallPipe()
292
- Arbitrary Memory Read/Write with Mach OOL Descriptors
296
- Kernel Memory Layout: II - Kernel base
298
- Refinement: Read (small) Primitive
299
- Refinement: Read (large) Primitive
299
- Refinement: Write Gadget
300
Apple Fixes
301
References
302

Chapter 15: Evasi0n 7
303
The Loader
304
- Initial Contact
305
- Injecting the Application
305
- Unsandboxing afcd
306
- Dyld Injection (I): Loading gameover.dylib
306
- Privilege Escalation
307
- Dylib Injection (II): Replacing xpcd_cache.dylib
308
- Dylib Injection (III): Trojaning libmis.dylib
309
- Reproducing the jailbreak
310
The Untether
311
Kernel Mode exploits
313
- Exploitation
316
Apple Fixes
319
References
320

Chapter 16: Pangu 7 (PanguAxe) (盘古斧)
321
The Loader
322
- The Dummy App
322
- Certificate Injection
323
- The Jailbreak Payload
324
- The Untether
325
- Flow
326
Kernel-mode Exploits
327
- Leaking the kernel stack
327
- Breaking early_random()
330
- Kernel Memory Overwrite(1): IODataQueue
332
- Kernel Memory Overwrite(2): IOHIDEventServiceUserClient
333
- Refinement: Arbitrary kernel memory overwrite
334
Apple Fixes
335
References
336

Chapter 17: Pangu 8 (軒轅劍)
337
The Loader
338
User mode exploits
339
- Certificate Injection
339
- Loading the Exploit Library
339
- Bypassing code signatures
341
The Untether
343
Apple Fixes
344
References
346

Chapter 18: TaiG (太极)
347
The Loader
348
- Sandbox Escape: AFC and BackupAgent
349
- DDI Race Condition
350
- The Fake DDI
351
- libmis.dylib and overlapping segments (again)
353
- Final steps
353
The Untether
354
Kernel-mode Exploits
356
- KASLR Info Leak via OSBundleMachOHeaders (again)
356
- Experiment: Observing the Get Loaded Kext Info exploited
357
- mach_port_kobject strikes again
358
- IOHIDFamily... Again...
360
- Experiment: Obtaining a kernel dump using TaiG 1
365
Apple Fixes
367
References
369

Chapter 19: TaiG 2
371
Code Signing Bypass
372
The Untether
379
Kernel Exploit
380
Apple Fixes
382
References
384

Chapter 20: Pangu 9 (伏羲琴)
385
The Loader
386
- Loading the Jailbreak App (10-20%)
388
- Backing up (30%)
388
- Configuring the Environment (45%)
388
- After reboot (55%)
389
- Launching the Pangu App (75%)
390
- WW..What?!
391
The Jailbreak Payload
392
Kernel-Mode Exploit
393
- Old Faithful
393
- The Exploit
394
- Arbitrary Code Execution - I: Bypassing KASLR
396
- Arbitrary Code Execution - II: Inspecting gadgets
396
Code signing bypass
398
- Experiment: Examining Pangu 9 shared cache
399
- Experiment: Examining Pangu 9 shared cache (cont.)
400
The Untether
401
- Anti-Anti-Debugging
401
Apple Fixes
403
References
404

Chapter 21: Pangu 9.3 (女娲石)
405
The Kernel Exploit
406
- The Bug
406
- The Exploit primitive
408
- Defeating KASLR
408
- Arbitrary Code Execution
409
The Apple Fix
410

Chapter 22: Pegasus (Trident)
411
Exploit Flow
412
- Stage1
412
- Stage2
413
- Stage3
414
Kernel Memory Read and KASLR Bypass
416
Arbitrary Kernel Memory Write
418
Persistence
419
- Javascript payload
420
Apple Fixes
422
References
422

423
The Info Leak
424
- Experiment: Figuring out what the leaked kernel address is
426
Zone grooming
427
mach_ports_register
428
Putting it all together - a Phœnix rises!
429
Apple Fixes
431
References
431
mach_ports_register
428

Chapter 23: mach_portal
433
Exploit Flow
434
Mach port name urefs handling
435
- Applying the attack to launchd
437
Crashing powerd
438
XNU UaF in set_dp_control_port
441
Disabling protections
443
- Defeating KASLR
443
- Unsandboxing - The "ShaiHulud Maneuver"
443
- Root filesystem r/w
443
Bypassing code signatures
444
Apple Fixes
446
References
446

Chapter 24: Yalu (10.0-10.2)
447
Primitives
448
- [Read/Write]Anywhere64
448
- [FuncAnywhere32]
448
- Platform Detection
450
KPP Bypass
451
- kppsh1
452
- e0
453
Post-Exploitation
454
10.2: A deadly trap and a recipe for disaster
455
- The bug
455
- The exploit (Beer)
456
- Kernel read-write
458
- Experiment: Adapting a PoC to a different kernel version
459
- The exploit (Todesco & Grassi)
460
- Constructing a fake Mach object
460
- Triggering the overflow
462
- Defeating KASLR
464
- Getting the kernel task port
465
Final Notes
466
References
466

469
Bypassing KASLR
470
- The Bug
470
- The Exploit
471
Kernel Memory Corruption
473
- The Exploit
473
- Kernel function call primitive
474
Post-Exploitation: The Jailbreak Toolkit
476
- Prerequisite: Manipulating the process and task lists
476
- Rootify
478
- Shai Hulud
479
- Remounting the root filesystem as read-write
480
- Entitlements
482
- Injecting entitlements - I - The CS Blob
482
- Injecting Entitlements - II - AMFI
483
- Replacing entitlements
485
- Borrowing entitlements
486
- Platformize
487
- Bypassing code signing
488
- The AMFI Trust Cache
488
- amfid
488
- Code injection
488
- More minutiae
488
- Sandbox annoyances
490
- References
490

Appendix A: MacOS Hardening Guide
497

511
Mandatory Access Control (MACF)
511
GateKeeper (MacOS)
512
- Application Notary
512
AMFI
512
- CoreTrust (iOS 12)
512
SandBox
513
Privacy
513
APFS Snapshot mount (iOS 11.3)
514