ent_patching.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. uint64_t bluetooth_exception_osarray(void) {
  154. static uint64_t btcache = 0;
  155. if (btcache == 0) {
  156. btcache = OSUnserializeXML(
  157. "<array>"
  158. "<string>RemoteBluetoothAddress</string>"
  159. "</array>"
  160. );
  161. }
  162. return btcache;
  163. }
  164. int fixup_platform_application(const char *path,
  165. uint64_t macho_offset,
  166. const void *blob,
  167. uint32_t cs_length,
  168. uint8_t cd_hash[20],
  169. uint32_t csdir_offset,
  170. const CS_GenericBlob *entitlements) {
  171. int ret;
  172. uint64_t vfs_context = get_vfs_context();
  173. if (vfs_context == 0) {
  174. ret = -1;
  175. goto out;
  176. }
  177. int fd = open(path, O_RDONLY);
  178. if (fd < 0) {
  179. ret = -2;
  180. goto out;
  181. }
  182. uint64_t *vpp = malloc(sizeof(vnode_t *));
  183. ret = get_vnode_fromfd(vfs_context, fd, vpp);
  184. if (ret != 0) {
  185. ret = -3;
  186. goto out;
  187. }
  188. uint64_t vnode = rk64(*vpp);
  189. if (vnode == 0) {
  190. ret = -4;
  191. goto out;
  192. }
  193. ret = check_vtype(vnode);
  194. if (ret != 0) {
  195. ret = -5;
  196. goto out;
  197. }
  198. uint64_t vu_ubcinfo = get_vu_ubcinfo(vnode);
  199. if (vu_ubcinfo == 0) {
  200. ret = -6;
  201. goto out;
  202. }
  203. uint64_t cs_blobs = get_csblobs(vu_ubcinfo);
  204. if (cs_blobs == 0) {
  205. uint64_t new_cs_blob = construct_cs_blob(blob,
  206. cs_length,
  207. cd_hash,
  208. csdir_offset,
  209. macho_offset);
  210. if (new_cs_blob < 1) {
  211. NSLog(@"failed to construct csblob");
  212. ret = -7;
  213. goto out;
  214. }
  215. wk64(vu_ubcinfo + offsetof(struct ubc_info, cs_blobs), new_cs_blob);
  216. cs_blobs = rk64(vu_ubcinfo + offsetof(struct ubc_info, cs_blobs));
  217. // we now need to update a few other bits and bobs
  218. // memory_object_signed
  219. // uip->ui_control->moc_object->code_signed = 1
  220. ret = set_memory_object_code_signed(vu_ubcinfo);
  221. if (ret != 0) {
  222. ret = -8;
  223. goto out;
  224. }
  225. // disabled for now... causes panics on 2nd run of the binary
  226. // something to do with a mutex lock.. i don't care to figure out what
  227. // set generation count
  228. // wk64(vu_ubcinfo + offsetof(struct ubc_info, cs_add_gen), 1);
  229. // NSLog(@"cs_add_gen: %llx", rk64(vu_ubcinfo + offsetof(struct ubc_info, cs_add_gen)));
  230. // record_mtime
  231. uint64_t vnode_attr = kalloc(sizeof(struct vnode_attr));
  232. wk64(vnode_attr + offsetof(struct vnode_attr, va_supported), 0);
  233. wk64(vnode_attr + offsetof(struct vnode_attr, va_active), 1LL << 14);
  234. wk64(vnode_attr + offsetof(struct vnode_attr, va_vaflags), 0);
  235. // vnode_getattr
  236. ret = kexecute(offset_vnode_getattr, vnode, vnode_attr, vfs_context, 0, 0, 0, 0);
  237. if (ret != 0) {
  238. NSLog(@"vnode_attr failed - ret value: %d", ret);
  239. } else {
  240. uint64_t mtime = rk64(vnode_attr + offsetof(struct vnode_attr, va_modify_time));
  241. if (mtime == 0) {
  242. NSLog(@"mtime is 0!");
  243. } else {
  244. NSLog(@"got mtime: %llx", mtime);
  245. wk64(vu_ubcinfo + offsetof(struct ubc_info, cs_mtime), mtime);
  246. }
  247. }
  248. }
  249. if (entitlements == NULL) {
  250. // generate some new entitlements
  251. // this is all we're here to do, really :-)
  252. const char *cstring = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  253. "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
  254. "<plist version=\"1.0\">"
  255. "<dict>"
  256. "<key>platform-application</key>" // escape container restriction
  257. "<true/>"
  258. "<key>com.apple.private.security.no-container</key>" // no container
  259. "<true/>"
  260. "<key>get-task-allow</key>" // task_for_pid
  261. "<true/>"
  262. "<key>com.apple.private.skip-library-validation</key>" // allow invalid libs
  263. "<true/>"
  264. "<key>com.apple.private.MobileGestalt.AllowedProtectedKeys</key>"
  265. "<array>"
  266. "<string>RemoteBluetoothAddress</string>"
  267. "</array>"
  268. "</dict>"
  269. "</plist>";
  270. uint64_t dict = OSUnserializeXML(cstring);
  271. csblob_ent_dict_set(cs_blobs, dict);
  272. csblob_update_csflags(dict, CS_GET_TASK_ALLOW);
  273. // Update csb_entitlements_blob
  274. int size = 8 + strlen(cstring);
  275. CS_GenericBlob *entitlements_blob = (CS_GenericBlob *)malloc(size);
  276. entitlements_blob->magic = CSMAGIC_EMBEDDED_ENTITLEMENTS;
  277. entitlements_blob->length = 8 + strlen(cstring);
  278. stpcpy(entitlements_blob->data, cstring);
  279. // Copy the data into kernel, and write to the csb_entitlements_blob field
  280. uint64_t entptr = kalloc(size);
  281. kwrite(entptr, entitlements_blob, size);
  282. wk64(cs_blobs + offsetof(struct cs_blob, csb_entitlements_blob), entptr);
  283. free(entitlements_blob);
  284. } else {
  285. // there are some entitlements, let's parse them, update the osdict w/
  286. // platform-application (true), and write them into kern
  287. uint64_t dict = OSUnserializeXML(entitlements->data);
  288. // gotta check for get-task-allow as it sets another csflag
  289. // remember: csflags have to be *perfect* otherwise the trick won't work
  290. // the reason this is *before* we add it manually is because the kernel won't
  291. // know about the manually added entitlement, and therefore this flag won't be set
  292. // (assuming it wasn't already in the existing entitlements)
  293. ret = OSDictionary_GetItem(dict, "get-task-allow");
  294. if (ret != 0) {
  295. csblob_update_csflags(cs_blobs, CS_GET_TASK_ALLOW);
  296. }
  297. OSDictionary_SetItem(dict, "platform-application", find_OSBoolean_True());
  298. OSDictionary_SetItem(dict, "com.apple.private.security.no-container", find_OSBoolean_True());
  299. OSDictionary_SetItem(dict, "get-task-allow", find_OSBoolean_True());
  300. OSDictionary_SetItem(dict, "com.apple.private.skip-library-validation", find_OSBoolean_True());
  301. OSDictionary_SetItem(dict, "com.apple.private.security.no-sandbox", find_OSBoolean_True());
  302. OSDictionary_SetItem(dict, "com.apple.private.MobileGestalt.AllowedProtectedKeys", bluetooth_exception_osarray());
  303. csblob_ent_dict_set(cs_blobs, dict);
  304. // map the genblob up to csb_entitlements_blob
  305. // idk if we necessarily need to do this but w/e
  306. // TODO: fix this so it uses the *new* entitlements, not the original ones (duh)
  307. // Note to self: this field seems to be checked in the case of things like uicache, which requires
  308. // the 'com.apple.lsapplicationworkspace.rebuildappdatabases' entitlement. I suspect this field is
  309. // designed to be 'userland-viewable'
  310. int size = ntohl(entitlements->length);
  311. uint64_t entptr = kalloc(size);
  312. kwrite(entptr, entitlements, size);
  313. wk64(cs_blobs + offsetof(struct cs_blob, csb_entitlements_blob), entptr);
  314. }
  315. ret = 0;
  316. out:
  317. if (fd >= 0)
  318. close(fd);
  319. return ret;
  320. }