kern_utils.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #import <Foundation/Foundation.h>
  2. #import <sys/stat.h>
  3. #import "kern_utils.h"
  4. #import "helpers/kmem.h"
  5. #import "helpers/patchfinder64.h"
  6. #import "helpers/kexecute.h"
  7. #import "helpers/offsetof.h"
  8. #import "helpers/osobject.h"
  9. #import "sandbox.h"
  10. #define PROC_PIDPATHINFO_MAXSIZE (4*MAXPATHLEN)
  11. int proc_pidpath(pid_t pid, void *buffer, uint32_t buffersize);
  12. #define CS_VALID 0x0000001 /* dynamically valid */
  13. #define CS_ADHOC 0x0000002 /* ad hoc signed */
  14. #define CS_GET_TASK_ALLOW 0x0000004 /* has get-task-allow entitlement */
  15. #define CS_INSTALLER 0x0000008 /* has installer entitlement */
  16. #define CS_HARD 0x0000100 /* don't load invalid pages */
  17. #define CS_KILL 0x0000200 /* kill process if it becomes invalid */
  18. #define CS_CHECK_EXPIRATION 0x0000400 /* force expiration checking */
  19. #define CS_RESTRICT 0x0000800 /* tell dyld to treat restricted */
  20. #define CS_ENFORCEMENT 0x0001000 /* require enforcement */
  21. #define CS_REQUIRE_LV 0x0002000 /* require library validation */
  22. #define CS_ENTITLEMENTS_VALIDATED 0x0004000
  23. #define CS_ALLOWED_MACHO 0x00ffffe
  24. #define CS_EXEC_SET_HARD 0x0100000 /* set CS_HARD on any exec'ed process */
  25. #define CS_EXEC_SET_KILL 0x0200000 /* set CS_KILL on any exec'ed process */
  26. #define CS_EXEC_SET_ENFORCEMENT 0x0400000 /* set CS_ENFORCEMENT on any exec'ed process */
  27. #define CS_EXEC_SET_INSTALLER 0x0800000 /* set CS_INSTALLER on any exec'ed process */
  28. #define CS_KILLED 0x1000000 /* was killed by kernel for invalidity */
  29. #define CS_DYLD_PLATFORM 0x2000000 /* dyld used to load this is a platform binary */
  30. #define CS_PLATFORM_BINARY 0x4000000 /* this is a platform binary */
  31. #define CS_PLATFORM_PATH 0x8000000 /* platform binary by the fact of path (osx only) */
  32. #define CS_DEBUGGED 0x10000000 /* process is currently or has previously been debugged and allowed to run with invalid pages */
  33. #define CS_SIGNED 0x20000000 /* process has a signature (may have gone invalid) */
  34. #define CS_DEV_CODE 0x40000000 /* code is dev signed, cannot be loaded into prod signed code */
  35. uint64_t proc_find(int pd, int tries) {
  36. while (tries-- > 0) {
  37. uint64_t proc = rk64(kernprocaddr + 0x08);
  38. while (proc) {
  39. uint32_t proc_pid = rk32(proc + 0x10);
  40. if (proc_pid == pd) {
  41. return proc;
  42. }
  43. proc = rk64(proc + 0x08);
  44. }
  45. }
  46. return 0;
  47. }
  48. char *proc_name(int pd) {
  49. uint64_t proc = proc_find(pd, 1);
  50. if (proc == 0) {
  51. return NULL;
  52. }
  53. char *proc_name = (char *)calloc(40, sizeof(char));
  54. kread(proc + 0x26c, proc_name, 40);
  55. return proc_name;
  56. }
  57. CACHED_FIND(uint64_t, our_task_addr) {
  58. uint64_t our_proc = proc_find(getpid(), 1);
  59. if (our_proc == 0) {
  60. fprintf(stderr, "failed to find our_task_addr!\n");
  61. exit(EXIT_FAILURE);
  62. }
  63. return rk64(our_proc + offsetof_task);
  64. }
  65. uint64_t find_port(mach_port_name_t port) {
  66. uint64_t task_addr = our_task_addr();
  67. uint64_t itk_space = rk64(task_addr + offsetof_itk_space);
  68. uint64_t is_table = rk64(itk_space + offsetof_ipc_space_is_table);
  69. uint32_t port_index = port >> 8;
  70. const int sizeof_ipc_entry_t = 0x18;
  71. uint64_t port_addr = rk64(is_table + (port_index * sizeof_ipc_entry_t));
  72. return port_addr;
  73. }
  74. void set_csflags(uint64_t proc) {
  75. uint32_t csflags = rk32(proc + offsetof_p_csflags);
  76. csflags = (csflags | CS_PLATFORM_BINARY | CS_INSTALLER | CS_GET_TASK_ALLOW | CS_DEBUGGED) & ~(CS_RESTRICT | CS_HARD | CS_KILL);
  77. wk32(proc + offsetof_p_csflags, csflags);
  78. }
  79. void set_csblob(uint64_t proc) {
  80. uint64_t textvp = rk64(proc + offsetof_p_textvp); // vnode of executable
  81. off_t textoff = rk64(proc + offsetof_p_textoff);
  82. if (textvp != 0){
  83. uint32_t vnode_type_tag = rk32(textvp + offsetof_v_type);
  84. uint16_t vnode_type = vnode_type_tag & 0xffff;
  85. uint16_t vnode_tag = (vnode_type_tag >> 16);
  86. if (vnode_type == 1) {
  87. uint64_t ubcinfo = rk64(textvp + offsetof_v_ubcinfo);
  88. uint64_t csblobs = rk64(ubcinfo + offsetof_ubcinfo_csblobs);
  89. while (csblobs != 0) {
  90. cpu_type_t csblob_cputype = rk32(csblobs + offsetof_csb_cputype);
  91. unsigned int csblob_flags = rk32(csblobs + offsetof_csb_flags);
  92. off_t csb_base_offset = rk64(csblobs + offsetof_csb_base_offset);
  93. uint64_t csb_entitlements = rk64(csblobs + offsetof_csb_entitlements_offset);
  94. unsigned int csb_signer_type = rk32(csblobs + offsetof_csb_signer_type);
  95. unsigned int csb_platform_binary = rk32(csblobs + offsetof_csb_platform_binary);
  96. unsigned int csb_platform_path = rk32(csblobs + offsetof_csb_platform_path);
  97. wk32(csblobs + offsetof_csb_platform_binary, 1);
  98. csb_platform_binary = rk32(csblobs + offsetof_csb_platform_binary);
  99. csblobs = rk64(csblobs);
  100. }
  101. }
  102. }
  103. }
  104. const char* abs_path_exceptions[] = {
  105. "/meridian",
  106. "/Library",
  107. "/private/var/mobile/Library",
  108. "/private/var/mnt",
  109. NULL
  110. };
  111. uint64_t get_exception_osarray(void) {
  112. static uint64_t cached = 0;
  113. if (cached == 0) {
  114. cached = OSUnserializeXML(
  115. "<array>"
  116. "<string>/meridian/</string>"
  117. "<string>/Library/</string>"
  118. "<string>/private/var/mobile/Library/</string>"
  119. "<string>/private/var/mnt/</string>"
  120. "</array>"
  121. );
  122. }
  123. return cached;
  124. }
  125. static const char *exc_key = "com.apple.security.exception.files.absolute-path.read-only";
  126. void set_sandbox_extensions(uint64_t proc) {
  127. uint64_t proc_ucred = rk64(proc + 0x100);
  128. uint64_t sandbox = rk64(rk64(proc_ucred + 0x78) + 0x8 + 0x8);
  129. if (sandbox == 0) {
  130. fprintf(stderr, "no sandbox, skipping \n");
  131. return;
  132. }
  133. if (has_file_extension(sandbox, abs_path_exceptions[0])) {
  134. fprintf(stderr, "already has '%s', skipping \n", abs_path_exceptions[0]);
  135. return;
  136. }
  137. uint64_t ext = 0;
  138. const char** path = abs_path_exceptions;
  139. while (*path != NULL) {
  140. ext = extension_create_file(*path, ext);
  141. if (ext == 0) {
  142. fprintf(stderr, "extension_create_file(%s) failed, panic! \n", *path);
  143. NSLog(@"extension_create_file(%s) failed, panic!", *path);
  144. }
  145. ++path;
  146. }
  147. if (ext != 0) {
  148. extension_add(ext, sandbox, exc_key);
  149. }
  150. }
  151. void set_amfi_entitlements(uint64_t proc) {
  152. uint64_t proc_ucred = rk64(proc + 0x100);
  153. uint64_t amfi_entitlements = rk64(rk64(proc_ucred + 0x78) + 0x8);
  154. OSDictionary_SetItem(amfi_entitlements, "get-task-allow", find_OSBoolean_True());
  155. OSDictionary_SetItem(amfi_entitlements, "com.apple.private.skip-library-validation", find_OSBoolean_True());
  156. uint64_t present = OSDictionary_GetItem(amfi_entitlements, exc_key);
  157. int rv = 0;
  158. if (present == 0) {
  159. rv = OSDictionary_SetItem(amfi_entitlements, exc_key, get_exception_osarray());
  160. } else if (present != get_exception_osarray()) {
  161. unsigned int itemCount = OSArray_ItemCount(present);
  162. BOOL foundEntitlements = NO;
  163. uint64_t itemBuffer = OSArray_ItemBuffer(present);
  164. for (int i = 0; i < itemCount; i++) {
  165. uint64_t item = rk64(itemBuffer + (i * sizeof(void *)));
  166. char *entitlementString = OSString_CopyString(item);
  167. if (strcmp(entitlementString, "/meridian/") == 0){
  168. foundEntitlements = YES;
  169. free(entitlementString);
  170. break;
  171. }
  172. free(entitlementString);
  173. }
  174. if (!foundEntitlements){
  175. rv = OSArray_Merge(present, get_exception_osarray());
  176. } else {
  177. rv = 1;
  178. }
  179. } else {
  180. rv = 1;
  181. }
  182. if (rv != 1) {
  183. NSLog(@"Setting exc FAILED! amfi_entitlements: 0x%llx present: 0x%llx\n", amfi_entitlements, present);
  184. }
  185. }
  186. int setcsflagsandplatformize(int pid) {
  187. uint64_t proc = proc_find(pid, 3);
  188. if (proc == 0) {
  189. NSLog(@"Unable to find pid %d to entitle!", pid);
  190. return 1;
  191. }
  192. set_csflags(proc);
  193. set_amfi_entitlements(proc);
  194. set_sandbox_extensions(proc);
  195. set_csblob(proc);
  196. return 0;
  197. }