forked from enarx/enarx
/
mod.rs
999 lines (838 loc) · 29.7 KB
/
mod.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
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
// SPDX-License-Identifier: Apache-2.0
//! FIXME: add docs
macro_rules! debug {
($dst:expr, $($arg:tt)*) => {
#[allow(unused_must_use)] {
if $crate::DEBUG {
use core::fmt::Write;
write!($dst, $($arg)*);
}
}
};
}
macro_rules! debugln {
($dst:expr) => { debugln!($dst,) };
($dst:expr, $($arg:tt)*) => {
if $crate::DEBUG {
use core::fmt::Write;
let _ = writeln!($dst, $($arg)*);
}
};
}
pub(crate) mod gdb;
pub(crate) mod key;
pub(crate) mod usermem;
use crate::handler::usermem::UserMemScope;
use crate::heap::Access;
use crate::heap::Heap;
use crate::thread::{
NewThread, NewThreadFromRegisters, Tcb, Tcs, ThreadMem, NEW_THREAD_QUEUE, THREADS_FREE,
THREAD_ID_CNT,
};
use crate::{
shim_address, CSSA_0_STACK_SIZE, CSSA_1_PLUS_STACK_SIZE, DEBUG, ENARX_EXEC_END,
ENARX_EXEC_START, ENCL_SIZE, NUM_SSA,
};
use core::arch::asm;
use core::arch::x86_64::CpuidResult;
use core::ffi::{c_int, c_size_t, c_ulong, c_void};
use core::fmt::Write;
use core::mem::size_of;
use core::ptr::read_unaligned;
use core::ptr::NonNull;
use core::sync::atomic::{AtomicU32, Ordering};
use mmledger::{Region, Span};
use primordial::{Address, Offset, Page};
use sallyport::guest::{self, Handler as _, Platform, ThreadLocalStorage};
use sallyport::item::enarxcall::sgx::{Report, ReportData, TargetInfo, TECH};
use sallyport::item::enarxcall::{SYS_GETATT, SYS_GETKEY};
use sallyport::libc::{
off_t, pid_t, CloneFlags, SYS_clock_gettime, EACCES, EAGAIN, EINVAL, EIO, EMSGSIZE, ENOMEM,
ENOSYS, ENOTSUP, MAP_ANONYMOUS, MAP_PRIVATE, PROT_EXEC, PROT_READ, PROT_WRITE, STDERR_FILENO,
};
use sgx::page::{Class, Flags};
use sgx::ssa::StateSaveArea;
use sgx::ssa::Vector;
use spin::{Lazy, RwLock};
use x86_64::addr::VirtAddr;
use x86_64::structures::paging::Page as PageAddr;
// Opcode constants, details in Volume 2 of the Intel 64 and IA-32 Architectures Software
// Developer's Manual
const OP_SYSCALL: u16 = 0x050f;
const OP_CPUID: u16 = 0xa20f;
/// The keep heap
pub static HEAP: Lazy<RwLock<Heap>> = Lazy::new(|| {
let start = unsafe { &ENARX_EXEC_END as *const _ } as usize;
let end = shim_address() + ENCL_SIZE;
let span: Span = Region::new(Address::new(start), Address::new(end)).into();
RwLock::new(Heap::new(span.start, span.count))
});
// For `Handler::mmap_guest()`
static ZERO: Page = Page::zeroed();
fn is_prot_allowed(prot: c_int) -> bool {
prot == PROT_READ || prot == (PROT_READ | PROT_WRITE) || prot == (PROT_READ | PROT_EXEC)
}
fn flags_from_libc(prot: c_int) -> Flags {
let mut flags = Flags::empty();
if prot & PROT_READ != 0 {
flags |= Flags::READ;
}
if prot & PROT_WRITE != 0 {
flags |= Flags::WRITE;
}
if prot & PROT_EXEC != 0 {
flags |= Flags::EXECUTE;
}
flags
}
fn access_from_libc(prot: c_int) -> Access {
let mut access = Access::empty();
if prot & PROT_READ != 0 {
access |= Access::READ;
}
if prot & PROT_WRITE != 0 {
access |= Access::WRITE;
}
if prot & PROT_EXEC != 0 {
access |= Access::EXECUTE;
}
access
}
/// Thread local storage for the current thread
pub struct Handler<'a> {
block: &'a mut [usize],
ssa: &'a mut StateSaveArea,
tcb: &'a mut Tcb,
start: u64,
}
impl<'a> Write for Handler<'a> {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
let buf = s.as_bytes();
let len = buf.len();
let mut written = 0;
while written < len {
written += self
.write(STDERR_FILENO, &buf[written..])
.map_err(|_| core::fmt::Error)?;
}
Ok(())
}
}
impl guest::Handler for Handler<'_> {
fn sally(&mut self) -> sallyport::Result<()> {
// prevent earlier writes from being moved beyond this point
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::Release);
unsafe {
// Safety: Enclave exit and re-enter should have left all registers intact.
asm!("syscall");
}
// prevent later reads from being moved before this point
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::Acquire);
Ok(())
}
fn block(&self) -> &[usize] {
self.block
}
fn block_mut(&mut self) -> &mut [usize] {
self.block
}
fn thread_local_storage(&mut self) -> &mut ThreadLocalStorage {
&mut self.tcb.tls
}
fn arch_prctl(
&mut self,
_platform: &impl Platform,
_code: c_int,
_addr: c_ulong,
) -> sallyport::Result<()> {
let tid = self.tcb.tid;
debugln!(self, "[{tid}] arch_prctl should have never been called");
Err(ENOSYS)
}
fn brk(
&mut self,
_platform: &impl Platform,
addr: Option<NonNull<c_void>>,
) -> sallyport::Result<NonNull<c_void>> {
let addr = Address::<usize, Page>::new(
addr.map(|v| (v.as_ptr() as usize + Page::SIZE - 1) & !(Page::SIZE - 1))
.unwrap_or(0),
);
let mut heap = HEAP.write();
let max = heap.brk_max();
let addr = heap.brk(addr);
if addr > max {
self.mmap_host(
NonNull::new(max.raw() as *mut _).unwrap(),
addr.raw() - max.raw(),
PROT_READ | PROT_WRITE,
)?;
self.mmap_guest(max, addr - max, Flags::READ | Flags::WRITE);
}
Ok(NonNull::new(addr.raw() as *mut _).unwrap())
}
fn clone(
&mut self,
flags: CloneFlags,
stack: NonNull<c_void>,
ptid: Option<&AtomicU32>,
clear_on_exit: Option<&AtomicU32>,
tls: NonNull<c_void>,
) -> sallyport::Result<c_int> {
let tid = self.tcb.tid;
if flags
!= CloneFlags::VM
| CloneFlags::FS
| CloneFlags::FILES
| CloneFlags::SIGHAND
| CloneFlags::THREAD
| CloneFlags::SYSVSEM
| CloneFlags::SETTLS
| CloneFlags::PARENT_SETTID
| CloneFlags::CHILD_CLEARTID
| CloneFlags::DETACHED
{
return Err(ENOTSUP);
}
let clear_on_exit = clear_on_exit.ok_or(EINVAL)?;
let ptid = ptid.ok_or(EINVAL)?;
debugln!(
self,
"[{tid}] clone({flags:?}, stack = {stack:p}, ptid = {ptid:p}, clear_on_exit = {clear_on_exit:p}, tls = {tls:p})",
ptid = ptid as *const _,
clear_on_exit = clear_on_exit as *const _,
stack = stack.as_ptr(),
tls = tls.as_ptr()
);
let new_tid = THREAD_ID_CNT.fetch_add(1, Ordering::SeqCst);
let mut regs = self.ssa.gpr;
regs.rsp = stack.as_ptr() as _;
regs.fsbase = tls.as_ptr() as _;
let mut threads_free_guard = THREADS_FREE.write();
let addr = if *threads_free_guard == 0 {
debugln!(self, "[{tid}] allocating new thread");
self.thread_mem_alloc().map_err(|_| EAGAIN)? as usize
} else {
*threads_free_guard -= 1;
0
};
drop(threads_free_guard);
NEW_THREAD_QUEUE
.write()
.push(NewThread::Thread(NewThreadFromRegisters {
clear_on_exit: clear_on_exit as *const _ as _,
regs,
tid: new_tid,
}))
.unwrap();
ptid.store(new_tid as _, Ordering::Relaxed);
let ret = self.spawn(addr);
debugln!(self, "[{tid}] spawn() = {ret:#?}");
ret?;
Ok(new_tid)
}
fn exit(&mut self, status: c_int) -> sallyport::Result<()> {
let tid = self.tcb.tid;
let addr = self.tcb.clear_on_exit;
if let Some(addr) = addr {
debugln!(self, "[{tid}] clear TID at {addr:p}");
unsafe { (*addr.as_ptr()).store(0, Ordering::Relaxed) };
let _ = self.unpark();
} else {
debugln!(self, "[{tid}] no TID to clear");
}
if self.tcb.return_to_main.rip != 0 {
self.ssa.gpr.rsp = self.tcb.return_to_main.rsp;
// The syscall handler will add 2 to rip to skip the syscall instruction (which we don't have anymore)
self.ssa.gpr.rip = self.tcb.return_to_main.rip - 2;
self.ssa.gpr.rbx = self.tcb.return_to_main.rbx;
self.ssa.gpr.rbp = self.tcb.return_to_main.rbp;
self.ssa.gpr.gsbase = self.tcb.return_to_main.gsbase;
self.ssa.gpr.fsbase = self.tcb.return_to_main.fsbase;
self.ssa.gpr.rcx = status as _;
debugln!(self, "[{tid}] exiting with status {status}",);
} else {
debugln!(self, "return_to_main.rip == 0");
self.print_ssa_stack_trace();
self.attacked()
}
Ok(())
}
fn madvise(
&mut self,
_platform: &impl Platform,
_addr: NonNull<c_void>,
_length: c_size_t,
_advice: c_int,
) -> sallyport::Result<()> {
Ok(())
}
fn mmap(
&mut self,
_platform: &impl Platform,
addr: Option<NonNull<c_void>>,
len: c_size_t,
prot: c_int,
flags: c_int,
fd: c_int,
offset: off_t,
) -> sallyport::Result<NonNull<c_void>> {
let tid = self.tcb.tid;
let addr = addr.map(|v| v.as_ptr() as usize).unwrap_or(0);
// TODO: https://github.com/enarx/enarx/issues/1892
let prot = prot | PROT_READ;
if addr != 0 || len == 0 || fd != -1 || offset != 0 || flags != MAP_PRIVATE | MAP_ANONYMOUS
{
return Err(ENOTSUP);
}
if prot != 0 && !is_prot_allowed(prot) {
return Err(EACCES);
}
let length = Offset::from_items((len + Page::SIZE - 1) / Page::SIZE);
let access = access_from_libc(prot);
let mut heap = HEAP.write();
if let Some(addr) = heap.mmap(None, length, access) {
let ret = NonNull::new(addr.raw() as *mut c_void).unwrap();
if let Err(e) = self.mmap_host(
NonNull::new(addr.raw() as *mut _).unwrap(),
length.bytes(),
PROT_READ | PROT_WRITE,
) {
debugln!(self, "[{tid}] ERROR mmap_host() = {e:#?}");
// undo the mmap
heap.munmap(addr, length).unwrap_or_else(|e| {
panic!(
"[{tid}] ERROR heap.munmap({addr:#x}, {length:#x}) = {e:#?}",
addr = addr.raw(),
length = length.bytes(),
)
});
return Err(e);
}
self.mmap_guest(addr, length, flags_from_libc(prot));
// If the previous operations succeeded, the virtual memory area
// (VMA) is already RW.
if prot != PROT_READ | PROT_WRITE {
self.mprotect_unlocked(&mut heap, ret, length.bytes() as c_size_t, prot)
.or_else(|e| {
self.munmap_unlocked(&mut heap, ret, length.bytes())
.and(Err(e))
})?;
}
Ok(ret)
} else {
debugln!(self, "[{tid}] ERROR heap.mmap() failed!!!!");
Err(ENOMEM)
}
}
fn mprotect(
&mut self,
_platform: &impl Platform,
addr: NonNull<c_void>,
len: c_size_t,
prot: c_int,
) -> sallyport::Result<()> {
let mut heap = HEAP.write();
self.mprotect_unlocked(&mut heap, addr, len, prot)
}
fn munmap(
&mut self,
_platform: &impl Platform,
addr: NonNull<c_void>,
length: c_size_t,
) -> sallyport::Result<()> {
let mut heap = HEAP.write();
self.munmap_unlocked(&mut heap, addr, length)
}
fn set_tid_address(&mut self, tidptr: &mut c_int) -> sallyport::Result<pid_t> {
let tid = self.tcb.tid;
debugln!(
self,
"[{tid}] set_tid_address at {tidptr:p}",
tidptr = tidptr as *const _
);
self.tcb.clear_on_exit = NonNull::new(tidptr as *mut c_int as *mut AtomicU32);
Ok(tid)
}
}
impl<'a> Handler<'a> {
fn new(
ssa: &'a mut StateSaveArea,
block: &'a mut [usize],
tcb: &'a mut Tcb,
start: u64,
) -> Self {
Self {
ssa,
block,
tcb,
start,
}
}
/// Finish handling an exception
pub fn finish(ssa: &'a mut StateSaveArea, block: Option<&'a mut [usize]>, tcb: &'a mut Tcb) {
if let Some(Vector::InvalidOpcode) = ssa.vector() {
if let OP_SYSCALL | OP_CPUID = unsafe { read_unaligned(ssa.gpr.rip as _) } {
// Skip the instruction.
ssa.gpr.rip += 2;
return;
}
}
if let Some(block) = block {
let mut h = Self::new(ssa, block, tcb, 0);
h.print_ssa_stack_trace();
}
unsafe { asm!("ud2", options(noreturn)) };
}
/// Handle an exception
pub fn handle(
ssa: &'a mut StateSaveArea,
block: &'a mut [usize],
tcb: &'a mut Tcb,
start: u64,
) {
let mut h = Self::new(ssa, block, tcb, start);
match h.ssa.vector() {
Some(Vector::InvalidOpcode) => match unsafe { read_unaligned(h.ssa.gpr.rip as _) } {
OP_SYSCALL => h.handle_syscall(),
OP_CPUID => h.handle_cpuid(),
r => {
debugln!(h, "unsupported opcode: {:#04x}", r);
h.print_ssa_stack_trace();
#[cfg(feature = "dbg")]
if r as u8 == 0xCC {
h.ssa.gpr.rip += 1;
return;
}
#[cfg(feature = "gdb")]
if r as u8 == 0xCC {
let rip = h.ssa.gpr.rip;
if unsafe { crate::handler::gdb::unset_bp(rip) } {
debugln!(h, "unset_bp: {:#x}", rip);
}
}
#[cfg(feature = "gdb")]
h.gdb_session();
if r == unsafe { read_unaligned(h.ssa.gpr.rip as _) } {
let _ = h.exit_group(1);
unreachable!()
}
}
},
#[cfg(feature = "gdb")]
Some(Vector::Page) => {
h.print_ssa_stack_trace();
h.gdb_session();
let _ = h.exit_group(1);
unreachable!()
}
_ => {
if cfg!(feature = "dbg") {
h.print_ssa_stack_trace();
}
h.attacked()
}
}
}
fn get_key(
&mut self,
platform: &impl Platform,
buf: usize,
buf_len: usize,
) -> Result<usize, c_int> {
if buf == 0 {
return Ok(key::SGX_KEY_LEN);
}
if buf_len > isize::MAX as usize {
return Err(EINVAL);
}
if buf_len < key::SGX_KEY_LEN {
return Err(EMSGSIZE);
}
let buf = platform.validate_slice_mut::<u8>(buf, buf_len)?;
let key_request = key::Request {
name: key::Names::SealKey,
policy: key::Policy::MRSIGNER,
isvsvn: 0,
..Default::default()
};
let key_response = key_request.enclu_egetkey().map_err(|e| {
debugln!(self, "enclu_egetkey: {}", e);
EIO
})?;
buf[..key::SGX_KEY_LEN].copy_from_slice(&key_response.key);
Ok(key::SGX_KEY_LEN)
}
fn get_attestation(
&mut self,
platform: &impl Platform,
hash: usize,
hash_len: usize,
buf: usize,
buf_len: usize,
) -> Result<[usize; 2], c_int> {
if cfg!(feature = "disable-sgx-attestation") {
return Ok([0, 0]);
}
let mut target_info = TargetInfo::default();
self.get_sgx_target_info(&mut target_info)?;
let quote_size = self.get_sgx_quote_size()?;
if buf == 0 {
return Ok([quote_size, TECH]);
}
if buf_len > isize::MAX as usize {
return Err(EINVAL);
}
if buf_len < quote_size {
return Err(EMSGSIZE);
}
if hash_len != 64 {
return Err(EINVAL);
}
let hash = {
let h = platform.validate_slice::<u8>(hash, hash_len)?;
let mut hash = [0u8; 64];
hash.copy_from_slice(h);
hash
};
let buf = platform.validate_slice_mut::<u8>(buf, buf_len)?;
// Generate Report
let report: Report = target_info.enclu_ereport(&ReportData(hash));
let len = self.get_sgx_quote(&report, buf)?;
Ok([len, TECH])
}
fn handle_syscall(&mut self) {
let orig_rdx = self.ssa.gpr.rdx;
let nr = self.ssa.gpr.rax as usize;
let tid = self.tcb.tid;
// reduce log spam
if nr != SYS_clock_gettime as _ {
debugln!(self, "[{tid}] syscall {nr} ...");
}
let usermemscope = UserMemScope;
match nr as i64 {
SYS_GETKEY => {
let ret = self.get_key(&usermemscope, self.ssa.gpr.rdi as _, self.ssa.gpr.rsi as _);
match ret {
Err(e) => self.ssa.gpr.rax = -e as u64,
Ok(rax) => {
self.ssa.gpr.rax = rax as u64;
self.ssa.gpr.rdx = orig_rdx;
}
}
}
SYS_GETATT => {
let ret = self.get_attestation(
&usermemscope,
self.ssa.gpr.rdi as _,
self.ssa.gpr.rsi as _,
self.ssa.gpr.rdx as _,
self.ssa.gpr.r10 as _,
);
match ret {
Err(e) => self.ssa.gpr.rax = -e as u64,
Ok([rax, rdx]) => {
self.ssa.gpr.rax = rax as u64;
self.ssa.gpr.rdx = rdx as u64;
}
}
}
_ => unsafe {
// Safety:
// with `usermemscope` we
// * limit the lifetime of objects created from the userspace syscall arguments to this function.
// * make sure only memory of the userspace application is addressed
let ret = self.syscall(
&usermemscope,
[
nr,
self.ssa.gpr.rdi as usize,
self.ssa.gpr.rsi as usize,
self.ssa.gpr.rdx as usize,
self.ssa.gpr.r10 as usize,
self.ssa.gpr.r8 as usize,
self.ssa.gpr.r9 as usize,
],
);
match ret {
Err(e) => self.ssa.gpr.rax = -e as u64,
Ok([rax, _]) => {
self.ssa.gpr.rax = rax as u64;
self.ssa.gpr.rdx = orig_rdx;
}
}
},
};
self.ssa.gpr.rip += 2;
// reduce log spam
if nr != SYS_clock_gettime as _ {
debugln!(self, "[{tid}] syscall {nr} = {}", self.ssa.gpr.rax as isize);
}
}
fn handle_cpuid(&mut self) {
let mut cpuid_result: CpuidResult = CpuidResult {
eax: 0,
ebx: 0,
ecx: 0,
edx: 0,
};
debug!(
self,
"cpuid({:08x}, {:08x})",
self.ssa.gpr.rax.clone(),
self.ssa.gpr.rcx.clone(),
);
self.cpuid(
self.ssa.gpr.rax as _,
self.ssa.gpr.rcx as _,
&mut cpuid_result,
)
.unwrap();
self.ssa.gpr.rax = cpuid_result.eax.into();
self.ssa.gpr.rbx = cpuid_result.ebx.into();
self.ssa.gpr.rcx = cpuid_result.ecx.into();
self.ssa.gpr.rdx = cpuid_result.edx.into();
debugln!(
self,
" = ({:08x}, {:08x}, {:08x}, {:08x})",
self.ssa.gpr.rax.clone(),
self.ssa.gpr.rbx.clone(),
self.ssa.gpr.rcx.clone(),
self.ssa.gpr.rdx.clone()
);
self.ssa.gpr.rip += 2;
}
/// Acknowledge pages committed by the host with ENCLS[EAUG].
fn mmap_guest(
&mut self,
addr: Address<usize, Page>,
length: Offset<usize, Page>,
flags: Flags,
) {
let zero_virt_addr = VirtAddr::new(ZERO.as_ptr() as u64);
// # Safety
//
// The address must be page aligned.
let zero_page_addr = unsafe { PageAddr::from_start_address_unchecked(zero_virt_addr) };
for i in 0..length.items() {
let virt_addr = VirtAddr::new((addr.raw() + i * Page::SIZE) as u64);
// # Safety
//
// The address must be page aligned.
let page_addr = unsafe { PageAddr::from_start_address_unchecked(virt_addr) };
Class::Regular
.info(flags)
.accept_copy(page_addr, zero_page_addr)
.unwrap_or_else(|_| self.attacked());
}
}
fn mprotect_unlocked(
&mut self,
heap: &mut Heap,
addr_in: NonNull<c_void>,
length_in: c_size_t,
prot: c_int,
) -> sallyport::Result<()> {
let addr = addr_in.as_ptr() as usize;
// TODO: Simplify:
let pages = ((length_in + Page::SIZE - 1) & !(Page::SIZE - 1)) / Page::SIZE;
if addr & 0xfff != 0 || pages == 0 {
return Err(EINVAL);
}
if !is_prot_allowed(prot) {
return Err(EACCES);
}
let addr = Address::new(addr);
let length = Offset::from_items(pages);
if heap.contains(addr, length).is_none() {
return Err(ENOMEM);
}
self.mprotect_host(addr_in, length.bytes(), prot)?;
for i in 0..pages {
let virt_addr = VirtAddr::new((addr.raw() + i * Page::SIZE) as u64);
// Safety: The address is guaranteed to be page aligned, because
// `addr` was checked to be page aligned and only a multiple of
// pages was added.
let page_addr = unsafe { PageAddr::from_start_address_unchecked(virt_addr) };
// TODO: https://github.com/enarx/enarx/issues/1892
Class::Regular
.info(Flags::READ | Flags::RESTRICTED)
.accept(page_addr)
.unwrap_or_else(|_| self.attacked());
Class::Regular.info(flags_from_libc(prot)).extend(page_addr);
}
let access = access_from_libc(prot);
heap.mmap(Some(addr), length, access);
Ok(())
}
fn munmap_unlocked(
&mut self,
heap: &mut Heap,
addr_in: NonNull<c_void>,
length_in: c_size_t,
) -> sallyport::Result<()> {
let tid = self.tcb.tid;
let addr = addr_in.as_ptr() as usize;
let pages = ((length_in + Page::SIZE - 1) & !(Page::SIZE - 1)) / Page::SIZE;
if addr & 0xfff != 0 || pages == 0 {
return Err(EINVAL);
}
let addr = Address::new(addr);
let length = Offset::from_items(pages);
if heap.contains(addr, length).is_none() {
return Ok(());
}
// Process the ledger first, before doing anything else, because it can
// legitly fail when running out of resources.
if let Err(e) = heap.munmap(addr, length) {
debugln!(self, "[{tid}] ERROR munmap: heap.munmap FAILED !!! {e:?}");
return Err(ENOMEM);
}
// On the other hand, failing in any of these operations is expected to
// crash the enclave because it is due either to a software bug, or a
// malicious host.
if let Err(e) = self.modify_sgx_page_type(addr_in, length.bytes(), Class::Trimmed as _) {
debugln!(
self,
"[{tid}] ERROR munmap: modify_sgx_page_type FAILED !!! {e:?}"
);
self.attacked();
}
for i in 0..pages {
let virt_addr = VirtAddr::new((addr.raw() + i * Page::SIZE) as u64);
// # Safety
//
// The address must be page aligned.
let page_addr = unsafe { PageAddr::from_start_address_unchecked(virt_addr) };
Class::Trimmed
.info(Flags::MODIFIED)
.accept(page_addr)
.unwrap_or_else(|_| self.attacked());
}
self.munmap_host(addr_in, length.bytes())
.unwrap_or_else(|_| self.attacked());
Ok(())
}
/// Print a stack trace using the SSA registers.
fn print_ssa_stack_trace(&mut self) {
if DEBUG {
debugln!(self, "{:#x?}", self.ssa.gpr.clone());
unsafe { self.print_stack_trace(self.ssa.gpr.rip, self.ssa.gpr.rbp) }
}
}
/// Print out `rip` relative to the shim (S) or the exec (E) base address.
///
/// This can be used with `addr2line` and the executable with debug info
/// to get the function name and line number.
unsafe fn print_rip(&mut self, rip: u64) {
let enarx_exec_start = &ENARX_EXEC_START as *const _ as u64;
let enarx_exec_end = &ENARX_EXEC_END as *const _ as u64;
let exec_range = enarx_exec_start..enarx_exec_end;
if exec_range.contains(&rip) {
let rip_pie = rip - enarx_exec_start;
debugln!(self, "E {:>#016x}", rip_pie);
} else {
let rip_pie = rip - shim_address() as u64;
debugln!(self, "S {:>#016x}", rip_pie);
}
}
/// Print a stack trace with the old `rbp` stack frame pointers
unsafe fn print_stack_trace(&mut self, rip: u64, mut rbp: u64) {
// TODO: parse the elf and actually find the text sections.
let encl_start = self as *const _ as u64 / ENCL_SIZE as u64 * ENCL_SIZE as u64;
let encl_end = encl_start + ENCL_SIZE as u64;
let encl_range = encl_start..encl_end;
debugln!(self, "TRACE:");
self.print_rip(rip);
// Maximum 64 frames
for _frame in 0..64 {
if rbp == 0 || rbp & 7 != 0 {
break;
}
if !encl_range.contains(&rbp) {
debugln!(self, "invalid rbp: {:>#016x}", rbp);
break;
}
match rbp.checked_add(size_of::<usize>() as _) {
None => break,
Some(rip_rbp) => {
let rip = *(rip_rbp as *const u64);
match rip.checked_sub(1) {
None => break,
Some(0) => break,
Some(rip) => {
self.print_rip(rip);
rbp = *(rbp as *const u64);
}
}
}
}
}
}
fn thread_mem_alloc(&mut self) -> sallyport::Result<*const Tcs> {
let usermemscope = UserMemScope;
// Allocate the whole block of memory used for the thread.
// It is easier to do this in one go and punch holes in it,
// than to allocate each part separately.
let addr = self.mmap(
&usermemscope,
None,
size_of::<ThreadMem>(),
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS,
-1,
0,
)?;
// # Safety
// ThreadMem is a POD type and the memory is aligned and zeroed.
let tm = unsafe { &mut *(addr.as_ptr() as *mut ThreadMem) };
let stack_end_1 = NonNull::new(&mut tm.cssa_stack as *mut _ as *mut c_void).unwrap();
let stack_end_0 = NonNull::new(&mut tm.stack as *mut _ as *mut c_void).unwrap();
let tcs = &tm.tcs as *const Tcs;
let ssa = tm.ssa.as_ptr();
// address is relative to the enclave base (`shim_address()`)
tm.tcs.ossa = ssa as u64 - shim_address() as u64;
// start with level 0
tm.tcs.cssa = 0;
// number of SSA frames
tm.tcs.nssa = NUM_SSA as _;
// address is relative to the enclave base
tm.tcs.oentry = self.start - shim_address() as u64;
// unmap stack guard pages, so a stack overflow will cause a page fault
self.munmap(&usermemscope, stack_end_1, Page::SIZE)
.unwrap_or_else(|_| self.attacked());
self.munmap(&usermemscope, stack_end_0, Page::SIZE)
.unwrap_or_else(|_| self.attacked());
self.modify_sgx_page_type(
NonNull::new(tcs as *mut c_void).unwrap(),
Page::SIZE,
Class::Tcs as _,
)
.unwrap_or_else(|_| self.attacked());
let virt_addr = VirtAddr::from_ptr(tcs);
// # Safety
//
// The address must be page aligned.
let page_addr = unsafe { PageAddr::from_start_address_unchecked(virt_addr) };
Class::Tcs
.info(Flags::MODIFIED)
.accept(page_addr)
.unwrap_or_else(|_| self.attacked());
Ok(tcs)
}
// FIXME: https://github.com/enarx/enarx/issues/2251
#[allow(dead_code)]
fn thread_mem_free(&mut self, tcs: *const Tcs) -> sallyport::Result<()> {
let usermemscope = UserMemScope;
let tm_start = tcs as usize - CSSA_1_PLUS_STACK_SIZE - CSSA_0_STACK_SIZE - Page::SIZE;
let tm_addr = NonNull::new(tm_start as *const ThreadMem as *mut c_void).unwrap();
debugln!(
self,
"drop_thread_mem: {:#?} {}",
tm_addr,
size_of::<ThreadMem>()
);
self.munmap(&usermemscope, tm_addr, size_of::<ThreadMem>())?;
Ok(())
}
}