patchfinder64.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. //
  2. // patchfinder64.c
  3. // extra_recipe
  4. //
  5. // Created by xerub on 06/06/2017.
  6. // Copyright © 2017 xerub. All rights reserved.
  7. //
  8. #include <assert.h>
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include "patchfinder64.h"
  12. #include "kmem.h"
  13. #define CACHED_FIND_UINT64(name) CACHED_FIND(uint64_t, name)
  14. typedef uint64_t addr_t;
  15. #define IS64(image) (*(uint8_t *)(image) & 1)
  16. #define MACHO(p) ((*(unsigned int *)(p) & ~1) == 0xfeedface)
  17. /* generic stuff *************************************************************/
  18. #define UCHAR_MAX 255
  19. static unsigned char *
  20. boyermoore_horspool_memmem(const unsigned char* haystack, size_t hlen,
  21. const unsigned char* needle, size_t nlen)
  22. {
  23. size_t last, scan = 0;
  24. size_t bad_char_skip[UCHAR_MAX + 1]; /* Officially called:
  25. * bad character shift */
  26. /* Sanity checks on the parameters */
  27. if (nlen <= 0 || !haystack || !needle)
  28. return NULL;
  29. /* ---- Preprocess ---- */
  30. /* Initialize the table to default value */
  31. /* When a character is encountered that does not occur
  32. * in the needle, we can safely skip ahead for the whole
  33. * length of the needle.
  34. */
  35. for (scan = 0; scan <= UCHAR_MAX; scan = scan + 1)
  36. bad_char_skip[scan] = nlen;
  37. /* C arrays have the first byte at [0], therefore:
  38. * [nlen - 1] is the last byte of the array. */
  39. last = nlen - 1;
  40. /* Then populate it with the analysis of the needle */
  41. for (scan = 0; scan < last; scan = scan + 1)
  42. bad_char_skip[needle[scan]] = last - scan;
  43. /* ---- Do the matching ---- */
  44. /* Search the haystack, while the needle can still be within it. */
  45. while (hlen >= nlen)
  46. {
  47. /* scan from the end of the needle */
  48. for (scan = last; haystack[scan] == needle[scan]; scan = scan - 1)
  49. if (scan == 0) /* If the first byte matches, we've found it. */
  50. return (void *)haystack;
  51. /* otherwise, we need to skip some bytes and start again.
  52. Note that here we are getting the skip value based on the last byte
  53. of needle, no matter where we didn't match. So if needle is: "abcd"
  54. then we are skipping based on 'd' and that value will be 4, and
  55. for "abcdd" we again skip on 'd' but the value will be only 1.
  56. The alternative of pretending that the mismatched character was
  57. the last character is slower in the normal case (E.g. finding
  58. "abcd" in "...azcd..." gives 4 by using 'd' but only
  59. 4-2==2 using 'z'. */
  60. hlen -= bad_char_skip[haystack[last]];
  61. haystack += bad_char_skip[haystack[last]];
  62. }
  63. return NULL;
  64. }
  65. /* disassembler **************************************************************/
  66. static int HighestSetBit(int N, uint32_t imm)
  67. {
  68. int i;
  69. for (i = N - 1; i >= 0; i--) {
  70. if (imm & (1 << i)) {
  71. return i;
  72. }
  73. }
  74. return -1;
  75. }
  76. static uint64_t ZeroExtendOnes(unsigned M, unsigned N) // zero extend M ones to N width
  77. {
  78. (void)N;
  79. return ((uint64_t)1 << M) - 1;
  80. }
  81. static uint64_t RORZeroExtendOnes(unsigned M, unsigned N, unsigned R)
  82. {
  83. uint64_t val = ZeroExtendOnes(M, N);
  84. if (R == 0) {
  85. return val;
  86. }
  87. return ((val >> R) & (((uint64_t)1 << (N - R)) - 1)) | ((val & (((uint64_t)1 << R) - 1)) << (N - R));
  88. }
  89. static uint64_t Replicate(uint64_t val, unsigned bits)
  90. {
  91. uint64_t ret = val;
  92. unsigned shift;
  93. for (shift = bits; shift < 64; shift += bits) { // XXX actually, it is either 32 or 64
  94. ret |= (val << shift);
  95. }
  96. return ret;
  97. }
  98. static int DecodeBitMasks(unsigned immN, unsigned imms, unsigned immr, int immediate, uint64_t *newval)
  99. {
  100. unsigned levels, S, R, esize;
  101. int len = HighestSetBit(7, (immN << 6) | (~imms & 0x3F));
  102. if (len < 1) {
  103. return -1;
  104. }
  105. levels = ZeroExtendOnes(len, 6);
  106. if (immediate && (imms & levels) == levels) {
  107. return -1;
  108. }
  109. S = imms & levels;
  110. R = immr & levels;
  111. esize = 1 << len;
  112. *newval = Replicate(RORZeroExtendOnes(S + 1, esize, R), esize);
  113. return 0;
  114. }
  115. static int DecodeMov(uint32_t opcode, uint64_t total, int first, uint64_t *newval)
  116. {
  117. unsigned o = (opcode >> 29) & 3;
  118. unsigned k = (opcode >> 23) & 0x3F;
  119. unsigned rn, rd;
  120. uint64_t i;
  121. if (k == 0x24 && o == 1) { // MOV (bitmask imm) <=> ORR (immediate)
  122. unsigned s = (opcode >> 31) & 1;
  123. unsigned N = (opcode >> 22) & 1;
  124. if (s == 0 && N != 0) {
  125. return -1;
  126. }
  127. rn = (opcode >> 5) & 0x1F;
  128. if (rn == 31) {
  129. unsigned imms = (opcode >> 10) & 0x3F;
  130. unsigned immr = (opcode >> 16) & 0x3F;
  131. return DecodeBitMasks(N, imms, immr, 1, newval);
  132. }
  133. } else if (k == 0x25) { // MOVN/MOVZ/MOVK
  134. unsigned s = (opcode >> 31) & 1;
  135. unsigned h = (opcode >> 21) & 3;
  136. if (s == 0 && h > 1) {
  137. return -1;
  138. }
  139. i = (opcode >> 5) & 0xFFFF;
  140. h *= 16;
  141. i <<= h;
  142. if (o == 0) { // MOVN
  143. *newval = ~i;
  144. return 0;
  145. } else if (o == 2) { // MOVZ
  146. *newval = i;
  147. return 0;
  148. } else if (o == 3 && !first) { // MOVK
  149. *newval = (total & ~((uint64_t)0xFFFF << h)) | i;
  150. return 0;
  151. }
  152. } else if ((k | 1) == 0x23 && !first) { // ADD (immediate)
  153. unsigned h = (opcode >> 22) & 3;
  154. if (h > 1) {
  155. return -1;
  156. }
  157. rd = opcode & 0x1F;
  158. rn = (opcode >> 5) & 0x1F;
  159. if (rd != rn) {
  160. return -1;
  161. }
  162. i = (opcode >> 10) & 0xFFF;
  163. h *= 12;
  164. i <<= h;
  165. if (o & 2) { // SUB
  166. *newval = total - i;
  167. return 0;
  168. } else { // ADD
  169. *newval = total + i;
  170. return 0;
  171. }
  172. }
  173. return -1;
  174. }
  175. /* patchfinder ***************************************************************/
  176. static addr_t
  177. step64(const uint8_t *buf, addr_t start, size_t length, uint32_t what, uint32_t mask)
  178. {
  179. addr_t end = start + length;
  180. while (start < end) {
  181. uint32_t x = *(uint32_t *)(buf + start);
  182. if ((x & mask) == what) {
  183. return start;
  184. }
  185. start += 4;
  186. }
  187. return 0;
  188. }
  189. // str8 = step64_back(kernel, ref, ref - bof, INSN_STR8);
  190. static addr_t
  191. step64_back(const uint8_t *buf, addr_t start, size_t length, uint32_t what, uint32_t mask)
  192. {
  193. addr_t end = start - length;
  194. while (start >= end) {
  195. uint32_t x = *(uint32_t *)(buf + start);
  196. if ((x & mask) == what) {
  197. return start;
  198. }
  199. start -= 4;
  200. }
  201. return 0;
  202. }
  203. // Finds start of function
  204. static addr_t
  205. bof64(const uint8_t *buf, addr_t start, addr_t where)
  206. {
  207. for (; where >= start; where -= 4) {
  208. uint32_t op = *(uint32_t *)(buf + where);
  209. if ((op & 0xFFC003FF) == 0x910003FD) {
  210. unsigned delta = (op >> 10) & 0xFFF;
  211. //printf("%x: ADD X29, SP, #0x%x\n", where, delta);
  212. if ((delta & 0xF) == 0) {
  213. addr_t prev = where - ((delta >> 4) + 1) * 4;
  214. uint32_t au = *(uint32_t *)(buf + prev);
  215. if ((au & 0xFFC003E0) == 0xA98003E0) {
  216. //printf("%x: STP x, y, [SP,#-imm]!\n", prev);
  217. return prev;
  218. }
  219. }
  220. }
  221. }
  222. return 0;
  223. }
  224. static addr_t
  225. xref64(const uint8_t *buf, addr_t start, addr_t end, addr_t what)
  226. {
  227. addr_t i;
  228. uint64_t value[32];
  229. memset(value, 0, sizeof(value));
  230. end &= ~3;
  231. for (i = start & ~3; i < end; i += 4) {
  232. uint32_t op = *(uint32_t *)(buf + i);
  233. unsigned reg = op & 0x1F;
  234. if ((op & 0x9F000000) == 0x90000000) {
  235. signed adr = ((op & 0x60000000) >> 18) | ((op & 0xFFFFE0) << 8);
  236. //printf("%llx: ADRP X%d, 0x%llx\n", i, reg, ((long long)adr << 1) + (i & ~0xFFF));
  237. value[reg] = ((long long)adr << 1) + (i & ~0xFFF);
  238. /*} else if ((op & 0xFFE0FFE0) == 0xAA0003E0) {
  239. unsigned rd = op & 0x1F;
  240. unsigned rm = (op >> 16) & 0x1F;
  241. //printf("%llx: MOV X%d, X%d\n", i, rd, rm);
  242. value[rd] = value[rm];*/
  243. } else if ((op & 0xFF000000) == 0x91000000) {
  244. unsigned rn = (op >> 5) & 0x1F;
  245. unsigned shift = (op >> 22) & 3;
  246. unsigned imm = (op >> 10) & 0xFFF;
  247. if (shift == 1) {
  248. imm <<= 12;
  249. } else {
  250. //assert(shift == 0);
  251. if (shift > 1) continue;
  252. }
  253. //printf("%llx: ADD X%d, X%d, 0x%x\n", i, reg, rn, imm);
  254. value[reg] = value[rn] + imm;
  255. } else if ((op & 0xF9C00000) == 0xF9400000) {
  256. unsigned rn = (op >> 5) & 0x1F;
  257. unsigned imm = ((op >> 10) & 0xFFF) << 3;
  258. //printf("%llx: LDR X%d, [X%d, 0x%x]\n", i, reg, rn, imm);
  259. if (!imm) continue; // XXX not counted as true xref
  260. value[reg] = value[rn] + imm; // XXX address, not actual value
  261. /*} else if ((op & 0xF9C00000) == 0xF9000000) {
  262. unsigned rn = (op >> 5) & 0x1F;
  263. unsigned imm = ((op >> 10) & 0xFFF) << 3;
  264. //printf("%llx: STR X%d, [X%d, 0x%x]\n", i, reg, rn, imm);
  265. if (!imm) continue; // XXX not counted as true xref
  266. value[rn] = value[rn] + imm; // XXX address, not actual value*/
  267. } else if ((op & 0x9F000000) == 0x10000000) {
  268. signed adr = ((op & 0x60000000) >> 18) | ((op & 0xFFFFE0) << 8);
  269. //printf("%llx: ADR X%d, 0x%llx\n", i, reg, ((long long)adr >> 11) + i);
  270. value[reg] = ((long long)adr >> 11) + i;
  271. } else if ((op & 0xFF000000) == 0x58000000) {
  272. unsigned adr = (op & 0xFFFFE0) >> 3;
  273. //printf("%llx: LDR X%d, =0x%llx\n", i, reg, adr + i);
  274. value[reg] = adr + i; // XXX address, not actual value
  275. }
  276. if (value[reg] == what) {
  277. return i;
  278. }
  279. }
  280. return 0;
  281. }
  282. static addr_t
  283. calc64(const uint8_t *buf, addr_t start, addr_t end, int which)
  284. {
  285. addr_t i;
  286. uint64_t value[32];
  287. memset(value, 0, sizeof(value));
  288. end &= ~3;
  289. for (i = start & ~3; i < end; i += 4) {
  290. uint32_t op = *(uint32_t *)(buf + i);
  291. unsigned reg = op & 0x1F;
  292. if ((op & 0x9F000000) == 0x90000000) {
  293. signed adr = ((op & 0x60000000) >> 18) | ((op & 0xFFFFE0) << 8);
  294. //printf("%llx: ADRP X%d, 0x%llx\n", i, reg, ((long long)adr << 1) + (i & ~0xFFF));
  295. value[reg] = ((long long)adr << 1) + (i & ~0xFFF);
  296. /*} else if ((op & 0xFFE0FFE0) == 0xAA0003E0) {
  297. unsigned rd = op & 0x1F;
  298. unsigned rm = (op >> 16) & 0x1F;
  299. //printf("%llx: MOV X%d, X%d\n", i, rd, rm);
  300. value[rd] = value[rm];*/
  301. } else if ((op & 0xFF000000) == 0x91000000) {
  302. unsigned rn = (op >> 5) & 0x1F;
  303. unsigned shift = (op >> 22) & 3;
  304. unsigned imm = (op >> 10) & 0xFFF;
  305. if (shift == 1) {
  306. imm <<= 12;
  307. } else {
  308. //assert(shift == 0);
  309. if (shift > 1) continue;
  310. }
  311. //printf("%llx: ADD X%d, X%d, 0x%x\n", i, reg, rn, imm);
  312. value[reg] = value[rn] + imm;
  313. } else if ((op & 0xF9C00000) == 0xF9400000) {
  314. unsigned rn = (op >> 5) & 0x1F;
  315. unsigned imm = ((op >> 10) & 0xFFF) << 3;
  316. //printf("%llx: LDR X%d, [X%d, 0x%x]\n", i, reg, rn, imm);
  317. if (!imm) continue; // XXX not counted as true xref
  318. value[reg] = value[rn] + imm; // XXX address, not actual value
  319. } else if ((op & 0xF9C00000) == 0xF9000000) {
  320. unsigned rn = (op >> 5) & 0x1F;
  321. unsigned imm = ((op >> 10) & 0xFFF) << 3;
  322. //printf("%llx: STR X%d, [X%d, 0x%x]\n", i, reg, rn, imm);
  323. if (!imm) continue; // XXX not counted as true xref
  324. value[rn] = value[rn] + imm; // XXX address, not actual value
  325. } else if ((op & 0x9F000000) == 0x10000000) {
  326. signed adr = ((op & 0x60000000) >> 18) | ((op & 0xFFFFE0) << 8);
  327. //printf("%llx: ADR X%d, 0x%llx\n", i, reg, ((long long)adr >> 11) + i);
  328. value[reg] = ((long long)adr >> 11) + i;
  329. } else if ((op & 0xFF000000) == 0x58000000) {
  330. unsigned adr = (op & 0xFFFFE0) >> 3;
  331. //printf("%llx: LDR X%d, =0x%llx\n", i, reg, adr + i);
  332. value[reg] = adr + i; // XXX address, not actual value
  333. }
  334. }
  335. return value[which];
  336. }
  337. static addr_t
  338. calc64mov(const uint8_t *buf, addr_t start, addr_t end, int which)
  339. {
  340. addr_t i;
  341. uint64_t value[32];
  342. memset(value, 0, sizeof(value));
  343. end &= ~3;
  344. for (i = start & ~3; i < end; i += 4) {
  345. uint32_t op = *(uint32_t *)(buf + i);
  346. unsigned reg = op & 0x1F;
  347. uint64_t newval;
  348. int rv = DecodeMov(op, value[reg], 0, &newval);
  349. if (rv == 0) {
  350. if (((op >> 31) & 1) == 0) {
  351. newval &= 0xFFFFFFFF;
  352. }
  353. value[reg] = newval;
  354. }
  355. }
  356. return value[which];
  357. }
  358. static addr_t
  359. find_call64(const uint8_t *buf, addr_t start, size_t length)
  360. {
  361. return step64(buf, start, length, 0x94000000, 0xFC000000);
  362. }
  363. static addr_t
  364. follow_call64(const uint8_t *buf, addr_t call)
  365. {
  366. long long w;
  367. w = *(uint32_t *)(buf + call) & 0x3FFFFFF;
  368. w <<= 64 - 26;
  369. w >>= 64 - 26 - 2;
  370. return call + w;
  371. }
  372. static addr_t
  373. follow_cbz(const uint8_t *buf, addr_t cbz)
  374. {
  375. return cbz + ((*(int *)(buf + cbz) & 0x3FFFFE0) << 10 >> 13);
  376. }
  377. /* kernel iOS10 **************************************************************/
  378. #include <fcntl.h>
  379. #include <stdio.h>
  380. #include <stdlib.h>
  381. #include <unistd.h>
  382. #include <mach-o/loader.h>
  383. #ifndef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
  384. #define __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
  385. #endif
  386. #ifdef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
  387. #include <mach/mach.h>
  388. size_t kread(uint64_t where, void *p, size_t size);
  389. #endif
  390. static uint8_t *kernel = NULL;
  391. static size_t kernel_size = 0;
  392. static addr_t xnucore_base = 0;
  393. static addr_t xnucore_size = 0;
  394. static addr_t prelink_base = 0;
  395. static addr_t prelink_size = 0;
  396. static addr_t cstring_base = 0;
  397. static addr_t cstring_size = 0;
  398. static addr_t pstring_base = 0;
  399. static addr_t pstring_size = 0;
  400. static addr_t kerndumpbase = -1;
  401. static addr_t kernel_entry = 0;
  402. static void *kernel_mh = 0;
  403. static addr_t kernel_delta = 0;
  404. int
  405. init_kernel(addr_t base, const char *filename)
  406. {
  407. size_t rv;
  408. uint8_t buf[0x4000];
  409. unsigned i, j;
  410. const struct mach_header *hdr = (struct mach_header *)buf;
  411. const uint8_t *q;
  412. addr_t min = -1;
  413. addr_t max = 0;
  414. int is64 = 0;
  415. #ifdef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
  416. #define close(f)
  417. rv = kread(base, buf, sizeof(buf));
  418. if (rv != sizeof(buf)) {
  419. return -1;
  420. }
  421. #else /* __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ */
  422. int fd = open(filename, O_RDONLY);
  423. if (fd < 0) {
  424. return -1;
  425. }
  426. rv = read(fd, buf, sizeof(buf));
  427. if (rv != sizeof(buf)) {
  428. close(fd);
  429. return -1;
  430. }
  431. #endif /* __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ */
  432. if (!MACHO(buf)) {
  433. close(fd);
  434. return -1;
  435. }
  436. if (IS64(buf)) {
  437. is64 = 4;
  438. }
  439. q = buf + sizeof(struct mach_header) + is64;
  440. for (i = 0; i < hdr->ncmds; i++) {
  441. const struct load_command *cmd = (struct load_command *)q;
  442. if (cmd->cmd == LC_SEGMENT_64) {
  443. const struct segment_command_64 *seg = (struct segment_command_64 *)q;
  444. if (min > seg->vmaddr) {
  445. min = seg->vmaddr;
  446. }
  447. if (max < seg->vmaddr + seg->vmsize) {
  448. max = seg->vmaddr + seg->vmsize;
  449. }
  450. if (!strcmp(seg->segname, "__TEXT_EXEC")) {
  451. xnucore_base = seg->vmaddr;
  452. xnucore_size = seg->filesize;
  453. }
  454. if (!strcmp(seg->segname, "__PLK_TEXT_EXEC")) {
  455. prelink_base = seg->vmaddr;
  456. prelink_size = seg->filesize;
  457. }
  458. if (!strcmp(seg->segname, "__TEXT")) {
  459. const struct section_64 *sec = (struct section_64 *)(seg + 1);
  460. for (j = 0; j < seg->nsects; j++) {
  461. if (!strcmp(sec[j].sectname, "__cstring")) {
  462. cstring_base = sec[j].addr;
  463. cstring_size = sec[j].size;
  464. }
  465. }
  466. }
  467. if (!strcmp(seg->segname, "__PRELINK_TEXT")) {
  468. const struct section_64 *sec = (struct section_64 *)(seg + 1);
  469. for (j = 0; j < seg->nsects; j++) {
  470. if (!strcmp(sec[j].sectname, "__text")) {
  471. pstring_base = sec[j].addr;
  472. pstring_size = sec[j].size;
  473. }
  474. }
  475. }
  476. if (!strcmp(seg->segname, "__LINKEDIT")) {
  477. kernel_delta = seg->vmaddr - min - seg->fileoff;
  478. }
  479. }
  480. if (cmd->cmd == LC_UNIXTHREAD) {
  481. uint32_t *ptr = (uint32_t *)(cmd + 1);
  482. uint32_t flavor = ptr[0];
  483. struct {
  484. uint64_t x[29]; /* General purpose registers x0-x28 */
  485. uint64_t fp; /* Frame pointer x29 */
  486. uint64_t lr; /* Link register x30 */
  487. uint64_t sp; /* Stack pointer x31 */
  488. uint64_t pc; /* Program counter */
  489. uint32_t cpsr; /* Current program status register */
  490. } *thread = (void *)(ptr + 2);
  491. if (flavor == 6) {
  492. kernel_entry = thread->pc;
  493. }
  494. }
  495. q = q + cmd->cmdsize;
  496. }
  497. kerndumpbase = min;
  498. xnucore_base -= kerndumpbase;
  499. prelink_base -= kerndumpbase;
  500. cstring_base -= kerndumpbase;
  501. pstring_base -= kerndumpbase;
  502. kernel_size = max - min;
  503. #ifdef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
  504. kernel = malloc(kernel_size);
  505. if (!kernel) {
  506. return -1;
  507. }
  508. rv = kread(kerndumpbase, kernel, kernel_size);
  509. if (rv != kernel_size) {
  510. free(kernel);
  511. return -1;
  512. }
  513. kernel_mh = kernel + base - min;
  514. (void)filename;
  515. #undef close
  516. #else /* __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ */
  517. kernel = calloc(1, kernel_size);
  518. if (!kernel) {
  519. close(fd);
  520. return -1;
  521. }
  522. q = buf + sizeof(struct mach_header) + is64;
  523. for (i = 0; i < hdr->ncmds; i++) {
  524. const struct load_command *cmd = (struct load_command *)q;
  525. if (cmd->cmd == LC_SEGMENT_64) {
  526. const struct segment_command_64 *seg = (struct segment_command_64 *)q;
  527. size_t sz = pread(fd, kernel + seg->vmaddr - min, seg->filesize, seg->fileoff);
  528. if (sz != seg->filesize) {
  529. close(fd);
  530. free(kernel);
  531. return -1;
  532. }
  533. if (!kernel_mh) {
  534. kernel_mh = kernel + seg->vmaddr - min;
  535. }
  536. if (!strcmp(seg->segname, "__LINKEDIT")) {
  537. kernel_delta = seg->vmaddr - min - seg->fileoff;
  538. }
  539. }
  540. q = q + cmd->cmdsize;
  541. }
  542. close(fd);
  543. (void)base;
  544. #endif /* __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ */
  545. return 0;
  546. }
  547. void
  548. term_kernel(void)
  549. {
  550. free(kernel);
  551. }
  552. /* these operate on VA ******************************************************/
  553. #define INSN_RET 0xD65F03C0, 0xFFFFFFFF
  554. #define INSN_CALL 0x94000000, 0xFC000000
  555. #define INSN_B 0x14000000, 0xFC000000
  556. #define INSN_CBZ 0x34000000, 0xFC000000
  557. #define INSN_ADRP 0x90000000, 0x9F000000
  558. addr_t
  559. find_register_value(addr_t where, int reg)
  560. {
  561. addr_t val;
  562. addr_t bof = 0;
  563. where -= kerndumpbase;
  564. if (where > xnucore_base) {
  565. bof = bof64(kernel, xnucore_base, where);
  566. if (!bof) {
  567. bof = xnucore_base;
  568. }
  569. } else if (where > prelink_base) {
  570. bof = bof64(kernel, prelink_base, where);
  571. if (!bof) {
  572. bof = prelink_base;
  573. }
  574. }
  575. val = calc64(kernel, bof, where, reg);
  576. if (!val) {
  577. return 0;
  578. }
  579. return val + kerndumpbase;
  580. }
  581. addr_t
  582. find_reference(addr_t to, int n, int prelink)
  583. {
  584. addr_t ref, end;
  585. addr_t base = xnucore_base;
  586. addr_t size = xnucore_size;
  587. if (prelink) {
  588. base = prelink_base;
  589. size = prelink_size;
  590. }
  591. if (n <= 0) {
  592. n = 1;
  593. }
  594. end = base + size;
  595. to -= kerndumpbase;
  596. do {
  597. ref = xref64(kernel, base, end, to);
  598. if (!ref) {
  599. return 0;
  600. }
  601. base = ref + 4;
  602. } while (--n > 0);
  603. return ref + kerndumpbase;
  604. }
  605. addr_t
  606. find_strref(const char *string, int n, int prelink)
  607. {
  608. uint8_t *str;
  609. addr_t base = cstring_base;
  610. addr_t size = cstring_size;
  611. if (prelink) {
  612. base = pstring_base;
  613. size = pstring_size;
  614. }
  615. str = boyermoore_horspool_memmem(kernel + base, size, (uint8_t *)string, strlen(string));
  616. if (!str) {
  617. return 0;
  618. }
  619. return find_reference(str - kernel + kerndumpbase, n, prelink);
  620. }
  621. /****** fun *******/
  622. CACHED_FIND_UINT64(find_add_x0_x0_0x40_ret) {
  623. addr_t off;
  624. uint32_t *k;
  625. k = (uint32_t *)(kernel + xnucore_base);
  626. for (off = 0; off < xnucore_size - 4; off += 4, k++) {
  627. if (k[0] == 0x91010000 && k[1] == 0xD65F03C0) {
  628. return off + xnucore_base + kerndumpbase;
  629. }
  630. }
  631. k = (uint32_t *)(kernel + prelink_base);
  632. for (off = 0; off < prelink_size - 4; off += 4, k++) {
  633. if (k[0] == 0x91010000 && k[1] == 0xD65F03C0) {
  634. return off + prelink_base + kerndumpbase;
  635. }
  636. }
  637. return 0;
  638. }
  639. CACHED_FIND_UINT64(find_OSBoolean_True) {
  640. addr_t val;
  641. addr_t ref = find_strref("Delay Autounload", 0, 0);
  642. if (!ref) {
  643. return 0;
  644. }
  645. ref -= kerndumpbase;
  646. addr_t weird_instruction = 0;
  647. for (int i = 4; i < 4*0x100; i+=4) {
  648. uint32_t op = *(uint32_t *)(kernel + ref + i);
  649. if (op == 0x320003E0) {
  650. weird_instruction = ref+i;
  651. break;
  652. }
  653. }
  654. if (!weird_instruction) {
  655. return 0;
  656. }
  657. val = calc64(kernel, ref, weird_instruction, 8);
  658. if (!val) {
  659. return 0;
  660. }
  661. return rk64(val + kerndumpbase);
  662. }
  663. CACHED_FIND_UINT64(find_OSBoolean_False) {
  664. return find_OSBoolean_True() + 8;
  665. }
  666. CACHED_FIND_UINT64(find_osunserializexml) {
  667. addr_t ref = find_strref("OSUnserializeXML: %s near line %d\n", 1, 0);
  668. ref -= kerndumpbase;
  669. uint64_t start = bof64(kernel, xnucore_base, ref);
  670. return start + kerndumpbase;
  671. }
  672. CACHED_FIND_UINT64(find_smalloc) {
  673. addr_t ref = find_strref("sandbox memory allocation failure", 1, 1);
  674. ref -= kerndumpbase;
  675. uint64_t start = bof64(kernel, prelink_base, ref);
  676. return start + kerndumpbase;
  677. }