-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpacmankit.rs
639 lines (560 loc) · 19.5 KB
/
pacmankit.rs
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
/*!
* Shared information between the user attack and the PacmanKit kext.
*/
use std::ffi::{CStr, CString};
use crate::iokit::*;
use crate::mach::*;
use crate::timer;
/// Offset in bytes within a PacmanUser IOUserClient to the helper field
pub const PACMANKIT_TO_HELPER : u64 = 0xE0;
/**
* The operations supported by the PacmanKit kext.
*
* Each of these is an ::externalMethod selector.
*/
#[repr(u32)]
pub enum PacmanKitOp {
KernelBase = 0x00,
Read = 0x01,
Write = 0x02,
KernelVirt2Phys = 0x03,
UserVirt2Phys = 0x04,
IOUserClientLeak = 0x05,
GimmeMemory = 0x06,
FreeMemory = 0x07,
TellMeRegs = 0x08,
ReadForTiming = 0x09,
ExecForTiming = 0x0A,
LeakMethod = 0x0B,
ReadForSpectre = 0x0C,
ExecForSpectre = 0x0D,
CallServiceRoutine= 0x0E,
ForgeSignData = 0x0F,
ForgeAuthData = 0x10,
ForgeSignInst = 0x11,
ForgeAuthInst = 0x12,
LeakCurProc = 0x13,
}
/**
* An object representing a connection to the PacmanKit IOUserClient in the PacmanKit kext.
*
* This can be used to run all the operations provided by the PacmanKit kext.
*/
pub struct PacmanKitConnection(IOConnect, IOService);
impl PacmanKitConnection {
/**
* Create a new PacmanKitConnection.
* This opens a new IOUserClient and may fail.
*
* # Return Value
* Returns the `kern_return_t` error on failure, a valid PacmanKitConnection on success.
*/
pub unsafe fn init() -> Option<Self> {
let mut kret : KernReturn;
let mut name : IOName = [0;128];
let mut handle : IOConnect = 0;
let service_name = CString::new("PacmanKit").unwrap();
let serv = IOServiceGetMatchingService(kIOMainPortDefault, IOServiceMatching(service_name.as_ptr()));
if IO_OBJECT_NULL == serv {
println!("Couldn't find PacmanKit. Did you remember to install it?");
return None;
}
IORegistryEntryGetName(serv, &mut name);
kret = IOServiceOpen(serv, mach_task_self(), 0, &mut handle);
if KERN_SUCCESS != kret {
println!("Couldn't connect to IOService {:?} (error {:?}", CStr::from_ptr(&name as *const _), CStr::from_ptr(mach_error_string(kret)));
IOObjectRelease(serv);
return None;
}
return Some(Self(
handle,
serv
));
}
/**
* Returns the kernel base address (pointer to the macho header of the kernelcache).
*/
pub unsafe fn get_kernel_base(&self) -> Result<u64, KernReturn> {
let mut kaslr_base = 0;
let mut output_cnt = 1;
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::KernelBase as u32,
core::ptr::null(),
0,
&mut kaslr_base,
&mut output_cnt
);
if kret != KERN_SUCCESS {
println!("Couldn't leak kernel base! (error {:?})", CStr::from_ptr(mach_error_string(kret)));
return Err(kret);
}
return Ok(kaslr_base);
}
/**
* Read a u64 from kernel virtual memory.
*/
pub unsafe fn kernel_read(&self, addr: u64) -> Result<u64, KernReturn> {
let mut output_cnt = 1;
let mut read_out = 0;
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::Read as u32,
&addr,
1,
&mut read_out,
&mut output_cnt
);
if KERN_SUCCESS != kret {
println!("Couldn't read from kernel memory (error {:?})", CStr::from_ptr(mach_error_string(kret)));
return Err(kret);
}
return Ok(read_out);
}
/**
* Write a u64 into kernel memory.
*/
pub unsafe fn kernel_write(&self, addr: u64, val: u64) -> Result<(), KernReturn> {
let args : [u64; 2] = [addr, val];
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::Write as u32,
args.as_ptr(),
2,
core::ptr::null_mut(),
core::ptr::null_mut()
);
if KERN_SUCCESS != kret {
println!("Couldn't write to kernel memory (error {:?})", CStr::from_ptr(mach_error_string(kret)));
return Err(kret);
}
return Ok(());
}
/**
* Translate a kernel virtual address to its physical address.
*/
pub unsafe fn kernel_virt_to_phys(&self, addr: u64) -> Result<u64, KernReturn> {
let mut output_cnt = 1;
let mut translate_out = 0;
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::KernelVirt2Phys as u32,
&addr,
1,
&mut translate_out,
&mut output_cnt
);
if KERN_SUCCESS != kret {
println!("Couldn't translate kernel address (error {:?})", CStr::from_ptr(mach_error_string(kret)));
return Err(kret);
}
return Ok(translate_out);
}
/**
* Translate a user virtual address to its physical address.
*/
pub unsafe fn user_virt_to_phys(&self, addr: u64) -> Result<u64, KernReturn> {
let mut output_cnt = 1;
let mut translate_out = 0;
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::UserVirt2Phys as u32,
&addr,
1,
&mut translate_out,
&mut output_cnt
);
if KERN_SUCCESS != kret {
println!("Couldn't translate user address (error {:?})", CStr::from_ptr(mach_error_string(kret)));
return Err(kret);
}
return Ok(translate_out);
}
/**
* Returns a pointer to this IOUserClient in the kernel.
*/
pub unsafe fn get_handle_loc(&self) -> Result<u64, KernReturn> {
let mut handle_loc = 0;
let mut output_cnt = 1;
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::IOUserClientLeak as u32,
core::ptr::null(),
0,
&mut handle_loc,
&mut output_cnt
);
if KERN_SUCCESS != kret {
println!("Couldn't get IOConnect virtual address (error {:?})", CStr::from_ptr(mach_error_string(kret)));
return Err(kret);
}
return Ok(handle_loc);
}
/**
* Returns a pointer to a kernel memory region mmap'ed by IOMallocAligned to a page size.
*/
pub unsafe fn kernel_mmap(&self) -> Result<u64, KernReturn> {
let mut mmap_ptr = 0;
let mut output_cnt = 1;
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::GimmeMemory as u32,
core::ptr::null(),
0,
&mut mmap_ptr,
&mut output_cnt
);
if KERN_SUCCESS != kret {
println!("Couldn't get kernel mmap (error {:?})", CStr::from_ptr(mach_error_string(kret)));
return Err(kret);
}
return Ok(mmap_ptr);
}
/**
* Frees memory allocated by kernel_mmap.
*/
pub unsafe fn kernel_free(&self) -> Result<(), KernReturn> {
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::FreeMemory as u32,
core::ptr::null(),
0,
core::ptr::null_mut(),
core::ptr::null_mut()
);
if KERN_SUCCESS != kret {
println!("Couldn't get kernel mmap (error {:?})", CStr::from_ptr(mach_error_string(kret)));
return Err(kret);
}
return Ok(());
}
pub unsafe fn list_timer_regs(&self) {
let mut rval : [u64; 2] = [0, 0];
let mut num_args : u32 = 2;
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::TellMeRegs as u32,
core::ptr::null(),
0,
rval.as_mut_ptr(),
&mut num_args as *mut _,
);
if KERN_SUCCESS != kret {
println!("Couldn't read timer MSRs (error {:?})", CStr::from_ptr(mach_error_string(kret)));
return;
}
println!("PMCR0 is 0x{:X}", rval[0]);
println!("CNTKCTL_EL1 is 0x{:X}", rval[1]);
return;
}
/**
* Read a u64 from kernel virtual memory without any IOMemoryDescriptor calls.
* This *CAN* panic the kernel!
*
* # Arguments
* * `addr`: A kernel address to load
* * `do_it`: Should the load actually run?
*
* # Return Value
* Returns the number of cycles taken if `do_it` was true. Else, returns an undefined value.
*/
pub unsafe fn kernel_read_for_timing(&self, addr: u64, do_it: bool) -> Result<u64, KernReturn> {
let mut output_cnt = 1;
let mut read_out = 0;
let args : [u64; 2] = [addr, do_it as u64];
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::ReadForTiming as u32,
args.as_ptr(),
2,
&mut read_out,
&mut output_cnt
);
if KERN_SUCCESS != kret {
println!("Couldn't read from kernel memory (error {:?})", CStr::from_ptr(mach_error_string(kret)));
return Err(kret);
}
return Ok(read_out - timer::TIMER_OVERHEAD_PCORE);
}
/**
* Exec a u64 from kernel virtual memory without any IOMemoryDescriptor calls.
* This *CAN* panic the kernel!
*
* # Arguments
* * `addr`: A kernel address to exec
* * `do_it`: Should the call actually run?
*
* # Return Value
* Returns the number of cycles taken if `do_it` was true. Else, returns an undefined value.
*/
pub unsafe fn kernel_exec_for_timing(&self, addr: u64, do_it: bool) -> Result<u64, KernReturn> {
let mut output_cnt = 1;
let mut read_out = 0;
let args : [u64; 2] = [addr, do_it as u64];
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::ExecForTiming as u32,
args.as_ptr(),
2,
&mut read_out,
&mut output_cnt
);
if KERN_SUCCESS != kret {
println!("Couldn't exec from kernel memory (error {:?})", CStr::from_ptr(mach_error_string(kret)));
return Err(kret);
}
return Ok(read_out - timer::TIMER_OVERHEAD_PCORE);
}
/**
* Returns a pointer to a kernel method that just runs `ret`.
*/
pub unsafe fn leak_retpoline(&self) -> Result<u64, KernReturn> {
let mut method_leak_ptr : [u64; 3] = [0; 3];
let mut output_cnt = 3;
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::LeakMethod as u32,
core::ptr::null(),
0,
method_leak_ptr.as_mut_ptr(),
&mut output_cnt
);
if KERN_SUCCESS != kret {
println!("Couldn't get reveal address of a kernel method (error {:?})", CStr::from_ptr(mach_error_string(kret)));
return Err(kret);
}
return Ok(method_leak_ptr[0]);
}
/**
* Returns a pointer to the `LIMIT` variable in the PacmanKit kext.
*/
pub unsafe fn leak_limit_location(&self) -> Result<u64, KernReturn> {
let mut method_leak_ptr : [u64; 3] = [0; 3];
let mut output_cnt = 3;
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::LeakMethod as u32,
core::ptr::null(),
0,
method_leak_ptr.as_mut_ptr(),
&mut output_cnt
);
if KERN_SUCCESS != kret {
println!("Couldn't get reveal address of the kext limit (error {:?})", CStr::from_ptr(mach_error_string(kret)));
return Err(kret);
}
return Ok(method_leak_ptr[1]);
}
/**
* Returns a pointer to the win() method in the PacmanKit kext.
*/
pub unsafe fn leak_win(&self) -> Result<u64, KernReturn> {
panic!("This has been deprecated- use leak_retpoline to reveal a region full of `ret`s that can be used");
// let mut method_leak_ptr : [u64; 3] = [0; 3];
// let mut output_cnt = 3;
// let kret = IOConnectCallScalarMethod(
// self.0,
// PacmanKitOp::LeakMethod as u32,
// core::ptr::null(),
// 0,
// method_leak_ptr.as_mut_ptr(),
// &mut output_cnt
// );
// if KERN_SUCCESS != kret {
// println!("Couldn't get reveal address of the kext limit (error {:?})", CStr::from_ptr(mach_error_string(kret)));
// return Err(kret);
// }
// return Ok(method_leak_ptr[2]);
}
/**
* Read a u64 from kernel virtual memory without any IOMemoryDescriptor calls.
* This *CAN* panic the kernel!
*
* This is very similar to kernel_read_for_timing except it features no synchronization
* barriers (so speculation can cause the load to happen) and does not report any latencies.
*
* # Arguments
* * `addr`: A kernel address to load
* * `idx`: Index passed in to the 'bounds check'
*
* # Return Value
* Returns Nothing.
*/
pub unsafe fn kernel_read_for_spectre(&self, addr: u64, idx: u64) -> Result<(), KernReturn> {
let args : [u64; 2] = [addr, idx];
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::ReadForSpectre as u32,
args.as_ptr(),
2,
core::ptr::null_mut(),
core::ptr::null_mut(),
);
if KERN_SUCCESS != kret {
println!("Couldn't read from kernel memory (error {:?})", CStr::from_ptr(mach_error_string(kret)));
return Err(kret);
}
return Ok(());
}
/**
* Exec a u64 from kernel virtual memory without any IOMemoryDescriptor calls.
* This *CAN* panic the kernel!
*
* This is very similar to kernel_exec_for_timing except it features no synchronization
* barriers (so speculation can cause the load to happen) and does not report any latencies.
*
* # Arguments
* * `addr`: A kernel address to exec
* * `idx`: Index passed in to the 'bounds check'
*
* # Return Value
* Returns Nothing.
*/
pub unsafe fn kernel_exec_for_spectre(&self, addr: u64, idx: u64) -> Result<(), KernReturn> {
let args : [u64; 2] = [addr, idx];
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::ExecForSpectre as u32,
args.as_ptr(),
2,
core::ptr::null_mut(),
core::ptr::null_mut(),
);
if KERN_SUCCESS != kret {
println!("Couldn't exec from kernel memory (error {:?})", CStr::from_ptr(mach_error_string(kret)));
return Err(kret);
}
return Ok(());
}
pub unsafe fn call_service_routine(&self, arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64, arg6: u64) -> Result<u64, KernReturn> {
let args : [u64; 6] = [arg1, arg2, arg3, arg4, arg5, arg6];
let mut output_cnt = 1;
let mut output_val = 0u64;
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::CallServiceRoutine as u32,
args.as_ptr(),
6,
&mut output_val,
&mut output_cnt
);
// Ignore errors...
if KERN_SUCCESS != kret {
// println!("Couldn't call service routine {}!", arg1);
// return Err(kret);
}
return Ok(output_val);
}
/// Returns the correct PACDA signature from the kernel. This can ONLY be used for testing!
/// The real attack will need to use brute force to find this. We only use this method to learn
/// the ground truth for generating plots and tuning the algorithm.
pub unsafe fn forge_sign_data(&self, addr: u64, salt: u64) -> Result<u64, KernReturn> {
let args : [u64; 2] = [addr, salt];
let mut output_cnt = 1;
let mut output_val = 1;
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::ForgeSignData as u32,
args.as_ptr(),
2,
&mut output_val,
&mut output_cnt
);
if KERN_SUCCESS != kret {
println!("Couldn't forge PACDA for addr 0x{:X} with salt 0x{:X}", addr, salt);
return Err(kret);
}
return Ok(output_val);
}
/// Returns the correct AUTDA signature from the kernel. This can ONLY be used for testing!
/// The real attack cannot do this.
pub unsafe fn forge_auth_data(&self, addr: u64, salt: u64) -> Result<u64, KernReturn> {
let args : [u64; 2] = [addr, salt];
let mut output_cnt = 1;
let mut output_val = 1;
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::ForgeAuthData as u32,
args.as_ptr(),
2,
&mut output_val,
&mut output_cnt
);
if KERN_SUCCESS != kret {
println!("Couldn't forge AUTDA for addr 0x{:X} with salt 0x{:X}", addr, salt);
return Err(kret);
}
return Ok(output_val);
}
/// Returns the correct PACIA signature from the kernel. This can ONLY be used for testing!
/// The real attack will need to use brute force to find this. We only use this method to learn
/// the ground truth for generating plots and tuning the algorithm.
pub unsafe fn forge_sign_inst(&self, addr: u64, salt: u64) -> Result<u64, KernReturn> {
let args : [u64; 2] = [addr, salt];
let mut output_cnt = 1;
let mut output_val = 1;
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::ForgeSignInst as u32,
args.as_ptr(),
2,
&mut output_val,
&mut output_cnt
);
if KERN_SUCCESS != kret {
println!("Couldn't forge PACIA for addr 0x{:X} with salt 0x{:X}", addr, salt);
return Err(kret);
}
return Ok(output_val);
}
/// Returns the correct AUTIA signature from the kernel. This can ONLY be used for testing!
/// The real attack cannot do this.
pub unsafe fn forge_auth_inst(&self, addr: u64, salt: u64) -> Result<u64, KernReturn> {
let args : [u64; 2] = [addr, salt];
let mut output_cnt = 1;
let mut output_val = 1;
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::ForgeAuthInst as u32,
args.as_ptr(),
2,
&mut output_val,
&mut output_cnt
);
if KERN_SUCCESS != kret {
println!("Couldn't forge AUTIA for addr 0x{:X} with salt 0x{:X}", addr, salt);
return Err(kret);
}
return Ok(output_val);
}
/// Leak the current proc pointer
pub unsafe fn current_proc(&self) -> Result<u64, KernReturn> {
let mut leak_ptr : [u64; 1] = [0; 1];
let mut output_cnt = 1;
let kret = IOConnectCallScalarMethod(
self.0,
PacmanKitOp::LeakCurProc as u32,
core::ptr::null(),
0,
leak_ptr.as_mut_ptr(),
&mut output_cnt
);
if KERN_SUCCESS != kret {
println!("Couldn't call current_proc() (error {:?})", CStr::from_ptr(mach_error_string(kret)));
return Err(kret);
}
return Ok(leak_ptr[0]);
}
}
impl Drop for PacmanKitConnection {
/**
* Clean up our IOKit connection.
*/
fn drop(&mut self) {
println!("Dropping a PacmanKitConnection ({:X}, {:X})", self.0, self.1);
unsafe {
IOServiceClose(self.0);
IOObjectRelease(self.1);
}
}
}