sgx_process.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_process.c
  15. *
  16. * This source file contains functions to create a child process and terminate
  17. * the running process. Child does not inherit any objects or memory from its
  18. * parent pricess. A Parent process may not modify the execution of its
  19. * children. It can wait for a child to exit using its handle. Also, parent and
  20. * child may communicate through I/O streams provided by the parent to the child
  21. * at creation.
  22. */
  23. #include <pal_linux.h>
  24. #include <pal_rtld.h>
  25. #include "sgx_internal.h"
  26. #include "sgx_tls.h"
  27. #include "sgx_enclave.h"
  28. #include <asm/fcntl.h>
  29. #include <asm/errno.h>
  30. #include <linux/fs.h>
  31. #define PAL_LOADER RUNTIME_FILE("pal-Linux-SGX")
  32. struct proc_args {
  33. PAL_SEC_STR exec_name;
  34. unsigned int instance_id;
  35. unsigned int parent_process_id;
  36. int stream_fd;
  37. int cargo_fd;
  38. PAL_SEC_STR pipe_prefix;
  39. };
  40. /*
  41. * vfork() shares stack between child and parent. Any stack modifications in
  42. * child are reflected in parent's stack. Compiler may unwittingly modify
  43. * child's stack for its own purposes and thus corrupt parent's stack
  44. * (e.g., GCC re-uses the same stack area for local vars with non-overlapping
  45. * lifetimes).
  46. * Introduce noinline function with stack area used only by child.
  47. * Make this function non-local to keep function signature.
  48. * NOTE: more tricks may be needed to prevent unexpected optimization for
  49. * future compiler.
  50. */
  51. static int __attribute_noinline
  52. vfork_exec(int child_stream, int parent_stream, int parent_cargo, const char** argv) {
  53. int ret = ARCH_VFORK();
  54. if (ret)
  55. return ret;
  56. /* child: close parent's FDs, rewire child stream to init FD, and execve */
  57. INLINE_SYSCALL(close, 1, parent_stream);
  58. INLINE_SYSCALL(close, 1, parent_cargo);
  59. ret = INLINE_SYSCALL(dup2, 2, child_stream, PROC_INIT_FD);
  60. if (!IS_ERR(ret)) {
  61. extern char** environ;
  62. ret = INLINE_SYSCALL(execve, 3, PAL_LOADER, argv, environ);
  63. /* shouldn't get to here */
  64. SGX_DBG(DBG_E, "unexpected failure of new process\n");
  65. }
  66. __asm__ volatile ("hlt");
  67. return 0;
  68. }
  69. int sgx_create_process(const char* uri, int nargs, const char** args, int* stream_fd, int* cargo_fd) {
  70. int ret, rete, child;
  71. int fds[4] = { -1, -1, -1, -1 };
  72. if (!uri || !strstartswith_static(uri, URI_PREFIX_FILE))
  73. return -EINVAL;
  74. int socktype = SOCK_STREAM;
  75. if (IS_ERR((ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX, socktype, 0, &fds[0]))) ||
  76. IS_ERR((ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX, socktype, 0, &fds[2]))))
  77. goto out;
  78. const char ** argv = __alloca(sizeof(const char *) * (nargs + 2));
  79. argv[0] = PAL_LOADER;
  80. memcpy(argv + 1, args, sizeof(const char *) * nargs);
  81. argv[nargs + 1] = NULL;
  82. /* Child's signal handler may mess with parent's memory during vfork(),
  83. * so block signals
  84. */
  85. ret = block_async_signals(true);
  86. if (ret < 0) {
  87. ret = -ret;
  88. goto out;
  89. }
  90. ret = vfork_exec(/*child_stream=*/fds[0], /*parent_stream=*/fds[1], /*parent_cargo=*/fds[3], argv);
  91. if (IS_ERR(ret))
  92. goto out;
  93. /* parent continues here */
  94. child = ret;
  95. /* children unblock async signals by sgx_signal_setup() */
  96. ret = block_async_signals(false);
  97. if (ret < 0) {
  98. ret = -ret;
  99. goto out;
  100. }
  101. INLINE_SYSCALL(close, 1, fds[0]); /* child stream */
  102. INLINE_SYSCALL(close, 1, fds[2]); /* child cargo */
  103. struct pal_sec * pal_sec = &pal_enclave.pal_sec;
  104. struct proc_args proc_args;
  105. memcpy(proc_args.exec_name, uri, sizeof(PAL_SEC_STR));
  106. proc_args.instance_id = pal_sec->instance_id;
  107. proc_args.parent_process_id = pal_sec->pid;
  108. proc_args.stream_fd = fds[0];
  109. proc_args.cargo_fd = fds[2];
  110. memcpy(proc_args.pipe_prefix, pal_sec->pipe_prefix, sizeof(PAL_SEC_STR));
  111. ret = INLINE_SYSCALL(write, 3, fds[1], &proc_args, sizeof(struct proc_args));
  112. if (IS_ERR(ret) || (size_t)ret < sizeof(struct proc_args)) {
  113. ret = -EPERM;
  114. goto out;
  115. }
  116. ret = INLINE_SYSCALL(read, 3, fds[1], &rete, sizeof(int));
  117. if (IS_ERR(ret) || (size_t)ret < sizeof(int)) {
  118. ret = -EPERM;
  119. goto out;
  120. }
  121. if (IS_ERR(rete)) {
  122. ret = rete;
  123. goto out;
  124. }
  125. INLINE_SYSCALL(fcntl, 3, fds[1], F_SETFD, FD_CLOEXEC);
  126. INLINE_SYSCALL(fcntl, 3, fds[3], F_SETFD, FD_CLOEXEC);
  127. if (stream_fd)
  128. *stream_fd = fds[1];
  129. if (cargo_fd)
  130. *cargo_fd = fds[3];
  131. ret = child;
  132. out:
  133. if (IS_ERR(ret)) {
  134. for (int i = 0; i < 4; i++)
  135. if (fds[i] >= 0)
  136. INLINE_SYSCALL(close, 1, fds[i]);
  137. }
  138. return ret;
  139. }
  140. int sgx_init_child_process (struct pal_sec * pal_sec)
  141. {
  142. struct proc_args proc_args;
  143. int ret = INLINE_SYSCALL(read, 3, PROC_INIT_FD, &proc_args, sizeof(struct proc_args));
  144. if (IS_ERR(ret)) {
  145. if (ERRNO(ret) == EBADF)
  146. return 0;
  147. return ret;
  148. }
  149. int child_status = 0;
  150. ret = INLINE_SYSCALL(write, 3, PROC_INIT_FD, &child_status, sizeof(int));
  151. if (IS_ERR(ret))
  152. return ret;
  153. memcpy(pal_sec->exec_name, proc_args.exec_name, sizeof(PAL_SEC_STR));
  154. pal_sec->instance_id = proc_args.instance_id;
  155. pal_sec->ppid = proc_args.parent_process_id;
  156. pal_sec->stream_fd = proc_args.stream_fd;
  157. pal_sec->cargo_fd = proc_args.cargo_fd;
  158. memcpy(pal_sec->pipe_prefix, proc_args.pipe_prefix, sizeof(PAL_SEC_STR));
  159. return 1;
  160. }