liberpal-skeleton.c 12.6 KB
Newer Older
1 2 3 4
// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
// Copyright(c) 2016-18 Intel Corporation.

#include <elf.h>
5
#include <errno.h>
6 7 8 9 10 11 12 13 14 15
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
16
#include <sys/wait.h>
17
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 25
18
#include <sys/types.h>
19 20 21
#else
#include <sys/sysmacros.h>
#endif
22 23
#include "defines.h"
#include "sgx_call.h"
24
#include "liberpal-skeleton.h"
25

26
#define PAGE_SIZE  4096
27

28 29
#define SGX_REG_PAGE_FLAGS \
	(SGX_SECINFO_REG | SGX_SECINFO_R | SGX_SECINFO_W | SGX_SECINFO_X)
30

31 32 33
#define IMAGE		"encl.bin"
#define SIGSTRUCT	"encl.ss"
#define TOKEN		"encl.token"
34

35
static struct sgx_secs secs;
36
static pal_stdio_fds pal_stdio;
37
static bool initialized = false;
38
static int exit_code;
39
static char *sgx_dev_path;
40
static bool no_sgx_flc = false;
41
static bool enclave_debug = true;
42
static int wait_timeout;
43
bool debugging = false;
44
bool is_oot_driver;
45 46 47 48 49
/*
 * For SGX in-tree driver, dev_fd cannot be closed until an enclave instance
 * intends to exit.
 */
static int enclave_fd = -1;
50
void *tcs_busy;
51 52 53 54 55 56 57 58

static bool is_sgx_device(const char *dev)
{
	struct stat st;
	int rc;

	rc = stat(dev, &st);
	if (!rc) {
59
		if ((st.st_mode & S_IFCHR) && (major(st.st_rdev) == 10))
60 61 62 63 64 65
			return true;
	}

	return false;
}

66
__attribute__((constructor)) static void detect_driver_type(void)
67 68 69 70 71 72 73 74 75 76
{
	if (is_sgx_device("/dev/isgx")) {
		sgx_dev_path = "/dev/isgx";
		is_oot_driver = true;
		return;
	}

	sgx_dev_path = "/dev/sgx/enclave";
	is_oot_driver = false;
}
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
static uint64_t create_enclave_range(int dev_fd, uint64_t size)
{
	void *area;
	int fd;
	int flags = MAP_SHARED;

	if (is_oot_driver) {
		fd = dev_fd;
	} else {
		fd = -1;
		flags |= MAP_ANONYMOUS;
	}

	area = mmap(NULL, size * 2, PROT_NONE, flags, fd, 0);
	if (area == MAP_FAILED) {
		perror("mmap");
		return 0;
	}

	uint64_t base = ((uint64_t)area + size - 1) & ~(size - 1);
	munmap(area, base - (uint64_t)area);
	munmap((void *)(base + size), (uint64_t)area + size - base);

	if (is_oot_driver) {
		if (mprotect((void *)base, size, PROT_READ | PROT_WRITE | PROT_EXEC)) {
			perror("mprotect");
			munmap((void *)base, size);
			return 0;
		}
	}

	return base;
}

112 113
static bool encl_create(int dev_fd, unsigned long bin_size,
			struct sgx_secs *secs)
114
{
115 116 117 118 119
	struct sgx_enclave_create ioc;
	int rc;

	memset(secs, 0, sizeof(*secs));
	secs->ssa_frame_size = 1;
120 121 122
	secs->attributes = SGX_ATTR_MODE64BIT;
	if (enclave_debug)
		secs->attributes |= SGX_ATTR_DEBUG;
123
	secs->xfrm = 7;
124

125 126 127
	for (secs->size = PAGE_SIZE; secs->size < bin_size; )
		secs->size <<= 1;

128 129
	uint64_t base = create_enclave_range(dev_fd, secs->size);
	if (!base)
130 131
		return false;

132
	secs->base = base;
133 134 135 136 137 138 139 140 141
	ioc.src = (unsigned long)secs;
	rc = ioctl(dev_fd, SGX_IOC_ENCLAVE_CREATE, &ioc);
	if (rc) {
		fprintf(stderr, "ECREATE failed rc=%d, err=%d.\n", rc, errno);
		munmap((void *)secs->base, secs->size);
		return false;
	}

	return true;
142 143
}

144 145 146

static bool encl_add_pages_with_mrmask(int dev_fd, uint64_t addr, void *data,
				       unsigned long length, uint64_t flags)
147
{
148
	struct sgx_enclave_add_pages_with_mrmask ioc;
149 150
	struct sgx_secinfo secinfo;
	int rc;
151

152 153
	memset(&secinfo, 0, sizeof(secinfo));
	secinfo.flags = flags;
154

155 156 157 158
	ioc.src = (uint64_t)data;
	ioc.addr = addr;
	ioc.secinfo = (unsigned long)&secinfo;
	ioc.mrmask = (__u16)-1;
159

160 161
	uint64_t added_size = 0;
	while (added_size < length) {
162
		rc = ioctl(dev_fd, SGX_IOC_ENCLAVE_ADD_PAGES_WITH_MRMASK, &ioc);
163 164 165
		if (rc) {
			fprintf(stderr, "EADD failed rc=%d.\n", rc);
			return false;
166
		}
167 168 169 170 171 172 173 174 175

		ioc.addr += PAGE_SIZE;
		ioc.src += PAGE_SIZE;
		added_size += PAGE_SIZE;
	}

	return true;
}

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
static bool encl_add_pages(int dev_fd, uint64_t addr, void *data,
			   unsigned long length, uint64_t flags)
{
	struct sgx_enclave_add_pages ioc;
	struct sgx_secinfo secinfo;
	int rc;

	memset(&secinfo, 0, sizeof(secinfo));
	secinfo.flags = flags;

	ioc.src = (uint64_t)data;
	ioc.offset = addr;
	ioc.length = length;
	ioc.secinfo = (unsigned long)&secinfo;
	ioc.flags = SGX_PAGE_MEASURE;

	rc = ioctl(dev_fd, SGX_IOC_ENCLAVE_ADD_PAGES, &ioc);
	if (rc) {
		fprintf(stderr, "EADD failed rc=%d.\n", rc);
		return false;
	}

	if (ioc.count != length) {
		fprintf(stderr, "EADD short of data.\n");
		return false;
	}

	return true;
}

206
static bool encl_build(struct sgx_secs *secs, void *bin, unsigned long bin_size, 
207 208
		       struct sgx_sigstruct *sigstruct,
		       struct sgx_einittoken *token)
209 210 211 212
{
	int dev_fd;
	int rc;

213
	dev_fd = open(sgx_dev_path, O_RDWR);
214
	if (dev_fd < 0) {
215
		fprintf(stderr, "Unable to open %s\n", sgx_dev_path);
216
		return false;
217 218
	}

219 220 221
	if (!(sigstruct->body.attributes & SGX_ATTR_DEBUG))
		enclave_debug = false;

222 223 224
	if (!encl_create(dev_fd, bin_size, secs))
		goto out_dev_fd;

225
	if (is_oot_driver) {
226
		if (!encl_add_pages_with_mrmask(dev_fd, secs->base, bin, PAGE_SIZE, SGX_SECINFO_TCS))
227 228 229 230 231 232 233 234 235 236 237 238
			goto out_map;

		if (!encl_add_pages_with_mrmask(dev_fd, secs->base + PAGE_SIZE, bin + PAGE_SIZE,
						bin_size - PAGE_SIZE, SGX_REG_PAGE_FLAGS))
			goto out_map;
	} else {
		if (!encl_add_pages(dev_fd, 0, bin, PAGE_SIZE, SGX_SECINFO_TCS))
			goto out_map;

		if (!encl_add_pages(dev_fd, PAGE_SIZE, bin + PAGE_SIZE,
				    bin_size - PAGE_SIZE, SGX_REG_PAGE_FLAGS))
			goto out_map;
239
	}
240

241
	if (is_oot_driver || no_sgx_flc) {
242 243 244 245 246 247
		struct sgx_enclave_init_with_token ioc;
		ioc.addr = secs->base;
		ioc.sigstruct = (uint64_t)sigstruct;
		ioc.einittoken = (uint64_t)token;
		rc = ioctl(dev_fd, SGX_IOC_ENCLAVE_INIT_WITH_TOKEN, &ioc);
	} else {
248 249 250 251
		struct sgx_enclave_init ioc;
		ioc.sigstruct = (uint64_t)sigstruct;
		rc = ioctl(dev_fd, SGX_IOC_ENCLAVE_INIT, &ioc);
	}
252 253 254 255 256 257

	if (rc) {
		printf("EINIT failed rc=%d\n", rc);
		goto out_map;
	}

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
	if (is_oot_driver)
		close(dev_fd);
	else {
		void *rc;

		rc = mmap((void *)secs->base, PAGE_SIZE,
			  PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED,
			  dev_fd, 0);
		if (rc == MAP_FAILED) {
			perror("mmap TCS");
			goto out_map;
		}

		rc = mmap((void *)secs->base + PAGE_SIZE, bin_size - PAGE_SIZE,
			  PROT_READ | PROT_WRITE | PROT_EXEC, MAP_FIXED | MAP_SHARED,
			  dev_fd, 0);
		if (rc == MAP_FAILED) {
			perror("mmap text & data");
			goto out_map;
		}

		enclave_fd = dev_fd;
	}

282 283 284 285 286 287 288 289
	return true;
out_map:
	munmap((void *)secs->base, secs->size);
out_dev_fd:
	close(dev_fd);
	return false;
}

290
static bool get_file_size(const char *path, off_t *bin_size)
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
{
	struct stat sb;
	int ret;

	ret = stat(path, &sb);
	if (ret) {
		perror("stat");
		return false;
	}

	if (!sb.st_size || sb.st_size & 0xfff) {
		fprintf(stderr, "Invalid blob size %lu\n", sb.st_size);
		return false;
	}

	*bin_size = sb.st_size;
	return true;
}

310
static bool encl_data_map(const char *path, void **bin, off_t *bin_size)
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
{
	int fd;

	fd = open(path, O_RDONLY);
	if (fd == -1)  {
		fprintf(stderr, "open() %s failed, errno=%d.\n", path, errno);
		return false;
	}

	if (!get_file_size(path, bin_size))
		goto err_out;

	*bin = mmap(NULL, *bin_size, PROT_READ, MAP_PRIVATE, fd, 0);
	if (*bin == MAP_FAILED) {
		fprintf(stderr, "mmap() %s failed, errno=%d.\n", path, errno);
		goto err_out;
	}

	close(fd);
	return true;

err_out:
	close(fd);
	return false;
}

337
static bool load_sigstruct(const char *path, void *sigstruct)
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
{
	int fd;

	fd = open(path, O_RDONLY);
	if (fd == -1)  {
		fprintf(stderr, "open() %s failed, errno=%d.\n", path, errno);
		return false;
	}

	if (read(fd, sigstruct, sizeof(struct sgx_sigstruct)) !=
	    sizeof(struct sgx_sigstruct)) {
		fprintf(stderr, "read() %s failed, errno=%d.\n", path, errno);
		close(fd);
		return false;
	}

	close(fd);
	return true;
}

358
static bool load_token(const char *path, void *token)
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
{
	int fd;

	fd = open(path, O_RDONLY);
	if (fd == -1)  {
		fprintf(stderr, "open() %s failed, errno=%d.\n", path, errno);
		return false;
	}

	if (read(fd, token, sizeof(struct sgx_einittoken)) !=
		sizeof(struct sgx_einittoken)) {
		fprintf(stderr, "read() %s failed, errno=%d.\n", path, errno);
		close(fd);
		return false;
	}

	close(fd);
	return true;
}

379 380 381 382
static void check_opts(const char *opt)
{
	if (!strcmp(opt, "no-sgx-flc"))
		no_sgx_flc = true;
383 384
	else if (!strcmp(opt, "debug"))
		debugging = true;
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
}

static void parse_args(const char *args)
{
	char *a = strdup(args);
	if (!a)
		return;

	char *opt = strtok(a, " ");
	check_opts(opt);

	if (!opt) {
		free(a);
		return;
	}

	do {
		char *opt = strtok(NULL, " ");
		if (!opt)
			break;

		check_opts(opt);
	} while (1);

	free(a);
}

412
int __pal_init(pal_attr_t *attr)
413 414 415 416 417 418
{
	struct sgx_sigstruct sigstruct;
	struct sgx_einittoken token;
	off_t bin_size;
	void *bin;

419 420
	parse_args(attr->args);

421 422 423 424 425 426
	tcs_busy = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE,
			MAP_SHARED | MAP_ANONYMOUS, -1, 0);
	if (tcs_busy == MAP_FAILED)
		return -EINVAL;
	*(uint8_t *)tcs_busy = 0;
	
427
	if (!encl_data_map(IMAGE, &bin, &bin_size))
428
		return -ENOENT;
429 430

	if (!load_sigstruct(SIGSTRUCT, &sigstruct))
431
		return -ENOENT;
432 433

	if (!load_token(TOKEN, &token))
434
		return -ENOENT;
435 436

	if (!encl_build(&secs, bin, bin_size, &sigstruct, &token))
437
		return -EINVAL;
438 439

	initialized = true;	
440

441 442 443
	return 0;
}

444
int __pal_exec(char *path, char *argv[], pal_stdio_fds *stdio, int *exit_code)
445
{
446 447 448 449
	if (path == NULL || argv == NULL || stdio == NULL || exit_code == NULL) {
		return -1;
	}

450
	FILE *fp = fdopen(stdio->stderr, "w");
451 452
	if (!fp)
		return -1;
453 454

	if (!initialized) {
455
		fprintf(fp, "enclave runtime skeleton uninitialized yet!\n");
456
		fclose(fp);
457
		return -1;
458
	}
459

460 461
	memcpy(&pal_stdio, stdio, sizeof(pal_stdio_fds));

462
	uint64_t result = 0;
463 464 465 466 467 468 469 470
	int ret = SGX_ENTER_1_ARG(ECALL_MAGIC, (void *)secs.base, &result);
	if (ret) {
		fprintf(fp, "failed to initialize enclave\n");
		fclose(fp);
		return ret;
	}
	if (result != INIT_MAGIC) {
		fprintf(fp, "Unexpected result: 0x%lx != 0x%lx\n", result, INIT_MAGIC);
471
		fclose(fp);
472 473 474
		return -1;
	}

475 476 477 478 479 480 481 482 483
	for (int i = 0; argv[i]; i++) {
		if (!strcmp(argv[i], "wait_timeout") && argv[i+1]) {
			wait_timeout = atoi(argv[i+1]);
			if (wait_timeout > 0)
				sleep(wait_timeout);
			break;
		}
	}

484
	fprintf(fp, "Enclave runtime skeleton initialization succeeded\n");
485
	fclose(fp);
486

487
	*exit_code = 0;
488

489 490 491
	return 0;
}

492 493 494 495 496 497 498 499
int __pal_create_process(pal_create_process_args *args)
{
	int pid;

	if (args == NULL || args->path == NULL || args->argv == NULL || args->pid == NULL || args->stdio == NULL) {
		return -1;
	}

500 501 502 503 504 505 506
	/* SGX out-of-tree driver disallows the creation of shared enclave mapping
	 * between parent and child process, so simply launching __pal_exec() directly here.
	 */
	if (is_oot_driver) {
		return __pal_exec(args->path, args->argv, args->stdio, &exit_code);
	}

507 508
	FILE *fp = fdopen(args->stdio->stderr, "w");
	if (!fp)
509
		return -1;
510 511 512 513 514 515 516 517 518 519 520

	if (!initialized) {
		fprintf(fp, "Enclave runtime skeleton uninitialized yet!\n");
		fclose(fp);
		return -1;
	}

	if ((pid = fork()) < 0) {
		fclose(fp);
		return -1;
	} else if (pid == 0) {
521 522 523 524 525 526 527
		int exit_code, ret;

		ret = __pal_exec(args->path, args->argv, args->stdio, &exit_code);
		exit(ret ? ret : exit_code);
	} else
		*args->pid = pid;

528
	fclose(fp);
529 530 531 532 533 534 535 536 537 538 539
	return 0;
}

int wait4child(pal_exec_args *attr)
{
	int status;

	if (attr == NULL || attr->exit_value == NULL) {
		return -1;
	}

540 541 542 543 544
	if (!initialized) {
		fprintf(stderr, "Enclave runtime skeleton uninitialized yet!\n");
		return -1;
	}

545 546 547 548 549
	if (is_oot_driver) {
		*attr->exit_value = exit_code;
		return exit_code;
	}

550 551 552 553 554 555 556 557
	waitpid(attr->pid, &status, 0);

	if (WIFEXITED(status) || WIFSIGNALED(status))
		*attr->exit_value = WEXITSTATUS(status);

	return 0;
}

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
int __pal_get_local_report(void *targetinfo, int targetinfo_len, void *report, int* report_len)
{
	uint8_t report_data[64] = { 0, };
	struct sgx_report report_align;
	int ret;

	if (!initialized) {
		fprintf(stderr, "Enclave runtime skeleton uninitialized yet!\n");
		return -1;
	}

	if (targetinfo == NULL || targetinfo_len != sizeof(struct sgx_target_info)) {
		fprintf(stderr, "Input parameter targetinfo is NULL or targentinfo_len != sizeof(struct sgx_target_info)!\n");
		return -1;
	}

	if (report == NULL || report_len == NULL || *report_len < SGX_REPORT_SIZE) {
		fprintf(stderr, "Input parameter report is NULL or report_len is not enough!\n");
		return -1;
	}

	ret = SGX_ENTER_3_ARGS(ECALL_REPORT, (void *)secs.base, targetinfo,
						report_data, &report_align);
	if (ret) {
		fprintf(stderr, "failed to get report\n");
		return ret;
	}

	memcpy(report, &report_align, SGX_REPORT_SIZE);
	if (debugging) {
		fprintf(stdout, "succeed to get local report\n");
	}

	return 0;
}

594 595
int __pal_kill(int pid, int sig)
{
596 597 598 599 600
	if (!initialized) {
		fprintf(stderr, "Enclave runtime skeleton uninitialized yet!\n");
		return -1;
	}

601 602 603 604
	/* No implementation */
	return 0;
}

605
int __pal_destory(void)
606
{
607 608 609 610
	FILE *fp = fdopen(pal_stdio.stderr, "w");
	if (!fp)
		return -1;

611
	if (!initialized) {
612 613
		fprintf(fp, "Enclave runtime skeleton uninitialized yet!\n");
		fclose(fp);
614 615
		return -1;
	}
616

617
	fclose(fp);
618 619
	close(enclave_fd);

620 621
	return 0;
}