HITSB
HITSB
Jonathan Levin
http://NewOSXBook.com/
http://technologeeks.com
Sandbox versions
Version OS Version Notable Features
.. OS X 10.5/iOS 1-3 Initial version, white list approach
.. OS X 10.6/iOS 4
165 OS X 10.7/iOS 5 Basic containers
220 OS X 10.8/iOS 6 Sandbox exceptions
278-300 OS X 10.9/iOS 7 IOKit get property, vnode renaming
358 OS X 10.10/iOS 8 Rootless (introduction, non-enforcing), get-task,
AMFI integration (in OS X version), kexts (kind of)
460 OS X 10.11/iOS 9 Rootless enforcement, container manager
Host special ports, kexts, OSX NVRAM finally protected
Policy moved to __DATA.__const (iOS 9.2)
592 OS X 10.12/iOS 10 Container Manager enforcement (iOS)
User data items
Plan
• Prerequisite: MACF
• MacOS (“App Sandboxing”)
• *OS (Containers)
• Reversing (MacOS, iOS implementations)
• Sandbox APIs
Prerequisite: MACF
• Mandatory Access Control Framework serves as substrate
– XNU’s implementation modeled after FreeBSD’s
– Compare - SELinux/SEAndroid
Policy Module
Syscall/trap #n
Function calls out to Mandatory
Access Control Framework Policy module inspects
operation, and allows or
denies
Sandboxing
• Original sandbox approach – “seatbelt” – opt in:
– You’d have to ask to confined (like, want to go to jail!)
– Like its namesake, most people find it borderline troublesome.
libSystem.B.initializer
libdispatch_init
libsecinit_initialize_once <key>SECINITD_REGISTRATION_MESSAGE_SHORT_NAME_KEY</key>
<string>TextEdit</string>
<key>SECINITD_REGISTRATION_MESSAGE_IS_SANDBOX_CANDIDATE_KEY</key>
<bool>true</bool>
_libsecinit_setup_secinitd_client <key>SECINITD_REGISTRATION_MESSAGE_ENTITLEMENTS_DICT_KEY</key>
<dict> ... Entitlement dictionary</dict> s
e
xpc_copy_entitlements_for_pid c
i
n
xpc_pipe_routine <key>SECINITD_REPLY_MESSAGE_CONTAINER_ID_KEY</key> i
<string>com.apple.TextEdit</string> t
<key>SECINITD_REPLY_MESSAGE_QTN_PROC_FLAGS_KEY</key>
<integer>10</integer>
d
_libsecinit_setup_app_sandbox <key>SECINITD_REPLY_MESSAGE_CONTAINER_ROOT_PATH_KEY</key>
<string>/Users/morpheus/Library/Containers/com.apple.TextEdit/Data“</string>
<key>SECINITD_REPLY_MESSAGE_SANDBOX_PROFILE_DATA_KEY</key <data>
0x00003a014c004d0.. Compiled sandbox profile...</data>
__sandbox_ms <key>SECINITD_REPLY_MESSAGE_VERSION_NUMBER_KEY</key>
<integer>1</integer>
<key>SECINITD_MESSAGE_TYPE_KEY</key>
5) If decision is affirmative, libsecinit <integer>2</key>
voluntarily imposes sandbox on process <key>SECINITD_REPLY_FAILURE_CODE</key>
<integer>0</key>
App Sandbox
iOS: Containers
iOS 10 Containers
iOS: Containers
• The sandboxd has been entirely removed in iOS as of 9.x
– Still used in MacOS, primarily for tracing
AMFI
• Sandbox and AMFI make good bedfellows
• AMFI ensures signature, provides entitlement services
• Sandbox depends on AMFI (as of 358 in MacOS)
morpheus@Zephyr(~)$ kextstat
19 2 0xffffff7f8100f000 0xd000 0xd000 com.apple.driver.AppleMobileFileIntegrity (1.0.5) <7 6 5 4 3 2 1>
22 2 0xffffff7f8101c000 0x5000 0x5000 com.apple.kext.AppleMatch (1.0.0d1) <4 1>
23 1 0xffffff7f81021000 0x17000 0x17000 com.apple.security.sandbox (300.0) <22 19 7 6 5 4 3 2
Deconstructing Sandbox
• MacOS Sandbox.kext can serve as a good reference
– Largely same codebase, with some differences, but symbolicated
* - MacOS implementation slightly different (includes csr_check, etc). iOS also inlines eval_filter into eval
Profiles
Reversing Profiles
• Sandbox Profiles are written in tinyScheme (UGH!)
– In MacOS – plaintext, in /System/Library/Sandbox/Profiles
• Per framework profiles also exist for Apple’s frameworks
– in iOS – compiled & built-in!
• The gist:
– (version 1) (only version supported)
– (deny default) (least privilege)
– (allow ……) (selectively allow APIs)
– (deny ……) (selectively disallow APIs)
Sandbox-exec
• Simple binary (300-500 lines of ASM)
morpheus@Zephyr (~)$ sandbox-exec
Usage: sandbox-exec [options] command [args]
Options:
-f profile-file Read profile from file.
-n profile-name Use pre-defined profile.
-p profile-string Specify profile on the command line.
-D key=value Define a profile parameter.
Exactly one of -f, -n, -p must be specified.
Built-in Profiles
• MacOS originally had 4 “built-in” profiles
– Weren’t so useful in the first place and largely deprecated
• iOS extends that to dozens of profiles
– Can be found in kext
– Can also be found in iOS’s libsandbox.1.dylib
• AGXCompilerService ... wifiFirmwareLoader
Create executable extension Allow App to access and launch own executable
Sandbox APIs
• Sandbox usermode APIs provided by two libraries:
– /usr/lib/system/libsystem_sandbox.dylib
• Re-exported by LibSystem.B.dylib
• Mostly direct APIs to kext
– /usr/lib/libsandbox.1.dylib
• Profile compilation
• TinyScheme implementation statically linked in
• Plenty of Scheme strings/profile definitions in __TEXT.__const
Sandbox APIs
• mac_syscall (#381) used extensively:
Sandbox APIs
• Syscall implementations differ in between OSes, versions!
Op Sandbox function Purpose
0-1 _set_profile[_builtin] Set a profile (=label & containment) of a process
Sandbox APIs
• sandbox_check especially useful:
– Widely used in tweaks to gauge sandbox restrictions
– Commonly used with SANDBOX_CHECK_NO_REPORT
• Performs check silently, without any user-mode output
Demo: sbtool
Demo: sbtool
Sandbox Extensions
Extensions allow exceptions to a given profile
Sandbox Extensions
Apple’s App provide even more extensions for themselves:
Extension
com.apple.security.exception.shared-preference.read-write
com.apple.sandbox.application-group
com.apple.tcc.kTCCServiceAddressBook
com.apple.sandbox.executable
com.apple.app-sandbox.read
com.apple.security.exception.mach-lookup.global-name
com.apple.security.exception.iokit-user-client-class
com.apple.security.exception.files
com.apple.sandbox.container
Sandbox Extensions
• Before sandboxing, caller can set extensions (unless forbidden)
Take Aways
• If you’re even loosely interested in OSX/iOS:
– The sandbox is the first, possibly last line of security
– In iOS, provides the most important obstacle to jailbreaking
– In MacOS, containerizes AppStore Apps, and implements SIP
• http://NewOSXBook.com/articles/hitsb.html
– Source of sandbox_exec clone
– Sbtool – open source
– Ongoing documentation on profile reversing
– Fully symbolicated companion file for iOS 10 kext