This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathenclave_pages.c
424 lines (345 loc) · 14.3 KB
/
enclave_pages.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
#include "enclave_pages.h"
#include "api.h"
#include "list.h"
#include "pal_error.h"
#include "pal_internal.h"
#include "pal_linux.h"
#include "pal_security.h"
#include "gsgx.h"
struct atomic_int g_allocated_pages;
static size_t g_page_size = PRESET_PAGESIZE;
static void* g_heap_bottom;
static void* g_heap_top;
static size_t g_pal_internal_mem_used = 0;
/* list of VMAs of used memory areas kept in DESCENDING order; note that preallocated PAL internal
* memory relies on this descending order of allocations (from high addresses to low), see
* _DkGetAvailableUserAddressRange() for more details */
DEFINE_LIST(heap_vma);
struct heap_vma {
LIST_TYPE(heap_vma) list;
void* bottom;
void* top;
bool is_pal_internal;
};
DEFINE_LISTP(heap_vma);
static LISTP_TYPE(heap_vma) g_heap_vma_list = LISTP_INIT;
static PAL_LOCK g_heap_vma_lock = LOCK_INIT;
/* heap_vma objects are taken from pre-allocated pool to avoid recursive mallocs */
#define MAX_HEAP_VMAS 100000
static struct heap_vma g_heap_vma_pool[MAX_HEAP_VMAS];
static size_t g_heap_vma_num = 0;
static struct heap_vma* g_free_vma = NULL;
/* returns uninitialized heap_vma, the caller is responsible for setting at least bottom/top */
static struct heap_vma* __alloc_vma(void) {
assert(_DkInternalIsLocked(&g_heap_vma_lock));
if (g_free_vma) {
/* simple optimization: if there is a cached free vma object, use it */
assert((uintptr_t)g_free_vma >= (uintptr_t)&g_heap_vma_pool[0]);
assert((uintptr_t)g_free_vma <= (uintptr_t)&g_heap_vma_pool[MAX_HEAP_VMAS - 1]);
struct heap_vma* ret = g_free_vma;
g_free_vma = NULL;
g_heap_vma_num++;
return ret;
}
/* FIXME: this loop may become perf bottleneck on large number of vma objects; however,
* experiments show that this number typically does not exceed 20 (thanks to VMA merging) */
for (size_t i = 0; i < MAX_HEAP_VMAS; i++) {
if (!g_heap_vma_pool[i].bottom && !g_heap_vma_pool[i].top) {
/* found empty slot in the pool, use it */
g_heap_vma_num++;
return &g_heap_vma_pool[i];
}
}
return NULL;
}
static void __free_vma(struct heap_vma* vma) {
assert(_DkInternalIsLocked(&g_heap_vma_lock));
assert((uintptr_t)vma >= (uintptr_t)&g_heap_vma_pool[0]);
assert((uintptr_t)vma <= (uintptr_t)&g_heap_vma_pool[MAX_HEAP_VMAS - 1]);
g_free_vma = vma;
vma->top = 0;
vma->bottom = 0;
g_heap_vma_num--;
}
int init_enclave_pages(void) {
g_heap_bottom = g_pal_sec.heap_min;
g_heap_top = g_pal_sec.heap_max;
return 0;
}
#define SE_DECLSPEC_ALIGN(x) __attribute__((aligned(x)))
static int free_edmm_page_range(void *start, size_t size)
{
void *addr = (void *)((uintptr_t)start & (~(PRESET_PAGESIZE-1)));
void *end = addr + size;
int ret = 0;
struct sgx_range rg;
log_debug("%s: start = %lx, size = %lx\n", __func__, start, size);
SE_DECLSPEC_ALIGN(sizeof(sgx_arch_sec_info_t)) sgx_arch_sec_info_t si;
si.flags = SGX_SECINFO_FLAGS_TRIM | SGX_SECINFO_FLAGS_MODIFIED;
memset(&si.reserved, 0, sizeof(si.reserved));
rg.start_addr = (unsigned long) addr;
rg.nr_pages = size / PRESET_PAGESIZE;
ret = ocall_trim_epc_pages(&rg);
if (ret) {
log_debug("EPC trim page on [%p, %p) failed (%d)\n", addr, end, ret);
return -1;
}
for ( ; addr < end; addr += PRESET_PAGESIZE) {
ret = sgx_accept(&si, addr);
if (ret) {
log_debug("EDMM accept page failed while trimming: %p %d\n", (void *)addr, ret);
return -1;
}
}
ret = ocall_notify_accept(&rg);
if (ret) {
log_debug("EPC notify_accept on [%p, %p) failed (%d)\n", addr, end, ret);
return -1;
}
return 0;
}
static int get_edmm_page_range(void *start, size_t size, bool executable)
{
uintptr_t lo = (uintptr_t)start;
uintptr_t addr = lo + size;
log_debug("%s: start = %lx, size = %lx, is_executable = %s\n", __func__, start, size,
(executable) ? "TRUE" : "FALSE");
SE_DECLSPEC_ALIGN(sizeof(sgx_arch_sec_info_t)) sgx_arch_sec_info_t si;
si.flags = SGX_SECINFO_FLAGS_R | SGX_SECINFO_FLAGS_W | SGX_SECINFO_FLAGS_REG |
SGX_SECINFO_FLAGS_PENDING;
memset(&si.reserved, 0, sizeof(si.reserved));
SE_DECLSPEC_ALIGN(sizeof(sgx_arch_sec_info_t)) sgx_arch_sec_info_t smi = si;
smi.flags |= SGX_SECINFO_FLAGS_X;
while (lo < addr) {
int ret;
addr -= PRESET_PAGESIZE;
ret = sgx_accept(&si, (const void *)addr);
if (ret) {
log_debug("EDMM accept page failed: %p %d\n", (void *)addr, ret);
return -1;
}
/* EMODPE doesn't return any value */
if (executable)
sgx_modpe(&smi, (const void *)addr);
}
return 0;
}
static void* __create_vma_and_merge(void* addr, size_t size, bool is_pal_internal,
struct heap_vma* vma_above, size_t* new_allocation) {
assert(_DkInternalIsLocked(&g_heap_vma_lock));
assert(addr && size);
if (addr < g_heap_bottom)
return NULL;
/* find enclosing VMAs and check that pal-internal VMAs do not overlap with normal VMAs */
struct heap_vma* vma_below;
if (vma_above) {
vma_below = LISTP_NEXT_ENTRY(vma_above, &g_heap_vma_list, list);
} else {
/* no VMA above `addr`; VMA right below `addr` must be the first (highest-address) in list */
vma_below = LISTP_FIRST_ENTRY(&g_heap_vma_list, struct heap_vma, list);
}
/* check whether [addr, addr + size) overlaps with above VMAs of different type */
struct heap_vma* check_vma_above = vma_above;
while (check_vma_above && addr + size > check_vma_above->bottom) {
if (check_vma_above->is_pal_internal != is_pal_internal) {
return NULL;
}
check_vma_above = LISTP_PREV_ENTRY(check_vma_above, &g_heap_vma_list, list);
}
/* check whether [addr, addr + size) overlaps with below VMAs of different type */
struct heap_vma* check_vma_below = vma_below;
while (check_vma_below && addr < check_vma_below->top) {
if (check_vma_below->is_pal_internal != is_pal_internal) {
return NULL;
}
check_vma_below = LISTP_NEXT_ENTRY(check_vma_below, &g_heap_vma_list, list);
}
/* create VMA with [addr, addr+size); in case of existing overlapping VMAs, the created VMA is
* merged with them and the old VMAs are discarded, similar to mmap(MAX_FIXED) */
struct heap_vma* vma = __alloc_vma();
if (!vma)
return NULL;
vma->bottom = addr;
vma->top = addr + size;
vma->is_pal_internal = is_pal_internal;
/* how much memory was freed because [addr, addr + size) overlapped with VMAs */
size_t freed = 0;
/* Try to merge VMAs as an optimization:
* (1) start from `vma_above` and iterate through VMAs with higher-addresses for merges
* (2) start from `vma_below` and iterate through VMAs with lower-addresses for merges.
* Note that we never merge normal VMAs with pal-internal VMAs. */
while (vma_above && vma_above->bottom <= vma->top &&
vma_above->is_pal_internal == vma->is_pal_internal) {
/* newly created VMA grows into above VMA; expand newly created VMA and free above-VMA */
freed += vma_above->top - vma_above->bottom;
struct heap_vma* vma_above_above = LISTP_PREV_ENTRY(vma_above, &g_heap_vma_list, list);
vma->bottom = MIN(vma_above->bottom, vma->bottom);
vma->top = MAX(vma_above->top, vma->top);
LISTP_DEL(vma_above, &g_heap_vma_list, list);
__free_vma(vma_above);
vma_above = vma_above_above;
}
while (vma_below && vma_below->top >= vma->bottom &&
vma_below->is_pal_internal == vma->is_pal_internal) {
/* newly created VMA grows into below VMA; expand newly create VMA and free below-VMA */
freed += vma_below->top - vma_below->bottom;
struct heap_vma* vma_below_below = LISTP_NEXT_ENTRY(vma_below, &g_heap_vma_list, list);
vma->bottom = MIN(vma_below->bottom, vma->bottom);
vma->top = MAX(vma_below->top, vma->top);
LISTP_DEL(vma_below, &g_heap_vma_list, list);
__free_vma(vma_below);
vma_below = vma_below_below;
}
INIT_LIST_HEAD(vma, list);
LISTP_ADD_AFTER(vma, vma_above, &g_heap_vma_list, list);
if (vma->bottom >= vma->top) {
log_error("Bad memory bookkeeping: %p - %p\n", vma->bottom, vma->top);
ocall_exit(/*exitcode=*/1, /*is_exitgroup=*/true);
}
assert(vma->top - vma->bottom >= (ptrdiff_t)freed);
size_t allocated = vma->top - vma->bottom - freed;
*new_allocation = allocated;
__atomic_add_fetch(&g_allocated_pages.counter, allocated / g_page_size, __ATOMIC_SEQ_CST);
if (is_pal_internal) {
assert(allocated <= g_pal_internal_mem_size - g_pal_internal_mem_used);
g_pal_internal_mem_used += allocated;
}
return addr;
}
void* get_enclave_pages(void* addr, size_t size, bool is_pal_internal) {
void* ret = NULL;
size_t new_allocation = 0;
if (!size)
return NULL;
size = ALIGN_UP(size, g_page_size);
addr = ALIGN_DOWN_PTR(addr, g_page_size);
assert(access_ok(addr, size));
struct heap_vma* vma_above = NULL;
struct heap_vma* vma;
_DkInternalLock(&g_heap_vma_lock);
if (is_pal_internal && size > g_pal_internal_mem_size - g_pal_internal_mem_used) {
/* requested PAL-internal allocation would exceed the limit, fail */
return NULL;
}
if (addr) {
/* caller specified concrete address; find VMA right-above this address */
if (addr < g_heap_bottom || addr + size > g_heap_top)
goto out;
LISTP_FOR_EACH_ENTRY(vma, &g_heap_vma_list, list) {
if (vma->bottom < addr) {
/* current VMA is not above `addr`, thus vma_above is VMA right-above `addr` */
break;
}
vma_above = vma;
}
ret = __create_vma_and_merge(addr, size, is_pal_internal, vma_above, &new_allocation);
} else {
/* caller did not specify address; find first (highest-address) empty slot that fits */
void* vma_above_bottom = g_heap_top;
LISTP_FOR_EACH_ENTRY(vma, &g_heap_vma_list, list) {
if (vma->top < vma_above_bottom - size) {
ret = __create_vma_and_merge(vma_above_bottom - size, size, is_pal_internal,
vma_above, &new_allocation);
goto out;
}
vma_above = vma;
vma_above_bottom = vma_above->bottom;
}
/* corner case: there may be enough space between heap bottom and the lowest-address VMA */
if (g_heap_bottom < vma_above_bottom - size)
ret = __create_vma_and_merge(vma_above_bottom - size, size, is_pal_internal, vma_above, &new_allocation);
}
out:
_DkInternalUnlock(&g_heap_vma_lock);
if (g_pal_sec.edmm_enable_heap && new_allocation && ret) {
if (get_edmm_page_range(ret, size, 1) < 0)
ret = NULL;
}
return ret;
}
int free_enclave_pages(void* addr, size_t size) {
int ret = 0;
if (!size)
return -PAL_ERROR_NOMEM;
size = ALIGN_UP(size, g_page_size);
if (!access_ok(addr, size) || !IS_ALIGNED_PTR(addr, g_page_size) || addr < g_heap_bottom ||
addr + size > g_heap_top) {
return -PAL_ERROR_INVAL;
}
_DkInternalLock(&g_heap_vma_lock);
/* VMA list contains both normal and pal-internal VMAs; it is impossible to free an area
* that overlaps with VMAs of two types at the same time, so we fail in such cases */
bool is_pal_internal_set = false;
bool is_pal_internal = false;
/* how much memory was actually freed, since [addr, addr + size) can overlap with VMAs */
size_t freed = 0;
struct heap_vma* vma;
struct heap_vma* p;
LISTP_FOR_EACH_ENTRY_SAFE(vma, p, &g_heap_vma_list, list) {
if (vma->bottom >= addr + size)
continue;
if (vma->top <= addr)
break;
/* found VMA overlapping with area to free; check it is either normal or pal-internal */
if (!is_pal_internal_set) {
is_pal_internal = vma->is_pal_internal;
is_pal_internal_set = true;
}
if (is_pal_internal != vma->is_pal_internal) {
log_error("Area to free (address %p, size %lu) overlaps with both normal and "
"pal-internal VMAs\n",
addr, size);
ret = -PAL_ERROR_INVAL;
goto out;
}
freed += MIN(vma->top, addr + size) - MAX(vma->bottom, addr);
if (vma->bottom < addr) {
/* create VMA [vma->bottom, addr); this may leave VMA [addr + size, vma->top), see below */
struct heap_vma* new = __alloc_vma();
if (!new) {
log_error("Cannot create split VMA during freeing of address %p\n", addr);
ret = -PAL_ERROR_NOMEM;
goto out;
}
new->top = addr;
new->bottom = vma->bottom;
new->is_pal_internal = vma->is_pal_internal;
INIT_LIST_HEAD(new, list);
LIST_ADD(new, vma, list);
}
/* compress overlapping VMA to [addr + size, vma->top) */
vma->bottom = addr + size;
if (vma->top <= addr + size) {
/* memory area to free completely covers/extends above the rest of the VMA */
LISTP_DEL(vma, &g_heap_vma_list, list);
__free_vma(vma);
}
}
__atomic_sub_fetch(&g_allocated_pages.counter, freed / g_page_size, __ATOMIC_SEQ_CST);
if (is_pal_internal_set && is_pal_internal) {
assert(g_pal_internal_mem_used >= freed);
g_pal_internal_mem_used -= freed;
}
out:
_DkInternalUnlock(&g_heap_vma_lock);
if (ret >=0 && g_pal_sec.edmm_enable_heap) {
if (free_edmm_page_range(addr, size) < 0)
ret = NULL;
}
return ret;
}
/* returns current highest available address on the enclave heap */
void* get_enclave_heap_top(void) {
_DkInternalLock(&g_heap_vma_lock);
void* addr = g_heap_top;
struct heap_vma* vma;
LISTP_FOR_EACH_ENTRY(vma, &g_heap_vma_list, list) {
if (vma->top < addr) {
goto out;
}
addr = vma->bottom;
}
out:
_DkInternalUnlock(&g_heap_vma_lock);
return addr;
}