kern_utils.m 8.9 KB

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