patchfinder64.m 25 KB

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