This repository has been archived by the owner on Apr 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 177
/
main.m
722 lines (534 loc) · 20.2 KB
/
main.m
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
#include <stdio.h>
#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
#include <CommonCrypto/CommonCrypto.h>
#include <mach-o/loader.h>
#include <mach/mach.h>
#include <sys/syscall.h>
#include <sys/snapshot.h>
#include <dirent.h>
#include "vnode_utils.h"
uint64_t get_vnode_with_chdir(const char* path);
//---------maphys and vnodebypass----------//
//Thanks to 0x7ff
//original code : https://github.com/0x7ff/maphys/blob/master/maphys.c
//original code : https://github.com/XsF1re/vnodebypass/blob/master/main.m
#ifdef __arm64e__
# define CPU_DATA_RTCLOCK_DATAP_OFF (0x190)
#else
# define CPU_DATA_RTCLOCK_DATAP_OFF (0x198)
#endif
#define VM_KERNEL_LINK_ADDRESS (0xFFFFFFF007004000ULL)
#define kCFCoreFoundationVersionNumber_iOS_12_0 (1535.12)
#define kCFCoreFoundationVersionNumber_iOS_13_0_b2 (1656)
#define kCFCoreFoundationVersionNumber_iOS_13_0_b1 (1652.20)
#define KADDR_FMT "0x%" PRIX64
#define VM_KERN_MEMORY_CPU (9)
#define RD(a) extract32(a, 0, 5)
#define RN(a) extract32(a, 5, 5)
#define IS_RET(a) ((a) == 0xD65F03C0U)
#define ADRP_ADDR(a) ((a) & ~0xFFFULL)
#define ADRP_IMM(a) (ADR_IMM(a) << 12U)
#define IO_OBJECT_NULL ((io_object_t)0)
#define ADD_X_IMM(a) extract32(a, 10, 12)
#define LDR_X_IMM(a) (sextract64(a, 5, 19) << 2U)
#define IS_ADR(a) (((a) & 0x9F000000U) == 0x10000000U)
#define IS_ADRP(a) (((a) & 0x9F000000U) == 0x90000000U)
#define IS_ADD_X(a) (((a) & 0xFFC00000U) == 0x91000000U)
#define IS_LDR_X(a) (((a) & 0xFF000000U) == 0x58000000U)
#define LDR_X_UNSIGNED_IMM(a) (extract32(a, 10, 12) << 3U)
#define IS_LDR_X_UNSIGNED_IMM(a) (((a) & 0xFFC00000U) == 0xF9400000U)
#define ADR_IMM(a) ((sextract64(a, 5, 19) << 2U) | extract32(a, 29, 2))
#ifndef SEG_TEXT_EXEC
# define SEG_TEXT_EXEC "__TEXT_EXEC"
#endif
#ifndef SECT_CSTRING
# define SECT_CSTRING "__cstring"
#endif
#ifndef MIN
# define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
typedef uint64_t kaddr_t;
typedef mach_port_t io_object_t;
typedef io_object_t io_service_t;
typedef io_object_t io_connect_t;
typedef io_object_t io_registry_entry_t;
typedef struct {
struct section_64 s64;
char *data;
} sec_64_t;
typedef struct {
sec_64_t sec_text, sec_cstring;
} pfinder_t;
typedef struct {
kaddr_t key, val;
} dict_entry_t;
kern_return_t
mach_vm_write(vm_map_t, mach_vm_address_t, vm_offset_t, mach_msg_type_number_t);
kern_return_t
mach_vm_read_overwrite(vm_map_t, mach_vm_address_t, mach_vm_size_t, mach_vm_address_t, mach_vm_size_t *);
kern_return_t
mach_vm_machine_attribute(vm_map_t, mach_vm_address_t, mach_vm_size_t, vm_machine_attribute_t, vm_machine_attribute_val_t *);
kern_return_t
mach_vm_region(vm_map_t, mach_vm_address_t *, mach_vm_size_t *, vm_region_flavor_t, vm_region_info_t, mach_msg_type_number_t *, mach_port_t *);
extern const mach_port_t kIOMasterPortDefault;
static kaddr_t allproc, our_task;
static kaddr_t proc;
static task_t tfp0 = MACH_PORT_NULL;
static uint32_t
extract32(uint32_t val, unsigned start, unsigned len) {
return (val >> start) & (~0U >> (32U - len));
}
static uint64_t
sextract64(uint64_t val, unsigned start, unsigned len) {
return (uint64_t)((int64_t)(val << (64U - len - start)) >> (64U - len));
}
static kern_return_t
init_tfp0(void) {
kern_return_t ret = task_for_pid(mach_task_self(), 0, &tfp0);
mach_port_t host;
pid_t pid;
if(ret != KERN_SUCCESS) {
host = mach_host_self();
if(MACH_PORT_VALID(host)) {
printf("host: 0x%" PRIX32 "\n", host);
ret = host_get_special_port(host, HOST_LOCAL_NODE, 4, &tfp0);
printf("I think you're using unc0ver, But load it anyway.\n");
return ret; //TO USE UNC0VER, TEMPORARY
}
mach_port_deallocate(mach_task_self(), host);
}
if(ret == KERN_SUCCESS && MACH_PORT_VALID(tfp0)) {
if(pid_for_task(tfp0, &pid) == KERN_SUCCESS && pid == 0) {
return ret;
}
mach_port_deallocate(mach_task_self(), tfp0);
}
printf("Failed to init tfp0.\n");
return KERN_FAILURE;
}
static kern_return_t
kread_buf(kaddr_t addr, void *buf, mach_vm_size_t sz) {
mach_vm_address_t p = (mach_vm_address_t)buf;
mach_vm_size_t read_sz, out_sz = 0;
while(sz != 0) {
read_sz = MIN(sz, vm_kernel_page_size - (addr & vm_kernel_page_mask));
if(mach_vm_read_overwrite(tfp0, addr, read_sz, p, &out_sz) != KERN_SUCCESS || out_sz != read_sz) {
return KERN_FAILURE;
}
p += read_sz;
sz -= read_sz;
addr += read_sz;
}
return KERN_SUCCESS;
}
static kern_return_t
kread_addr(kaddr_t addr, kaddr_t *val) {
return kread_buf(addr, val, sizeof(*val));
}
static kern_return_t
kwrite_buf(kaddr_t addr, const void *buf, mach_msg_type_number_t sz) {
vm_machine_attribute_val_t mattr_val = MATTR_VAL_CACHE_FLUSH;
mach_vm_address_t p = (mach_vm_address_t)buf;
mach_msg_type_number_t write_sz;
while(sz != 0) {
write_sz = (mach_msg_type_number_t)MIN(sz, vm_kernel_page_size - (addr & vm_kernel_page_mask));
if(mach_vm_write(tfp0, addr, p, write_sz) != KERN_SUCCESS || mach_vm_machine_attribute(tfp0, addr, write_sz, MATTR_CACHE, &mattr_val) != KERN_SUCCESS) {
return KERN_FAILURE;
}
p += write_sz;
sz -= write_sz;
addr += write_sz;
}
return KERN_SUCCESS;
}
static kaddr_t
get_kbase(kaddr_t *kslide) {
mach_msg_type_number_t cnt = TASK_DYLD_INFO_COUNT;
vm_region_extended_info_data_t extended_info;
task_dyld_info_data_t dyld_info;
kaddr_t addr, rtclock_datap;
struct mach_header_64 mh64;
mach_port_t obj_nm;
mach_vm_size_t sz;
if(task_info(tfp0, TASK_DYLD_INFO, (task_info_t)&dyld_info, &cnt) == KERN_SUCCESS && dyld_info.all_image_info_size != 0) {
*kslide = dyld_info.all_image_info_size;
return VM_KERNEL_LINK_ADDRESS + *kslide;
}
cnt = VM_REGION_EXTENDED_INFO_COUNT;
for(addr = 0; mach_vm_region(tfp0, &addr, &sz, VM_REGION_EXTENDED_INFO, (vm_region_info_t)&extended_info, &cnt, &obj_nm) == KERN_SUCCESS; addr += sz) {
mach_port_deallocate(mach_task_self(), obj_nm);
if(extended_info.user_tag == VM_KERN_MEMORY_CPU && extended_info.protection == VM_PROT_DEFAULT) {
if(kread_addr(addr + CPU_DATA_RTCLOCK_DATAP_OFF, &rtclock_datap) != KERN_SUCCESS) {
break;
}
printf("rtclock_datap: " KADDR_FMT "\n", rtclock_datap);
rtclock_datap = trunc_page_kernel(rtclock_datap);
do {
if(rtclock_datap <= VM_KERNEL_LINK_ADDRESS) {
return 0;
}
rtclock_datap -= vm_kernel_page_size;
if(kread_buf(rtclock_datap, &mh64, sizeof(mh64)) != KERN_SUCCESS) {
return 0;
}
} while(mh64.magic != MH_MAGIC_64 || mh64.cputype != CPU_TYPE_ARM64 || mh64.filetype != MH_EXECUTE);
*kslide = rtclock_datap - VM_KERNEL_LINK_ADDRESS;
return rtclock_datap;
}
}
return 0;
}
static kern_return_t
find_section(kaddr_t sg64_addr, struct segment_command_64 sg64, const char *sect_name, struct section_64 *sp) {
kaddr_t s64_addr, s64_end;
for(s64_addr = sg64_addr + sizeof(sg64), s64_end = s64_addr + (sg64.cmdsize - sizeof(*sp)); s64_addr < s64_end; s64_addr += sizeof(*sp)) {
if(kread_buf(s64_addr, sp, sizeof(*sp)) != KERN_SUCCESS) {
break;
}
if(strncmp(sp->segname, sg64.segname, sizeof(sp->segname)) == 0 && strncmp(sp->sectname, sect_name, sizeof(sp->sectname)) == 0) {
return KERN_SUCCESS;
}
}
return KERN_FAILURE;
}
static void
sec_reset(sec_64_t *sec) {
memset(&sec->s64, '\0', sizeof(sec->s64));
sec->data = NULL;
}
static void
sec_term(sec_64_t *sec) {
free(sec->data);
sec_reset(sec);
}
static kern_return_t
sec_init(sec_64_t *sec) {
if((sec->data = malloc(sec->s64.size)) != NULL) {
if(kread_buf(sec->s64.addr, sec->data, sec->s64.size) == KERN_SUCCESS) {
return KERN_SUCCESS;
}
sec_term(sec);
}
return KERN_FAILURE;
}
static void
pfinder_reset(pfinder_t *pfinder) {
sec_reset(&pfinder->sec_text);
sec_reset(&pfinder->sec_cstring);
}
static void
pfinder_term(pfinder_t *pfinder) {
sec_term(&pfinder->sec_text);
sec_term(&pfinder->sec_cstring);
pfinder_reset(pfinder);
}
static kern_return_t
pfinder_init(pfinder_t *pfinder, kaddr_t kbase) {
kern_return_t ret = KERN_FAILURE;
struct segment_command_64 sg64;
kaddr_t sg64_addr, sg64_end;
struct mach_header_64 mh64;
struct section_64 s64;
pfinder_reset(pfinder);
if(kread_buf(kbase, &mh64, sizeof(mh64)) == KERN_SUCCESS && mh64.magic == MH_MAGIC_64 && mh64.cputype == CPU_TYPE_ARM64 && mh64.filetype == MH_EXECUTE) {
for(sg64_addr = kbase + sizeof(mh64), sg64_end = sg64_addr + (mh64.sizeofcmds - sizeof(sg64)); sg64_addr < sg64_end; sg64_addr += sg64.cmdsize) {
if(kread_buf(sg64_addr, &sg64, sizeof(sg64)) != KERN_SUCCESS) {
break;
}
if(sg64.cmd == LC_SEGMENT_64) {
if(strncmp(sg64.segname, SEG_TEXT_EXEC, sizeof(sg64.segname)) == 0 && find_section(sg64_addr, sg64, SECT_TEXT, &s64) == KERN_SUCCESS) {
pfinder->sec_text.s64 = s64;
printf("sec_text_addr: " KADDR_FMT ", sec_text_sz: 0x%" PRIX64 "\n", s64.addr, s64.size);
} else if(strncmp(sg64.segname, SEG_TEXT, sizeof(sg64.segname)) == 0 && find_section(sg64_addr, sg64, SECT_CSTRING, &s64) == KERN_SUCCESS) {
pfinder->sec_cstring.s64 = s64;
printf("sec_cstring_addr: " KADDR_FMT ", sec_cstring_sz: 0x%" PRIX64 "\n", s64.addr, s64.size);
}
}
if(pfinder->sec_text.s64.size != 0 && pfinder->sec_cstring.s64.size != 0) {
if(sec_init(&pfinder->sec_text) == KERN_SUCCESS) {
ret = sec_init(&pfinder->sec_cstring);
}
break;
}
}
}
if(ret != KERN_SUCCESS) {
pfinder_term(pfinder);
}
return ret;
}
static kaddr_t
pfinder_xref_rd(pfinder_t pfinder, uint32_t rd, kaddr_t start, kaddr_t to) {
uint64_t x[32] = { 0 };
uint32_t insn;
for(; start >= pfinder.sec_text.s64.addr && start < pfinder.sec_text.s64.addr + (pfinder.sec_text.s64.size - sizeof(insn)); start += sizeof(insn)) {
memcpy(&insn, pfinder.sec_text.data + (start - pfinder.sec_text.s64.addr), sizeof(insn));
if(IS_LDR_X(insn)) {
x[RD(insn)] = start + LDR_X_IMM(insn);
} else if(IS_ADR(insn)) {
x[RD(insn)] = start + ADR_IMM(insn);
} else if(IS_ADRP(insn)) {
x[RD(insn)] = ADRP_ADDR(start) + ADRP_IMM(insn);
continue;
} else if(IS_ADD_X(insn)) {
x[RD(insn)] = x[RN(insn)] + ADD_X_IMM(insn);
} else if(IS_LDR_X_UNSIGNED_IMM(insn)) {
x[RD(insn)] = x[RN(insn)] + LDR_X_UNSIGNED_IMM(insn);
} else if(IS_RET(insn)) {
memset(x, '\0', sizeof(x));
}
if(RD(insn) == rd) {
if(to == 0) {
return x[rd];
}
if(x[rd] == to) {
return start;
}
}
}
return 0;
}
static kaddr_t
pfinder_xref_str(pfinder_t pfinder, const char *str, uint32_t rd) {
const char *p, *e;
size_t len;
for(p = pfinder.sec_cstring.data, e = p + pfinder.sec_cstring.s64.size; p < e; p += len) {
len = strlen(p) + 1;
if(strncmp(str, p, len) == 0) {
return pfinder_xref_rd(pfinder, rd, pfinder.sec_text.s64.addr, pfinder.sec_cstring.s64.addr + (kaddr_t)(p - pfinder.sec_cstring.data));
}
}
return 0;
}
static kaddr_t
pfinder_allproc(pfinder_t pfinder) {
kaddr_t ref = pfinder_xref_str(pfinder, "shutdownwait", 2);
if(ref == 0) {
ref = pfinder_xref_str(pfinder, "shutdownwait", 3); /* msleep */
}
return pfinder_xref_rd(pfinder, 8, ref, 0);
}
static kern_return_t
pfinder_init_offsets(pfinder_t pfinder) {
if((allproc = pfinder_allproc(pfinder)) != 0) {
printf("allproc: " KADDR_FMT "\n", allproc);
return KERN_SUCCESS;
}
return KERN_FAILURE;
}
uint32_t kernel_read32(uint64_t where) {
uint32_t out;
kread_buf(where, &out, sizeof(uint32_t));
return out;
}
uint64_t kernel_read64(uint64_t where) {
uint64_t out;
kread_buf(where, &out, sizeof(uint64_t));
return out;
}
void kernel_write32(uint64_t where, uint32_t what) {
uint32_t _what = what;
kwrite_buf(where, &_what, sizeof(uint32_t));
}
void kernel_write64(uint64_t where, uint64_t what) {
uint64_t _what = what;
kwrite_buf(where, &_what, sizeof(uint64_t));
}
//---------maphys and vnodebypass end----------//
//---------jelbrekLib------//
//Thanks to @Jakeashacks
//original code : https://github.com/jakeajames/jelbrekLib/blob/master/vnode_utils.m#L62
bool copy_file_in_memory(char *original, char *replacement) {
uint64_t orig = get_vnode_with_chdir(original);
uint64_t fake = get_vnode_with_chdir(replacement);
if(orig == 0 || fake == 0){
printf("hardlink error orig = %llu, fake = %llu\n", orig, fake);
return false;
}
struct vnode rvp, fvp;
kread_buf(orig, &rvp, sizeof(struct vnode));
kread_buf(fake, &fvp, sizeof(struct vnode));
fvp.v_usecount = rvp.v_usecount;
fvp.v_kusecount = rvp.v_kusecount;
//fvp.v_parent = rvp.v_parent; ?
fvp.v_freelist = rvp.v_freelist;
fvp.v_mntvnodes = rvp.v_mntvnodes;
fvp.v_ncchildren = rvp.v_ncchildren;
fvp.v_nclinks = rvp.v_nclinks;
kwrite_buf(orig, &fvp, sizeof(struct vnode));
return true;
}
//-------jelbrekLib end----------//
#define FAKEROOTDIR "/private/var/MobileSoftwareUpdate/mnt1"
extern CFNotificationCenterRef CFNotificationCenterGetDistributedCenter(void);
static uint64_t kbase = 0;
static uint64_t kslide = 0;
static uint32_t off_p_pid = 0;
static uint32_t off_p_pfd = 0;
static uint32_t off_fd_rdir = 0;
static uint32_t off_fd_cdir = 0;
static uint32_t off_vnode_iocount = 0;
static uint32_t off_vnode_usecount = 0;
//static uint32_t off_p_csflags = 0x298;
int koffset_init() {
if(kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_13_0_b2){
// ios 13
off_p_pid = 0x68;
off_p_pfd = 0x108;
off_fd_rdir = 0x40;
off_fd_cdir = 0x38;
off_vnode_iocount = 0x64;
off_vnode_usecount = 0x60;
return 0;
}
if(kCFCoreFoundationVersionNumber == kCFCoreFoundationVersionNumber_iOS_13_0_b1){
//ios 13b1
return -1;
}
if(kCFCoreFoundationVersionNumber <= kCFCoreFoundationVersionNumber_iOS_13_0_b1
&& kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_12_0){
//ios 12
off_p_pid = 0x60;
off_p_pfd = 0x100;
off_fd_rdir = 0x40;
off_fd_cdir = 0x38;
off_vnode_iocount = 0x64;
off_vnode_usecount = 0x60;
return 0;
}
return -1;
}
uint64_t proc_of_pid(pid_t pid) {
uint64_t proc = kernel_read64(allproc);
uint64_t current_pid = 0;
while(proc){
current_pid = kernel_read32(proc + off_p_pid);
if (current_pid == pid) return proc;
proc = kernel_read64(proc);
}
return 0;
}
uint64_t get_vnode_with_chdir(const char* path){
int err = chdir(path);
if(err) return 0;
uint64_t proc = proc_of_pid(getpid());
uint64_t filedesc = kernel_read64(proc + off_p_pfd);
uint64_t vp = kernel_read64(filedesc + off_fd_cdir);
uint32_t usecount = kernel_read32(vp + off_vnode_usecount);
uint32_t iocount = kernel_read32(vp + off_vnode_iocount);
kernel_write32(vp + off_vnode_usecount, usecount+1);
kernel_write32(vp + off_vnode_iocount, iocount+1);
return vp;
}
bool change_rootvnode(const char* path, pid_t pid){
uint64_t vp = get_vnode_with_chdir(path);
if(!vp) return false;
uint64_t proc = proc_of_pid(pid);
if(!proc) return false;
uint64_t filedesc = kernel_read64(proc + off_p_pfd);
kernel_write64(filedesc + off_fd_cdir, vp);
kernel_write64(filedesc + off_fd_rdir, vp);
uint32_t fd_flags = kernel_read32(filedesc + 0x58);
fd_flags |= 1; // FD_CHROOT = 1;
kernel_write32(filedesc + 0x58, fd_flags);
return true;
}
void set_vnode_usecount(uint64_t vnode_ptr, uint32_t count){
if(vnode_ptr == 0) return;
uint32_t usecount = kernel_read32(vnode_ptr + off_vnode_usecount);
uint32_t iocount = kernel_read32(vnode_ptr + off_vnode_iocount);
printf("vp = 0x%llx, usecount = %d, iocount = %d\n", vnode_ptr, usecount, iocount);
kernel_write32(vnode_ptr + off_vnode_usecount, count);
kernel_write32(vnode_ptr + off_vnode_iocount, count);
}
void receive_notify_chrooter(CFNotificationCenterRef center,
void * observer,
CFStringRef name,
const void * object,
CFDictionaryRef userInfo){
NSDictionary* info = (__bridge NSDictionary*)userInfo;
NSLog(@"receive notify %@", info);
pid_t pid = [info[@"Pid"] intValue];
change_rootvnode(FAKEROOTDIR, pid);
set_vnode_usecount(get_vnode_with_chdir(FAKEROOTDIR), 0xf000);
usleep(100000);
kill(pid, SIGCONT);
}
bool is_empty(const char* path){
DIR* dir = opendir(path);
struct dirent* ent;
int count = 0;
while ((ent = readdir(dir)) != NULL) {
count++;
}
if(count == 2){
return YES;
}else{
return NO;
}
}
int main(int argc, char *argv[], char *envp[]) {
pfinder_t pfinder;
kern_return_t err;
if(koffset_init() != 0){
printf("failed get koffsets\n");
}
err = init_tfp0();
if(err != KERN_SUCCESS){
printf("failed init_tfp0\n");
return 1;
}
kbase = get_kbase(&kslide);
if(kbase == 0){
printf("failed get_kbase\n");
return 1;
}
err = pfinder_init(&pfinder, kbase);
if(err != KERN_SUCCESS){
printf("failed pfinder_init\n");
return 1;
}
err = pfinder_init_offsets(pfinder);
if(err != KERN_SUCCESS){
printf("failed pfinder_init_offsets\n");
return 1;
}
if(!is_empty(FAKEROOTDIR) && access(FAKEROOTDIR"/private/var/containers", F_OK) != 0){
printf("error already mounted\n");
return 1;
}
if (is_empty(FAKEROOTDIR)){
printf("kbase = %llx\n", kbase);
printf("kslide = %llx\n", kslide);
if(kbase == 0){
printf("error kbase is 0\n");
return 1;
}
int fd = open("/", O_RDONLY);
printf("open root directory fd = %d\n", fd);
err = fs_snapshot_mount(fd, FAKEROOTDIR, "orig-fs", 0);
if(err != 0){
printf("mount snapshot error = %d\n", err);
return 1;
}
err = mount("devfs", FAKEROOTDIR"/dev", 0, 0);
if(err != 0){
printf("mount devfs error = %d\n", err);
return 1;
}
close(fd);
if(access(FAKEROOTDIR"/private/var/containers", F_OK) != 0){
printf("hardlink /private/var\n");
copy_file_in_memory(FAKEROOTDIR"/private/var", "/private/var");
//set_vnode_usecount(get_vnode_with_chdir("/private/var/MobileSoftwareUpdate/mnt1/private/var"), 0xffff);
}
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wimplicit-function-declaration"
CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(), NULL, receive_notify_chrooter, (__bridge CFStringRef)@"jp.akusio.chrooter", NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
#pragma clang diagnostic pop
printf("start changerootfs\n");
CFRunLoopRun();
return 1;
}