From 6a7e75cb758ec0fb9dead322b3b4baed56ebd0e4 Mon Sep 17 00:00:00 2001 From: Dmitrii Kuvaiskii Date: Fri, 20 Mar 2020 13:06:51 -0700 Subject: [PATCH] [Pal/{Linux,Linux-SGX}] Remove cargo host-level file descriptor In process-type PAL handle, there were two underlying host FDs: `stream` and `cargo`/`cargo_fd`. The first one was used to send the checkpoint and other messages between parent and child, whereas the second one was used to send PAL handles via cmsg ancillary data Linux mechanism. In reality, `cargo`/`cargo_fd` can be replaced with `stream` in all scenarios. This commit does that. --- Pal/src/host/Linux-SGX/db_main.c | 1 - Pal/src/host/Linux-SGX/db_process.c | 17 ++-------- Pal/src/host/Linux-SGX/db_streams.c | 4 +-- Pal/src/host/Linux-SGX/enclave_ocalls.c | 5 +-- Pal/src/host/Linux-SGX/enclave_ocalls.h | 3 +- Pal/src/host/Linux-SGX/ocall_types.h | 1 - Pal/src/host/Linux-SGX/pal_host.h | 1 - Pal/src/host/Linux-SGX/pal_linux.h | 2 +- Pal/src/host/Linux-SGX/pal_security.h | 3 +- Pal/src/host/Linux-SGX/sgx_enclave.c | 3 +- Pal/src/host/Linux-SGX/sgx_process.c | 30 ++++++----------- Pal/src/host/Linux/db_process.c | 43 +++++++++---------------- Pal/src/host/Linux/db_streams.c | 4 +-- Pal/src/host/Linux/pal_host.h | 1 - 14 files changed, 38 insertions(+), 80 deletions(-) diff --git a/Pal/src/host/Linux-SGX/db_main.c b/Pal/src/host/Linux-SGX/db_main.c index 0c6ec907..4ecc6222 100644 --- a/Pal/src/host/Linux-SGX/db_main.c +++ b/Pal/src/host/Linux-SGX/db_main.c @@ -274,7 +274,6 @@ void pal_linux_main(char * uptr_args, uint64_t args_size, pal_sec.manifest_name[sizeof(pal_sec.manifest_name) - 1] = '\0'; pal_sec.stream_fd = sec_info.stream_fd; - pal_sec.cargo_fd = sec_info.cargo_fd; COPY_ARRAY(pal_sec.pipe_prefix, sec_info.pipe_prefix); pal_sec.qe_targetinfo = sec_info.qe_targetinfo; diff --git a/Pal/src/host/Linux-SGX/db_process.c b/Pal/src/host/Linux-SGX/db_process.c index 2750164f..f114e055 100644 --- a/Pal/src/host/Linux-SGX/db_process.c +++ b/Pal/src/host/Linux-SGX/db_process.c @@ -254,22 +254,20 @@ int _DkProcessCreate (PAL_HANDLE * handle, const char * uri, const char ** args) unsigned int child_pid; int stream_fd; - int cargo_fd; int nargs = 0, ret; if (args) for (const char ** a = args ; *a ; a++) nargs++; - ret = ocall_create_process(uri, nargs, args, &stream_fd, &cargo_fd, &child_pid); + ret = ocall_create_process(uri, nargs, args, &stream_fd, &child_pid); if (ret < 0) return ret; PAL_HANDLE child = malloc(HANDLE_SIZE(process)); SET_HANDLE_TYPE(child, process); - HANDLE_HDR(child)->flags |= RFD(0)|WFD(0)|RFD(1)|WFD(1); + HANDLE_HDR(child)->flags |= RFD(0)|WFD(0); child->process.stream = stream_fd; - child->process.cargo = cargo_fd; child->process.pid = child_pid; child->process.nonblocking = PAL_FALSE; child->process.ssl_ctx = NULL; @@ -321,10 +319,9 @@ int init_child_process (PAL_HANDLE * parent_handle) { PAL_HANDLE parent = malloc(HANDLE_SIZE(process)); SET_HANDLE_TYPE(parent, process); - HANDLE_HDR(parent)->flags |= RFD(0)|WFD(0)|RFD(1)|WFD(1); + HANDLE_HDR(parent)->flags |= RFD(0)|WFD(0); parent->process.stream = pal_sec.stream_fd; - parent->process.cargo = pal_sec.cargo_fd; parent->process.pid = pal_sec.ppid; parent->process.nonblocking = PAL_FALSE; parent->process.ssl_ctx = NULL; @@ -414,11 +411,6 @@ static int proc_close (PAL_HANDLE handle) handle->process.stream = PAL_IDX_POISON; } - if (handle->process.cargo != PAL_IDX_POISON) { - ocall_close(handle->process.cargo); - handle->process.cargo = PAL_IDX_POISON; - } - if (handle->process.ssl_ctx) { _DkStreamSecureFree((LIB_SSL_CONTEXT*)handle->process.ssl_ctx); handle->process.ssl_ctx = NULL; @@ -447,9 +439,6 @@ static int proc_delete (PAL_HANDLE handle, int access) if (handle->process.stream != PAL_IDX_POISON) ocall_shutdown(handle->process.stream, shutdown); - if (handle->process.cargo != PAL_IDX_POISON) - ocall_shutdown(handle->process.cargo, shutdown); - return 0; } diff --git a/Pal/src/host/Linux-SGX/db_streams.c b/Pal/src/host/Linux-SGX/db_streams.c index 6b1ab36d..b0216c10 100644 --- a/Pal/src/host/Linux-SGX/db_streams.c +++ b/Pal/src/host/Linux-SGX/db_streams.c @@ -281,7 +281,7 @@ int _DkSendHandle(PAL_HANDLE hdl, PAL_HANDLE cargo) { fds[nfds++] = cargo->generic.fds[i]; } - int ch = hdl->process.cargo; + int ch = hdl->process.stream; ssize_t ret; ret = ocall_send(ch, &hdl_hdr, sizeof(struct hdl_header), NULL, 0, NULL, 0); @@ -313,7 +313,7 @@ int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE* cargo) { if (!IS_HANDLE_TYPE(hdl, process)) return -PAL_ERROR_BADHANDLE; - int ch = hdl->process.cargo; + int ch = hdl->process.stream; ssize_t ret = ocall_recv(ch, &hdl_hdr, sizeof(struct hdl_header), NULL, NULL, NULL, NULL); diff --git a/Pal/src/host/Linux-SGX/enclave_ocalls.c b/Pal/src/host/Linux-SGX/enclave_ocalls.c index e5ac4f73..96a24af9 100644 --- a/Pal/src/host/Linux-SGX/enclave_ocalls.c +++ b/Pal/src/host/Linux-SGX/enclave_ocalls.c @@ -640,8 +640,7 @@ int ocall_clone_thread (void) return sgx_ocall(OCALL_CLONE_THREAD, dummy); } -int ocall_create_process(const char* uri, int nargs, const char** args, int* stream_fd, - int* cargo_fd, unsigned int* pid) { +int ocall_create_process(const char* uri, int nargs, const char** args, int* stream_fd, unsigned int* pid) { int retval = 0; int ulen = uri ? strlen(uri) + 1 : 0; ms_ocall_create_process_t * ms; @@ -677,8 +676,6 @@ int ocall_create_process(const char* uri, int nargs, const char** args, int* str *pid = ms->ms_pid; if (stream_fd) *stream_fd = ms->ms_stream_fd; - if (cargo_fd) - *cargo_fd = ms->ms_cargo_fd; } sgx_reset_ustack(old_ustack); diff --git a/Pal/src/host/Linux-SGX/enclave_ocalls.h b/Pal/src/host/Linux-SGX/enclave_ocalls.h index 09405d85..5b2fd675 100644 --- a/Pal/src/host/Linux-SGX/enclave_ocalls.h +++ b/Pal/src/host/Linux-SGX/enclave_ocalls.h @@ -76,8 +76,7 @@ int ocall_resume_thread (void * tcs); int ocall_clone_thread (void); -int ocall_create_process(const char* uri, int nargs, const char** args, int* stream_fd, - int* cargo_fd, unsigned int* pid); +int ocall_create_process(const char* uri, int nargs, const char** args, int* stream_fd, unsigned int* pid); int ocall_futex(int* uaddr, int op, int val, int64_t timeout_us); diff --git a/Pal/src/host/Linux-SGX/ocall_types.h b/Pal/src/host/Linux-SGX/ocall_types.h index e096a128..0d69f809 100644 --- a/Pal/src/host/Linux-SGX/ocall_types.h +++ b/Pal/src/host/Linux-SGX/ocall_types.h @@ -165,7 +165,6 @@ typedef struct { unsigned int ms_pid; const char * ms_uri; int ms_stream_fd; - int ms_cargo_fd; int ms_nargs; const char * ms_args[]; } ms_ocall_create_process_t; diff --git a/Pal/src/host/Linux-SGX/pal_host.h b/Pal/src/host/Linux-SGX/pal_host.h index 64be9cae..ca040f99 100644 --- a/Pal/src/host/Linux-SGX/pal_host.h +++ b/Pal/src/host/Linux-SGX/pal_host.h @@ -146,7 +146,6 @@ typedef struct pal_handle struct { PAL_IDX stream; - PAL_IDX cargo; PAL_IDX pid; PAL_BOL nonblocking; PAL_SESSION_KEY session_key; diff --git a/Pal/src/host/Linux-SGX/pal_linux.h b/Pal/src/host/Linux-SGX/pal_linux.h index 78e8a2bb..655f2682 100644 --- a/Pal/src/host/Linux-SGX/pal_linux.h +++ b/Pal/src/host/Linux-SGX/pal_linux.h @@ -227,7 +227,7 @@ extern struct pal_enclave_config { #else -int sgx_create_process(const char* uri, int nargs, const char** args, int* stream_fd, int* cargo_fd); +int sgx_create_process(const char* uri, int nargs, const char** args, int* stream_fd); #ifdef DEBUG # ifndef SIGCHLD diff --git a/Pal/src/host/Linux-SGX/pal_security.h b/Pal/src/host/Linux-SGX/pal_security.h index 92794465..22e13373 100644 --- a/Pal/src/host/Linux-SGX/pal_security.h +++ b/Pal/src/host/Linux-SGX/pal_security.h @@ -43,9 +43,8 @@ struct pal_sec { PAL_SEC_STR manifest_name; - /* child's stream and cargo FDs created and sent over by parent */ + /* child's stream FD created and sent over by parent */ PAL_IDX stream_fd; - PAL_IDX cargo_fd; /* additional information */ PAL_SEC_STR pipe_prefix; diff --git a/Pal/src/host/Linux-SGX/sgx_enclave.c b/Pal/src/host/Linux-SGX/sgx_enclave.c index d11cd704..48cddef0 100644 --- a/Pal/src/host/Linux-SGX/sgx_enclave.c +++ b/Pal/src/host/Linux-SGX/sgx_enclave.c @@ -261,8 +261,7 @@ static long sgx_ocall_create_process(void * pms) { ms_ocall_create_process_t * ms = (ms_ocall_create_process_t *) pms; ODEBUG(OCALL_CREATE_PROCESS, ms); - long ret = sgx_create_process(ms->ms_uri, ms->ms_nargs, ms->ms_args, - &ms->ms_stream_fd, &ms->ms_cargo_fd); + long ret = sgx_create_process(ms->ms_uri, ms->ms_nargs, ms->ms_args, &ms->ms_stream_fd); if (ret < 0) return ret; ms->ms_pid = ret; diff --git a/Pal/src/host/Linux-SGX/sgx_process.c b/Pal/src/host/Linux-SGX/sgx_process.c index 0bf757a4..b15fca19 100644 --- a/Pal/src/host/Linux-SGX/sgx_process.c +++ b/Pal/src/host/Linux-SGX/sgx_process.c @@ -42,7 +42,6 @@ struct proc_args { unsigned int instance_id; unsigned int parent_process_id; int stream_fd; - int cargo_fd; PAL_SEC_STR pipe_prefix; }; @@ -58,14 +57,13 @@ struct proc_args { * future compiler. */ static int __attribute_noinline -vfork_exec(int child_stream, int parent_stream, int parent_cargo, const char** argv) { +vfork_exec(int child_stream, int parent_stream, const char** argv) { int ret = ARCH_VFORK(); if (ret) return ret; /* child: close parent's FDs, rewire child stream to init FD, and execve */ INLINE_SYSCALL(close, 1, parent_stream); - INLINE_SYSCALL(close, 1, parent_cargo); ret = INLINE_SYSCALL(dup2, 2, child_stream, PROC_INIT_FD); if (!IS_ERR(ret)) { @@ -79,16 +77,15 @@ vfork_exec(int child_stream, int parent_stream, int parent_cargo, const char** a return 0; } -int sgx_create_process(const char* uri, int nargs, const char** args, int* stream_fd, int* cargo_fd) { +int sgx_create_process(const char* uri, int nargs, const char** args, int* stream_fd) { int ret, rete, child; - int fds[4] = { -1, -1, -1, -1 }; + int fds[2] = {-1, -1}; if (!uri || !strstartswith_static(uri, URI_PREFIX_FILE)) return -EINVAL; int socktype = SOCK_STREAM; - if (IS_ERR((ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX, socktype, 0, &fds[0]))) || - IS_ERR((ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX, socktype, 0, &fds[2])))) + if (IS_ERR((ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX, socktype, 0, fds)))) goto out; const char ** argv = __alloca(sizeof(const char *) * (nargs + 2)); @@ -96,16 +93,14 @@ int sgx_create_process(const char* uri, int nargs, const char** args, int* strea memcpy(argv + 1, args, sizeof(const char *) * nargs); argv[nargs + 1] = NULL; - /* Child's signal handler may mess with parent's memory during vfork(), - * so block signals - */ + /* child's signal handler may mess with parent's memory during vfork(), so block signals */ ret = block_async_signals(true); if (ret < 0) { ret = -ret; goto out; } - ret = vfork_exec(/*child_stream=*/fds[0], /*parent_stream=*/fds[1], /*parent_cargo=*/fds[3], argv); + ret = vfork_exec(/*child_stream=*/fds[0], /*parent_stream=*/fds[1], argv); if (IS_ERR(ret)) goto out; @@ -120,7 +115,6 @@ int sgx_create_process(const char* uri, int nargs, const char** args, int* strea } INLINE_SYSCALL(close, 1, fds[0]); /* child stream */ - INLINE_SYSCALL(close, 1, fds[2]); /* child cargo */ struct pal_sec * pal_sec = &pal_enclave.pal_sec; struct proc_args proc_args; @@ -128,7 +122,6 @@ int sgx_create_process(const char* uri, int nargs, const char** args, int* strea proc_args.instance_id = pal_sec->instance_id; proc_args.parent_process_id = pal_sec->pid; proc_args.stream_fd = fds[0]; - proc_args.cargo_fd = fds[2]; memcpy(proc_args.pipe_prefix, pal_sec->pipe_prefix, sizeof(PAL_SEC_STR)); ret = INLINE_SYSCALL(write, 3, fds[1], &proc_args, sizeof(struct proc_args)); @@ -149,19 +142,17 @@ int sgx_create_process(const char* uri, int nargs, const char** args, int* strea } INLINE_SYSCALL(fcntl, 3, fds[1], F_SETFD, FD_CLOEXEC); - INLINE_SYSCALL(fcntl, 3, fds[3], F_SETFD, FD_CLOEXEC); if (stream_fd) *stream_fd = fds[1]; - if (cargo_fd) - *cargo_fd = fds[3]; ret = child; out: if (IS_ERR(ret)) { - for (int i = 0; i < 4; i++) - if (fds[i] >= 0) - INLINE_SYSCALL(close, 1, fds[i]); + if (fds[0] >= 0) + INLINE_SYSCALL(close, 1, fds[0]); + if (fds[1] >= 0) + INLINE_SYSCALL(close, 1, fds[1]); } return ret; @@ -187,7 +178,6 @@ int sgx_init_child_process (struct pal_sec * pal_sec) pal_sec->instance_id = proc_args.instance_id; pal_sec->ppid = proc_args.parent_process_id; pal_sec->stream_fd = proc_args.stream_fd; - pal_sec->cargo_fd = proc_args.cargo_fd; memcpy(pal_sec->pipe_prefix, proc_args.pipe_prefix, sizeof(PAL_SEC_STR)); return 1; diff --git a/Pal/src/host/Linux/db_process.c b/Pal/src/host/Linux/db_process.c index 2ff47967..dfc0b3de 100644 --- a/Pal/src/host/Linux/db_process.c +++ b/Pal/src/host/Linux/db_process.c @@ -35,6 +35,7 @@ #include "pal_linux_defs.h" #include "pal_rtld.h" #include "pal_security.h" + typedef __kernel_pid_t pid_t; #include #include @@ -45,16 +46,15 @@ typedef __kernel_pid_t pid_t; #include #include -static inline int create_process_handle (PAL_HANDLE * parent, - PAL_HANDLE * child) -{ - PAL_HANDLE phdl = NULL, chdl = NULL; - int fds[4] = { -1, -1, -1, -1 }; +static inline int create_process_handle(PAL_HANDLE* parent, PAL_HANDLE* child) { + PAL_HANDLE phdl = NULL; + PAL_HANDLE chdl = NULL; + int fds[2] = {-1, -1}; int socktype = SOCK_STREAM | SOCK_CLOEXEC; int ret; - if (IS_ERR((ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX, socktype, 0, &fds[0]))) || - IS_ERR((ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX, socktype, 0, &fds[2])))) { + ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX, socktype, 0, fds); + if (IS_ERR(ret)) { ret = -PAL_ERROR_DENIED; goto out; } @@ -66,9 +66,8 @@ static inline int create_process_handle (PAL_HANDLE * parent, } SET_HANDLE_TYPE(phdl, process); - HANDLE_HDR(phdl)->flags |= RFD(0)|WFD(0)|RFD(1)|WFD(1); + HANDLE_HDR(phdl)->flags |= RFD(0)|WFD(0); phdl->process.stream = fds[0]; - phdl->process.cargo = fds[2]; phdl->process.pid = linux_state.pid; phdl->process.nonblocking = PAL_FALSE; @@ -79,9 +78,8 @@ static inline int create_process_handle (PAL_HANDLE * parent, } SET_HANDLE_TYPE(chdl, process); - HANDLE_HDR(chdl)->flags |= RFD(0)|WFD(0)|RFD(1)|WFD(1); + HANDLE_HDR(chdl)->flags |= RFD(0)|WFD(0); chdl->process.stream = fds[1]; - chdl->process.cargo = fds[3]; chdl->process.pid = 0; /* unknown yet */ chdl->process.nonblocking = PAL_FALSE; @@ -90,13 +88,12 @@ static inline int create_process_handle (PAL_HANDLE * parent, ret = 0; out: if (ret < 0) { - if (phdl) - _DkObjectClose(phdl); - if (chdl) - _DkObjectClose(chdl); - for (int i = 0; i < 4; i++) - if (fds[i] != -1) - INLINE_SYSCALL(close, 1, fds[i]); + free(phdl); + free(chdl); + if (fds[0] != -1) + INLINE_SYSCALL(close, 1, fds[0]); + if (fds[1] != -1) + INLINE_SYSCALL(close, 1, fds[1]); } return ret; } @@ -105,7 +102,7 @@ struct proc_param { PAL_HANDLE parent; PAL_HANDLE exec; PAL_HANDLE manifest; - const char ** argv; + const char** argv; }; struct proc_args { @@ -498,11 +495,6 @@ static int proc_close (PAL_HANDLE handle) handle->process.stream = PAL_IDX_POISON; } - if (handle->process.cargo != PAL_IDX_POISON) { - INLINE_SYSCALL(close, 1, handle->process.cargo); - handle->process.cargo = PAL_IDX_POISON; - } - return 0; } @@ -526,9 +518,6 @@ static int proc_delete (PAL_HANDLE handle, int access) if (handle->process.stream != PAL_IDX_POISON) INLINE_SYSCALL(shutdown, 2, handle->process.stream, shutdown); - if (handle->process.cargo != PAL_IDX_POISON) - INLINE_SYSCALL(shutdown, 2, handle->process.cargo, shutdown); - return 0; } diff --git a/Pal/src/host/Linux/db_streams.c b/Pal/src/host/Linux/db_streams.c index 4977b146..30312028 100644 --- a/Pal/src/host/Linux/db_streams.c +++ b/Pal/src/host/Linux/db_streams.c @@ -282,7 +282,7 @@ int _DkSendHandle(PAL_HANDLE hdl, PAL_HANDLE cargo) { // ~ Initialize common parameter formessage passing // Channel between parent and child - int ch = hdl->process.cargo; + int ch = hdl->process.stream; // Declare variables required for sending the message struct msghdr hdr; // message header @@ -361,7 +361,7 @@ int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE* cargo) { // ~ Initialize common parameter for message passing // Channel between parent and child - int ch = hdl->process.cargo; + int ch = hdl->process.stream; struct msghdr hdr; struct iovec iov[1]; diff --git a/Pal/src/host/Linux/pal_host.h b/Pal/src/host/Linux/pal_host.h index e1615394..9de8f0f8 100644 --- a/Pal/src/host/Linux/pal_host.h +++ b/Pal/src/host/Linux/pal_host.h @@ -135,7 +135,6 @@ typedef struct pal_handle struct { PAL_IDX stream; - PAL_IDX cargo; PAL_IDX pid; PAL_BOL nonblocking; } process;