-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathenclave_ocalls.c
2436 lines (2054 loc) · 84.5 KB
/
enclave_ocalls.c
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
/*
* This is for enclave to make ocalls to untrusted runtime.
*
* Most ocall implementations retry the host-level operations on -EINTR, except for a few ocalls
* that are expected to be able to return -EINTR (read, write, recv, send, accept, connect, sleep,
* futex). The -EINTR error happens when an async signal arrives from the host OS, with two
* sub-cases: (a) signal arrives during a slow host-level syscall or (b) signal arrives during other
* untrusted-PAL code execution. In both cases, the untrusted-PAL signal handler injects -EINTR and
* forces an ocall return. To prevent ocalls from returning -EINTR to unsuspecting LibOS/user app,
* here we retry a host-level syscall. In some cases, this may lead to FD leaks or incorrect
* semantics (e.g., a retried open() syscall may have succeeded the first time, but a signal arrived
* right-after this syscall and forced -EINTR, thus leaving an FD from the first try open and
* leaking). See also `man 7 signal` and `sgx_exception.c: handle_async_signal()`.
*
* FIXME: Ideally, the untrusted-PAL signal handler must inspect the interrupted RIP and unwind/fix
* the untrusted state and decide whether to inject -EINTR or report success (e.g., if a
* signal arrives right-after an open() syscall, the signal handler must update ocall return
* values and report success instead of injecting -EINTR).
*/
#include <asm/errno.h>
#include <linux/futex.h>
#include <stdalign.h>
#include <stdbool.h>
#include <stdint.h>
#include "api.h"
#include "asan.h"
#include "cpu.h"
#include "enclave_ocalls.h"
#include "pal_internal.h"
#include "pal_ocall_types.h"
#include "pal_rpc_queue.h"
#include "sgx_attest.h"
#include "spinlock.h"
/* Check against this limit if the buffer to be allocated fits on the untrusted stack; if not,
* buffer will be allocated on untrusted heap. Conservatively set this limit to 1/4 of the
* actual stack size. Currently THREAD_STACK_SIZE = 2MB, so this limit is 512KB.
* Note that the main thread is special in that it is handled by Linux, with the typical stack
* size of 8MB. Thus, 512KB limit also works well for the main thread. */
#define MAX_UNTRUSTED_STACK_BUF (THREAD_STACK_SIZE / 4)
/* global pointer to a single untrusted queue, all accesses must be protected by g_rpc_queue->lock
*/
rpc_queue_t* g_rpc_queue = NULL;
static long sgx_exitless_ocall(uint64_t code, void* ocall_args) {
/* perform OCALL with enclave exit if no RPC queue (i.e., no exitless); no need for atomics
* because this pointer is set only once at enclave initialization */
if (!g_rpc_queue)
return sgx_ocall(code, ocall_args);
/* allocate request in a new stack frame on OCALL stack; note that request's lock is used in
* futex() and must be aligned to at least 4B */
void* old_ustack = sgx_prepare_ustack();
rpc_request_t* req = sgx_alloc_on_ustack_aligned(sizeof(*req), alignof(*req));
if (!req) {
sgx_reset_ustack(old_ustack);
return -ENOMEM;
}
COPY_VALUE_TO_UNTRUSTED(&req->ocall_index, code);
COPY_VALUE_TO_UNTRUSTED(&req->buffer, ocall_args);
spinlock_init(&req->lock);
/* grab the lock on this request (it is the responsibility of RPC thread to unlock it when
* done); this always succeeds immediately since enclave thread is currently the only owner
* of the lock */
spinlock_lock(&req->lock);
/* enqueue OCALL request into RPC queue; some RPC thread will dequeue it, issue a syscall
* and, after syscall is finished, release the request's spinlock */
bool enqueued = rpc_enqueue(g_rpc_queue, req);
if (!enqueued) {
/* no space in queue: all RPC threads are busy with outstanding ocalls; fallback to normal
* syscall path with enclave exit */
sgx_reset_ustack(old_ustack);
return sgx_ocall(code, ocall_args);
}
/* wait till request processing is finished; try spinlock first */
bool locked = spinlock_lock_timeout(&req->lock, RPC_SPINLOCK_TIMEOUT);
/* at this point:
* - either RPC thread is done with OCALL and released the request's spinlock,
* and our enclave thread grabbed lock but it doesn't matter at this point
* (OCALL is done, locked == true, no need to wait on futex)
* - or OCALL is still pending and the request is still blocked on spinlock
* (OCALL is not done, locked == false, let's wait on futex) */
if (!locked) {
/* OCALL takes a lot of time, so fallback to waiting on a futex; at this point we exit
* enclave to perform syscall; this code is based on Mutex 2 from Futexes are Tricky */
uint32_t c = SPINLOCK_UNLOCKED;
/* at this point can be a subtle data race: RPC thread is only now done with OCALL and
* moved lock in UNLOCKED state; in this racey case, lock = UNLOCKED = 0 and we do not
* wait on futex (note that enclave thread grabbed lock but it doesn't matter) */
if (!spinlock_cmpxchg(&req->lock, &c, SPINLOCK_LOCKED_NO_WAITERS)) {
/* allocate futex args on OCALL stack */
struct ocall_futex* ocall_futex_args;
ocall_futex_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_futex_args),
alignof(*ocall_futex_args));
if (!ocall_futex_args) {
sgx_reset_ustack(old_ustack);
return -ENOMEM;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_futex_args->futex, &req->lock.lock);
COPY_VALUE_TO_UNTRUSTED(&ocall_futex_args->op, FUTEX_WAIT_PRIVATE);
COPY_VALUE_TO_UNTRUSTED(&ocall_futex_args->timeout_us, (uint64_t)-1); /* never time out */
do {
/* at this point lock = LOCKED_*; before waiting on futex, need to move lock to
* LOCKED_WITH_WAITERS; note that check on cmpxchg of lock = UNLOCKED = 0 is for
* the same data race as above */
if (c == SPINLOCK_LOCKED_WITH_WAITERS || /* shortcut: don't need to move lock state */
spinlock_cmpxchg(&req->lock, &c, SPINLOCK_LOCKED_WITH_WAITERS)) {
/* at this point, futex(wait) syscall expects lock to be in LOCKED_WITH_WAITERS
* set by enclave thread above; if RPC thread moved it back to UNLOCKED, futex()
* immediately returns */
COPY_VALUE_TO_UNTRUSTED(&ocall_futex_args->val, SPINLOCK_LOCKED_WITH_WAITERS);
int ret = sgx_ocall(OCALL_FUTEX, ocall_futex_args);
if (ret < 0 && ret != -EAGAIN) {
sgx_reset_ustack(old_ustack);
return -EPERM;
}
}
c = SPINLOCK_UNLOCKED;
} while (!spinlock_cmpxchg(&req->lock, &c, SPINLOCK_LOCKED_WITH_WAITERS));
/* while-loop is required for spurious futex wake-ups: our enclave thread must wait
* until lock moves to UNLOCKED (note that enclave thread grabs lock but it doesn't
* matter at this point) */
}
}
sgx_reset_ustack(old_ustack);
return COPY_UNTRUSTED_VALUE(&req->result);
}
__attribute_no_sanitize_address
noreturn void ocall_exit(int exitcode, int is_exitgroup) {
struct ocall_exit* ocall_exit_args;
sgx_prepare_ustack();
ocall_exit_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_exit_args),
alignof(*ocall_exit_args));
if (!ocall_exit_args) {
/* We can't really recover from here. Should be unreachable without the host doing malicious
* things. */
die_or_inf_loop();
}
COPY_VALUE_TO_UNTRUSTED(&ocall_exit_args->exitcode, exitcode);
COPY_VALUE_TO_UNTRUSTED(&ocall_exit_args->is_exitgroup, is_exitgroup);
#ifdef ASAN
/* Unpoison the stacks allocated for this thread. They can be later used for a new thread. */
uintptr_t initial_stack_addr = GET_ENCLAVE_TCB(initial_stack_addr);
asan_unpoison_region(initial_stack_addr - ENCLAVE_STACK_SIZE, ENCLAVE_STACK_SIZE);
uintptr_t sig_stack_low = GET_ENCLAVE_TCB(sig_stack_low);
uintptr_t sig_stack_high = GET_ENCLAVE_TCB(sig_stack_high);
asan_unpoison_region(sig_stack_low, sig_stack_high - sig_stack_low);
#endif
// There are two reasons for this loop:
// 1. Ocalls can be interuppted.
// 2. We can't trust the outside to actually exit, so we need to ensure
// that we never return even when the outside tries to trick us (this
// case should be already catched by enclave_entry.S).
while (true) {
sgx_ocall(OCALL_EXIT, ocall_exit_args);
}
}
int ocall_mmap_untrusted(void** addrptr, size_t size, int prot, int flags, int fd, off_t offset) {
int retval = 0;
struct ocall_mmap_untrusted* ocall_mmap_args;
void* old_ustack = sgx_prepare_ustack();
ocall_mmap_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_mmap_args),
alignof(*ocall_mmap_args));
if (!ocall_mmap_args) {
sgx_reset_ustack(old_ustack);
return -EPERM;
}
if (!addrptr) {
sgx_reset_ustack(old_ustack);
return -EINVAL;
}
void* requested_addr = *addrptr;
if (flags & MAP_FIXED || flags & MAP_FIXED_NOREPLACE) {
if (!sgx_is_valid_untrusted_ptr(requested_addr, size, PAGE_SIZE)) {
sgx_reset_ustack(old_ustack);
return -EINVAL;
}
} else {
requested_addr = NULL; /* for sanity */
}
COPY_VALUE_TO_UNTRUSTED(&ocall_mmap_args->addr, requested_addr);
COPY_VALUE_TO_UNTRUSTED(&ocall_mmap_args->size, size);
COPY_VALUE_TO_UNTRUSTED(&ocall_mmap_args->prot, prot);
COPY_VALUE_TO_UNTRUSTED(&ocall_mmap_args->flags, flags);
COPY_VALUE_TO_UNTRUSTED(&ocall_mmap_args->fd, fd);
COPY_VALUE_TO_UNTRUSTED(&ocall_mmap_args->offset, offset);
do {
retval = sgx_exitless_ocall(OCALL_MMAP_UNTRUSTED, ocall_mmap_args);
} while (retval == -EINTR);
if (retval < 0) {
if (retval != -EACCES && retval != -EAGAIN && retval != -EBADF && retval != -EINVAL &&
retval != -ENFILE && retval != -ENODEV && retval != -ENOMEM && retval != -EEXIST &&
retval != -EPERM) {
retval = -EPERM;
}
sgx_reset_ustack(old_ustack);
return retval;
}
void* returned_addr = COPY_UNTRUSTED_VALUE(&ocall_mmap_args->addr);
if (flags & MAP_FIXED || flags & MAP_FIXED_NOREPLACE) {
/* addrptr already contains the mmap'ed address, no need to update it */
if (returned_addr != requested_addr) {
sgx_reset_ustack(old_ustack);
return -EPERM;
}
} else {
/* update addrptr with the mmap'ed address */
if (!sgx_is_valid_untrusted_ptr(returned_addr, size, PAGE_SIZE)) {
sgx_reset_ustack(old_ustack);
return -EPERM;
}
*addrptr = returned_addr;
}
sgx_reset_ustack(old_ustack);
return 0;
}
int ocall_munmap_untrusted(const void* addr, size_t size) {
int retval = 0;
struct ocall_munmap_untrusted* ocall_munmap_args;
if (!sgx_is_valid_untrusted_ptr(addr, size, PAGE_SIZE))
return -EINVAL;
void* old_ustack = sgx_prepare_ustack();
ocall_munmap_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_munmap_args),
alignof(*ocall_munmap_args));
if (!ocall_munmap_args) {
sgx_reset_ustack(old_ustack);
return -EPERM;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_munmap_args->addr, addr);
COPY_VALUE_TO_UNTRUSTED(&ocall_munmap_args->size, size);
do {
retval = sgx_exitless_ocall(OCALL_MUNMAP_UNTRUSTED, ocall_munmap_args);
} while (retval == -EINTR);
if (retval < 0 && retval != -EINVAL) {
retval = -EPERM;
}
sgx_reset_ustack(old_ustack);
return retval;
}
/*
* Memorize untrusted memory area to avoid mmap/munmap per each read/write IO. Because this cache
* is per-thread, we don't worry about concurrency. The cache will be carried over thread
* exit/creation. On fork/exec emulation, untrusted code does vfork/exec, so the mmapped cache
* will be released by exec host syscall.
*
* In case of AEX and consequent signal handling, current thread may be interrupted in the middle
* of using the cache. If there are OCALLs during signal handling, they could interfere with the
* normal-execution use of the cache, so 'in_use' atomic protects against it. OCALLs during signal
* handling do not use the cache and always explicitly mmap/munmap untrusted memory; 'need_munmap'
* indicates whether explicit munmap is needed at the end of such OCALL.
*/
static int ocall_mmap_untrusted_cache(size_t size, void** addrptr, bool* need_munmap) {
int ret;
*addrptr = NULL;
*need_munmap = false;
struct untrusted_area* cache = &pal_get_enclave_tcb()->untrusted_area_cache;
uint64_t in_use = 0;
if (!__atomic_compare_exchange_n(&cache->in_use, &in_use, 1, /*weak=*/false, __ATOMIC_RELAXED,
__ATOMIC_RELAXED)) {
/* AEX signal handling case: cache is in use, so make explicit mmap/munmap */
ret = ocall_mmap_untrusted(addrptr, size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, /*fd=*/-1, /*offset=*/0);
if (ret < 0) {
return ret;
}
*need_munmap = true;
return 0;
}
COMPILER_BARRIER();
/* normal execution case: cache was not in use, so use it/allocate new one for reuse */
if (cache->valid) {
if (cache->size >= size) {
*addrptr = cache->addr;
return 0;
}
ret = ocall_munmap_untrusted(cache->addr, cache->size);
if (ret < 0) {
cache->valid = false;
COMPILER_BARRIER();
__atomic_store_n(&cache->in_use, 0, __ATOMIC_RELAXED);
return ret;
}
}
ret = ocall_mmap_untrusted(addrptr, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE,
/*fd=*/-1, /*offset=*/0);
if (ret < 0) {
cache->valid = false;
COMPILER_BARRIER();
__atomic_store_n(&cache->in_use, 0, __ATOMIC_RELAXED);
} else {
cache->valid = true;
cache->addr = *addrptr;
cache->size = size;
}
return ret;
}
static void ocall_munmap_untrusted_cache(void* addr, size_t size, bool need_munmap) {
if (need_munmap) {
ocall_munmap_untrusted(addr, size);
/* there is not much we can do in case of error */
} else {
struct untrusted_area* cache = &pal_get_enclave_tcb()->untrusted_area_cache;
__atomic_store_n(&cache->in_use, 0, __ATOMIC_RELAXED);
}
}
int ocall_cpuid(unsigned int leaf, unsigned int subleaf, unsigned int values[static 4]) {
int retval = 0;
struct ocall_cpuid* ocall_cpuid_args;
void* old_ustack = sgx_prepare_ustack();
ocall_cpuid_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_cpuid_args),
alignof(*ocall_cpuid_args));
if (!ocall_cpuid_args) {
sgx_reset_ustack(old_ustack);
return -EPERM;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_cpuid_args->leaf, leaf);
COPY_VALUE_TO_UNTRUSTED(&ocall_cpuid_args->subleaf, subleaf);
do {
/* cpuid must be retrieved in the context of current logical core, cannot use exitless */
retval = sgx_ocall(OCALL_CPUID, ocall_cpuid_args);
} while (retval == -EINTR);
if (retval < 0) {
log_error("OCALL_CPUID returned an error (impossible on benign host)");
_PalProcessExit(1);
}
if (!retval) {
values[0] = COPY_UNTRUSTED_VALUE(&ocall_cpuid_args->values[0]);
values[1] = COPY_UNTRUSTED_VALUE(&ocall_cpuid_args->values[1]);
values[2] = COPY_UNTRUSTED_VALUE(&ocall_cpuid_args->values[2]);
values[3] = COPY_UNTRUSTED_VALUE(&ocall_cpuid_args->values[3]);
}
sgx_reset_ustack(old_ustack);
return retval;
}
int ocall_open(const char* pathname, int flags, unsigned short mode) {
int retval = 0;
size_t path_size = pathname ? strlen(pathname) + 1 : 0;
struct ocall_open* ocall_open_args;
void* old_ustack = sgx_prepare_ustack();
ocall_open_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_open_args),
alignof(*ocall_open_args));
if (!ocall_open_args) {
sgx_reset_ustack(old_ustack);
return -EPERM;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_open_args->flags, flags);
COPY_VALUE_TO_UNTRUSTED(&ocall_open_args->mode, mode);
void* untrusted_pathname = sgx_copy_to_ustack(pathname, path_size);
if (!untrusted_pathname) {
sgx_reset_ustack(old_ustack);
return -EPERM;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_open_args->pathname, untrusted_pathname);
do {
retval = sgx_exitless_ocall(OCALL_OPEN, ocall_open_args);
} while (retval == -EINTR);
if (retval < 0 && retval != -EACCES && retval != -EEXIST && retval != -EINVAL &&
retval != -EISDIR && retval != -ELOOP && retval != -EMFILE &&
retval != -ENAMETOOLONG && retval != -ENFILE && retval != -ENODEV &&
retval != -ENOENT && retval != -ENOMEM && retval != -ENOTDIR && retval != -EROFS &&
retval != -EWOULDBLOCK) {
retval = -EPERM;
}
sgx_reset_ustack(old_ustack);
return retval;
}
int ocall_close(int fd) {
int retval = 0;
struct ocall_close* ocall_close_args;
void* old_ustack = sgx_prepare_ustack();
ocall_close_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_close_args),
alignof(*ocall_close_args));
if (!ocall_close_args) {
sgx_reset_ustack(old_ustack);
return -EPERM;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_close_args->fd, fd);
/* We should never restart host-level `close` syscall on errors (including `EINTR`), but
* `sgx_ocall_close` does not forward any errors (always returns `0`), so this can only be
* an injected `EINTR`. */
do {
retval = sgx_exitless_ocall(OCALL_CLOSE, ocall_close_args);
} while (retval == -EINTR);
if (retval < 0 && retval != -EBADF && retval != -EIO) {
retval = -EPERM;
}
sgx_reset_ustack(old_ustack);
return retval;
}
ssize_t ocall_read(int fd, void* buf, size_t count) {
ssize_t retval = 0;
void* obuf = NULL;
struct ocall_read* ocall_read_args;
void* untrusted_buf;
bool need_munmap = false;
void* old_ustack = sgx_prepare_ustack();
if (count > MAX_UNTRUSTED_STACK_BUF) {
retval = ocall_mmap_untrusted_cache(ALLOC_ALIGN_UP(count), &obuf, &need_munmap);
if (retval < 0) {
sgx_reset_ustack(old_ustack);
return retval;
}
untrusted_buf = obuf;
} else {
untrusted_buf = sgx_alloc_on_ustack(count);
if (!untrusted_buf) {
retval = -EPERM;
goto out;
}
}
ocall_read_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_read_args),
alignof(*ocall_read_args));
if (!ocall_read_args) {
retval = -EPERM;
goto out;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_read_args->fd, fd);
COPY_VALUE_TO_UNTRUSTED(&ocall_read_args->count, count);
COPY_VALUE_TO_UNTRUSTED(&ocall_read_args->buf, untrusted_buf);
retval = sgx_exitless_ocall(OCALL_READ, ocall_read_args);
if (retval < 0 && retval != -EAGAIN && retval != -EWOULDBLOCK && retval != -EBADF &&
retval != -EINTR && retval != -EINVAL && retval != -EIO && retval != -EISDIR) {
retval = -EPERM;
}
if (retval > 0) {
if ((size_t)retval > count) {
retval = -EPERM;
goto out;
}
if (!sgx_copy_to_enclave(buf, count, untrusted_buf, retval)) {
retval = -EPERM;
goto out;
}
}
out:
sgx_reset_ustack(old_ustack);
if (obuf)
ocall_munmap_untrusted_cache(obuf, ALLOC_ALIGN_UP(count), need_munmap);
return retval;
}
ssize_t ocall_write(int fd, const void* buf, size_t count) {
ssize_t retval = 0;
void* obuf = NULL;
struct ocall_write* ocall_write_args;
const void* untrusted_buf;
bool need_munmap = false;
void* old_ustack = sgx_prepare_ustack();
if (sgx_is_valid_untrusted_ptr(buf, count, /*alignment=*/1)) {
/* buf is in untrusted memory (e.g., allowed file mmaped in untrusted memory) */
untrusted_buf = buf;
} else if (sgx_is_completely_within_enclave(buf, count)) {
/* typical case of buf inside of enclave memory */
if (count > MAX_UNTRUSTED_STACK_BUF) {
/* buf is too big and may overflow untrusted stack, so use untrusted heap */
retval = ocall_mmap_untrusted_cache(ALLOC_ALIGN_UP(count), &obuf, &need_munmap);
if (retval < 0) {
goto out;
}
memcpy(obuf, buf, count);
untrusted_buf = obuf;
} else {
untrusted_buf = sgx_copy_to_ustack(buf, count);
if (!untrusted_buf) {
retval = -EPERM;
goto out;
}
}
} else {
/* buf is partially in/out of enclave memory */
retval = -EPERM;
goto out;
}
ocall_write_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_write_args),
alignof(*ocall_write_args));
if (!ocall_write_args) {
retval = -EPERM;
goto out;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_write_args->fd, fd);
COPY_VALUE_TO_UNTRUSTED(&ocall_write_args->count, count);
COPY_VALUE_TO_UNTRUSTED(&ocall_write_args->buf, untrusted_buf);
retval = sgx_exitless_ocall(OCALL_WRITE, ocall_write_args);
if (retval < 0 && retval != -EAGAIN && retval != -EWOULDBLOCK && retval != -EBADF &&
retval != -EFBIG && retval != -EINTR && retval != -EINVAL && retval != -EIO &&
retval != -ENOSPC && retval != -EPIPE) {
retval = -EPERM;
}
if (retval > 0 && (size_t)retval > count) {
retval = -EPERM;
goto out;
}
out:
sgx_reset_ustack(old_ustack);
if (obuf)
ocall_munmap_untrusted_cache(obuf, ALLOC_ALIGN_UP(count), need_munmap);
return retval;
}
ssize_t ocall_pread(int fd, void* buf, size_t count, off_t offset) {
long retval = 0;
void* obuf = NULL;
struct ocall_pread* ocall_pread_args;
void* untrusted_buf;
bool need_munmap = false;
void* old_ustack = sgx_prepare_ustack();
if (count > MAX_UNTRUSTED_STACK_BUF) {
retval = ocall_mmap_untrusted_cache(ALLOC_ALIGN_UP(count), &obuf, &need_munmap);
if (retval < 0) {
sgx_reset_ustack(old_ustack);
return retval;
}
untrusted_buf = obuf;
} else {
untrusted_buf = sgx_alloc_on_ustack(count);
if (!untrusted_buf) {
retval = -EPERM;
goto out;
}
}
ocall_pread_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_pread_args),
alignof(*ocall_pread_args));
if (!ocall_pread_args) {
retval = -EPERM;
goto out;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_pread_args->fd, fd);
COPY_VALUE_TO_UNTRUSTED(&ocall_pread_args->count, count);
COPY_VALUE_TO_UNTRUSTED(&ocall_pread_args->offset, offset);
COPY_VALUE_TO_UNTRUSTED(&ocall_pread_args->buf, untrusted_buf);
retval = sgx_exitless_ocall(OCALL_PREAD, ocall_pread_args);
if (retval < 0 && retval != -EAGAIN && retval != -EWOULDBLOCK && retval != -EBADF &&
retval != -EINTR && retval != -EINVAL && retval != -EIO && retval != -EISDIR &&
retval != -ENXIO && retval != -EOVERFLOW && retval != -ESPIPE) {
retval = -EPERM;
}
if (retval > 0) {
if ((size_t)retval > count) {
retval = -EPERM;
goto out;
}
if (!sgx_copy_to_enclave(buf, count, untrusted_buf, retval)) {
retval = -EPERM;
}
}
out:
sgx_reset_ustack(old_ustack);
if (obuf)
ocall_munmap_untrusted_cache(obuf, ALLOC_ALIGN_UP(count), need_munmap);
return retval;
}
ssize_t ocall_pwrite(int fd, const void* buf, size_t count, off_t offset) {
long retval = 0;
void* obuf = NULL;
struct ocall_pwrite* ocall_pwrite_args;
const void* untrusted_buf;
bool need_munmap = false;
void* old_ustack = sgx_prepare_ustack();
if (sgx_is_valid_untrusted_ptr(buf, count, /*alignment=*/1)) {
/* buf is in untrusted memory (e.g., allowed file mmaped in untrusted memory) */
untrusted_buf = buf;
} else if (sgx_is_completely_within_enclave(buf, count)) {
/* typical case of buf inside of enclave memory */
if (count > MAX_UNTRUSTED_STACK_BUF) {
/* buf is too big and may overflow untrusted stack, so use untrusted heap */
retval = ocall_mmap_untrusted_cache(ALLOC_ALIGN_UP(count), &obuf, &need_munmap);
if (retval < 0) {
goto out;
}
memcpy(obuf, buf, count);
untrusted_buf = obuf;
} else {
untrusted_buf = sgx_copy_to_ustack(buf, count);
if (!untrusted_buf) {
retval = -EPERM;
goto out;
}
}
} else {
/* buf is partially in/out of enclave memory */
retval = -EPERM;
goto out;
}
ocall_pwrite_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_pwrite_args),
alignof(*ocall_pwrite_args));
if (!ocall_pwrite_args) {
retval = -EPERM;
goto out;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_pwrite_args->fd, fd);
COPY_VALUE_TO_UNTRUSTED(&ocall_pwrite_args->count, count);
COPY_VALUE_TO_UNTRUSTED(&ocall_pwrite_args->offset, offset);
COPY_VALUE_TO_UNTRUSTED(&ocall_pwrite_args->buf, untrusted_buf);
retval = sgx_exitless_ocall(OCALL_PWRITE, ocall_pwrite_args);
if (retval < 0 && retval != -EAGAIN && retval != -EWOULDBLOCK && retval != -EBADF &&
retval != -EFBIG && retval != -EINTR && retval != -EINVAL && retval != -EIO &&
retval != -ENOSPC && retval != -ENXIO && retval != -EOVERFLOW && retval != -EPIPE &&
retval != -ESPIPE) {
retval = -EPERM;
}
if (retval > 0 && (size_t)retval > count) {
retval = -EPERM;
goto out;
}
out:
sgx_reset_ustack(old_ustack);
if (obuf)
ocall_munmap_untrusted_cache(obuf, ALLOC_ALIGN_UP(count), need_munmap);
return retval;
}
int ocall_fstat(int fd, struct stat* buf) {
int retval = 0;
struct ocall_fstat* ocall_fstat_args;
void* old_ustack = sgx_prepare_ustack();
ocall_fstat_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_fstat_args),
alignof(*ocall_fstat_args));
if (!ocall_fstat_args) {
sgx_reset_ustack(old_ustack);
return -EPERM;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_fstat_args->fd, fd);
do {
retval = sgx_exitless_ocall(OCALL_FSTAT, ocall_fstat_args);
} while (retval == -EINTR);
if (retval < 0 && retval != -EACCES && retval != -EBADF && retval != -ELOOP &&
retval != -ENAMETOOLONG && retval != -ENOENT && retval != -ENOMEM &&
retval != -ENOTDIR) {
retval = -EPERM;
}
if (!retval) {
if (!sgx_copy_to_enclave(buf, sizeof(*buf), &ocall_fstat_args->stat, sizeof(struct stat))) {
retval = -EPERM;
}
/* TODO: sanitize `buf`, e.g. that `buf->st_size >= 0` */
}
sgx_reset_ustack(old_ustack);
return retval;
}
int ocall_fionread(int fd) {
int retval = 0;
struct ocall_fionread* ocall_fionread_args;
void* old_ustack = sgx_prepare_ustack();
ocall_fionread_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_fionread_args),
alignof(*ocall_fionread_args));
if (!ocall_fionread_args) {
sgx_reset_ustack(old_ustack);
return -EPERM;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_fionread_args->fd, fd);
do {
retval = sgx_exitless_ocall(OCALL_FIONREAD, ocall_fionread_args);
} while (retval == -EINTR);
if (retval < 0 && retval != -EBADF && retval != -EINVAL && retval != -ENOTTY) {
retval = -EPERM;
}
sgx_reset_ustack(old_ustack);
return retval;
}
int ocall_fsetnonblock(int fd, int nonblocking) {
int retval = 0;
struct ocall_fsetnonblock* ocall_fsetnonblock_args;
void* old_ustack = sgx_prepare_ustack();
ocall_fsetnonblock_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_fsetnonblock_args),
alignof(*ocall_fsetnonblock_args));
if (!ocall_fsetnonblock_args) {
sgx_reset_ustack(old_ustack);
return -EPERM;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_fsetnonblock_args->fd, fd);
COPY_VALUE_TO_UNTRUSTED(&ocall_fsetnonblock_args->nonblocking, nonblocking);
do {
retval = sgx_exitless_ocall(OCALL_FSETNONBLOCK, ocall_fsetnonblock_args);
} while (retval == -EINTR);
if (retval < 0 && retval != -EACCES && retval != -EAGAIN && retval != -EBADF &&
retval != -EINVAL && retval != -EPERM) {
retval = -EPERM;
}
sgx_reset_ustack(old_ustack);
return retval;
}
/* TODO: Unneeded OCALL? Gramine doesn't have a notion of permissions currently. */
int ocall_fchmod(int fd, unsigned short mode) {
int retval = 0;
struct ocall_fchmod* ocall_fchmod_args;
void* old_ustack = sgx_prepare_ustack();
ocall_fchmod_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_fchmod_args),
alignof(*ocall_fchmod_args));
if (!ocall_fchmod_args) {
sgx_reset_ustack(old_ustack);
return -EPERM;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_fchmod_args->fd, fd);
COPY_VALUE_TO_UNTRUSTED(&ocall_fchmod_args->mode, mode);
do {
retval = sgx_exitless_ocall(OCALL_FCHMOD, ocall_fchmod_args);
} while (retval == -EINTR);
if (retval < 0 && retval != -EACCES && retval != -EIO && retval != -ELOOP &&
retval != -ENAMETOOLONG && retval != -ENOENT && retval != -ENOMEM &&
retval != -ENOTDIR && retval != -EPERM && retval != -EROFS) {
retval = -EPERM;
}
sgx_reset_ustack(old_ustack);
return retval;
}
int ocall_fsync(int fd) {
int retval = 0;
struct ocall_fsync* ocall_fsync_args;
void* old_ustack = sgx_prepare_ustack();
ocall_fsync_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_fsync_args),
alignof(*ocall_fsync_args));
if (!ocall_fsync_args) {
sgx_reset_ustack(old_ustack);
return -EPERM;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_fsync_args->fd, fd);
do {
retval = sgx_exitless_ocall(OCALL_FSYNC, ocall_fsync_args);
} while (retval == -EINTR);
if (retval < 0 && retval != -EBADF && retval != -EIO && retval != -EINVAL && retval != -EROFS) {
retval = -EPERM;
}
sgx_reset_ustack(old_ustack);
return retval;
}
int ocall_ftruncate(int fd, uint64_t length) {
int retval = 0;
struct ocall_ftruncate* ocall_ftruncate_args;
void* old_ustack = sgx_prepare_ustack();
ocall_ftruncate_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_ftruncate_args),
alignof(*ocall_ftruncate_args));
if (!ocall_ftruncate_args) {
sgx_reset_ustack(old_ustack);
return -EPERM;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_ftruncate_args->fd, fd);
COPY_VALUE_TO_UNTRUSTED(&ocall_ftruncate_args->length, length);
do {
retval = sgx_exitless_ocall(OCALL_FTRUNCATE, ocall_ftruncate_args);
} while (retval == -EINTR);
if (retval < 0 && retval != -EBADF && retval != -EINVAL && retval != -EPERM &&
retval != -EROFS) {
retval = -EPERM;
}
sgx_reset_ustack(old_ustack);
return retval;
}
int ocall_mkdir(const char* pathname, unsigned short mode) {
int retval = 0;
size_t path_size = pathname ? strlen(pathname) + 1 : 0;
struct ocall_mkdir* ocall_mkdir_args;
void* old_ustack = sgx_prepare_ustack();
ocall_mkdir_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_mkdir_args),
alignof(*ocall_mkdir_args));
if (!ocall_mkdir_args) {
sgx_reset_ustack(old_ustack);
return -EPERM;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_mkdir_args->mode, mode);
void* untrusted_pathname = sgx_copy_to_ustack(pathname, path_size);
if (!untrusted_pathname) {
sgx_reset_ustack(old_ustack);
return -EPERM;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_mkdir_args->pathname, untrusted_pathname);
do {
retval = sgx_exitless_ocall(OCALL_MKDIR, ocall_mkdir_args);
} while (retval == -EINTR);
if (retval < 0 && retval != -EACCES && retval != -EEXIST && retval != -EINVAL &&
retval != -ELOOP && retval != -EMLINK && retval != -ENAMETOOLONG &&
retval != -ENOENT && retval != -ENOMEM && retval != -ENOSPC && retval != -ENOTDIR &&
retval != -EPERM && retval != -EROFS) {
retval = -EPERM;
}
sgx_reset_ustack(old_ustack);
return retval;
}
int ocall_getdents(int fd, struct linux_dirent64* dirp, size_t dirp_size) {
int retval = 0;
struct ocall_getdents* ocall_getdents_args;
void* old_ustack = sgx_prepare_ustack();
ocall_getdents_args = sgx_alloc_on_ustack_aligned(sizeof(*ocall_getdents_args),
alignof(*ocall_getdents_args));
if (!ocall_getdents_args) {
retval = -EPERM;
goto out;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_getdents_args->fd, fd);
COPY_VALUE_TO_UNTRUSTED(&ocall_getdents_args->size, dirp_size);
void* untrusted_dirp = sgx_alloc_on_ustack_aligned(dirp_size, alignof(*dirp));
if (!untrusted_dirp) {
retval = -EPERM;
goto out;
}
COPY_VALUE_TO_UNTRUSTED(&ocall_getdents_args->dirp, untrusted_dirp);
do {
retval = sgx_exitless_ocall(OCALL_GETDENTS, ocall_getdents_args);
} while (retval == -EINTR);
if (retval < 0 && retval != -EBADF && retval != -EINVAL && retval != -ENOENT &&
retval != -ENOTDIR) {
retval = -EPERM;
}
if (retval > 0) {
size_t size = (size_t)retval;
if (size > dirp_size) {
retval = -EPERM;
goto out;
}
if (!sgx_copy_to_enclave(dirp, dirp_size, untrusted_dirp, size)) {
retval = -EPERM;
goto out;
}
size_t size_left = size;
while (size_left > offsetof(struct linux_dirent64, d_name)) {
/* `drip->d_off` is understandable only by the fs driver in kernel, we have no way of
* validating it. */
if (dirp->d_reclen > size_left) {
retval = -EPERM;
goto out;
}
size_left -= dirp->d_reclen;
dirp = (struct linux_dirent64*)((char*)dirp + dirp->d_reclen);
}
if (size_left != 0) {
retval = -EPERM;
goto out;
}
}
out:
sgx_reset_ustack(old_ustack);
return retval;
}
int ocall_resume_thread(void* tcs) {
int retval = 0;
do {
retval = sgx_exitless_ocall(OCALL_RESUME_THREAD, tcs);
} while (retval == -EINTR);
if (retval < 0 && retval != -EINVAL && retval != -EPERM && retval != -ESRCH) {
retval = -EPERM;
}
return retval;
}
int ocall_clone_thread(void* dynamic_tcs) {
int retval = 0;
/* FIXME: if there was an EINTR, there may be an untrusted thread left over */
do {
/* clone must happen in the context of current (enclave) thread, cannot use exitless;
* in particular, the new (enclave) thread must have the same signal mask as the current
* enclave thread (and NOT signal mask of the RPC thread) */
retval = sgx_ocall(OCALL_CLONE_THREAD, dynamic_tcs);
} while (retval == -EINTR);