ent_patching.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #include <stdlib.h>
  2. #include <stddef.h>
  3. #include <Foundation/Foundation.h>
  4. #include "cs_dingling.h"
  5. #include "helpers/kexecute.h"
  6. #include "helpers/kmem.h"
  7. #include "helpers/osobject.h"
  8. #include "helpers/patchfinder64.h"
  9. #include "ubc_headers.h"
  10. #include "kern_utils.h"
  11. uint64_t get_vfs_context() {
  12. // vfs_context_t vfs_context_current(void)
  13. uint64_t vfs_context = kexecute(offset_vfs_context_current, 1, 0, 0, 0, 0, 0, 0);
  14. vfs_context = zm_fix_addr(vfs_context);
  15. return vfs_context;
  16. }
  17. int get_vnode_fromfd(uint64_t vfs_context, int fd, uint64_t *vpp) {
  18. uint64_t vnode = kalloc(sizeof(vnode_t *));
  19. // int vnode_getfromfd(vfs_context_t cfx, int fd, vnode_t vpp)
  20. int ret = kexecute(offset_vnode_getfromfd, vfs_context, fd, vnode, 0, 0, 0, 0);
  21. if (ret != 0) {
  22. return -1;
  23. }
  24. *vpp = vnode;
  25. return 0;
  26. }
  27. int check_vtype(uint64_t vnode) {
  28. /*
  29. struct vnode { // `vnode`
  30. ...
  31. uint16_t `v_type`;
  32. */
  33. uint16_t v_type = rk64(vnode + offsetof(struct vnode, v_type));
  34. return (v_type == VREG) ? 0 : 1;
  35. }
  36. uint64_t get_vu_ubcinfo(uint64_t vnode) {
  37. /*
  38. struct vnode { // `vnode`
  39. ...
  40. union {
  41. struct ubc_info *vu_ubcinfo;
  42. } v_un;
  43. */
  44. return rk64(vnode + offsetof(struct vnode, v_un));
  45. }
  46. uint64_t get_csblobs(uint64_t vu_ubcinfo) {
  47. /*
  48. struct ubc_info { // `vu_ubcinfo`
  49. ....
  50. struct cs_blob *cs_blobs;
  51. */
  52. return rk64(vu_ubcinfo + offsetof(struct ubc_info, cs_blobs));
  53. }
  54. void csblob_ent_dict_set(uint64_t cs_blobs, uint64_t dict) {
  55. // void csblob_entitlements_dictionary_set(struct cs_blob *csblob, void *entitlements)
  56. kexecute(offset_csblob_ent_dict_set, cs_blobs, dict, 0, 0, 0, 0, 0);
  57. }
  58. void csblob_update_csflags(uint64_t cs_blobs, uint64_t flags_to_add) {
  59. uint64_t csflags = rk64(cs_blobs + offsetof(struct cs_blob, csb_flags));
  60. csflags |= flags_to_add;
  61. wk64(cs_blobs + offsetof(struct cs_blob, csb_flags), csflags);
  62. }
  63. int set_memory_object_code_signed(uint64_t vu_ubcinfo) {
  64. uint64_t ui_control = rk64(vu_ubcinfo + offsetof(struct ubc_info, ui_control));
  65. if (ui_control == 0) {
  66. NSLog(@"failed to get ui_control");
  67. return 1;
  68. }
  69. uint64_t moc_object = rk64(ui_control + 0x8); // offsetof(struct memory_object_control, moc_object)
  70. if (moc_object == 0) {
  71. NSLog(@"failed to get moc_object");
  72. return 1;
  73. }
  74. uint64_t code_signed_addr = moc_object + 0xb8;
  75. uint32_t curr_code_signed = rk32(code_signed_addr);
  76. // `code_signed` is only 1 bit
  77. curr_code_signed |= 0x100;
  78. wk32(code_signed_addr, curr_code_signed);
  79. return 0;
  80. }
  81. uint64_t cs_hash_ptr = 0;
  82. uint64_t find_csb_hashtype(uint32_t hashType) {
  83. // We're keeping hold of this just incase the patchfind for `cs_find_md` fails
  84. if (cs_hash_ptr == 0) {
  85. const struct cs_hash hash = {
  86. .cs_type = CS_HASHTYPE_SHA1,
  87. .cs_size = CS_SHA1_LEN,
  88. .cs_init = offset_sha1_init,
  89. .cs_update = offset_sha1_update,
  90. .cs_final = offset_sha1_final
  91. };
  92. cs_hash_ptr = kalloc(sizeof(hash));
  93. kwrite(cs_hash_ptr, &hash, sizeof(hash));
  94. }
  95. uint64_t cs_find_md = find_cs_find_md();
  96. if (cs_find_md == 0) {
  97. // Dammit :( If the hash isn't SHA1 it now won't run,
  98. // but if we return 0 it will just KP. I'd rather a Killed: 9
  99. NSLog(@"FATAL ERROR! Unable to find 'cs_find_md'!!");
  100. return cs_hash_ptr;
  101. }
  102. return rk64(cs_find_md + ((hashType - 1) * 0x8));
  103. }
  104. uint64_t construct_cs_blob(const void *cs,
  105. uint32_t cs_length,
  106. uint8_t cd_hash[20],
  107. uint32_t chosen_off,
  108. uint64_t macho_offset) {
  109. uint64_t entire_csdir = kalloc(cs_length);
  110. kwrite(entire_csdir, cs, cs_length);
  111. const CS_CodeDirectory *blob = (const CS_CodeDirectory *)((uintptr_t)cs + chosen_off);
  112. uint64_t cs_blob = kalloc(sizeof(struct cs_blob));
  113. wk64(cs_blob + offsetof(struct cs_blob, csb_next), 0);
  114. wk32(cs_blob + offsetof(struct cs_blob, csb_cpu_type), -1); // kern will update this for us :-)
  115. uint32_t csb_flags = (ntohl(blob->flags) & CS_ALLOWED_MACHO) | CS_VALID | CS_SIGNED;
  116. wk32(cs_blob + offsetof(struct cs_blob, csb_flags), csb_flags);
  117. wk64(cs_blob + offsetof(struct cs_blob, csb_base_offset), macho_offset);
  118. wk64(cs_blob + offsetof(struct cs_blob, csb_start_offset), 0);
  119. if (ntohl(blob->version) >= CS_SUPPORTSSCATTER && (ntohl(blob->scatterOffset))) {
  120. const SC_Scatter *scatter = (const SC_Scatter *)((const char *)blob + ntohl(blob->scatterOffset));
  121. wk64(cs_blob + offsetof(struct cs_blob, csb_start_offset), ((off_t)ntohl(scatter->base)) * (1U << blob->pageSize));
  122. }
  123. wk64(cs_blob + offsetof(struct cs_blob, csb_end_offset), ((vm_offset_t)ntohl(blob->codeLimit) +
  124. ((1U << blob->pageSize) - 1) &
  125. ~((vm_offset_t)((1U << blob->pageSize) - 1))));
  126. wk64(cs_blob + offsetof(struct cs_blob, csb_mem_size), cs_length);
  127. wk64(cs_blob + offsetof(struct cs_blob, csb_mem_offset), 0);
  128. wk64(cs_blob + offsetof(struct cs_blob, csb_mem_kaddr), entire_csdir);
  129. kwrite(cs_blob + offsetof(struct cs_blob, csb_cdhash), cd_hash, CS_CDHASH_LEN);
  130. wk64(cs_blob + offsetof(struct cs_blob, csb_hashtype), find_csb_hashtype(blob->hashType));
  131. wk64(cs_blob + offsetof(struct cs_blob, csb_hash_pagesize), (1U << blob->pageSize));
  132. wk64(cs_blob + offsetof(struct cs_blob, csb_hash_pagemask), (1U << blob->pageSize) - 1);
  133. wk64(cs_blob + offsetof(struct cs_blob, csb_hash_pageshift), blob->pageSize);
  134. wk64(cs_blob + offsetof(struct cs_blob, csb_hash_firstlevel_pagesize), 0);
  135. wk64(cs_blob + offsetof(struct cs_blob, csb_cd), entire_csdir + chosen_off);
  136. wk64(cs_blob + offsetof(struct cs_blob, csb_teamid), 0);
  137. wk64(cs_blob + offsetof(struct cs_blob, csb_entitlements_blob), 0);
  138. wk64(cs_blob + offsetof(struct cs_blob, csb_entitlements), 0); /* we'll update this later */
  139. wk32(cs_blob + offsetof(struct cs_blob, csb_platform_binary), 0);
  140. wk32(cs_blob + offsetof(struct cs_blob, csb_platform_path), 0);
  141. if (csb_flags & CS_PLATFORM_BINARY) {
  142. wk64(cs_blob + offsetof(struct cs_blob, csb_platform_binary), 1);
  143. wk64(cs_blob + offsetof(struct cs_blob, csb_platform_path), !!(csb_flags & CS_PLATFORM_PATH));
  144. } else if ((ntohl(blob->version) >= CS_SUPPORTSTEAMID) &&
  145. (blob->teamOffset > 0)) {
  146. const char *name = ((const char *)blob) + ntohl(blob->teamOffset);
  147. uint64_t teamid_addr = kalloc(strlen(name));
  148. kwrite(teamid_addr, name, strlen(name));
  149. wk64(cs_blob + offsetof(struct cs_blob, csb_teamid), teamid_addr);
  150. }
  151. return cs_blob;
  152. }
  153. int fixup_platform_application(const char *path,
  154. uint64_t macho_offset,
  155. const void *blob,
  156. uint32_t cs_length,
  157. uint8_t cd_hash[20],
  158. uint32_t csdir_offset,
  159. const CS_GenericBlob *entitlements) {
  160. int ret;
  161. uint64_t vfs_context = get_vfs_context();
  162. if (vfs_context == 0) {
  163. ret = -1;
  164. goto out;
  165. }
  166. int fd = open(path, O_RDONLY);
  167. if (fd < 0) {
  168. ret = -2;
  169. goto out;
  170. }
  171. uint64_t *vpp = malloc(sizeof(vnode_t *));
  172. ret = get_vnode_fromfd(vfs_context, fd, vpp);
  173. if (ret != 0) {
  174. ret = -3;
  175. goto out;
  176. }
  177. uint64_t vnode = rk64(*vpp);
  178. if (vnode == 0) {
  179. ret = -4;
  180. goto out;
  181. }
  182. ret = check_vtype(vnode);
  183. if (ret != 0) {
  184. ret = -5;
  185. goto out;
  186. }
  187. uint64_t vu_ubcinfo = get_vu_ubcinfo(vnode);
  188. if (vu_ubcinfo == 0) {
  189. ret = -6;
  190. goto out;
  191. }
  192. uint64_t cs_blobs = get_csblobs(vu_ubcinfo);
  193. if (cs_blobs == 0) {
  194. uint64_t new_cs_blob = construct_cs_blob(blob,
  195. cs_length,
  196. cd_hash,
  197. csdir_offset,
  198. macho_offset);
  199. if (new_cs_blob < 1) {
  200. NSLog(@"failed to construct csblob");
  201. ret = -7;
  202. goto out;
  203. }
  204. wk64(vu_ubcinfo + offsetof(struct ubc_info, cs_blobs), new_cs_blob);
  205. cs_blobs = rk64(vu_ubcinfo + offsetof(struct ubc_info, cs_blobs));
  206. // we now need to update a few other bits and bobs
  207. // memory_object_signed
  208. // uip->ui_control->moc_object->code_signed = 1
  209. ret = set_memory_object_code_signed(vu_ubcinfo);
  210. if (ret != 0) {
  211. ret = -8;
  212. goto out;
  213. }
  214. // disabled for now... causes panics on 2nd run of the binary
  215. // something to do with a mutex lock.. i don't care to figure out what
  216. // set generation count
  217. // wk64(vu_ubcinfo + offsetof(struct ubc_info, cs_add_gen), 1);
  218. // NSLog(@"cs_add_gen: %llx", rk64(vu_ubcinfo + offsetof(struct ubc_info, cs_add_gen)));
  219. // record_mtime
  220. uint64_t vnode_attr = kalloc(sizeof(struct vnode_attr));
  221. wk64(vnode_attr + offsetof(struct vnode_attr, va_supported), 0);
  222. wk64(vnode_attr + offsetof(struct vnode_attr, va_active), 1LL << 14);
  223. wk64(vnode_attr + offsetof(struct vnode_attr, va_vaflags), 0);
  224. // vnode_getattr
  225. ret = kexecute(offset_vnode_getattr, vnode, vnode_attr, vfs_context, 0, 0, 0, 0);
  226. if (ret != 0) {
  227. NSLog(@"vnode_attr failed - ret value: %d", ret);
  228. } else {
  229. uint64_t mtime = rk64(vnode_attr + offsetof(struct vnode_attr, va_modify_time));
  230. if (mtime == 0) {
  231. NSLog(@"mtime is 0!");
  232. } else {
  233. NSLog(@"got mtime: %llx", mtime);
  234. wk64(vu_ubcinfo + offsetof(struct ubc_info, cs_mtime), mtime);
  235. }
  236. }
  237. }
  238. if (entitlements == NULL) {
  239. // generate some new entitlements
  240. // this is all we're here to do, really :-)
  241. const char *cstring = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  242. "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
  243. "<plist version=\"1.0\">"
  244. "<dict>"
  245. "<key>platform-application</key>" // escape container restriction
  246. "<true/>"
  247. "<key>com.apple.private.security.no-container</key>" // no container
  248. "<true/>"
  249. "<key>get-task-allow</key>" // task_for_pid
  250. "<true/>"
  251. "<key>com.apple.private.skip-library-validation</key>" // allow invalid libs
  252. "<true/>"
  253. "</dict>"
  254. "</plist>";
  255. uint64_t dict = OSUnserializeXML(cstring);
  256. csblob_ent_dict_set(cs_blobs, dict);
  257. csblob_update_csflags(dict, CS_GET_TASK_ALLOW);
  258. // Update csb_entitlements_blob
  259. int size = 8 + strlen(cstring);
  260. CS_GenericBlob *entitlements_blob = (CS_GenericBlob *)malloc(size);
  261. entitlements_blob->magic = CSMAGIC_EMBEDDED_ENTITLEMENTS;
  262. entitlements_blob->length = 8 + strlen(cstring);
  263. stpcpy(entitlements_blob->data, cstring);
  264. // Copy the data into kernel, and write to the csb_entitlements_blob field
  265. uint64_t entptr = kalloc(size);
  266. kwrite(entptr, entitlements_blob, size);
  267. wk64(cs_blobs + offsetof(struct cs_blob, csb_entitlements_blob), entptr);
  268. free(entitlements_blob);
  269. } else {
  270. // there are some entitlements, let's parse them, update the osdict w/
  271. // platform-application (true), and write them into kern
  272. uint64_t dict = OSUnserializeXML(entitlements->data);
  273. // gotta check for get-task-allow as it sets another csflag
  274. // remember: csflags have to be *perfect* otherwise the trick won't work
  275. // the reason this is *before* we add it manually is because the kernel won't
  276. // know about the manually added entitlement, and therefore this flag won't be set
  277. // (assuming it wasn't already in the existing entitlements)
  278. ret = OSDictionary_GetItem(dict, "get-task-allow");
  279. if (ret != 0) {
  280. csblob_update_csflags(cs_blobs, CS_GET_TASK_ALLOW);
  281. }
  282. OSDictionary_SetItem(dict, "platform-application", find_OSBoolean_True());
  283. OSDictionary_SetItem(dict, "com.apple.private.security.no-container", find_OSBoolean_True());
  284. OSDictionary_SetItem(dict, "get-task-allow", find_OSBoolean_True());
  285. OSDictionary_SetItem(dict, "com.apple.private.skip-library-validation", find_OSBoolean_True());
  286. csblob_ent_dict_set(cs_blobs, dict);
  287. // map the genblob up to csb_entitlements_blob
  288. // idk if we necessarily need to do this but w/e
  289. // TODO: fix this so it uses the *new* entitlements, not the original ones (duh)
  290. // Note to self: this field seems to be checked in the case of things like uicache, which requires
  291. // the 'com.apple.lsapplicationworkspace.rebuildappdatabases' entitlement. I suspect this field is
  292. // designed to be 'userland-viewable'
  293. int size = ntohl(entitlements->length);
  294. uint64_t entptr = kalloc(size);
  295. kwrite(entptr, entitlements, size);
  296. wk64(cs_blobs + offsetof(struct cs_blob, csb_entitlements_blob), entptr);
  297. }
  298. ret = 0;
  299. out:
  300. if (fd >= 0)
  301. close(fd);
  302. return ret;
  303. }