kernel.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. //
  2. // kernel.m
  3. // v0rtex
  4. //
  5. // Created by Ben Sparkes on 16/12/2017.
  6. // Copyright © 2017 Ben Sparkes. All rights reserved.
  7. //
  8. #include "kernel.h"
  9. #include "common.h"
  10. #include "helpers.h"
  11. #include <mach/mach.h>
  12. size_t kread(uint64_t where, void *p, size_t size)
  13. {
  14. int rv;
  15. size_t offset = 0;
  16. while (offset < size) {
  17. mach_vm_size_t sz, chunk = 2048;
  18. if (chunk > size - offset) {
  19. chunk = size - offset;
  20. }
  21. rv = mach_vm_read_overwrite(tfp0, where + offset, chunk, (mach_vm_address_t)p + offset, &sz);
  22. if (rv || sz == 0) {
  23. break;
  24. }
  25. offset += sz;
  26. }
  27. return offset;
  28. }
  29. size_t kwrite(uint64_t where, const void *p, size_t size) {
  30. int rv;
  31. size_t offset = 0;
  32. while (offset < size) {
  33. size_t chunk = 2048;
  34. if (chunk > size - offset) {
  35. chunk = size - offset;
  36. }
  37. rv = mach_vm_write(tfp0,
  38. where + offset,
  39. (mach_vm_offset_t)p + offset,
  40. (mach_msg_type_number_t)chunk);
  41. if (rv) {
  42. printf("[kernel] error copying buffer into region: @%p \n", (void *)(offset + where));
  43. break;
  44. }
  45. offset +=chunk;
  46. }
  47. return offset;
  48. }
  49. uint64_t rk64(uint64_t kaddr) {
  50. uint64_t lower = rk32(kaddr);
  51. uint64_t higher = rk32(kaddr + 4);
  52. return ((higher << 32) | lower);
  53. }
  54. uint32_t rk32(uint64_t kaddr) {
  55. kern_return_t err;
  56. uint32_t val = 0;
  57. mach_vm_size_t outsize = 0;
  58. kern_return_t mach_vm_write(vm_map_t target_task,
  59. mach_vm_address_t address,
  60. vm_offset_t data,
  61. mach_msg_type_number_t dataCnt);
  62. err = mach_vm_read_overwrite(tfp0,
  63. (mach_vm_address_t)kaddr,
  64. (mach_vm_size_t)sizeof(uint32_t),
  65. (mach_vm_address_t)&val,
  66. &outsize);
  67. if (err != KERN_SUCCESS) {
  68. return 0;
  69. }
  70. if (outsize != sizeof(uint32_t)) {
  71. return 0;
  72. }
  73. return val;
  74. }
  75. void wk64(uint64_t kaddr, uint64_t val) {
  76. uint32_t lower = (uint32_t)(val & 0xffffffff);
  77. uint32_t higher = (uint32_t)(val >> 32);
  78. wk32(kaddr, lower);
  79. wk32(kaddr + 4, higher);
  80. }
  81. void wk32(uint64_t kaddr, uint32_t val) {
  82. if (tfp0 == MACH_PORT_NULL) {
  83. return;
  84. }
  85. kern_return_t err;
  86. err = mach_vm_write(tfp0,
  87. (mach_vm_address_t)kaddr,
  88. (vm_offset_t)&val,
  89. (mach_msg_type_number_t)sizeof(uint32_t));
  90. if (err != KERN_SUCCESS) {
  91. return;
  92. }
  93. }
  94. uint64_t remote_alloc(mach_port_t task_port, uint64_t size) {
  95. kern_return_t err;
  96. mach_vm_offset_t remote_addr = 0;
  97. mach_vm_size_t remote_size = (mach_vm_size_t)size;
  98. err = mach_vm_allocate(task_port, &remote_addr, remote_size, VM_FLAGS_ANYWHERE);
  99. if (err != KERN_SUCCESS){
  100. printf("unable to allocate buffer in remote process\n");
  101. return 0;
  102. }
  103. return (uint64_t)remote_addr;
  104. }
  105. uint64_t alloc_and_fill_remote_buffer(mach_port_t task_port,
  106. uint64_t local_address,
  107. uint64_t length) {
  108. kern_return_t err;
  109. uint64_t remote_address = remote_alloc(task_port, length);
  110. err = mach_vm_write(task_port, remote_address, (mach_vm_offset_t)local_address, (mach_msg_type_number_t)length);
  111. if (err != KERN_SUCCESS){
  112. printf("unable to write to remote memory \n");
  113. return 0;
  114. }
  115. return remote_address;
  116. }
  117. void remote_free(mach_port_t task_port, uint64_t base, uint64_t size) {
  118. kern_return_t err;
  119. err = mach_vm_deallocate(task_port, (mach_vm_address_t)base, (mach_vm_size_t)size);
  120. if (err != KERN_SUCCESS){
  121. printf("unabble to deallocate remote buffer\n");
  122. return;
  123. }
  124. }
  125. void remote_read_overwrite(mach_port_t task_port,
  126. uint64_t remote_address,
  127. uint64_t local_address,
  128. uint64_t length) {
  129. kern_return_t err;
  130. mach_vm_size_t outsize = 0;
  131. err = mach_vm_read_overwrite(task_port, (mach_vm_address_t)remote_address, (mach_vm_size_t)length, (mach_vm_address_t)local_address, &outsize);
  132. if (err != KERN_SUCCESS){
  133. printf("remote read failed\n");
  134. return;
  135. }
  136. if (outsize != length){
  137. printf("remote read was short (expected %llx, got %llx\n", length, outsize);
  138. return;
  139. }
  140. }
  141. uint64_t binary_load_address(mach_port_t tp) {
  142. kern_return_t err;
  143. mach_msg_type_number_t region_count = VM_REGION_BASIC_INFO_COUNT_64;
  144. memory_object_name_t object_name = MACH_PORT_NULL;
  145. mach_vm_size_t target_first_size = 0x1000;
  146. mach_vm_address_t target_first_addr = 0x0;
  147. struct vm_region_basic_info_64 region = {0};
  148. err = mach_vm_region(tp,
  149. &target_first_addr,
  150. &target_first_size,
  151. VM_REGION_BASIC_INFO_64,
  152. (vm_region_info_t)&region,
  153. &region_count,
  154. &object_name);
  155. if (err != KERN_SUCCESS) {
  156. printf("failed to get the region\n");
  157. return -1;
  158. }
  159. return target_first_addr;
  160. }
  161. uint64_t ktask_self_addr() {
  162. uint64_t self_proc = find_proc_by_pid(getpid());
  163. return rk64(self_proc + 0x18);
  164. }
  165. // credits to Jonathan Levin (Morpheus) for this awesome workaround
  166. // http://newosxbook.com/articles/PST2.html
  167. mach_port_t task_for_pid_workaround(int pid) {
  168. host_t myhost = mach_host_self();
  169. mach_port_t psDefault;
  170. mach_port_t psDefault_control;
  171. task_array_t tasks;
  172. mach_msg_type_number_t numTasks;
  173. kern_return_t kr;
  174. kr = processor_set_default(myhost, &psDefault);
  175. kr = host_processor_set_priv(myhost, psDefault, &psDefault_control);
  176. if (kr != KERN_SUCCESS) {
  177. fprintf(stderr, "host_processor_set_priv failed with error %x\n", kr);
  178. mach_error("host_processor_set_priv",kr);
  179. exit(1);
  180. }
  181. kr = processor_set_tasks(psDefault_control, &tasks, &numTasks);
  182. if (kr != KERN_SUCCESS) {
  183. fprintf(stderr,"processor_set_tasks failed with error %x\n",kr);
  184. exit(1);
  185. }
  186. for (int i = 0; i < numTasks; i++) {
  187. int t_pid;
  188. pid_for_task(tasks[i], &t_pid);
  189. if (pid == t_pid) return (tasks[i]);
  190. }
  191. return MACH_PORT_NULL;
  192. }
  193. // from Ian Beer's find_port.c
  194. uint64_t find_port_address(mach_port_name_t port) {
  195. uint64_t task_addr = ktask_self_addr();
  196. uint64_t itk_space = rk64(task_addr + 0x300);
  197. uint64_t is_table = rk64(itk_space + 0x20);
  198. uint32_t port_index = port >> 8;
  199. uint64_t port_addr = rk64(is_table + (port_index * 0x18));
  200. return port_addr;
  201. }
  202. uint64_t find_gadget_candidate(char** alternatives, size_t gadget_length) {
  203. void* haystack_start = (void*)atoi; // will do...
  204. size_t haystack_size = 100*1024*1024; // likewise...
  205. for (char* candidate = *alternatives; candidate != NULL; alternatives++) {
  206. void* found_at = memmem(haystack_start, haystack_size, candidate, gadget_length);
  207. if (found_at != NULL) {
  208. return (uint64_t)found_at;
  209. }
  210. }
  211. return 0;
  212. }
  213. uint64_t blr_x19_addr = 0;
  214. uint64_t find_blr_x19_gadget() {
  215. if (blr_x19_addr != 0) {
  216. return blr_x19_addr;
  217. }
  218. char* blr_x19 = "\x60\x02\x3f\xd6";
  219. char* candidates[] = {blr_x19, NULL};
  220. blr_x19_addr = find_gadget_candidate(candidates, 4);
  221. return blr_x19_addr;
  222. }
  223. // no support for non-register args
  224. #define MAX_REMOTE_ARGS 8
  225. // not in iOS SDK headers:
  226. extern void _pthread_set_self(pthread_t p);
  227. uint64_t call_remote(mach_port_t task_port, void* fptr, int n_params, ...) {
  228. if (n_params > MAX_REMOTE_ARGS || n_params < 0){
  229. NSLog(@"unsupported number of arguments to remote function (%d)\n", n_params);
  230. return 0;
  231. }
  232. kern_return_t err;
  233. uint64_t remote_stack_base = 0;
  234. uint64_t remote_stack_size = 4*1024*1024;
  235. remote_stack_base = remote_alloc(task_port, remote_stack_size);
  236. uint64_t remote_stack_middle = remote_stack_base + (remote_stack_size/2);
  237. // create a new thread in the target
  238. // just using the mach thread API doesn't initialize the pthread thread-local-storage
  239. // which means that stuff which relies on that will crash
  240. // we can sort-of make that work by calling _pthread_set_self(NULL) in the target process
  241. // which will give the newly created thread the same TLS region as the main thread
  242. _STRUCT_ARM_THREAD_STATE64 thread_state = {0};
  243. mach_msg_type_number_t thread_stateCnt = sizeof(thread_state)/4;
  244. // we'll start the thread running and call _pthread_set_self first:
  245. thread_state.__sp = remote_stack_middle;
  246. thread_state.__pc = (uint64_t)_pthread_set_self;
  247. // set these up to put us into a predictable state we can monitor for:
  248. uint64_t loop_lr = find_blr_x19_gadget();
  249. thread_state.__x[19] = loop_lr;
  250. thread_state.__lr = loop_lr;
  251. // set the argument to NULL:
  252. thread_state.__x[0] = 0;
  253. mach_port_t thread_port = MACH_PORT_NULL;
  254. err = thread_create_running(task_port, ARM_THREAD_STATE64, (thread_state_t)&thread_state, thread_stateCnt, &thread_port);
  255. if (err != KERN_SUCCESS){
  256. NSLog(@"error creating thread in child: %s\n", mach_error_string(err));
  257. return 0;
  258. }
  259. // NSLog(@"new thread running in child: %x\n", thread_port);
  260. // wait for it to hit the loop:
  261. while(1){
  262. // monitor the thread until we see it's in the infinite loop indicating it's done:
  263. err = thread_get_state(thread_port, ARM_THREAD_STATE64, (thread_state_t)&thread_state, &thread_stateCnt);
  264. if (err != KERN_SUCCESS){
  265. NSLog(@"error getting thread state: %s\n", mach_error_string(err));
  266. return 0;
  267. }
  268. if (thread_state.__pc == loop_lr && thread_state.__x[19] == loop_lr){
  269. // thread has returned from the target function
  270. break;
  271. }
  272. }
  273. // the thread should now have pthread local storage
  274. // pause it:
  275. err = thread_suspend(thread_port);
  276. if (err != KERN_SUCCESS){
  277. NSLog(@"unable to suspend target thread\n");
  278. return 0;
  279. }
  280. /*
  281. err = thread_abort(thread_port);
  282. if (err != KERN_SUCCESS){
  283. NSLog(@"unable to get thread out of any traps\n");
  284. return 0;
  285. }
  286. */
  287. // set up for the actual target call:
  288. thread_state.__sp = remote_stack_middle;
  289. thread_state.__pc = (uint64_t)fptr;
  290. // set these up to put us into a predictable state we can monitor for:
  291. thread_state.__x[19] = loop_lr;
  292. thread_state.__lr = loop_lr;
  293. va_list ap;
  294. va_start(ap, n_params);
  295. arg_desc* args[MAX_REMOTE_ARGS] = {0};
  296. uint64_t remote_buffers[MAX_REMOTE_ARGS] = {0};
  297. //uint64_t remote_buffer_sizes[MAX_REMOTE_ARGS] = {0};
  298. for (int i = 0; i < n_params; i++){
  299. arg_desc* arg = va_arg(ap, arg_desc*);
  300. args[i] = arg;
  301. switch(arg->type){
  302. case ARG_LITERAL:
  303. {
  304. thread_state.__x[i] = arg->value;
  305. break;
  306. }
  307. case ARG_BUFFER:
  308. case ARG_BUFFER_PERSISTENT:
  309. case ARG_INOUT_BUFFER:
  310. {
  311. uint64_t remote_buffer = alloc_and_fill_remote_buffer(task_port, arg->value, arg->length);
  312. remote_buffers[i] = remote_buffer;
  313. thread_state.__x[i] = remote_buffer;
  314. break;
  315. }
  316. case ARG_OUT_BUFFER:
  317. {
  318. uint64_t remote_buffer = remote_alloc(task_port, arg->length);
  319. // NSLog(@"allocated a remote out buffer: %llx\n", remote_buffer);
  320. remote_buffers[i] = remote_buffer;
  321. thread_state.__x[i] = remote_buffer;
  322. break;
  323. }
  324. default:
  325. {
  326. NSLog(@"invalid argument type!\n");
  327. }
  328. }
  329. }
  330. va_end(ap);
  331. err = thread_set_state(thread_port, ARM_THREAD_STATE64, (thread_state_t)&thread_state, thread_stateCnt);
  332. if (err != KERN_SUCCESS){
  333. NSLog(@"error setting new thread state: %s\n", mach_error_string(err));
  334. return 0;
  335. }
  336. // NSLog(@"thread state updated in target: %x\n", thread_port);
  337. err = thread_resume(thread_port);
  338. if (err != KERN_SUCCESS){
  339. NSLog(@"unable to resume target thread\n");
  340. return 0;
  341. }
  342. while(1){
  343. // monitor the thread until we see it's in the infinite loop indicating it's done:
  344. err = thread_get_state(thread_port, ARM_THREAD_STATE64, (thread_state_t)&thread_state, &thread_stateCnt);
  345. if (err != KERN_SUCCESS){
  346. NSLog(@"error getting thread state: %s\n", mach_error_string(err));
  347. return 0;
  348. }
  349. if (thread_state.__pc == loop_lr/*&& thread_state.__x[19] == loop_lr*/){
  350. // thread has returned from the target function
  351. break;
  352. }
  353. // thread isn't in the infinite loop yet, let it continue
  354. }
  355. // deallocate the remote thread
  356. err = thread_terminate(thread_port);
  357. if (err != KERN_SUCCESS){
  358. NSLog(@"failed to terminate thread\n");
  359. return 0;
  360. }
  361. mach_port_deallocate(mach_task_self(), thread_port);
  362. // handle post-call argument cleanup/copying:
  363. for (int i = 0; i < MAX_REMOTE_ARGS; i++){
  364. arg_desc* arg = args[i];
  365. if (arg == NULL){
  366. break;
  367. }
  368. switch (arg->type){
  369. case ARG_BUFFER:
  370. {
  371. remote_free(task_port, remote_buffers[i], arg->length);
  372. break;
  373. }
  374. case ARG_INOUT_BUFFER:
  375. case ARG_OUT_BUFFER:
  376. {
  377. // copy the contents back:
  378. remote_read_overwrite(task_port, remote_buffers[i], arg->value, arg->length);
  379. remote_free(task_port, remote_buffers[i], arg->length);
  380. break;
  381. }
  382. }
  383. }
  384. uint64_t ret_val = thread_state.__x[0];
  385. // NSLog(@"remote function call return value: %llx\n", ret_val);
  386. // deallocate the stack in the target:
  387. remote_free(task_port, remote_stack_base, remote_stack_size);
  388. return ret_val;
  389. }