forked from keystone-enclave/sm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathattest.c
198 lines (164 loc) · 6.03 KB
/
attest.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
//******************************************************************************
// Copyright (c) 2018, The Regents of the University of California (Regents).
// All Rights Reserved. See LICENSE for license details.
//------------------------------------------------------------------------------
#include "enclave.h"
#include "crypto.h"
#include "page.h"
#include <sbi/sbi_console.h>
typedef uintptr_t pte_t;
/* This will walk the entire vaddr space in the enclave, validating
linear at-most-once paddr mappings, and then hashing valid pages */
int validate_and_hash_epm(hash_ctx* hash_ctx, int level,
pte_t* tb, uintptr_t vaddr, int contiguous,
struct enclave* encl,
uintptr_t* runtime_max_seen,
uintptr_t* user_max_seen)
{
pte_t* walk;
int i;
//TODO check for failures
uintptr_t epm_start, epm_size;
uintptr_t utm_start, utm_size;
int idx = get_enclave_region_index(encl->eid, REGION_EPM);
epm_start = pmp_region_get_addr(encl->regions[idx].pmp_rid);
epm_size = pmp_region_get_size(encl->regions[idx].pmp_rid);
idx = get_enclave_region_index(encl->eid, REGION_UTM);
utm_start = pmp_region_get_addr(encl->regions[idx].pmp_rid);
utm_size = pmp_region_get_size(encl->regions[idx].pmp_rid);
/* iterate over PTEs */
for (walk=tb, i=0; walk < tb + (RISCV_PGSIZE/sizeof(pte_t)); walk += 1,i++)
{
if (*walk == 0) {
contiguous = 0;
continue;
}
uintptr_t vpn;
uintptr_t phys_addr = (*walk >> PTE_PPN_SHIFT) << RISCV_PGSHIFT;
/* Check for blatently invalid mappings */
int map_in_epm = (phys_addr >= epm_start &&
phys_addr < epm_start + epm_size);
int map_in_utm = (phys_addr >= utm_start &&
phys_addr < utm_start + utm_size);
/* EPM may map anything, UTM may not map pgtables */
if(!map_in_epm && (!map_in_utm || level != 1)){
goto fatal_bail;
}
/* propagate the highest bit of the VA */
if ( level == RISCV_PGLEVEL_TOP && i & RISCV_PGTABLE_HIGHEST_BIT )
vpn = ((-1UL << RISCV_PGLEVEL_BITS) | (i & RISCV_PGLEVEL_MASK));
else
vpn = ((vaddr << RISCV_PGLEVEL_BITS) | (i & RISCV_PGLEVEL_MASK));
uintptr_t va_start = vpn << RISCV_PGSHIFT;
/* include the first virtual address of a contiguous range */
if (level == 1 && !contiguous)
{
hash_extend(hash_ctx, &va_start, sizeof(uintptr_t));
//printm("VA hashed: 0x%lx\n", va_start);
contiguous = 1;
}
if (level == 1)
{
/*
* This is where we enforce the at-most-one-mapping property.
* To make our lives easier, we also require a 'linear' mapping
* (for each of the user and runtime spaces independently).
*
* That is: Given V1->P1 and V2->P2:
*
* V1 < V2 ==> P1 < P2 (Only for within a given space)
*
* V1 != V2 ==> P1 != P2
*
* We also validate that all utm vaddrs -> utm paddrs
*/
int in_runtime = ((phys_addr >= encl->pa_params.runtime_base) &&
(phys_addr < encl->pa_params.user_base));
int in_user = ((phys_addr >= encl->pa_params.user_base) &&
(phys_addr < encl->pa_params.free_base));
/* Validate U bit */
if(in_user && !(*walk & PTE_U)){
goto fatal_bail;
}
/* If the vaddr is in UTM, the paddr must be in UTM */
if(va_start >= encl->params.untrusted_ptr &&
va_start < (encl->params.untrusted_ptr + encl->params.untrusted_size) &&
!map_in_utm){
goto fatal_bail;
}
/* Do linear mapping validation */
if(in_runtime){
if(phys_addr <= *runtime_max_seen){
goto fatal_bail;
}
else{
*runtime_max_seen = phys_addr;
}
}
else if(in_user){
if(phys_addr <= *user_max_seen){
goto fatal_bail;
}
else{
*user_max_seen = phys_addr;
}
}
else if(map_in_utm){
// we checked this above, its OK
}
else{
//printm("BAD GENERIC MAP %x %x %x\n", in_runtime, in_user, map_in_utm);
goto fatal_bail;
}
/* Page is valid, add it to the hash */
/* if PTE is leaf, extend hash for the page */
hash_extend_page(hash_ctx, (void*)phys_addr);
//printm("PAGE hashed: 0x%lx (pa: 0x%lx)\n", vpn << RISCV_PGSHIFT, phys_addr);
}
else
{
/* otherwise, recurse on a lower level */
contiguous = validate_and_hash_epm(hash_ctx,
level - 1,
(pte_t*) phys_addr,
vpn,
contiguous,
encl,
runtime_max_seen,
user_max_seen);
if(contiguous == -1){
sbi_printf("BAD MAP: %lx->%lx epm %x %lx uer %x %lx\n",
va_start,phys_addr,
//in_runtime,
0,
encl->pa_params.runtime_base,
0,
//in_user,
encl->pa_params.user_base);
goto fatal_bail;
}
}
}
return contiguous;
fatal_bail:
return -1;
}
unsigned long validate_and_hash_enclave(struct enclave* enclave){
hash_ctx hash_ctx;
int ptlevel = RISCV_PGLEVEL_TOP;
hash_init(&hash_ctx);
// hash the runtime parameters
hash_extend(&hash_ctx, &enclave->params, sizeof(struct runtime_va_params_t));
uintptr_t runtime_max_seen=0;
uintptr_t user_max_seen=0;;
// hash the epm contents including the virtual addresses
int valid = validate_and_hash_epm(&hash_ctx,
ptlevel,
(pte_t*) (enclave->encl_satp << RISCV_PGSHIFT),
0, 0, enclave, &runtime_max_seen, &user_max_seen);
if(valid == -1){
return SBI_ERR_SM_ENCLAVE_ILLEGAL_PTE;
}
hash_finalize(enclave->hash, &hash_ctx);
return SBI_ERR_SM_ENCLAVE_SUCCESS;
}