From e37be9ed3ba83fcf4480a779153122a0a134d3de Mon Sep 17 00:00:00 2001 From: "YiLin.Li" Date: Mon, 17 Aug 2020 09:57:18 +0000 Subject: [PATCH] rune/libenclave/skeleton: add sanity check in skeleton PAL APIs. Signed-off-by: Yilin Li --- .../runtime/pal/skeleton/liberpal-skeleton.c | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/rune/libenclave/internal/runtime/pal/skeleton/liberpal-skeleton.c b/rune/libenclave/internal/runtime/pal/skeleton/liberpal-skeleton.c index b0198c9..8d8cb25 100644 --- a/rune/libenclave/internal/runtime/pal/skeleton/liberpal-skeleton.c +++ b/rune/libenclave/internal/runtime/pal/skeleton/liberpal-skeleton.c @@ -33,6 +33,7 @@ #define TOKEN "encl.token" static struct sgx_secs secs; +static pal_stdio_fds pal_stdio; static bool initialized = false; static char *sgx_dev_path; static bool no_sgx_flc = false; @@ -437,6 +438,10 @@ int __pal_init(pal_attr_t *attr) int __pal_exec(char *path, char *argv[], pal_stdio_fds *stdio, int *exit_code) { + if (path == NULL || argv == NULL || stdio == NULL || exit_code == NULL) { + return -1; + } + FILE *fp = fdopen(stdio->stderr, "w"); if (!fp) return -1; @@ -447,6 +452,8 @@ int __pal_exec(char *path, char *argv[], pal_stdio_fds *stdio, int *exit_code) return -1; } + memcpy(&pal_stdio, stdio, sizeof(pal_stdio_fds)); + uint64_t result = 0; int ret = SGX_ENTER_1_ARG(ECALL_MAGIC, (void *)secs.base, &result); if (ret) { @@ -476,9 +483,20 @@ int __pal_create_process(pal_create_process_args *args) return -1; } - if ((pid = fork()) < 0) + FILE *fp = fdopen(args->stdio->stderr, "w"); + if (!fp) return -1; - else if (pid == 0) { + + 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) { int exit_code, ret; ret = __pal_exec(args->path, args->argv, args->stdio, &exit_code); @@ -486,6 +504,7 @@ int __pal_create_process(pal_create_process_args *args) } else *args->pid = pid; + fclose(fp); return 0; } @@ -497,6 +516,11 @@ int wait4child(pal_exec_args *attr) return -1; } + if (!initialized) { + fprintf(stderr, "Enclave runtime skeleton uninitialized yet!\n"); + return -1; + } + waitpid(attr->pid, &status, 0); if (WIFEXITED(status) || WIFSIGNALED(status)) @@ -507,17 +531,28 @@ int wait4child(pal_exec_args *attr) int __pal_kill(int pid, int sig) { + if (!initialized) { + fprintf(stderr, "Enclave runtime skeleton uninitialized yet!\n"); + return -1; + } + /* No implementation */ return 0; } int __pal_destory(void) { + FILE *fp = fdopen(pal_stdio.stderr, "w"); + if (!fp) + return -1; + if (!initialized) { - fprintf(stderr, "Enclave runtime skeleton uninitialized yet!\n"); + fprintf(fp, "Enclave runtime skeleton uninitialized yet!\n"); + fclose(fp); return -1; } + fclose(fp); close(enclave_fd); return 0; -- GitLab