kern_utils.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #import <Foundation/Foundation.h>
  2. #import <sys/stat.h>
  3. #import "kern_utils.h"
  4. #import "kmem.h"
  5. #import "patchfinder64.h"
  6. #import "kexecute.h"
  7. #import "offsetof.h"
  8. #import "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 (will go away with rdar://problem/28322552) */
  36. uint64_t proc_find(int pd, int tries) {
  37. // TODO use kcall(proc_find) + ZM_FIX_ADDR
  38. while (tries-- > 0) {
  39. uint64_t proc = rk64(find_allproc());
  40. while (proc) {
  41. uint32_t pid = rk32(proc + offsetof_p_pid);
  42. if (pid == pd) {
  43. return proc;
  44. }
  45. proc = rk64(proc);
  46. }
  47. }
  48. return 0;
  49. }
  50. CACHED_FIND(uint64_t, our_task_addr) {
  51. uint64_t our_proc = proc_find(getpid(), 1);
  52. if (our_proc == 0) {
  53. fprintf(stderr,"failed to find our_task_addr!\n");
  54. exit(EXIT_FAILURE);
  55. }
  56. uint64_t addr = rk64(our_proc + offsetof_task);
  57. fprintf(stderr,"our_task_addr: 0x%llx\n", addr);
  58. return addr;
  59. }
  60. uint64_t find_port(mach_port_name_t port){
  61. uint64_t task_addr = our_task_addr();
  62. uint64_t itk_space = rk64(task_addr + offsetof_itk_space);
  63. uint64_t is_table = rk64(itk_space + offsetof_ipc_space_is_table);
  64. uint32_t port_index = port >> 8;
  65. const int sizeof_ipc_entry_t = 0x18;
  66. uint64_t port_addr = rk64(is_table + (port_index * sizeof_ipc_entry_t));
  67. return port_addr;
  68. }
  69. void fixupsetuid(int pid){
  70. char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
  71. bzero(pathbuf, sizeof(pathbuf));
  72. int ret = proc_pidpath(pid, pathbuf, sizeof(pathbuf));
  73. if (ret < 0){
  74. fprintf(stderr,"Unable to get path for PID %d\n", pid);
  75. return;
  76. }
  77. struct stat file_st;
  78. if (lstat(pathbuf, &file_st) == -1){
  79. #ifdef JAILBREAKDDEBUG
  80. fprintf(stderr,"Unable to get stat for file %s\n", pathbuf);
  81. #endif
  82. return;
  83. }
  84. if (file_st.st_mode & S_ISUID){
  85. uid_t fileUID = file_st.st_uid;
  86. #ifdef JAILBREAKDDEBUG
  87. fprintf(stderr,"Fixing up setuid for file owned by %u\n", fileUID);
  88. #endif
  89. uint64_t proc = proc_find(pid, 3);
  90. if (proc != 0) {
  91. uint64_t ucred = rk64(proc + offsetof_p_ucred);
  92. uid_t cr_svuid = rk32(ucred + offsetof_ucred_cr_svuid);
  93. #ifdef JAILBREAKDDEBUG
  94. fprintf(stderr,"Original sv_uid: %u\n", cr_svuid);
  95. #endif
  96. wk32(ucred + offsetof_ucred_cr_svuid, fileUID);
  97. #ifdef JAILBREAKDDEBUG
  98. fprintf(stderr,"New sv_uid: %u\n", fileUID);
  99. #endif
  100. }
  101. } else {
  102. #ifdef JAILBREAKDDEBUG
  103. fprintf(stderr,"File %s is not setuid!\n", pathbuf);
  104. #endif
  105. return;
  106. }
  107. }
  108. void set_csflags(uint64_t proc) {
  109. uint32_t csflags = rk32(proc + offsetof_p_csflags);
  110. #ifdef JAILBREAKDDEBUG
  111. fprintf(stderr,"Previous CSFlags: 0x%x\n", csflags);
  112. #endif
  113. csflags = (csflags | CS_PLATFORM_BINARY | CS_INSTALLER | CS_GET_TASK_ALLOW | CS_DEBUGGED) & ~(CS_RESTRICT | CS_HARD | CS_KILL);
  114. #ifdef JAILBREAKDDEBUG
  115. fprintf(stderr,"New CSFlags: 0x%x\n", csflags);
  116. #endif
  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. #ifdef JAILBREAKDDEBUG
  124. fprintf(stderr,"Old t_flags: 0x%x\n", t_flags);
  125. #endif
  126. t_flags |= TF_PLATFORM;
  127. wk32(task+offsetof_t_flags, t_flags);
  128. #ifdef JAILBREAKDDEBUG
  129. fprintf(stderr,"New t_flags: 0x%x\n", t_flags);
  130. #endif
  131. }
  132. void set_csblob(uint64_t proc) {
  133. uint64_t textvp = rk64(proc + offsetof_p_textvp); //vnode of executable
  134. off_t textoff = rk64(proc + offsetof_p_textoff);
  135. #ifdef JAILBREAKDDEBUG
  136. fprintf(stderr,"\t__TEXT at 0x%llx. Offset: 0x%llx\n", textvp, textoff);
  137. #endif
  138. if (textvp != 0){
  139. uint32_t vnode_type_tag = rk32(textvp + offsetof_v_type);
  140. uint16_t vnode_type = vnode_type_tag & 0xffff;
  141. uint16_t vnode_tag = (vnode_type_tag >> 16);
  142. #ifdef JAILBREAKDDEBUG
  143. fprintf(stderr,"\tVNode Type: 0x%x. Tag: 0x%x.\n", vnode_type, vnode_tag);
  144. #endif
  145. if (vnode_type == 1){
  146. uint64_t ubcinfo = rk64(textvp + offsetof_v_ubcinfo);
  147. #ifdef JAILBREAKDDEBUG
  148. fprintf(stderr,"\t\tUBCInfo at 0x%llx.\n\n", ubcinfo);
  149. #endif
  150. uint64_t csblobs = rk64(ubcinfo + offsetof_ubcinfo_csblobs);
  151. while (csblobs != 0){
  152. #ifdef JAILBREAKDDEBUG
  153. fprintf(stderr,"\t\t\tCSBlobs at 0x%llx.\n", csblobs);
  154. #endif
  155. cpu_type_t csblob_cputype = rk32(csblobs + offsetof_csb_cputype);
  156. unsigned int csblob_flags = rk32(csblobs + offsetof_csb_flags);
  157. off_t csb_base_offset = rk64(csblobs + offsetof_csb_base_offset);
  158. uint64_t csb_entitlements = rk64(csblobs + offsetof_csb_entitlements_offset);
  159. unsigned int csb_signer_type = rk32(csblobs + offsetof_csb_signer_type);
  160. unsigned int csb_platform_binary = rk32(csblobs + offsetof_csb_platform_binary);
  161. unsigned int csb_platform_path = rk32(csblobs + offsetof_csb_platform_path);
  162. #ifdef JAILBREAKDDEBUG
  163. fprintf(stderr,"\t\t\tCSBlob CPU Type: 0x%x. Flags: 0x%x. Offset: 0x%llx\n", csblob_cputype, csblob_flags, csb_base_offset);
  164. fprintf(stderr,"\t\t\tCSBlob Signer Type: 0x%x. Platform Binary: %d Path: %d\n", csb_signer_type, csb_platform_binary, csb_platform_path);
  165. #endif
  166. wk32(csblobs + offsetof_csb_platform_binary, 1);
  167. csb_platform_binary = rk32(csblobs + offsetof_csb_platform_binary);
  168. #ifdef JAILBREAKDDEBUG
  169. fprintf(stderr,"\t\t\tCSBlob Signer Type: 0x%x. Platform Binary: %d Path: %d\n", csb_signer_type, csb_platform_binary, csb_platform_path);
  170. fprintf(stderr,"\t\t\t\tEntitlements at 0x%llx.\n", csb_entitlements);
  171. #endif
  172. csblobs = rk64(csblobs);
  173. }
  174. }
  175. }
  176. }
  177. const char* abs_path_exceptions[] = {
  178. "/Library",
  179. // XXX there's some weird stuff about linking and special
  180. // handling for /private/var/mobile/* in sandbox
  181. "/private/var/mobile/Library",
  182. "/private/var/mnt",
  183. "/private/var/db",
  184. "/private/var/stash",
  185. NULL
  186. };
  187. uint64_t get_exception_osarray(void) {
  188. static uint64_t cached = 0;
  189. if (cached == 0) {
  190. // XXX use abs_path_exceptions
  191. cached = OSUnserializeXML("<array>"
  192. "<string>/Library/</string>"
  193. "<string>/private/var/mobile/Library/</string>"
  194. "<string>/private/var/mnt/</string>"
  195. "<string>/private/var/db/</string>"
  196. "<string>/private/var/stash/</string>"
  197. "</array>");
  198. }
  199. return cached;
  200. }
  201. static const char *exc_key = "com.apple.security.exception.files.absolute-path.read-only";
  202. void set_sandbox_extensions(uint64_t proc) {
  203. uint64_t proc_ucred = rk64(proc+0x100);
  204. uint64_t sandbox = rk64(rk64(proc_ucred+0x78) + 8 + 8);
  205. #ifdef JAILBREAKDDEBUG
  206. fprintf(stderr,"proc = 0x%llx & proc_ucred = 0x%llx & sandbox = 0x%llx\n", proc, proc_ucred, sandbox);
  207. #endif
  208. if (sandbox == 0) {
  209. #ifdef JAILBREAKDDEBUG
  210. fprintf(stderr,"no sandbox, skipping\n");
  211. #endif
  212. return;
  213. }
  214. if (has_file_extension(sandbox, abs_path_exceptions[0])) {
  215. #ifdef JAILBREAKDDEBUG
  216. fprintf(stderr,"already has '%s', skipping\n", abs_path_exceptions[0]);
  217. #endif
  218. return;
  219. }
  220. uint64_t ext = 0;
  221. const char** path = abs_path_exceptions;
  222. while (*path != NULL) {
  223. ext = extension_create_file(*path, ext);
  224. if (ext == 0) {
  225. fprintf(stderr,"extension_create_file(%s) failed, panic!\n", *path);
  226. }
  227. ++path;
  228. }
  229. #ifdef JAILBREAKDDEBUG
  230. fprintf(stderr,"last extension_create_file ext: 0x%llx\n", ext);
  231. #endif
  232. if (ext != 0) {
  233. extension_add(ext, sandbox, exc_key);
  234. }
  235. }
  236. void set_amfi_entitlements(uint64_t proc) {
  237. // AMFI entitlements
  238. #ifdef JAILBREAKDDEBUG
  239. fprintf(stderr,"AMFI:\n");
  240. #endif
  241. uint64_t proc_ucred = rk64(proc+0x100);
  242. uint64_t amfi_entitlements = rk64(rk64(proc_ucred+0x78)+0x8);
  243. #ifdef JAILBREAKDDEBUG
  244. fprintf(stderr,"Setting Entitlements...\n");
  245. #endif
  246. OSDictionary_SetItem(amfi_entitlements, "get-task-allow", find_OSBoolean_True());
  247. OSDictionary_SetItem(amfi_entitlements, "com.apple.private.skip-library-validation", find_OSBoolean_True());
  248. uint64_t present = OSDictionary_GetItem(amfi_entitlements, exc_key);
  249. int rv = 0;
  250. if (present == 0) {
  251. rv = OSDictionary_SetItem(amfi_entitlements, exc_key, get_exception_osarray());
  252. } else if (present != get_exception_osarray()) {
  253. unsigned int itemCount = OSArray_ItemCount(present);
  254. #ifdef JAILBREAKDDEBUG
  255. fprintf(stderr,"present != 0 (0x%llx)! item count: %d\n", present, itemCount);
  256. #endif
  257. BOOL foundEntitlements = NO;
  258. uint64_t itemBuffer = OSArray_ItemBuffer(present);
  259. for (int i = 0; i < itemCount; i++){
  260. uint64_t item = rk64(itemBuffer + (i * sizeof(void *)));
  261. #ifdef JAILBREAKDDEBUG
  262. fprintf(stderr,"Item %d: 0x%llx\n", i, item);
  263. #endif
  264. char *entitlementString = OSString_CopyString(item);
  265. if (strcmp(entitlementString, "/bootstrap/") == 0){
  266. foundEntitlements = YES;
  267. free(entitlementString);
  268. break;
  269. }
  270. free(entitlementString);
  271. }
  272. if (!foundEntitlements){
  273. rv = OSArray_Merge(present, get_exception_osarray());
  274. } else {
  275. rv = 1;
  276. }
  277. } else {
  278. #ifdef JAILBREAKDDEBUG
  279. fprintf(stderr,"Not going to merge array with itself :P\n");
  280. #endif
  281. rv = 1;
  282. }
  283. if (rv != 1) {
  284. fprintf(stderr,"Setting exc FAILED! amfi_entitlements: 0x%llx present: 0x%llx\n", amfi_entitlements, present);
  285. }
  286. }
  287. int setcsflagsandplatformize(int pid){
  288. uint64_t proc = proc_find(pid, 3);
  289. if (proc != 0) {
  290. #ifdef JAILBREAKDDEBUG
  291. fprintf(stderr,"setcsflagsandplatformize start on PID %d\n", pid);
  292. char name[40] = {0};
  293. kread(proc+0x268, name, 20);
  294. fprintf(stderr,"PID %d name is %s\n", pid, name);
  295. #endif
  296. set_csflags(proc);
  297. set_tfplatform(proc);
  298. set_amfi_entitlements(proc);
  299. set_sandbox_extensions(proc);
  300. set_csblob(proc);
  301. #ifdef JAILBREAKDDEBUG
  302. fprintf(stderr,"setcsflagsandplatformize done on PID %d\n", pid);
  303. #endif
  304. return 0;
  305. }
  306. fprintf(stderr,"Unable to find PID %d to entitle!\n", pid);
  307. return 1;
  308. }