kern_utils.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 fixupsetuid(int pid){
  76. char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
  77. bzero(pathbuf, sizeof(pathbuf));
  78. int ret = proc_pidpath(pid, pathbuf, sizeof(pathbuf));
  79. if (ret < 0){
  80. fprintf(stderr,"Unable to get path for PID %d\n", pid);
  81. return;
  82. }
  83. struct stat file_st;
  84. if (lstat(pathbuf, &file_st) == -1){
  85. #ifdef JAILBREAKDDEBUG
  86. fprintf(stderr,"Unable to get stat for file %s\n", pathbuf);
  87. #endif
  88. return;
  89. }
  90. if (file_st.st_mode & S_ISUID){
  91. uid_t fileUID = file_st.st_uid;
  92. #ifdef JAILBREAKDDEBUG
  93. fprintf(stderr,"Fixing up setuid for file owned by %u\n", fileUID);
  94. #endif
  95. uint64_t proc = proc_find(pid, 3);
  96. if (proc != 0) {
  97. uint64_t ucred = rk64(proc + offsetof_p_ucred);
  98. uid_t cr_svuid = rk32(ucred + offsetof_ucred_cr_svuid);
  99. #ifdef JAILBREAKDDEBUG
  100. fprintf(stderr,"Original sv_uid: %u\n", cr_svuid);
  101. #endif
  102. wk32(ucred + offsetof_ucred_cr_svuid, fileUID);
  103. #ifdef JAILBREAKDDEBUG
  104. fprintf(stderr,"New sv_uid: %u\n", fileUID);
  105. #endif
  106. }
  107. } else {
  108. #ifdef JAILBREAKDDEBUG
  109. fprintf(stderr,"File %s is not setuid!\n", pathbuf);
  110. #endif
  111. return;
  112. }
  113. }
  114. void set_csflags(uint64_t proc) {
  115. uint32_t csflags = rk32(proc + offsetof_p_csflags);
  116. csflags = (csflags | CS_PLATFORM_BINARY | CS_INSTALLER | CS_GET_TASK_ALLOW | CS_DEBUGGED) & ~(CS_RESTRICT | CS_HARD | CS_KILL);
  117. wk32(proc + offsetof_p_csflags, csflags);
  118. }
  119. void set_tfplatform(uint64_t proc) {
  120. // task.t_flags & TF_PLATFORM
  121. uint64_t task = rk64(proc + offsetof_task);
  122. uint32_t t_flags = rk32(task + offsetof_t_flags);
  123. fprintf(stderr,"Old t_flags: 0x%x\n", t_flags);
  124. t_flags |= TF_PLATFORM;
  125. wk32(task+offsetof_t_flags, t_flags);
  126. fprintf(stderr,"New t_flags: 0x%x\n", t_flags);
  127. }
  128. void set_csblob(uint64_t proc) {
  129. uint64_t textvp = rk64(proc + offsetof_p_textvp); // vnode of executable
  130. off_t textoff = rk64(proc + offsetof_p_textoff);
  131. if (textvp != 0){
  132. uint32_t vnode_type_tag = rk32(textvp + offsetof_v_type);
  133. uint16_t vnode_type = vnode_type_tag & 0xffff;
  134. uint16_t vnode_tag = (vnode_type_tag >> 16);
  135. if (vnode_type == 1) {
  136. uint64_t ubcinfo = rk64(textvp + offsetof_v_ubcinfo);
  137. uint64_t csblobs = rk64(ubcinfo + offsetof_ubcinfo_csblobs);
  138. while (csblobs != 0) {
  139. cpu_type_t csblob_cputype = rk32(csblobs + offsetof_csb_cputype);
  140. unsigned int csblob_flags = rk32(csblobs + offsetof_csb_flags);
  141. off_t csb_base_offset = rk64(csblobs + offsetof_csb_base_offset);
  142. uint64_t csb_entitlements = rk64(csblobs + offsetof_csb_entitlements_offset);
  143. unsigned int csb_signer_type = rk32(csblobs + offsetof_csb_signer_type);
  144. unsigned int csb_platform_binary = rk32(csblobs + offsetof_csb_platform_binary);
  145. unsigned int csb_platform_path = rk32(csblobs + offsetof_csb_platform_path);
  146. wk32(csblobs + offsetof_csb_platform_binary, 1);
  147. csb_platform_binary = rk32(csblobs + offsetof_csb_platform_binary);
  148. csblobs = rk64(csblobs);
  149. }
  150. }
  151. }
  152. }
  153. const char* abs_path_exceptions[] = {
  154. "/meridian",
  155. "/Library",
  156. "/private/var/mobile/Library",
  157. "/private/var/mnt",
  158. NULL
  159. };
  160. uint64_t get_exception_osarray(void) {
  161. static uint64_t cached = 0;
  162. if (cached == 0) {
  163. cached = OSUnserializeXML(
  164. "<array>"
  165. "<string>/meridian/</string>"
  166. "<string>/Library/</string>"
  167. "<string>/private/var/mobile/Library/</string>"
  168. "<string>/private/var/mnt/</string>"
  169. "</array>"
  170. );
  171. }
  172. return cached;
  173. }
  174. static const char *exc_key = "com.apple.security.exception.files.absolute-path.read-only";
  175. void set_sandbox_extensions(uint64_t proc) {
  176. uint64_t proc_ucred = rk64(proc + 0x100);
  177. uint64_t sandbox = rk64(rk64(proc_ucred + 0x78) + 0x8 + 0x8);
  178. if (sandbox == 0) {
  179. fprintf(stderr, "no sandbox, skipping \n");
  180. return;
  181. }
  182. if (has_file_extension(sandbox, abs_path_exceptions[0])) {
  183. fprintf(stderr, "already has '%s', skipping \n", abs_path_exceptions[0]);
  184. return;
  185. }
  186. uint64_t ext = 0;
  187. const char** path = abs_path_exceptions;
  188. while (*path != NULL) {
  189. ext = extension_create_file(*path, ext);
  190. if (ext == 0) {
  191. fprintf(stderr, "extension_create_file(%s) failed, panic! \n", *path);
  192. NSLog(@"extension_create_file(%s) failed, panic!", *path);
  193. }
  194. ++path;
  195. }
  196. if (ext != 0) {
  197. extension_add(ext, sandbox, exc_key);
  198. }
  199. }
  200. void set_amfi_entitlements(uint64_t proc) {
  201. uint64_t proc_ucred = rk64(proc + 0x100);
  202. uint64_t amfi_entitlements = rk64(rk64(proc_ucred + 0x78) + 0x8);
  203. OSDictionary_SetItem(amfi_entitlements, "get-task-allow", find_OSBoolean_True());
  204. OSDictionary_SetItem(amfi_entitlements, "com.apple.private.skip-library-validation", find_OSBoolean_True());
  205. uint64_t present = OSDictionary_GetItem(amfi_entitlements, exc_key);
  206. int rv = 0;
  207. if (present == 0) {
  208. rv = OSDictionary_SetItem(amfi_entitlements, exc_key, get_exception_osarray());
  209. } else if (present != get_exception_osarray()) {
  210. unsigned int itemCount = OSArray_ItemCount(present);
  211. BOOL foundEntitlements = NO;
  212. uint64_t itemBuffer = OSArray_ItemBuffer(present);
  213. for (int i = 0; i < itemCount; i++) {
  214. uint64_t item = rk64(itemBuffer + (i * sizeof(void *)));
  215. char *entitlementString = OSString_CopyString(item);
  216. if (strcmp(entitlementString, "/meridian/") == 0){
  217. foundEntitlements = YES;
  218. free(entitlementString);
  219. break;
  220. }
  221. free(entitlementString);
  222. }
  223. if (!foundEntitlements){
  224. rv = OSArray_Merge(present, get_exception_osarray());
  225. } else {
  226. rv = 1;
  227. }
  228. } else {
  229. rv = 1;
  230. }
  231. if (rv != 1) {
  232. NSLog(@"Setting exc FAILED! amfi_entitlements: 0x%llx present: 0x%llx\n", amfi_entitlements, present);
  233. }
  234. }
  235. int setcsflagsandplatformize(int pid) {
  236. uint64_t proc = proc_find(pid, 3);
  237. if (proc == 0) {
  238. NSLog(@"Unable to find pid %d to entitle!", pid);
  239. return 1;
  240. }
  241. fprintf(stderr,"setcsflagsandplatformize start on PID %d\n", pid);
  242. char name[40] = {0};
  243. kread(proc+0x268, name, 20);
  244. fprintf(stderr,"PID %d name is %s\n", pid, name);
  245. set_csflags(proc);
  246. set_tfplatform(proc);
  247. set_amfi_entitlements(proc);
  248. set_sandbox_extensions(proc);
  249. set_csblob(proc);
  250. return 0;
  251. }