1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright(c) 2016-20 Intel Corporation. */ 3 4 #include <assert.h> 5 #include <elf.h> 6 #include <errno.h> 7 #include <fcntl.h> 8 #include <stdbool.h> 9 #include <stdio.h> 10 #include <stdint.h> 11 #include <stdlib.h> 12 #include <string.h> 13 #include <unistd.h> 14 #include <sys/ioctl.h> 15 #include <sys/mman.h> 16 #include <sys/stat.h> 17 #include <sys/time.h> 18 #include <sys/types.h> 19 #include "defines.h" 20 #include "main.h" 21 22 void encl_delete(struct encl *encl) 23 { 24 if (encl->encl_base) 25 munmap((void *)encl->encl_base, encl->encl_size); 26 27 if (encl->bin) 28 munmap(encl->bin, encl->bin_size); 29 30 if (encl->fd) 31 close(encl->fd); 32 33 if (encl->segment_tbl) 34 free(encl->segment_tbl); 35 36 memset(encl, 0, sizeof(*encl)); 37 } 38 39 static bool encl_map_bin(const char *path, struct encl *encl) 40 { 41 struct stat sb; 42 void *bin; 43 int ret; 44 int fd; 45 46 fd = open(path, O_RDONLY); 47 if (fd == -1) { 48 perror("enclave executable open()"); 49 return false; 50 } 51 52 ret = stat(path, &sb); 53 if (ret) { 54 perror("enclave executable stat()"); 55 goto err; 56 } 57 58 bin = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 59 if (bin == MAP_FAILED) { 60 perror("enclave executable mmap()"); 61 goto err; 62 } 63 64 encl->bin = bin; 65 encl->bin_size = sb.st_size; 66 67 close(fd); 68 return true; 69 70 err: 71 close(fd); 72 return false; 73 } 74 75 static bool encl_ioc_create(struct encl *encl) 76 { 77 struct sgx_secs *secs = &encl->secs; 78 struct sgx_enclave_create ioc; 79 int rc; 80 81 assert(encl->encl_base != 0); 82 83 memset(secs, 0, sizeof(*secs)); 84 secs->ssa_frame_size = 1; 85 secs->attributes = SGX_ATTR_MODE64BIT; 86 secs->xfrm = 3; 87 secs->base = encl->encl_base; 88 secs->size = encl->encl_size; 89 90 ioc.src = (unsigned long)secs; 91 rc = ioctl(encl->fd, SGX_IOC_ENCLAVE_CREATE, &ioc); 92 if (rc) { 93 perror("SGX_IOC_ENCLAVE_CREATE failed"); 94 munmap((void *)secs->base, encl->encl_size); 95 return false; 96 } 97 98 return true; 99 } 100 101 static bool encl_ioc_add_pages(struct encl *encl, struct encl_segment *seg) 102 { 103 struct sgx_enclave_add_pages ioc; 104 struct sgx_secinfo secinfo; 105 int rc; 106 107 memset(&secinfo, 0, sizeof(secinfo)); 108 secinfo.flags = seg->flags; 109 110 ioc.src = (uint64_t)seg->src; 111 ioc.offset = seg->offset; 112 ioc.length = seg->size; 113 ioc.secinfo = (unsigned long)&secinfo; 114 if (seg->measure) 115 ioc.flags = SGX_PAGE_MEASURE; 116 else 117 ioc.flags = 0; 118 119 rc = ioctl(encl->fd, SGX_IOC_ENCLAVE_ADD_PAGES, &ioc); 120 if (rc < 0) { 121 perror("SGX_IOC_ENCLAVE_ADD_PAGES failed"); 122 return false; 123 } 124 125 return true; 126 } 127 128 129 130 bool encl_load(const char *path, struct encl *encl) 131 { 132 const char device_path[] = "/dev/sgx_enclave"; 133 Elf64_Phdr *phdr_tbl; 134 off_t src_offset; 135 Elf64_Ehdr *ehdr; 136 struct stat sb; 137 void *ptr; 138 int i, j; 139 int ret; 140 int fd = -1; 141 142 memset(encl, 0, sizeof(*encl)); 143 144 fd = open(device_path, O_RDWR); 145 if (fd < 0) { 146 perror("Unable to open /dev/sgx_enclave"); 147 goto err; 148 } 149 150 ret = stat(device_path, &sb); 151 if (ret) { 152 perror("device file stat()"); 153 goto err; 154 } 155 156 ptr = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED, fd, 0); 157 if (ptr == (void *)-1) { 158 perror("mmap for read"); 159 goto err; 160 } 161 munmap(ptr, PAGE_SIZE); 162 163 #define ERR_MSG \ 164 "mmap() succeeded for PROT_READ, but failed for PROT_EXEC.\n" \ 165 " Check that /dev does not have noexec set:\n" \ 166 " \tmount | grep \"/dev .*noexec\"\n" \ 167 " If so, remount it executable: mount -o remount,exec /dev\n\n" 168 169 ptr = mmap(NULL, PAGE_SIZE, PROT_EXEC, MAP_SHARED, fd, 0); 170 if (ptr == (void *)-1) { 171 fprintf(stderr, ERR_MSG); 172 goto err; 173 } 174 munmap(ptr, PAGE_SIZE); 175 176 encl->fd = fd; 177 178 if (!encl_map_bin(path, encl)) 179 goto err; 180 181 ehdr = encl->bin; 182 phdr_tbl = encl->bin + ehdr->e_phoff; 183 184 for (i = 0; i < ehdr->e_phnum; i++) { 185 Elf64_Phdr *phdr = &phdr_tbl[i]; 186 187 if (phdr->p_type == PT_LOAD) 188 encl->nr_segments++; 189 } 190 191 encl->segment_tbl = calloc(encl->nr_segments, 192 sizeof(struct encl_segment)); 193 if (!encl->segment_tbl) 194 goto err; 195 196 for (i = 0, j = 0; i < ehdr->e_phnum; i++) { 197 Elf64_Phdr *phdr = &phdr_tbl[i]; 198 unsigned int flags = phdr->p_flags; 199 struct encl_segment *seg; 200 201 if (phdr->p_type != PT_LOAD) 202 continue; 203 204 seg = &encl->segment_tbl[j]; 205 206 if (!!(flags & ~(PF_R | PF_W | PF_X))) { 207 fprintf(stderr, 208 "%d has invalid segment flags 0x%02x.\n", i, 209 phdr->p_flags); 210 goto err; 211 } 212 213 if (j == 0 && flags != (PF_R | PF_W)) { 214 fprintf(stderr, 215 "TCS has invalid segment flags 0x%02x.\n", 216 phdr->p_flags); 217 goto err; 218 } 219 220 if (j == 0) { 221 src_offset = phdr->p_offset & PAGE_MASK; 222 encl->src = encl->bin + src_offset; 223 224 seg->prot = PROT_READ | PROT_WRITE; 225 seg->flags = SGX_PAGE_TYPE_TCS << 8; 226 } else { 227 seg->prot = (phdr->p_flags & PF_R) ? PROT_READ : 0; 228 seg->prot |= (phdr->p_flags & PF_W) ? PROT_WRITE : 0; 229 seg->prot |= (phdr->p_flags & PF_X) ? PROT_EXEC : 0; 230 seg->flags = (SGX_PAGE_TYPE_REG << 8) | seg->prot; 231 } 232 233 seg->offset = (phdr->p_offset & PAGE_MASK) - src_offset; 234 seg->size = (phdr->p_filesz + PAGE_SIZE - 1) & PAGE_MASK; 235 seg->src = encl->src + seg->offset; 236 seg->measure = true; 237 238 j++; 239 } 240 241 assert(j == encl->nr_segments); 242 243 encl->src_size = encl->segment_tbl[j - 1].offset + 244 encl->segment_tbl[j - 1].size; 245 246 for (encl->encl_size = 4096; encl->encl_size < encl->src_size; ) 247 encl->encl_size <<= 1; 248 249 return true; 250 251 err: 252 if (fd != -1) 253 close(fd); 254 encl_delete(encl); 255 return false; 256 } 257 258 static bool encl_map_area(struct encl *encl) 259 { 260 size_t encl_size = encl->encl_size; 261 void *area; 262 263 area = mmap(NULL, encl_size * 2, PROT_NONE, 264 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 265 if (area == MAP_FAILED) { 266 perror("reservation mmap()"); 267 return false; 268 } 269 270 encl->encl_base = ((uint64_t)area + encl_size - 1) & ~(encl_size - 1); 271 272 munmap(area, encl->encl_base - (uint64_t)area); 273 munmap((void *)(encl->encl_base + encl_size), 274 (uint64_t)area + encl_size - encl->encl_base); 275 276 return true; 277 } 278 279 bool encl_build(struct encl *encl) 280 { 281 struct sgx_enclave_init ioc; 282 int ret; 283 int i; 284 285 if (!encl_map_area(encl)) 286 return false; 287 288 if (!encl_ioc_create(encl)) 289 return false; 290 291 /* 292 * Pages must be added before mapping VMAs because their permissions 293 * cap the VMA permissions. 294 */ 295 for (i = 0; i < encl->nr_segments; i++) { 296 struct encl_segment *seg = &encl->segment_tbl[i]; 297 298 if (!encl_ioc_add_pages(encl, seg)) 299 return false; 300 } 301 302 ioc.sigstruct = (uint64_t)&encl->sigstruct; 303 ret = ioctl(encl->fd, SGX_IOC_ENCLAVE_INIT, &ioc); 304 if (ret) { 305 perror("SGX_IOC_ENCLAVE_INIT failed"); 306 return false; 307 } 308 309 return true; 310 } 311