-
Notifications
You must be signed in to change notification settings - Fork 57
/
patchfinder64.c
1572 lines (1457 loc) · 41.4 KB
/
patchfinder64.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// patchfinder64.c
// extra_recipe
//
// Created by xerub on 06/06/2017.
// Copyright © 2017 xerub. All rights reserved.
//
#include <assert.h>
#include <stdint.h>
#include <string.h>
typedef unsigned long long addr_t;
#define IS64(image) (*(uint8_t *)(image) & 1)
#define MACHO(p) ((*(unsigned int *)(p) & ~1) == 0xfeedface)
/* generic stuff *************************************************************/
#define UCHAR_MAX 255
static unsigned char *
boyermoore_horspool_memmem(const unsigned char* haystack, size_t hlen,
const unsigned char* needle, size_t nlen)
{
size_t last, scan = 0;
size_t bad_char_skip[UCHAR_MAX + 1]; /* Officially called:
* bad character shift */
/* Sanity checks on the parameters */
if (nlen <= 0 || !haystack || !needle)
return NULL;
/* ---- Preprocess ---- */
/* Initialize the table to default value */
/* When a character is encountered that does not occur
* in the needle, we can safely skip ahead for the whole
* length of the needle.
*/
for (scan = 0; scan <= UCHAR_MAX; scan = scan + 1)
bad_char_skip[scan] = nlen;
/* C arrays have the first byte at [0], therefore:
* [nlen - 1] is the last byte of the array. */
last = nlen - 1;
/* Then populate it with the analysis of the needle */
for (scan = 0; scan < last; scan = scan + 1)
bad_char_skip[needle[scan]] = last - scan;
/* ---- Do the matching ---- */
/* Search the haystack, while the needle can still be within it. */
while (hlen >= nlen)
{
/* scan from the end of the needle */
for (scan = last; haystack[scan] == needle[scan]; scan = scan - 1)
if (scan == 0) /* If the first byte matches, we've found it. */
return (void *)haystack;
/* otherwise, we need to skip some bytes and start again.
Note that here we are getting the skip value based on the last byte
of needle, no matter where we didn't match. So if needle is: "abcd"
then we are skipping based on 'd' and that value will be 4, and
for "abcdd" we again skip on 'd' but the value will be only 1.
The alternative of pretending that the mismatched character was
the last character is slower in the normal case (E.g. finding
"abcd" in "...azcd..." gives 4 by using 'd' but only
4-2==2 using 'z'. */
hlen -= bad_char_skip[haystack[last]];
haystack += bad_char_skip[haystack[last]];
}
return NULL;
}
/* disassembler **************************************************************/
static int HighestSetBit(int N, uint32_t imm)
{
int i;
for (i = N - 1; i >= 0; i--) {
if (imm & (1 << i)) {
return i;
}
}
return -1;
}
static uint64_t ZeroExtendOnes(unsigned M, unsigned N) // zero extend M ones to N width
{
(void)N;
return ((uint64_t)1 << M) - 1;
}
static uint64_t RORZeroExtendOnes(unsigned M, unsigned N, unsigned R)
{
uint64_t val = ZeroExtendOnes(M, N);
if (R == 0) {
return val;
}
return ((val >> R) & (((uint64_t)1 << (N - R)) - 1)) | ((val & (((uint64_t)1 << R) - 1)) << (N - R));
}
static uint64_t Replicate(uint64_t val, unsigned bits)
{
uint64_t ret = val;
unsigned shift;
for (shift = bits; shift < 64; shift += bits) { // XXX actually, it is either 32 or 64
ret |= (val << shift);
}
return ret;
}
static int DecodeBitMasks(unsigned immN, unsigned imms, unsigned immr, int immediate, uint64_t *newval)
{
unsigned levels, S, R, esize;
int len = HighestSetBit(7, (immN << 6) | (~imms & 0x3F));
if (len < 1) {
return -1;
}
levels = ZeroExtendOnes(len, 6);
if (immediate && (imms & levels) == levels) {
return -1;
}
S = imms & levels;
R = immr & levels;
esize = 1 << len;
*newval = Replicate(RORZeroExtendOnes(S + 1, esize, R), esize);
return 0;
}
static int DecodeMov(uint32_t opcode, uint64_t total, int first, uint64_t *newval)
{
unsigned o = (opcode >> 29) & 3;
unsigned k = (opcode >> 23) & 0x3F;
unsigned rn, rd;
uint64_t i;
if (k == 0x24 && o == 1) { // MOV (bitmask imm) <=> ORR (immediate)
unsigned s = (opcode >> 31) & 1;
unsigned N = (opcode >> 22) & 1;
if (s == 0 && N != 0) {
return -1;
}
rn = (opcode >> 5) & 0x1F;
if (rn == 31) {
unsigned imms = (opcode >> 10) & 0x3F;
unsigned immr = (opcode >> 16) & 0x3F;
return DecodeBitMasks(N, imms, immr, 1, newval);
}
} else if (k == 0x25) { // MOVN/MOVZ/MOVK
unsigned s = (opcode >> 31) & 1;
unsigned h = (opcode >> 21) & 3;
if (s == 0 && h > 1) {
return -1;
}
i = (opcode >> 5) & 0xFFFF;
h *= 16;
i <<= h;
if (o == 0) { // MOVN
*newval = ~i;
return 0;
} else if (o == 2) { // MOVZ
*newval = i;
return 0;
} else if (o == 3 && !first) { // MOVK
*newval = (total & ~((uint64_t)0xFFFF << h)) | i;
return 0;
}
} else if ((k | 1) == 0x23 && !first) { // ADD (immediate)
unsigned h = (opcode >> 22) & 3;
if (h > 1) {
return -1;
}
rd = opcode & 0x1F;
rn = (opcode >> 5) & 0x1F;
if (rd != rn) {
return -1;
}
i = (opcode >> 10) & 0xFFF;
h *= 12;
i <<= h;
if (o & 2) { // SUB
*newval = total - i;
return 0;
} else { // ADD
*newval = total + i;
return 0;
}
}
return -1;
}
/* patchfinder ***************************************************************/
static addr_t
step64(const uint8_t *buf, addr_t start, size_t length, uint32_t what, uint32_t mask)
{
addr_t end = start + length;
while (start < end) {
uint32_t x = *(uint32_t *)(buf + start);
if ((x & mask) == what) {
return start;
}
start += 4;
}
return 0;
}
static addr_t
step64_back(const uint8_t *buf, addr_t start, size_t length, uint32_t what, uint32_t mask)
{
addr_t end = start - length;
while (start >= end) {
uint32_t x = *(uint32_t *)(buf + start);
if ((x & mask) == what) {
return start;
}
start -= 4;
}
return 0;
}
static addr_t
bof64(const uint8_t *buf, addr_t start, addr_t where)
{
for (; where >= start; where -= 4) {
uint32_t op = *(uint32_t *)(buf + where);
if ((op & 0xFFC003FF) == 0x910003FD) {
unsigned delta = (op >> 10) & 0xFFF;
//printf("%x: ADD X29, SP, #0x%x\n", where, delta);
if ((delta & 0xF) == 0) {
addr_t prev = where - ((delta >> 4) + 1) * 4;
uint32_t au = *(uint32_t *)(buf + prev);
if ((au & 0xFFC003E0) == 0xA98003E0) {
//printf("%x: STP x, y, [SP,#-imm]!\n", prev);
return prev;
}
// try something else
while (where > start) {
where -= 4;
au = *(uint32_t *)(buf + where);
// SUB SP, SP, #imm
if ((au & 0xFFC003FF) == 0xD10003FF && ((au >> 10) & 0xFFF) == delta + 0x10) {
return where;
}
// STP x, y, [SP,#imm]
if ((au & 0xFFC003E0) != 0xA90003E0) {
where += 4;
break;
}
}
}
}
}
return 0;
}
static addr_t
xref64(const uint8_t *buf, addr_t start, addr_t end, addr_t what)
{
addr_t i;
uint64_t value[32];
memset(value, 0, sizeof(value));
end &= ~3;
for (i = start & ~3; i < end; i += 4) {
uint32_t op = *(uint32_t *)(buf + i);
unsigned reg = op & 0x1F;
if ((op & 0x9F000000) == 0x90000000) {
signed adr = ((op & 0x60000000) >> 18) | ((op & 0xFFFFE0) << 8);
//printf("%llx: ADRP X%d, 0x%llx\n", i, reg, ((long long)adr << 1) + (i & ~0xFFF));
value[reg] = ((long long)adr << 1) + (i & ~0xFFF);
continue; // XXX should not XREF on its own?
/*} else if ((op & 0xFFE0FFE0) == 0xAA0003E0) {
unsigned rd = op & 0x1F;
unsigned rm = (op >> 16) & 0x1F;
//printf("%llx: MOV X%d, X%d\n", i, rd, rm);
value[rd] = value[rm];*/
} else if ((op & 0xFF000000) == 0x91000000) {
unsigned rn = (op >> 5) & 0x1F;
unsigned shift = (op >> 22) & 3;
unsigned imm = (op >> 10) & 0xFFF;
if (shift == 1) {
imm <<= 12;
} else {
//assert(shift == 0);
if (shift > 1) continue;
}
//printf("%llx: ADD X%d, X%d, 0x%x\n", i, reg, rn, imm);
value[reg] = value[rn] + imm;
} else if ((op & 0xF9C00000) == 0xF9400000) {
unsigned rn = (op >> 5) & 0x1F;
unsigned imm = ((op >> 10) & 0xFFF) << 3;
//printf("%llx: LDR X%d, [X%d, 0x%x]\n", i, reg, rn, imm);
if (!imm) continue; // XXX not counted as true xref
value[reg] = value[rn] + imm; // XXX address, not actual value
/*} else if ((op & 0xF9C00000) == 0xF9000000) {
unsigned rn = (op >> 5) & 0x1F;
unsigned imm = ((op >> 10) & 0xFFF) << 3;
//printf("%llx: STR X%d, [X%d, 0x%x]\n", i, reg, rn, imm);
if (!imm) continue; // XXX not counted as true xref
value[rn] = value[rn] + imm; // XXX address, not actual value*/
} else if ((op & 0x9F000000) == 0x10000000) {
signed adr = ((op & 0x60000000) >> 18) | ((op & 0xFFFFE0) << 8);
//printf("%llx: ADR X%d, 0x%llx\n", i, reg, ((long long)adr >> 11) + i);
value[reg] = ((long long)adr >> 11) + i;
} else if ((op & 0xFF000000) == 0x58000000) {
unsigned adr = (op & 0xFFFFE0) >> 3;
//printf("%llx: LDR X%d, =0x%llx\n", i, reg, adr + i);
value[reg] = adr + i; // XXX address, not actual value
}
if (value[reg] == what) {
return i;
}
}
return 0;
}
static addr_t
calc64(const uint8_t *buf, addr_t start, addr_t end, int which)
{
addr_t i;
uint64_t value[32];
memset(value, 0, sizeof(value));
end &= ~3;
for (i = start & ~3; i < end; i += 4) {
uint32_t op = *(uint32_t *)(buf + i);
unsigned reg = op & 0x1F;
if ((op & 0x9F000000) == 0x90000000) {
signed adr = ((op & 0x60000000) >> 18) | ((op & 0xFFFFE0) << 8);
//printf("%llx: ADRP X%d, 0x%llx\n", i, reg, ((long long)adr << 1) + (i & ~0xFFF));
value[reg] = ((long long)adr << 1) + (i & ~0xFFF);
/*} else if ((op & 0xFFE0FFE0) == 0xAA0003E0) {
unsigned rd = op & 0x1F;
unsigned rm = (op >> 16) & 0x1F;
//printf("%llx: MOV X%d, X%d\n", i, rd, rm);
value[rd] = value[rm];*/
} else if ((op & 0xFF000000) == 0x91000000) {
unsigned rn = (op >> 5) & 0x1F;
unsigned shift = (op >> 22) & 3;
unsigned imm = (op >> 10) & 0xFFF;
if (shift == 1) {
imm <<= 12;
} else {
//assert(shift == 0);
if (shift > 1) continue;
}
//printf("%llx: ADD X%d, X%d, 0x%x\n", i, reg, rn, imm);
value[reg] = value[rn] + imm;
} else if ((op & 0xF9C00000) == 0xF9400000) {
unsigned rn = (op >> 5) & 0x1F;
unsigned imm = ((op >> 10) & 0xFFF) << 3;
//printf("%llx: LDR X%d, [X%d, 0x%x]\n", i, reg, rn, imm);
if (!imm) continue; // XXX not counted as true xref
value[reg] = value[rn] + imm; // XXX address, not actual value
} else if ((op & 0xF9C00000) == 0xF9000000) {
unsigned rn = (op >> 5) & 0x1F;
unsigned imm = ((op >> 10) & 0xFFF) << 3;
//printf("%llx: STR X%d, [X%d, 0x%x]\n", i, reg, rn, imm);
if (!imm) continue; // XXX not counted as true xref
value[rn] = value[rn] + imm; // XXX address, not actual value
} else if ((op & 0x9F000000) == 0x10000000) {
signed adr = ((op & 0x60000000) >> 18) | ((op & 0xFFFFE0) << 8);
//printf("%llx: ADR X%d, 0x%llx\n", i, reg, ((long long)adr >> 11) + i);
value[reg] = ((long long)adr >> 11) + i;
} else if ((op & 0xFF000000) == 0x58000000) {
unsigned adr = (op & 0xFFFFE0) >> 3;
//printf("%llx: LDR X%d, =0x%llx\n", i, reg, adr + i);
value[reg] = adr + i; // XXX address, not actual value
}
}
return value[which];
}
static addr_t
calc64mov(const uint8_t *buf, addr_t start, addr_t end, int which)
{
addr_t i;
uint64_t value[32];
memset(value, 0, sizeof(value));
end &= ~3;
for (i = start & ~3; i < end; i += 4) {
uint32_t op = *(uint32_t *)(buf + i);
unsigned reg = op & 0x1F;
uint64_t newval;
int rv = DecodeMov(op, value[reg], 0, &newval);
if (rv == 0) {
if (((op >> 31) & 1) == 0) {
newval &= 0xFFFFFFFF;
}
value[reg] = newval;
}
}
return value[which];
}
static addr_t
find_call64(const uint8_t *buf, addr_t start, size_t length)
{
return step64(buf, start, length, 0x94000000, 0xFC000000);
}
static addr_t
follow_call64(const uint8_t *buf, addr_t call)
{
long long w;
w = *(uint32_t *)(buf + call) & 0x3FFFFFF;
w <<= 64 - 26;
w >>= 64 - 26 - 2;
return call + w;
}
static addr_t
follow_cbz(const uint8_t *buf, addr_t cbz)
{
return cbz + ((*(int *)(buf + cbz) & 0x3FFFFE0) << 10 >> 13);
}
static addr_t
xref64code(const uint8_t *buf, addr_t start, addr_t end, addr_t what)
{
addr_t i;
end &= ~3;
for (i = start & ~3; i < end; i += 4) {
uint32_t op = *(uint32_t *)(buf + i);
if ((op & 0x7C000000) == 0x14000000) {
addr_t where = follow_call64(buf, i);
//printf("%llx: B[L] 0x%llx\n", i + kerndumpbase, kerndumpbase + where);
if (where == what) {
return i;
}
}
}
return 0;
}
/* kernel iOS10 **************************************************************/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <mach-o/loader.h>
//#include "vfs.h" // img4lib
#ifdef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
#include <mach/mach.h>
size_t kread(uint64_t where, void *p, size_t size);
#endif
#ifdef VFS_H_included
#define INVALID_HANDLE NULL
static FHANDLE
OPEN(const char *filename, int oflag)
{
ssize_t rv;
char buf[28];
FHANDLE fd = file_open(filename, oflag);
if (!fd) {
return NULL;
}
rv = fd->read(fd, buf, 4);
fd->lseek(fd, 0, SEEK_SET);
if (rv == 4 && !MACHO(buf)) {
fd = img4_reopen(fd, NULL, 0);
if (!fd) {
return NULL;
}
rv = fd->read(fd, buf, sizeof(buf));
if (rv == sizeof(buf) && *(uint32_t *)buf == 0xBEBAFECA && __builtin_bswap32(*(uint32_t *)(buf + 4)) > 0) {
return sub_reopen(fd, __builtin_bswap32(*(uint32_t *)(buf + 16)), __builtin_bswap32(*(uint32_t *)(buf + 20)));
}
fd->lseek(fd, 0, SEEK_SET);
}
return fd;
}
#define CLOSE(fd) (fd)->close(fd)
#define READ(fd, buf, sz) (fd)->read(fd, buf, sz)
static ssize_t
PREAD(FHANDLE fd, void *buf, size_t count, off_t offset)
{
ssize_t rv;
//off_t pos = fd->lseek(FHANDLE fd, 0, SEEK_CUR);
fd->lseek(fd, offset, SEEK_SET);
rv = fd->read(fd, buf, count);
//fd->lseek(FHANDLE fd, pos, SEEK_SET);
return rv;
}
#else
#define FHANDLE int
#define INVALID_HANDLE -1
#define OPEN open
#define CLOSE close
#define READ read
#define PREAD pread
#endif
static uint8_t *kernel = NULL;
static int kernel_version = 0;
static size_t kernel_size = 0;
static addr_t xnucore_base = 0;
static addr_t xnucore_size = 0;
static addr_t prelink_base = 0;
static addr_t prelink_size = 0;
static addr_t pplcode_base = 0;
static addr_t pplcode_size = 0;
static addr_t cstring_base = 0;
static addr_t cstring_size = 0;
static addr_t pstring_base = 0;
static addr_t pstring_size = 0;
static addr_t kerndumpbase = -1;
static addr_t kernel_entry = 0;
static void *kernel_mh = 0;
static addr_t kernel_delta = 0;
int
init_kernel(addr_t base, const char *filename)
{
size_t rv;
uint8_t buf[0x4000];
uint8_t *vstr;
unsigned i, j;
const struct mach_header *hdr = (struct mach_header *)buf;
FHANDLE fd = INVALID_HANDLE;
const uint8_t *q;
addr_t min = -1;
addr_t max = 0;
int is64 = 0;
if (filename == NULL) {
#ifdef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
rv = kread(base, buf, sizeof(buf));
if (rv != sizeof(buf) || !MACHO(buf)) {
return -1;
}
#else
(void)base;
return -1;
#endif
} else {
fd = OPEN(filename, O_RDONLY);
if (fd == INVALID_HANDLE) {
return -1;
}
rv = READ(fd, buf, sizeof(buf));
if (rv != sizeof(buf) || !MACHO(buf)) {
CLOSE(fd);
return -1;
}
}
if (IS64(buf)) {
is64 = 4;
}
q = buf + sizeof(struct mach_header) + is64;
for (i = 0; i < hdr->ncmds; i++) {
const struct load_command *cmd = (struct load_command *)q;
if (cmd->cmd == LC_SEGMENT_64 && ((struct segment_command_64 *)q)->vmsize) {
const struct segment_command_64 *seg = (struct segment_command_64 *)q;
if (min > seg->vmaddr) {
min = seg->vmaddr;
}
if (max < seg->vmaddr + seg->vmsize) {
max = seg->vmaddr + seg->vmsize;
}
if (!strcmp(seg->segname, "__TEXT_EXEC")) {
xnucore_base = seg->vmaddr;
xnucore_size = seg->filesize;
}
if (!strcmp(seg->segname, "__PLK_TEXT_EXEC")) {
prelink_base = seg->vmaddr;
prelink_size = seg->filesize;
}
if (!strcmp(seg->segname, "__PPLTEXT")) {
pplcode_base = seg->vmaddr;
pplcode_size = seg->filesize;
}
if (!strcmp(seg->segname, "__TEXT")) {
const struct section_64 *sec = (struct section_64 *)(seg + 1);
for (j = 0; j < seg->nsects; j++) {
if (!strcmp(sec[j].sectname, "__cstring")) {
cstring_base = sec[j].addr;
cstring_size = sec[j].size;
}
}
}
if (!strcmp(seg->segname, "__PRELINK_TEXT")) {
const struct section_64 *sec = (struct section_64 *)(seg + 1);
for (j = 0; j < seg->nsects; j++) {
if (!strcmp(sec[j].sectname, "__text")) {
pstring_base = sec[j].addr;
pstring_size = sec[j].size;
}
}
}
}
if (cmd->cmd == LC_UNIXTHREAD) {
uint32_t *ptr = (uint32_t *)(cmd + 1);
uint32_t flavor = ptr[0];
struct {
uint64_t x[29]; /* General purpose registers x0-x28 */
uint64_t fp; /* Frame pointer x29 */
uint64_t lr; /* Link register x30 */
uint64_t sp; /* Stack pointer x31 */
uint64_t pc; /* Program counter */
uint32_t cpsr; /* Current program status register */
} *thread = (void *)(ptr + 2);
if (flavor == 6) {
kernel_entry = thread->pc;
}
}
q = q + cmd->cmdsize;
}
if (pstring_base == 0 && pstring_size == 0) {
pstring_base = cstring_base;
pstring_size = cstring_size;
}
if (prelink_base == 0 && prelink_size == 0) {
prelink_base = xnucore_base;
prelink_size = xnucore_size;
}
kerndumpbase = min;
xnucore_base -= kerndumpbase;
prelink_base -= kerndumpbase;
pplcode_base -= kerndumpbase;
cstring_base -= kerndumpbase;
pstring_base -= kerndumpbase;
kernel_size = max - min;
if (filename == NULL) {
#ifdef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
kernel = malloc(kernel_size);
if (!kernel) {
return -1;
}
rv = kread(kerndumpbase, kernel, kernel_size);
if (rv != kernel_size) {
free(kernel);
kernel = NULL;
return -1;
}
kernel_mh = kernel + base - min;
#endif
} else {
kernel = calloc(1, kernel_size);
if (!kernel) {
CLOSE(fd);
return -1;
}
q = buf + sizeof(struct mach_header) + is64;
for (i = 0; i < hdr->ncmds; i++) {
const struct load_command *cmd = (struct load_command *)q;
if (cmd->cmd == LC_SEGMENT_64) {
const struct segment_command_64 *seg = (struct segment_command_64 *)q;
size_t sz = PREAD(fd, kernel + seg->vmaddr - min, seg->filesize, seg->fileoff);
if (sz != seg->filesize) {
CLOSE(fd);
free(kernel);
kernel = NULL;
return -1;
}
if (!kernel_mh) {
kernel_mh = kernel + seg->vmaddr - min;
}
if (!strcmp(seg->segname, "__LINKEDIT")) {
kernel_delta = seg->vmaddr - min - seg->fileoff;
}
}
q = q + cmd->cmdsize;
}
CLOSE(fd);
}
vstr = boyermoore_horspool_memmem(kernel, kernel_size, (uint8_t *)"Darwin Kernel Version", sizeof("Darwin Kernel Version") - 1);
if (vstr) {
kernel_version = atoi((const char *)vstr + sizeof("Darwin Kernel Version"));
}
return 0;
}
void
term_kernel(void)
{
free(kernel);
}
/* these operate on VA ******************************************************/
#define INSN_RETAB 0xD65F0FFF, 0xFFFFFFFF
#define INSN_RET 0xD65F03C0, 0xFFFFFFFF
#define INSN_CALL 0x94000000, 0xFC000000
#define INSN_B 0x14000000, 0xFC000000
#define INSN_CBZ 0x34000000, 0xFC000000
#define INSN_BLR 0xD63F0000, 0xFFFFFC1F
addr_t
find_register_value(addr_t where, int reg)
{
addr_t val;
addr_t bof = 0;
where -= kerndumpbase;
if (where > xnucore_base) {
bof = bof64(kernel, xnucore_base, where);
if (!bof) {
bof = xnucore_base;
}
} else if (where > prelink_base) {
bof = bof64(kernel, prelink_base, where);
if (!bof) {
bof = prelink_base;
}
}
val = calc64(kernel, bof, where, reg);
if (!val) {
return 0;
}
return val + kerndumpbase;
}
addr_t
find_reference(addr_t to, int n, int where)
{
addr_t ref, end;
addr_t base = xnucore_base;
addr_t size = xnucore_size;
switch (where) {
case 1:
base = prelink_base;
size = prelink_size;
break;
case 2:
base = pplcode_base;
size = pplcode_size;
break;
}
if (n <= 0) {
n = 1;
}
end = base + size;
to -= kerndumpbase;
do {
ref = xref64(kernel, base, end, to);
if (!ref) {
return 0;
}
base = ref + 4;
} while (--n > 0);
return ref + kerndumpbase;
}
addr_t
find_strref(const char *string, int n, int where)
{
uint8_t *str;
addr_t base = cstring_base;
addr_t size = cstring_size;
switch (where) {
case 1:
base = pstring_base;
size = pstring_size;
break;
}
str = boyermoore_horspool_memmem(kernel + base, size, (uint8_t *)string, strlen(string));
if (!str) {
return 0;
}
return find_reference(str - kernel + kerndumpbase, n, where);
}
addr_t
find_gPhysBase(void)
{
addr_t ret, val;
addr_t ref = find_strref("pmap_alloc_page_for_kern", 1, 0);
if (!ref) {
return 0;
}
ref -= kerndumpbase;
if (kernel_version >= 18) {
// A12
ret = step64(kernel, ref, 384, INSN_RETAB);
if (!ret) {
ret = step64(kernel, ref, 384, INSN_RET);
}
} else {
ret = step64(kernel, ref, 64, INSN_RET);
}
if (!ret) {
// iOS 11
ref = step64(kernel, ref, 1024, INSN_RET);
if (!ref) {
return 0;
}
ret = step64(kernel, ref + 4, 64, INSN_RET);
if (!ret) {
return 0;
}
}
if (kernel_version >= 18) {
val = calc64(kernel, ref, ret, 9);
} else {
val = calc64(kernel, ref, ret, 8);
}
if (!val) {
return 0;
}
return val + kerndumpbase;
}
addr_t
find_ptov_table(void)
{
addr_t bof, val;
addr_t ref = find_strref("\"ml_static_vtop(): illegal VA:", 1, 0);
if (!ref) {
return 0;
}
ref -= kerndumpbase;
bof = bof64(kernel, xnucore_base, ref);
if (!bof) {
return 0;
}
val = calc64(kernel, bof, bof + 48, 8);
if (!val) {
return 0;
}
return val + kerndumpbase;
}
addr_t
find_kernel_pmap(void)
{
addr_t call, bof, val;
addr_t ref = find_strref("\"pmap_map_bd\"", 1, 0);
if (!ref) {
return 0;
}
ref -= kerndumpbase;
call = step64_back(kernel, ref, 64, INSN_CALL);
if (!call) {
return 0;
}
bof = bof64(kernel, xnucore_base, call);
if (!bof) {
return 0;
}
if (kernel_version == 18) {
// iOS 12
val = calc64(kernel, bof, call, 8);
} else {
val = calc64(kernel, bof, call, 2);
}
if (!val) {
return 0;
}
return val + kerndumpbase;
}
addr_t
find_amfiret(void)
{
addr_t ret;
addr_t ref = find_strref("AMFI: hook..execve() killing pid %u: %s\n", 1, 1);
if (!ref) {
return 0;
}
ref -= kerndumpbase;
ret = step64(kernel, ref, 512, INSN_RET);
if (!ret) {
return 0;
}
return ret + kerndumpbase;
}
addr_t
find_ret_0(void)
{
addr_t off;
uint32_t *k;
k = (uint32_t *)(kernel + xnucore_base);
for (off = 0; off < xnucore_size - 4; off += 4, k++) {
if (k[0] == 0xAA1F03E0 && k[1] == 0xD65F03C0) {
return off + xnucore_base + kerndumpbase;
}
}
k = (uint32_t *)(kernel + prelink_base);
for (off = 0; off < prelink_size - 4; off += 4, k++) {
if (k[0] == 0xAA1F03E0 && k[1] == 0xD65F03C0) {
return off + prelink_base + kerndumpbase;
}
}
return 0;
}
addr_t
find_amfi_memcmpstub(void)
{
addr_t call, dest, reg;
addr_t ref = find_strref("%s: Possible race detected. Rejecting.", 1, 1);
if (!ref) {
return 0;
}
ref -= kerndumpbase;
call = step64_back(kernel, ref, 64, INSN_CALL);
if (!call) {
return 0;
}
dest = follow_call64(kernel, call);
if (!dest) {
return 0;
}
reg = calc64(kernel, dest, dest + 8, 16);
if (!reg) {
return 0;
}
return reg + kerndumpbase;
}
addr_t
find_sbops(void)
{
addr_t off, what;
uint8_t *str = boyermoore_horspool_memmem(kernel + pstring_base, pstring_size, (uint8_t *)"Seatbelt sandbox policy", sizeof("Seatbelt sandbox policy") - 1);
if (!str) {
return 0;
}
what = str - kernel + kerndumpbase;
for (off = 0; off < kernel_size - prelink_base; off += 8) {
if (*(uint64_t *)(kernel + prelink_base + off) == what) {
return *(uint64_t *)(kernel + prelink_base + off + 24);
}
}
return 0;
}
addr_t
find_lwvm_mapio_patch(void)
{
addr_t call, dest, reg;
addr_t ref = find_strref("_mapForIO", 1, 1);
if (!ref) {
return 0;
}
ref -= kerndumpbase;
call = step64(kernel, ref, 64, INSN_CALL);
if (!call) {
return 0;
}
call = step64(kernel, call + 4, 64, INSN_CALL);
if (!call) {
return 0;
}
dest = follow_call64(kernel, call);
if (!dest) {
return 0;
}
reg = calc64(kernel, dest, dest + 8, 16);
if (!reg) {
return 0;
}
return reg + kerndumpbase;
}
addr_t
find_lwvm_mapio_newj(void)
{
addr_t call;
addr_t ref = find_strref("_mapForIO", 1, 1);
if (!ref) {
return 0;
}
ref -= kerndumpbase;
call = step64(kernel, ref, 64, INSN_CALL);
if (!call) {
return 0;
}
call = step64(kernel, call + 4, 64, INSN_CALL);
if (!call) {
return 0;
}
call = step64(kernel, call + 4, 64, INSN_CALL);
if (!call) {