kern_utils.m 10 KB

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