shim_exec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_epoll.c
  17. *
  18. * Implementation of system call "execve".
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_table.h>
  22. #include <shim_thread.h>
  23. #include <shim_fs.h>
  24. #include <shim_ipc.h>
  25. #include <shim_profile.h>
  26. #include <pal.h>
  27. #include <pal_error.h>
  28. #include <errno.h>
  29. #include <linux/futex.h>
  30. #include <sys/syscall.h>
  31. #include <sys/mman.h>
  32. #include <asm/prctl.h>
  33. static int close_on_exec (struct shim_fd_handle * fd_hdl,
  34. struct shim_handle_map * map, void * arg)
  35. {
  36. if (fd_hdl->flags & FD_CLOEXEC) {
  37. struct shim_handle * hdl = __detach_fd_handle(fd_hdl, NULL, map);
  38. close_handle(hdl);
  39. }
  40. return 0;
  41. }
  42. static int close_cloexec_handle (struct shim_handle_map * map)
  43. {
  44. return walk_handle_map(&close_on_exec, map, NULL);
  45. }
  46. DEFINE_PROFILE_CATAGORY(exec_rtld, exec);
  47. DEFINE_PROFILE_INTERVAL(alloc_new_stack_for_exec, exec_rtld);
  48. DEFINE_PROFILE_INTERVAL(arrange_arguments_for_exec, exec_rtld);
  49. DEFINE_PROFILE_INTERVAL(unmap_executable_for_exec, exec_rtld);
  50. DEFINE_PROFILE_INTERVAL(unmap_loaded_binaries_for_exec, exec_rtld);
  51. DEFINE_PROFILE_INTERVAL(unmap_all_vmas_for_exec, exec_rtld);
  52. DEFINE_PROFILE_INTERVAL(load_new_executable_for_exec, exec_rtld);
  53. static void * old_stack_top, * old_stack, * old_stack_red;
  54. static const char ** new_argp;
  55. static int new_argc;
  56. static elf_auxv_t * new_auxp;
  57. #define REQUIRED_ELF_AUXV 6
  58. int init_brk_from_executable (struct shim_handle * exec);
  59. int shim_do_execve_rtld (struct shim_handle * hdl, const char ** argv,
  60. const char ** envp)
  61. {
  62. BEGIN_PROFILE_INTERVAL();
  63. struct shim_thread * cur_thread = get_cur_thread();
  64. int ret;
  65. if ((ret = close_cloexec_handle(cur_thread->handle_map)) < 0)
  66. return ret;
  67. SAVE_PROFILE_INTERVAL(close_CLOEXEC_files_for_exec);
  68. void * tcb = malloc(sizeof(__libc_tcb_t));
  69. if (!tcb)
  70. return -ENOMEM;
  71. populate_tls(tcb, false);
  72. debug("set tcb to %p\n", tcb);
  73. put_handle(cur_thread->exec);
  74. get_handle(hdl);
  75. cur_thread->exec = hdl;
  76. old_stack_top = cur_thread->stack_top;
  77. old_stack = cur_thread->stack;
  78. old_stack_red = cur_thread->stack_red;
  79. cur_thread->stack_top = NULL;
  80. cur_thread->stack = NULL;
  81. cur_thread->stack_red = NULL;
  82. initial_envp = NULL;
  83. new_argc = 0;
  84. for (const char ** a = argv ; *a ; a++, new_argc++);
  85. if ((ret = init_stack(argv, envp, &new_argp,
  86. REQUIRED_ELF_AUXV, &new_auxp)) < 0)
  87. return ret;
  88. SAVE_PROFILE_INTERVAL(alloc_new_stack_for_exec);
  89. switch_stack(new_argp);
  90. cur_thread = get_cur_thread();
  91. UPDATE_PROFILE_INTERVAL();
  92. DkVirtualMemoryFree(old_stack, old_stack_top - old_stack);
  93. DkVirtualMemoryFree(old_stack_red, old_stack - old_stack_red);
  94. int flags = 0;
  95. bkeep_munmap(old_stack, old_stack_top - old_stack, &flags);
  96. bkeep_munmap(old_stack_red, old_stack - old_stack_red, &flags);
  97. remove_loaded_libraries();
  98. clean_link_map_list();
  99. SAVE_PROFILE_INTERVAL(unmap_loaded_binaries_for_exec);
  100. reset_brk();
  101. unmap_all_vmas();
  102. SAVE_PROFILE_INTERVAL(unmap_all_vmas_for_exec);
  103. if ((ret = load_elf_object(cur_thread->exec, NULL, 0)) < 0)
  104. shim_terminate();
  105. init_brk_from_executable(cur_thread->exec);
  106. load_elf_interp(cur_thread->exec);
  107. SAVE_PROFILE_INTERVAL(load_new_executable_for_exec);
  108. cur_thread->robust_list = NULL;
  109. #ifdef PROFILE
  110. if (ENTER_TIME)
  111. SAVE_PROFILE_INTERVAL_SINCE(syscall_execve, ENTER_TIME);
  112. #endif
  113. debug("execve: start execution\n");
  114. execute_elf_object(cur_thread->exec, new_argc, new_argp,
  115. REQUIRED_ELF_AUXV, new_auxp);
  116. return 0;
  117. }
  118. #include <shim_checkpoint.h>
  119. DEFINE_PROFILE_CATAGORY(exec, );
  120. DEFINE_PROFILE_INTERVAL(search_and_check_file_for_exec, exec);
  121. DEFINE_PROFILE_INTERVAL(open_file_for_exec, exec);
  122. DEFINE_PROFILE_INTERVAL(close_CLOEXEC_files_for_exec, exec);
  123. static int migrate_execve (struct shim_cp_store * cpstore,
  124. struct shim_thread * thread,
  125. struct shim_process * process, va_list ap)
  126. {
  127. struct shim_handle_map * handle_map;
  128. const char ** envp = va_arg(ap, const char **);
  129. int ret;
  130. BEGIN_PROFILE_INTERVAL();
  131. if ((ret = dup_handle_map(&handle_map, thread->handle_map)) < 0)
  132. return ret;
  133. set_handle_map(thread, handle_map);
  134. if ((ret = close_cloexec_handle(handle_map)) < 0)
  135. return ret;
  136. SAVE_PROFILE_INTERVAL(close_CLOEXEC_files_for_exec);
  137. /* Now we start to migrate bookkeeping for exec.
  138. The data we need to migrate are:
  139. 1. cur_threadrent thread
  140. 2. cur_threadrent filesystem
  141. 3. handle mapping
  142. 4. each handle */
  143. BEGIN_MIGRATION_DEF(execve,
  144. struct shim_thread * thread,
  145. struct shim_process * proc,
  146. const char ** envp)
  147. {
  148. DEFINE_MIGRATE(process, proc, sizeof(struct shim_process));
  149. DEFINE_MIGRATE(all_mounts, NULL, 0);
  150. DEFINE_MIGRATE(running_thread, thread, sizeof(struct shim_thread));
  151. DEFINE_MIGRATE(handle_map, thread->handle_map,
  152. sizeof (struct shim_handle_map));
  153. DEFINE_MIGRATE(migratable, NULL, 0);
  154. DEFINE_MIGRATE(environ, envp, 0);
  155. }
  156. END_MIGRATION_DEF(execve)
  157. return START_MIGRATE(cpstore, execve, thread, process, envp);
  158. }
  159. int shim_do_execve (const char * file, const char ** argv,
  160. const char ** envp)
  161. {
  162. struct shim_thread * cur_thread = get_cur_thread();
  163. struct shim_dentry * dent = NULL;
  164. int ret = 0, argc = 0;
  165. for (const char ** a = argv ; *a ; a++, argc++);
  166. if (!envp)
  167. envp = initial_envp;
  168. BEGIN_PROFILE_INTERVAL();
  169. DEFINE_LIST(sharg);
  170. struct sharg {
  171. LIST_TYPE(sharg) list;
  172. int len;
  173. char arg[0];
  174. };
  175. DEFINE_LISTP(sharg);
  176. LISTP_TYPE(sharg) shargs;
  177. INIT_LISTP(&shargs);
  178. reopen:
  179. /* XXX: Not sure what to do here yet */
  180. assert(cur_thread);
  181. if ((ret = path_lookupat(NULL, file, LOOKUP_OPEN, &dent, NULL)) < 0)
  182. return ret;
  183. struct shim_mount * fs = dent->fs;
  184. get_dentry(dent);
  185. if (!fs->d_ops->open) {
  186. ret = -EACCES;
  187. err:
  188. put_dentry(dent);
  189. return ret;
  190. }
  191. if (fs->d_ops->mode) {
  192. __kernel_mode_t mode;
  193. if ((ret = fs->d_ops->mode(dent, &mode, 1)) < 0)
  194. goto err;
  195. }
  196. SAVE_PROFILE_INTERVAL(search_and_check_file_for_exec);
  197. struct shim_handle * exec = NULL;
  198. if (!(exec = get_new_handle())) {
  199. ret = -ENOMEM;
  200. goto err;
  201. }
  202. set_handle_fs(exec, fs);
  203. exec->flags = O_RDONLY;
  204. exec->acc_mode = MAY_READ;
  205. ret = fs->d_ops->open(exec, dent, O_RDONLY);
  206. if (qstrempty(&exec->uri)) {
  207. put_handle(exec);
  208. return -EACCES;
  209. }
  210. int pathlen;
  211. char *path = dentry_get_path(dent, true, &pathlen);
  212. qstrsetstr(&exec->path, path, pathlen);
  213. if ((ret = check_elf_object(exec)) < 0 && ret != -EINVAL) {
  214. put_handle(exec);
  215. return ret;
  216. }
  217. if (ret == -EINVAL) { /* it's a shebang */
  218. LISTP_TYPE(sharg) new_shargs;
  219. struct sharg * next = NULL;
  220. bool ended = false, started = false;
  221. char buf[80];
  222. do {
  223. ret = do_handle_read(exec, buf, 80);
  224. if (ret <= 0)
  225. break;
  226. char * s = buf, * c = buf, * e = buf + ret;
  227. if (!started) {
  228. if (ret < 2 || buf[0] != '#' || buf[1] != '!')
  229. break;
  230. s += 2;
  231. c += 2;
  232. started = true;
  233. }
  234. for (; c < e ; c++) {
  235. if (*c == ' ' || *c == '\n' || c == e - 1) {
  236. int l = (*c == ' ' || * c == '\n') ? c - s : e - s;
  237. if (next) {
  238. struct sharg * sh =
  239. __alloca(sizeof(struct sharg) + next->len + l + 1);
  240. sh->len = next->len + l;
  241. memcpy(sh->arg, next->arg, next->len);
  242. memcpy(sh->arg + next->len, s, l);
  243. sh->arg[next->len + l] = 0;
  244. next = sh;
  245. } else {
  246. next = __alloca(sizeof(struct sharg) + l + 1);
  247. next->len = l;
  248. memcpy(next->arg, s, l);
  249. next->arg[l] = 0;
  250. }
  251. if (*c == ' ' || *c == '\n') {
  252. INIT_LIST_HEAD(next, list);
  253. listp_add_tail(next, &new_shargs, list);
  254. next = NULL;
  255. s = c + 1;
  256. if (*c == '\n') {
  257. ended = true;
  258. break;
  259. }
  260. }
  261. }
  262. }
  263. } while (!ended);
  264. if (started) {
  265. if (next) {
  266. INIT_LIST_HEAD(next, list);
  267. listp_add_tail(next, &new_shargs, list);
  268. }
  269. struct sharg * first =
  270. listp_first_entry(&new_shargs, struct sharg, list);
  271. assert(first);
  272. debug("detected as script: run by %s\n", first->arg);
  273. file = first->arg;
  274. listp_splice(&new_shargs, &shargs, list, sharg);
  275. put_handle(exec);
  276. goto reopen;
  277. }
  278. }
  279. SAVE_PROFILE_INTERVAL(open_file_for_exec);
  280. #if EXECVE_RTLD == 1
  281. if (!strcmp_static(PAL_CB(host_type), "Linux-SGX")) {
  282. int is_last = check_last_thread(cur_thread) == 0;
  283. if (is_last) {
  284. debug("execve() in the same process\n");
  285. return shim_do_execve_rtld(exec, argv, envp);
  286. }
  287. debug("execve() in a new process\n");
  288. }
  289. #endif
  290. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  291. if (!listp_empty(&shargs)) {
  292. struct sharg * sh;
  293. int shargc = 0, cnt = 0;
  294. listp_for_each_entry(sh, &shargs, list)
  295. shargc++;
  296. const char ** new_argv =
  297. __alloca(sizeof(const char *) * (argc + shargc + 1));
  298. listp_for_each_entry(sh, &shargs, list)
  299. new_argv[cnt++] = sh->arg;
  300. for (cnt = 0 ; cnt < argc ; cnt++)
  301. new_argv[shargc + cnt] = argv[cnt];
  302. new_argv[shargc + argc] = NULL;
  303. argv = new_argv;
  304. }
  305. lock(cur_thread->lock);
  306. put_handle(cur_thread->exec);
  307. cur_thread->exec = exec;
  308. void * stack = cur_thread->stack;
  309. void * stack_top = cur_thread->stack_top;
  310. void * tcb = cur_thread->tcb;
  311. bool user_tcb = cur_thread->user_tcb;
  312. void * frameptr = cur_thread->frameptr;
  313. cur_thread->stack = NULL;
  314. cur_thread->stack_top = NULL;
  315. cur_thread->frameptr = NULL;
  316. cur_thread->tcb = NULL;
  317. cur_thread->user_tcb = false;
  318. cur_thread->in_vm = false;
  319. unlock(cur_thread->lock);
  320. ret = do_migrate_process(&migrate_execve, exec, argv, cur_thread, envp);
  321. lock(cur_thread->lock);
  322. cur_thread->stack = stack;
  323. cur_thread->stack_top = stack_top;
  324. cur_thread->frameptr = frameptr;
  325. cur_thread->tcb = tcb;
  326. cur_thread->user_tcb = user_tcb;
  327. if (ret < 0) {
  328. cur_thread->in_vm = true;
  329. unlock(cur_thread->lock);
  330. return ret;
  331. }
  332. struct shim_handle_map * handle_map = cur_thread->handle_map;
  333. cur_thread->handle_map = NULL;
  334. unlock(cur_thread->lock);
  335. if (handle_map)
  336. put_handle_map(handle_map);
  337. if (cur_thread->dummy)
  338. switch_dummy_thread(cur_thread);
  339. try_process_exit(0, 0);
  340. return 0;
  341. }