sgx_process.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. unsigned int proc_fds[3];
  37. PAL_SEC_STR pipe_prefix;
  38. unsigned int mcast_port;
  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. int __attribute_noinline
  52. vfork_exec(int pipe_input, int proc_fds[3], const char** argv)
  53. {
  54. int ret = ARCH_VFORK();
  55. if (ret)
  56. return ret;
  57. /* child */
  58. for (int i = 0 ; i < 3 ; i++)
  59. INLINE_SYSCALL(close, 1, proc_fds[i]);
  60. ret = INLINE_SYSCALL(dup2, 2, pipe_input, PROC_INIT_FD);
  61. if (!IS_ERR(ret)) {
  62. extern char** environ;
  63. ret = INLINE_SYSCALL(execve, 3, PAL_LOADER, argv, environ);
  64. /* shouldn't get to here */
  65. SGX_DBG(DBG_E, "unexpected failure of new process\n");
  66. }
  67. __asm__ volatile ("hlt");
  68. return 0;
  69. }
  70. int sgx_create_process(const char* uri, int nargs, const char** args, int * retfds) {
  71. int ret, rete, child;
  72. int fds[6] = { -1, -1, -1, -1, -1, -1 };
  73. if (!uri || !strstartswith_static(uri, "file:"))
  74. return -EINVAL;
  75. if (IS_ERR((ret = INLINE_SYSCALL(pipe, 1, &fds[0]))) ||
  76. IS_ERR((ret = INLINE_SYSCALL(pipe, 1, &fds[2]))) ||
  77. IS_ERR((ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX, SOCK_STREAM,
  78. 0, &fds[4]))))
  79. goto out;
  80. int proc_fds[2][3] = {
  81. { fds[0], fds[3], fds[4] },
  82. { fds[2], fds[1], fds[5] },
  83. };
  84. const char ** argv = __alloca(sizeof(const char *) * (nargs + 2));
  85. argv[0] = PAL_LOADER;
  86. memcpy(argv + 1, args, sizeof(const char *) * nargs);
  87. argv[nargs + 1] = NULL;
  88. /* Child's signal handler may mess with parent's memory during vfork(),
  89. * so block signals
  90. */
  91. ret = block_async_signals(true);
  92. if (ret < 0) {
  93. ret = -ret;
  94. goto out;
  95. }
  96. ret = vfork_exec(proc_fds[0][0], proc_fds[1], argv);
  97. if (IS_ERR(ret))
  98. goto out;
  99. child = ret;
  100. /* children unblock async signals by sgx_signal_setup() */
  101. ret = block_async_signals(false);
  102. if (ret < 0) {
  103. ret = -ret;
  104. goto out;
  105. }
  106. for (int i = 0 ; i < 3 ; i++)
  107. INLINE_SYSCALL(close, 1, proc_fds[0][i]);
  108. int pipe_in = proc_fds[1][0], pipe_out = proc_fds[1][1];
  109. struct pal_sec * pal_sec = &pal_enclave.pal_sec;
  110. struct proc_args proc_args;
  111. memcpy(proc_args.exec_name, uri, sizeof(PAL_SEC_STR));
  112. proc_args.instance_id = pal_sec->instance_id;
  113. proc_args.parent_process_id = pal_sec->pid;
  114. proc_args.proc_fds[0] = proc_fds[0][0];
  115. proc_args.proc_fds[1] = proc_fds[0][1];
  116. proc_args.proc_fds[2] = proc_fds[0][2];
  117. memcpy(proc_args.pipe_prefix, pal_sec->pipe_prefix, sizeof(PAL_SEC_STR));
  118. proc_args.mcast_port = pal_sec->mcast_port;
  119. ret = INLINE_SYSCALL(write, 3, pipe_out, &proc_args,
  120. sizeof(struct proc_args));
  121. if (IS_ERR(ret) || (size_t)ret < sizeof(struct proc_args)) {
  122. ret = -EPERM;
  123. goto out;
  124. }
  125. ret = INLINE_SYSCALL(read, 3, pipe_in, &rete, sizeof(int));
  126. if (IS_ERR(ret) || (size_t)ret < sizeof(int)) {
  127. ret = -EPERM;
  128. goto out;
  129. }
  130. if (IS_ERR(rete)) {
  131. ret = rete;
  132. goto out;
  133. }
  134. for (int i = 0 ; i < 3 ; i++) {
  135. INLINE_SYSCALL(fcntl, 3, proc_fds[1][i], F_SETFD, FD_CLOEXEC);
  136. retfds[i] = proc_fds[1][i];
  137. }
  138. ret = child;
  139. out:
  140. if (IS_ERR(ret)) {
  141. for (int i = 0 ; i < 6 ; i++)
  142. if (fds[i] >= 0)
  143. INLINE_SYSCALL(close, 1, fds[i]);
  144. }
  145. return ret;
  146. }
  147. int sgx_init_child_process (struct pal_sec * pal_sec)
  148. {
  149. struct proc_args proc_args;
  150. int ret = INLINE_SYSCALL(read, 3, PROC_INIT_FD, &proc_args,
  151. sizeof(struct proc_args));
  152. if (IS_ERR(ret)) {
  153. if (ERRNO(ret) == EBADF)
  154. return 0;
  155. return ret;
  156. }
  157. int child_status = 0;
  158. ret = INLINE_SYSCALL(write, 3, proc_args.proc_fds[1], &child_status,
  159. sizeof(int));
  160. if (IS_ERR(ret))
  161. return ret;
  162. memcpy(pal_sec->exec_name, proc_args.exec_name, sizeof(PAL_SEC_STR));
  163. pal_sec->instance_id = proc_args.instance_id;
  164. pal_sec->ppid = proc_args.parent_process_id;
  165. pal_sec->proc_fds[0] = proc_args.proc_fds[0];
  166. pal_sec->proc_fds[1] = proc_args.proc_fds[1];
  167. pal_sec->proc_fds[2] = proc_args.proc_fds[2];
  168. memcpy(pal_sec->pipe_prefix, proc_args.pipe_prefix, sizeof(PAL_SEC_STR));
  169. pal_sec->mcast_port = proc_args.mcast_port;
  170. return 1;
  171. }