Friday, October 20, 2017

Reversing AppleMobileFileIntegrity (AMFI) Part 2

Recap
Let's start with a quick recap of part 1 :
  • We introduced MACF(Mandatory Access Control Framework)
  • Learned a little about KEXTs and how they relate to MACF
  • Got introduced to Policies in the context of MACF
  • Learned to identify policies on both macOS and iOS
  • Saw that AppleMobileFileIntegrity.KEXT is a policy module
  • Delved a little into policy registration to extract and identify callback functions

Ad-hoc Binaries
As discussed in part 1, AAPL has full control of all system binaries and added the hashes of these binaries in the KEXT. These binaries as we saw are referred to as being ad-hoc signed and their validation involves a simple lookup of the hash in the KEXT's TrustCache. 


The TrustCache is located in the __TEXT.__const  section of the KEXT. So if we extract the hash of the amfid binary we should be able to find it in the TrustCache. This is demonstrated below where we first extract the signature:

 macho-reverser:iOS10.3 macho-reverser$ jtool --sig amfid | grep CDHash  
           CDHash:        758a25a4549569ac0d36d3b69a92c937180a18e2 (computed)  
 macho-reverser:iOS10.3 macho-reverser$   

And then locate it in the TrustCache:

 macho-reverser:AMFI macho-reverser$ jtool -d __TEXT.__const com.apple.driver.AppleMobileFileIntegrity.kext  

You will have to search through the output as the CDHash is buried deep into the Cache.
amfid CDHash located in __TEXT.__const section
Now there was a bug here - and we will discuss it next - as for a while you could trick hijack amfid and take over it's operations. AAPL fixed this in iOS 9 and now the KEXT verifies the hash before proceeding - as seen at offset 0xFFFFFFF00644F018.

Verifying amfid CDHash in KEXT 
As a side note to help with symbolication I use joker with the -j option to generate a companion file.

Inter-Process Communication(IPC) 101
*OS is built on the XNU kernel. And at the core of XNU is Mach. This microkernel handles among other things Interprocess communications and messaging. Mach's IPC services rely on the notion of "ports" which serve as communication endpoints. Servers and clients alike can allocate ports, however servers require either some type of locator service to allow clients to find them or otherwise need to be well-known. This is where the bootstrap server comes in. It's accessible to all processes on the system which may communicate with it via the bootstrap_port. Clients can request over this port, that the server look up a given service by its name and match them with its port. Here the name is a fully qualified name like "com.apple.MobileFileIntegrity" for example.

launchd
It used to be that mach_init took on the role of bootstrap_server, however launchd has since taken over this role and claims the port(bootstrap_port) during its startup. launchd is the first user-space program to be started by the kernel and therefore has a pid of 1. It's mission is simple - launch jobs(processes) with a specified criteria. Because all processes are it's spawns they inherit access to the bootstrap_port. And so if a service wishes to register - pre-launchd it would have been via the now deprecated api's bootstrap_create_server and bootstrap_create_service - with launchd it can do so in the server's plist and call bootstrap_check_in which will result in launchd handing over the port when it is ready to service requests:

 kern_return_t bootstrap_check_in(mach_port_t bp, // bootstrap_port   
                                  const name_t service_name, // name of service  
                                  mach_port_t *sp); // out: server port  

Note also that launchd pre-registers the port in the server plist. This server port is usually ephemeral but may also be well known if the key HostSpecialPort is added. Finally on launchd, it runs both systems services(daemons) and per-user services(agents).

The server(daemon's) plists can be found in /System/Library/LaunchDaemons:


 macho-reverser:LaunchDaemons macho-reverser$ ls -l  
 total 48  
 -rw-r--r-- 1 root wheel  678 Jun 15 17:37 bootps.plist  
 -rw-r--r-- 1 root wheel  909 Apr 4 2017 com.apple.AirPlayXPCHelper.plist  
 -rw-r--r-- 1 root wheel  811 Jul 31 21:21 com.apple.AppleFileServer.plist  
 -rw-r--r-- 1 root wheel  729 Jan 26 2017 com.apple.AssetCache.builtin.plist  
 -rw-r--r-- 1 root wheel  433 Mar 1 2017 com.apple.AssetCacheActivatorService.plist  
 -rw-r--r-- 1 root wheel  448 Mar 1 2017 com.apple.AssetCacheLocatorService.plist  
 -rw-r--r-- 1 root wheel  437 Mar 1 2017 com.apple.AssetCacheTetheratorService.plist  
 --  

Ok, that was a lot of theory just now and you may be wondering how it all ties in. Well, if you look at amfid's server plist i.e. com.apple.MobileFileIntegrity.plist you will notice that the service will be registered using  HOST_SPECIAL_PORT(18):


amfid plist w/ host_special_port 
So what this means is that when the KEXT makes the up call to amfid it does so over this special port with a call to _host_get_special_port at offset 0xFFFFFFF00644EEE8. You can see the port being set at offset 0xFFFFFFF00644EEE0:
AMFI.kext up call to amfid
Stealing SPECIAL_PORTS
As we alluded to earlier, prior to iOS 9 amfid's special port could be usurped. This ccould be accomplished with a call to the host_set_amfid_port macro in <mach/host_special_ports.h>.

 #define host_set_amfid_port(host, port)     \  
      (host_set_special_port((host), HOST_AMFID_PORT, (port)))  

Let's look at an example of how we might be able to achieve this. Before proceeding however, let's first look at amfid's(225.50.12.0.0) normal initialization routine. So going back to jtool, we first list the segments:

 macho-reverser:iOS10.3 macho-reverser$ jtool -l amfid   
 LC 00: LC_SEGMENT_64     Mem: 0x000000000-0x100000000     __PAGEZERO  
 LC 01: LC_SEGMENT_64     Mem: 0x100000000-0x100004000     __TEXT  
      Mem: 0x100002bd8-0x100003778          __TEXT.__text     (Normal)  
      Mem: 0x100003778-0x1000039c4          __TEXT.__stubs     (Symbol Stubs)  
      Mem: 0x1000039c4-0x100003c28          __TEXT.__stub_helper     (Normal)  
      Mem: 0x100003c28-0x100003d48          __TEXT.__const       
      Mem: 0x100003d48-0x100003da6          __TEXT.__oslogstring     (C-String Literals)  
      Mem: 0x100003da6-0x100003fb8          __TEXT.__cstring     (C-String Literals)  
      Mem: 0x100003fb8-0x100004000          __TEXT.__unwind_info       
 LC 02: LC_SEGMENT_64     Mem: 0x100004000-0x100008000     __DATA  
      Mem: 0x100004000-0x100004090          __DATA.__got     (Non-Lazy Symbol Ptrs)  
      Mem: 0x100004090-0x100004218          __DATA.__la_symbol_ptr     (Lazy Symbol Ptrs)  
      Mem: 0x100004218-0x100004328          __DATA.__const       
      Mem: 0x100004328-0x100004348          __DATA.__cfstring       
      Mem: 0x100004348-0x100004350          __DATA.__data       
 LC 03: LC_SEGMENT_64     Mem: 0x100008000-0x100008000     __RESTRICT  
      Mem: 0x100008000-0x100008000          __RESTRICT.__restrict       
 LC 04: LC_SEGMENT_64     Mem: 0x100008000-0x10000c000     __LINKEDIT  
 LC 05: LC_DYLD_INFO       
 LC 06: LC_SYMTAB         
      Symbol table is at offset 0x8780 (34688), 69 entries  
      String table is at offset 0x8da0 (36256), 1464 bytes  
 LC 07: LC_DYSYMTAB        
        1 local symbols at index   0  
        1 external symbols at index 1  
        67 undefined symbols at index 2  
        No TOC  
        No modtab  
       116 Indirect symbols at offset 0x8bd0  
 LC 08: LC_LOAD_DYLINKER        /usr/lib/dyld  
 LC 09: LC_UUID             UUID: A3B3B122-E61E-3D39-B082-BEBE3FAA86CC  
 LC 10: LC_VERSION_MIN_IPHONEOS     Minimum iOS version:  10.3.0  
 LC 11: LC_SOURCE_VERSION        Source Version:     225.50.12.0.0  
 LC 12: LC_MAIN             Entry Point:       0x3134 (Mem: 0x100003134)  
 LC 13: LC_LOAD_DYLIB          /usr/lib/libmis.dylib  
 LC 14: LC_LOAD_DYLIB          /usr/lib/libMobileGestalt.dylib  
 LC 15: LC_LOAD_DYLIB          /System/Library/PrivateFrameworks/MobileKeyBag.framework/MobileKeyBag  
 LC 16: LC_LOAD_DYLIB          /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation  
 LC 17: LC_LOAD_DYLIB          /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit  
 LC 18: LC_LOAD_DYLIB          /usr/lib/libSystem.B.dylib  
 LC 19: LC_FUNCTION_STARTS       Offset: 34664, Size: 24 (0x8768-0x8780)   
 LC 20: LC_DATA_IN_CODE         Offset: 34688, Size: 0 (0x8780-0x8780)   
 LC 21: LC_CODE_SIGNATURE        Offset: 37728, Size: 384 (0x9360-0x94e0)   

And then we disassemble main (LC_MAIN) filtering on function calls. This will give us an idea of the general flow:

 macho-reverser:iOS10.3 macho-reverser$ jtool -d 0x100003134 amfid | grep BL  
 Disassembling from file offset 0x3134, Address 0x100003134 to next function, mmapped 0x11a561000  
   10000316c     BL   libSystem.B.dylib::_getopt     ; 0x100003928  
   10000319c     BL   libSystem.B.dylib::_os_log_create     ; 0x100003964  
   1000031dc     BL   libSystem.B.dylib::_openlog     ; 0x100003958  
   1000031e4     BL   libSystem.B.dylib::_setlogmask     ; 0x10000397c  
   1000031f4     BL   libSystem.B.dylib::_syslog     ; 0x1000039a0  
   100003210     BL   libSystem.B.dylib::_bootstrap_check_in     ; 0x1000038bc  
   100003218     BL   libSystem.B.dylib::___error     ; 0x100003898  
   100003220     BL   libSystem.B.dylib::_strerror     ; 0x100003988  
   100003234     BL   libSystem.B.dylib::_syslog     ; 0x1000039a0  
   100003270     BL   libSystem.B.dylib::_fprintf     ; 0x10000391c  
   100003278     BL   libSystem.B.dylib::_exit     ; 0x100003910  
   100003290     BL   libSystem.B.dylib::_dispatch_source_create     ; 0x1000038f8  
   1000032a8     BL   libSystem.B.dylib::_syslog     ; 0x1000039a0  
   1000032b0     BL   libSystem.B.dylib::_exit     ; 0x100003910  
   1000032b8     BL   libSystem.B.dylib::_dispatch_set_context     ; 0x1000038ec  
   1000032c8     BL   libSystem.B.dylib::_dispatch_source_set_event_handler_f     ; 0x100003904  
   1000032d0     BL   libSystem.B.dylib::_dispatch_resume     ; 0x1000038e0  
   1000032d4     BL   libSystem.B.dylib::_dispatch_main     ; 0x1000038c8  
   100003304     BL   libSystem.B.dylib::_syslog     ; 0x1000039a0  
   100003308     BL   libSystem.B.dylib::_xpc_transaction_begin     ; 0x1000039ac  
   10000331c     BL   libSystem.B.dylib::_dispatch_mig_server     ; 0x1000038d4  
   100003324     BL   libSystem.B.dylib::_xpc_transaction_end     ; 0x1000039b8  
   10000333c     BL   libSystem.B.dylib::_syslog     ; 0x1000039a0  
   100003350     BL   libSystem.B.dylib::_syslog     ; 0x1000039a0  
   100003418     BL   libSystem.B.dylib::_memchr     ; 0x100003934  
   1000034f8     BL   0x100002bd8  
   100003520     BL   libSystem.B.dylib::_strlen     ; 0x100003994  
   10000356c     BL   libSystem.B.dylib::___stack_chk_fail     ; 0x1000038a4  
   100003600     BL   0x100002f5c  
   1000036c0     BL   0x1000030ec  
   100003748     BLR   X8                      ; 0x100000cfeedfacf  

Note the call to _bootstrap_check_in that we discussed earlier. ARM'ed with this information, if we remove the calls we don't particularly care about like the logging routines etc,  we end up with the following PoC:

// Code inspired by Jonathan Levin 
#include <mach/mach.h>  
 #include <mach/mach_port.h>  
 #include <mach/mach_host.h>  
 #include <mach/host_priv.h>  
 #include <mach/host_special_ports.h>  
 #include <dispatch/dispatch.h>  
 #include <stdio.h>  
 #include <stdlib.h>  
 
#define CHECK_MACH_ERROR(a) do {kern_return_t rr = (a); if ((rr) != KERN_SUCCESS) \  
 { printf("Mach error %x (%s) on line %d of file %s\n", (rr), mach_error_string((rr)), __LINE__, __FILE__); abort(); } } while (0)  
void handler_f (void *arg)  
 {  
   // Just a skeleton....  
   printf("Handle yo biznizz!! \n");  
 }  
 int main(int argc, char **argv)  
 {  
      char *service_name = "com.apple.MobileFileIntegrity";  
      mach_port_t myhost = mach_host_self();  
      mach_port_t jackedAmfi = MACH_PORT_NULL;  
      host_priv_t host_priv;  
      kern_return_t err;  

      err = host_get_host_priv_port(myhost, &host_priv);  
      CHECK_MACH_ERROR(err);  

      err = mach_port_allocate(mach_task_self(), // task acquiring the port right,  
                               MACH_PORT_RIGHT_RECEIVE, // type of right,  
                               &jackedAmfi); // task's name for the port right;  
      
      // insert a send right: we will now have combined receive/send rights  
      err = mach_port_insert_right(mach_task_self(), jackedAmfi, jackedAmfi, MACH_MSG_TYPE_MAKE_SEND);  
      CHECK_MACH_ERROR(err); 
 
      // Bait and switch  
      err = host_set_amfid_port(host_priv, jackedAmfi);  
      CHECK_MACH_ERROR(err); 
 
      // GCD housekeeping  
      dispatch_source_t ms = dispatch_source_create(&_dispatch_source_type_mach_recv,   
         jackedAmfi,   
         0,          
         &_dispatch_main_q);  
      if (!ms) { printf("Error creating mig source"); exit(1); }  
      dispatch_set_context(ms, &ms);   
      dispatch_source_set_event_handler_f (ms, handler_f);  
      dispatch_resume (ms);  
      dispatch_main();  
 }  

It should be noted that the above PoC is incomplete as the handler logic needs to be added. But fear not, we get into this handler later on in the series.

Wrap Up
We have come to the end of part 2 in the series. To recap:
  • We got a HIGH level introduction to Mach IPC
  • Learned a bit about about launchd
  • Introduced the concept of special_ports
  • Determined how the KEXT communicated with it's daemon
  • Saw how special_ports can be usurped
That's all for now. Hope you enjoyed it! And as always if you notice anything that is not correct please don't hesitate to add a comment below...

References:
1. Mac OS X and iOS Internals: To the Apple's Core - For IPC & launchd section
2. MacOS and iOS Internals, Volume III: Security & Insecurity
3. Mac OS X Internals: A Systems Approach
4. *OS Internals - The Forum

Friday, October 13, 2017

Reversing AppleMobileFileIntegrity (AMFI) Part 1

Disclaimer: This topic has already been covered by countless authors. That said I am in the process of learning more about all things kernel - on both iOS/*OS - and figured I would make some notes as I went along. So consider this my scratch pad of sorts. Hopefully you find it useful.

Introduction
AppleMobileFileIntegrity(AMFI) is a core part of macOS and iOS security. And is by extension the bane of many a jailbreakers existence. It ensures the integrity of the code running on the OS and is the brains behind XNU's code signature verification.

AMFI consists of a KEXT(AppleMobileFileIntegrity.kext) and a usermode daemon(amfid). The KEXT by definition operates in kernel mode. Recall that AAPL fully controls ALL system binaries on iOS. This allowed them to compile a list of the individual hashes of these binaries and stash them in the kernel - more specifically in a area referred to as AMFI's Trust Cache. As a side note these pseudo signatures are referred to as ad-hoc. Validation is therefore a simple lookup of the hash in the Trust Cache. For third party apps however, things are a bit different. As in such instances AMFI calls up to it's partner in crime amfid the user-mode daemon for validation.

This series of posts will look at both AppleMobileFileIntegrity.kext and amfid. Let's begin by discussing the Mandatory Access Control Framework(MACF) a key component of APPL's ecosystem.

Background (MACF)
"The Mandatory Access Control Framework (MACF) is the substrate on top of which all of Apple's security, both MacOS and iOS, is implemented. By implementing a rich set of callouts for every user-controllable aspect of kernel functionality - system calls and Mach traps alike - it allows interested kernel components to enforce any set of rules - a policy - desired." - (Jonathan Levin, *OS Internals Volume III, p.45)

Of note is the fact that this a framework and the logic is implemented in kernel extensions(KEXTs) which must first  register their intention with the framework for any number of operations the framework intercepts. MACF will then call out to these extensions aka policies and allow them to do their thing. The general flow is as follows, let's say some process wants to perform a system call or mach trap:
  1. The corresponding function in the kernel is called(sysent/mac_trap_table -> syscall/trap #)
  2. This function reaches out to the MACF and basically asks "did any policy modules request to hook this functionality?"
  3. MACF then says "hold my beer, and calls out to the interested policies"
  4. The interested policy then says "ok dude you want to perform this operation...you're good to go....or nope not today"
  5. Now this is where it gets interesting, sometimes the policy module has a partner in crime operating in user mode and therefore has to make an up call. Enter in amfid. 
Ok, so how do we see these policies on the system? On macOS we can use jtool to(for i in *.kext; do if jtool -S $i 2> /dev/null | grep mac_policy > /dev/null; then echo $i; fi; done) iterate over the /System/Library/Extensions/ directory as shown below:

Enumerating policy clients on macOS
Kernel Extension(KEXTs)
Now because this blog series is about AMFI, the KEXT we are interested in is of course AppleMobileFileIntegrity.kext. If you are wondering what a KEXT is have no fear I got you. XNU by itself cannot provide all required functionality and so you need kernel extensions for things like drivers and plugins. In windows parlance a KEXT would be the equivalent of a driver (.sys). More importantly, KEXTs are also just Mach-O Bundles which it means it can be sliced and diced using a myriad of available tools.

The KEXT directory contains an Info.plist file that just has some metadata. Of note is the  CFBundleExecutable key which as the name implies contains the name of the binary that will be executed.
Info.plist snippet
The rest of the Contents/MacOS directory looks like this, where we see the actual AppleMobileFileIntegrity binary and verify that it is indeed a mach-O binary:

KEXT layout
One cool tool that can help with visuals is KextViewr from the Objective-See arsenal. While you are at it check out the rest of their AWESOME FREE tools.

Viewing KEXTs with KextViewr
The default location for KEXTs on macOS is - as we saw above - /System/Library/Extensions/. Not so on iOS though, as KEXTs are instead prelinked in the kernel.

So while getting the above information - policy clients - is straight forward on macOS, because the KEXTs are prelinked in iOS we have to extract them directly from the kernel. Before we do that we need to obtain said kernel. There are number of options available and of those I usually use one of the following:
  1. kdump from the iOS Kernel Utilites for pre iOS 10.x kernels. Keep in mind if you use this method certain parts of the kernel will be jettisoned (so there goes the symbol table.......).
  2. Extract from respective Firmware (starting with iOS 10 the kernel is no longer encrypted...thank the kernel gods)
Once obtained, you can use the joker tool with big K as shown below:
Enumerating policy clients on iOS
This will extract the KEXT's to /tmp directory. And then to identify policy kexts - recall from earlier that the kext has to register their interest with MACF - we can grep for mac_pocliy_register. More on this function later.

Enumerating callouts
Ok so at this this point we know that AppleMobileFileIntegrity.kext is a policy KEXT. But what is exactly is a policy and what does it look like from a reverse engineering perspective. Simply put, a policy is just a set rules that gets applied on full or partial kernel operation callouts. The interested KEXT first defines a mac_policy_conf structure and then registers it with MACF through a call to mac_policy_register. The mac_policy_conf structure is of paramount importance and can be found in /security/mac_policy.h.

mac_policy_conf structure
Of interest to us is the mpc_ops field which is a pointer to mac_policy_ops struct. This structure specifies the operation the KEXT is interested in and the name of the callback function to invoke when the operation is triggered. Now there are a ton of callouts available and the KEXT is usually only interested in a subset of those so it bzero()'s the structure and then sets the ones it is interested in.

mac_policy_ops structure snippet
Recall that the AppleMobileFileIntegrity.kext KEXT is a mach-O binary. So using jtool you can list the segments and the mac_policy_ops structure can be found in the __DATA.__const section.

Listing SEGMENTS on iOS 9.3.3
We then dump the __DATA.__const section (jtool -d __DATA.__const com.apple.driver.AppleMobileFileIntegrity.kext9.3.3)

Dumping __DATA.__const on iOS 9.3.3
And from that, offset five(5) i.e. 0xffffff801c740be8 points to the mac_policy_ops struct. Recall this was the fifth field in the mac_policy_conf structure. Navigating to that address we get an array of function pointers. And correlating this back to the mac_policy_ops structure looks like this:
mac_policy_ops function pointers in iOS 9.3.3
The seventh(7) pointer 0xffffff801c71608c points to mac_policy_ops.mpo_cred_check_label_update_execve, the twelfth(12) pointer 0xffffff801c716094 to mac_policy_ops.mpo_cred_label_associate and so on and so forth. The zeros you see goes back what we said earlier about the KEXT bzero'ing the structure and only setting the ones it is interested in.

As a casualty of the war against jailbreakers this doesn't work on iOS 10.x. Instead the initialization is done in code. So alas, the above steps will not work and a different approach is needed. Extracting the AppleMobileFileIntegrity.kext from an iOS 10 3.x kernel, opening it up in IDA and searching for references to mac_policy_register leads to:

AMFI KEXT iOS 10.x
Immediately you see references to "mac_policy_register failed" and if we work backwards, we get there by this branch instruction "BL sub_FFFFFFF0064514D8" at "0xFFFFFFF00644FA4C". It's therefore safe to assume that sub_FFFFFFF0064514D8 is indeed mac_policy_register(). Looking up this function reveals that it takes three arguments - pointer to a mac_policy_conf struct, out pointer to MACF policy handle and the second argument passed to the KEXT's entry point - as shown:

mac_policy_register
A call to ARMs
On ARM architectures, function arguments and return values are passed/stored in registers X0-X7. So in this case register X0 should hold the mac_policy_conf struct pointer. Let's remind ourselves of what this struct looks like:

mac_policy_struct again
ARMed (pun intended) with this knowledge, the previous disassembly should start making sense and can be annotated as follows:

 # mpc - mac_policy_conf  
 ----------------------------------------------------------------------------------------------------  
 # mpc->mpc_name="AMFI"
 __text:FFFFFFF00644F9EC STR       X9, [X8,#(qword_FFFFFFF006E68650 - 0xFFFFFFF006E68530)]  
 __text:FFFFFFF00644F9F0 ADRP      X9, #aAmfi_0@PAGE ; "AMFI"  
 __text:FFFFFFF00644F9F4 ADD       X9, X9, #aAmfi_0@PAGEOFF ; "AMFI"  
 __text:FFFFFFF00644F9F8 FMOV      D0, X9  
 # mpc=mpc_fullname="Apple Mobile File Integrity"  
 __text:FFFFFFF00644F9FC ADRP      X9, #aAppleMobileFil@PAGE ; "Apple Mobile File Integrity"  
 __text:FFFFFFF00644FA00 ADD       X9, X9, #aAppleMobileFil@PAGEOFF ; "Apple Mobile File Integrity"  
 __text:FFFFFFF00644FA04 INS       V0.D[1], X9  
 # Set mpc->name and mpc->fullname  
 __text:FFFFFFF00644FA08 ADRP      X0, #xmmword_FFFFFFF006E68FA8@PAGE  
 __text:FFFFFFF00644FA0C ADD       X0, X0, #xmmword_FFFFFFF006E68FA8@PAGEOFF  
 __text:FFFFFFF00644FA10 STR       Q0, [X0]  
 # Set mpc->mpc_labelname=""  
 __text:FFFFFFF00644FA14 ADRP      X9, #off_FFFFFFF006E695D8@PAGE  
 __text:FFFFFFF00644FA18 ADD       X9, X9, #off_FFFFFFF006E695D8@PAGEOFF  
 __text:FFFFFFF00644FA1C STR       X9, [X0,#(qword_FFFFFFF006E68FB8 - 0xFFFFFFF006E68FA8)]  
 # Set mpc->mpc_labelname_count=1  
 __text:FFFFFFF00644FA20 MOV       W9, #1  
 __text:FFFFFFF00644FA24 STR       W9, [X0,#(dword_FFFFFFF006E68FC0 - 0xFFFFFFF006E68FA8)]  
 # X8 has mac_policy_ops  
 __text:FFFFFFF00644FA28 STR       X8, [X0,#(qword_FFFFFFF006E68FC8 - 0xFFFFFFF006E68FA8)]  
 # Set mpc->mpc_loadtime_flags=0 and other fields  
 __text:FFFFFFF00644FA2C STR       WZR, [X0,#(dword_FFFFFFF006E68FD0 - 0xFFFFFFF006E68FA8)]  
 __text:FFFFFFF00644FA30 ADRP      X8, #dword_FFFFFFF00760B2E0@PAGE  
 __text:FFFFFFF00644FA34 ADD       X8, X8, #dword_FFFFFFF00760B2E0@PAGEOFF  
 __text:FFFFFFF00644FA38 STR       X8, [X0,#(qword_FFFFFFF006E68FD8 - 0xFFFFFFF006E68FA8)]  
 __text:FFFFFFF00644FA3C STR       WZR, [X0,#(dword_FFFFFFF006E68FE0 - 0xFFFFFFF006E68FA8)]  
 # call mac_policy_register - sub_FFFFFFF0064514D8  
 __text:FFFFFFF00644FA40 ADRP      X1, #unk_FFFFFFF006E6852C@PAGE  
 __text:FFFFFFF00644FA44 ADD       X1, X1, #unk_FFFFFFF006E6852C@PAGEOFF  
 __text:FFFFFFF00644FA48 MOV       X2, #0  
 __text:FFFFFFF00644FA4C BL       sub_FFFFFFF0064514D8  
 __text:FFFFFFF00644FA50 CBZ       W0, loc_FFFFFFF00644FA78  

And so in a nutshell this is how mpc_ops is populated. Where the struct is now stored in register X8. Let's look at an example of an assignment.

mpo_proc_check_get_task
The hook kicks in during the acquisition of task port rights. And you should keep in mind that if you obtain a task's port you own the task. So the hook is an important one. Opening the KEXT in IDA and searching for get-task-allow reveals the following:

get-task-allow search results
And after navigating through the results we eventually end up at the highlighted sub_FFFFFFF006450D50 function. The hook first checks if the target task has the get-task-allow entitlement at 0xFFFFFFF006450D7C:

get-task-allow check
If that fails, the hook then checks for the task_for_pid_allow entitlement at 0xFFFFFFF006450D9C. This is the "god-mode" entitlement as it allows the entitled task to access any other task via the task_for_pid Mach trap. Side-note: If you have ever performed debugging of iOS apps using debugserver then you would be familiar with this entitlement as you first have to sign the binary and grant the binary the entitlement.

 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">  
 <plist version="1.0">  
 <dict>  
      <key>com.apple.springboard.debugapplications</key>  
      <true/>  
      <key>get-task-allow</key>  
      <true/>  
      <key>task_for_pid-allow</key>  
      <true/>  
      <key>run-unsigned-code</key>  
      <true/>  
 </dict>  
 </plist>  

Likewise if you are doing any work on the kernel(pid 0) - like dumping a running kernel - you would call
 ret = task_for_pid(mach_task_self(), 0, &kernel_task);  // from ios-kern-utils kdump libkern.c
assuming of course that the jailbreak supports it. Going back to the hook and picking up where we left off if the calling task doesn't have the task_for_pid_allow entitlement then we see a call to sub_FFFFFFF006451014 which does a final check for unrestricted debugging:
final check for unrestricted debugging
Ok so now that we understand hook, how does it get assigned? Well if we search for cross references in IDA we land in a section just above the snippet we annotated earlier on:

mpo_proc_check_get_task hook initialization
And if we press k key in IDA at offset 0xFFFFFFF00644F9C4 we see *(X8 + arg_4F0) = X9 where #arg_4F0 as defined by IDA is hex 0x500 which translates to offset 160 in the mac_policy_ops struct which is mpo_proc_check_get_task.

 __text:FFFFFFF00644F9B8 0A 09 04 F9 STR       X10, [X8,#(qword_FFFFFFF006E68D40 - 0xFFFFFFF006E68530)]  
 __text:FFFFFFF00644F9BC 09 00 00 B0 ADRP      X9, #sub_FFFFFFF006450D50@PAGE  
 __text:FFFFFFF00644F9C0 29 41 35 91 ADD       X9, X9, #sub_FFFFFFF006450D50@PAGEOFF  
 __text:FFFFFFF00644F9C4 09 81 02 F9 STR       X9, [X8,#arg_4F0] 

Recall that register X8 holds the mac_policy_ops struct. And looking at the disassembly you see several other similar assignments.

Wrap Up
We have come to the end of part 1 in the series. To recap:
  • We introduced MACF
  • Learned a little about KEXTs and how they relate to MACF
  • Got introduced to Policies in the context of MACF
  • Learned to identify policies on both macOS and iOS
  • Saw that AppleMobileFileIntegrity.KEXT is a policy module
  • Delved a little into policy registration to extract and identify callback functions
In the next post we will look at little more into AppleMobileFileIntegrity.KEXT and how it communicates with it's user-mode partner in crime amfid. I hope you found this helpful. Also if you find errors please point them out in the comments. Like I said I am learning as I go along.

Happy hacking errrrm......reversing.....mi amigos !!