shim_exec.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. __disable_preempt(&((__libc_tcb_t *) tcb)->shim_tcb); // Temporarily disable preemption
  73. // during execve().
  74. debug("set tcb to %p\n", tcb);
  75. put_handle(cur_thread->exec);
  76. get_handle(hdl);
  77. cur_thread->exec = hdl;
  78. old_stack_top = cur_thread->stack_top;
  79. old_stack = cur_thread->stack;
  80. old_stack_red = cur_thread->stack_red;
  81. cur_thread->stack_top = NULL;
  82. cur_thread->stack = NULL;
  83. cur_thread->stack_red = NULL;
  84. initial_envp = NULL;
  85. new_argc = 0;
  86. for (const char ** a = argv ; *a ; a++, new_argc++);
  87. if ((ret = init_stack(argv, envp, &new_argp,
  88. REQUIRED_ELF_AUXV, &new_auxp)) < 0)
  89. return ret;
  90. SAVE_PROFILE_INTERVAL(alloc_new_stack_for_exec);
  91. switch_stack(new_argp);
  92. cur_thread = get_cur_thread();
  93. UPDATE_PROFILE_INTERVAL();
  94. DkVirtualMemoryFree(old_stack, old_stack_top - old_stack);
  95. DkVirtualMemoryFree(old_stack_red, old_stack - old_stack_red);
  96. if (bkeep_munmap(old_stack, old_stack_top - old_stack, 0) < 0 ||
  97. bkeep_munmap(old_stack_red, old_stack - old_stack_red, 0) < 0)
  98. bug();
  99. remove_loaded_libraries();
  100. clean_link_map_list();
  101. SAVE_PROFILE_INTERVAL(unmap_loaded_binaries_for_exec);
  102. reset_brk();
  103. size_t count = DEFAULT_VMA_COUNT;
  104. struct shim_vma_val * vmas = malloc(sizeof(struct shim_vma_val) * count);
  105. if (!vmas)
  106. return -ENOMEM;
  107. retry_dump_vmas:
  108. ret = dump_all_vmas(vmas, count);
  109. if (ret == -EOVERFLOW) {
  110. struct shim_vma_val * new_vmas
  111. = malloc(sizeof(struct shim_vma_val) * count * 2);
  112. if (!new_vmas) {
  113. free(vmas);
  114. return -ENOMEM;
  115. }
  116. free(vmas);
  117. vmas = new_vmas;
  118. count *= 2;
  119. goto retry_dump_vmas;
  120. }
  121. if (ret < 0) {
  122. free(vmas);
  123. return ret;
  124. }
  125. count = ret;
  126. for (struct shim_vma_val * vma = vmas ; vma < vmas + count ; vma++) {
  127. /* Don't free the current stack */
  128. if (vma->addr == cur_thread->stack)
  129. continue;
  130. /* Free all the mapped VMAs */
  131. if (!(vma->flags & VMA_UNMAPPED))
  132. DkVirtualMemoryFree(vma->addr, vma->length);
  133. /* Remove the VMAs */
  134. bkeep_munmap(vma->addr, vma->length, vma->flags);
  135. }
  136. free_vma_val_array(vmas, count);
  137. SAVE_PROFILE_INTERVAL(unmap_all_vmas_for_exec);
  138. if ((ret = load_elf_object(cur_thread->exec, NULL, 0)) < 0)
  139. shim_terminate();
  140. init_brk_from_executable(cur_thread->exec);
  141. load_elf_interp(cur_thread->exec);
  142. SAVE_PROFILE_INTERVAL(load_new_executable_for_exec);
  143. cur_thread->robust_list = NULL;
  144. #ifdef PROFILE
  145. if (ENTER_TIME)
  146. SAVE_PROFILE_INTERVAL_SINCE(syscall_execve, ENTER_TIME);
  147. #endif
  148. debug("execve: start execution\n");
  149. execute_elf_object(cur_thread->exec, new_argc, new_argp,
  150. REQUIRED_ELF_AUXV, new_auxp);
  151. return 0;
  152. }
  153. #include <shim_checkpoint.h>
  154. DEFINE_PROFILE_CATAGORY(exec, );
  155. DEFINE_PROFILE_INTERVAL(search_and_check_file_for_exec, exec);
  156. DEFINE_PROFILE_INTERVAL(open_file_for_exec, exec);
  157. DEFINE_PROFILE_INTERVAL(close_CLOEXEC_files_for_exec, exec);
  158. static int migrate_execve (struct shim_cp_store * cpstore,
  159. struct shim_thread * thread,
  160. struct shim_process * process, va_list ap)
  161. {
  162. struct shim_handle_map * handle_map;
  163. const char ** envp = va_arg(ap, const char **);
  164. int ret;
  165. BEGIN_PROFILE_INTERVAL();
  166. if ((ret = dup_handle_map(&handle_map, thread->handle_map)) < 0)
  167. return ret;
  168. set_handle_map(thread, handle_map);
  169. if ((ret = close_cloexec_handle(handle_map)) < 0)
  170. return ret;
  171. SAVE_PROFILE_INTERVAL(close_CLOEXEC_files_for_exec);
  172. /* Now we start to migrate bookkeeping for exec.
  173. The data we need to migrate are:
  174. 1. cur_threadrent thread
  175. 2. cur_threadrent filesystem
  176. 3. handle mapping
  177. 4. each handle */
  178. BEGIN_MIGRATION_DEF(execve,
  179. struct shim_thread * thread,
  180. struct shim_process * proc,
  181. const char ** envp)
  182. {
  183. DEFINE_MIGRATE(process, proc, sizeof(struct shim_process));
  184. DEFINE_MIGRATE(all_mounts, NULL, 0);
  185. DEFINE_MIGRATE(running_thread, thread, sizeof(struct shim_thread));
  186. DEFINE_MIGRATE(handle_map, thread->handle_map,
  187. sizeof (struct shim_handle_map));
  188. DEFINE_MIGRATE(migratable, NULL, 0);
  189. DEFINE_MIGRATE(environ, envp, 0);
  190. }
  191. END_MIGRATION_DEF(execve)
  192. return START_MIGRATE(cpstore, execve, thread, process, envp);
  193. }
  194. int shim_do_execve (const char * file, const char ** argv,
  195. const char ** envp)
  196. {
  197. struct shim_thread * cur_thread = get_cur_thread();
  198. struct shim_dentry * dent = NULL;
  199. int ret = 0, argc = 0;
  200. for (const char ** a = argv ; *a ; a++, argc++);
  201. if (!envp)
  202. envp = initial_envp;
  203. BEGIN_PROFILE_INTERVAL();
  204. DEFINE_LIST(sharg);
  205. struct sharg {
  206. LIST_TYPE(sharg) list;
  207. int len;
  208. char arg[0];
  209. };
  210. DEFINE_LISTP(sharg);
  211. LISTP_TYPE(sharg) shargs;
  212. INIT_LISTP(&shargs);
  213. reopen:
  214. /* XXX: Not sure what to do here yet */
  215. assert(cur_thread);
  216. if ((ret = path_lookupat(NULL, file, LOOKUP_OPEN, &dent, NULL)) < 0)
  217. return ret;
  218. struct shim_mount * fs = dent->fs;
  219. get_dentry(dent);
  220. if (!fs->d_ops->open) {
  221. ret = -EACCES;
  222. err:
  223. put_dentry(dent);
  224. return ret;
  225. }
  226. if (fs->d_ops->mode) {
  227. __kernel_mode_t mode;
  228. if ((ret = fs->d_ops->mode(dent, &mode, 1)) < 0)
  229. goto err;
  230. }
  231. SAVE_PROFILE_INTERVAL(search_and_check_file_for_exec);
  232. struct shim_handle * exec = NULL;
  233. if (!(exec = get_new_handle())) {
  234. ret = -ENOMEM;
  235. goto err;
  236. }
  237. set_handle_fs(exec, fs);
  238. exec->flags = O_RDONLY;
  239. exec->acc_mode = MAY_READ;
  240. ret = fs->d_ops->open(exec, dent, O_RDONLY);
  241. if (qstrempty(&exec->uri)) {
  242. put_handle(exec);
  243. return -EACCES;
  244. }
  245. int pathlen;
  246. char *path = dentry_get_path(dent, true, &pathlen);
  247. qstrsetstr(&exec->path, path, pathlen);
  248. if ((ret = check_elf_object(exec)) < 0 && ret != -EINVAL) {
  249. put_handle(exec);
  250. return ret;
  251. }
  252. if (ret == -EINVAL) { /* it's a shebang */
  253. LISTP_TYPE(sharg) new_shargs;
  254. struct sharg * next = NULL;
  255. bool ended = false, started = false;
  256. char buf[80];
  257. do {
  258. ret = do_handle_read(exec, buf, 80);
  259. if (ret <= 0)
  260. break;
  261. char * s = buf, * c = buf, * e = buf + ret;
  262. if (!started) {
  263. if (ret < 2 || buf[0] != '#' || buf[1] != '!')
  264. break;
  265. s += 2;
  266. c += 2;
  267. started = true;
  268. }
  269. for (; c < e ; c++) {
  270. if (*c == ' ' || *c == '\n' || c == e - 1) {
  271. int l = (*c == ' ' || * c == '\n') ? c - s : e - s;
  272. if (next) {
  273. struct sharg * sh =
  274. __alloca(sizeof(struct sharg) + next->len + l + 1);
  275. sh->len = next->len + l;
  276. memcpy(sh->arg, next->arg, next->len);
  277. memcpy(sh->arg + next->len, s, l);
  278. sh->arg[next->len + l] = 0;
  279. next = sh;
  280. } else {
  281. next = __alloca(sizeof(struct sharg) + l + 1);
  282. next->len = l;
  283. memcpy(next->arg, s, l);
  284. next->arg[l] = 0;
  285. }
  286. if (*c == ' ' || *c == '\n') {
  287. INIT_LIST_HEAD(next, list);
  288. listp_add_tail(next, &new_shargs, list);
  289. next = NULL;
  290. s = c + 1;
  291. if (*c == '\n') {
  292. ended = true;
  293. break;
  294. }
  295. }
  296. }
  297. }
  298. } while (!ended);
  299. if (started) {
  300. if (next) {
  301. INIT_LIST_HEAD(next, list);
  302. listp_add_tail(next, &new_shargs, list);
  303. }
  304. struct sharg * first =
  305. listp_first_entry(&new_shargs, struct sharg, list);
  306. assert(first);
  307. debug("detected as script: run by %s\n", first->arg);
  308. file = first->arg;
  309. listp_splice(&new_shargs, &shargs, list, sharg);
  310. put_handle(exec);
  311. goto reopen;
  312. }
  313. }
  314. SAVE_PROFILE_INTERVAL(open_file_for_exec);
  315. #if EXECVE_RTLD == 1
  316. if (!strcmp_static(PAL_CB(host_type), "Linux-SGX")) {
  317. int is_last = check_last_thread(cur_thread) == 0;
  318. if (is_last) {
  319. debug("execve() in the same process\n");
  320. return shim_do_execve_rtld(exec, argv, envp);
  321. }
  322. debug("execve() in a new process\n");
  323. }
  324. #endif
  325. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  326. if (!listp_empty(&shargs)) {
  327. struct sharg * sh;
  328. int shargc = 0, cnt = 0;
  329. listp_for_each_entry(sh, &shargs, list)
  330. shargc++;
  331. const char ** new_argv =
  332. __alloca(sizeof(const char *) * (argc + shargc + 1));
  333. listp_for_each_entry(sh, &shargs, list)
  334. new_argv[cnt++] = sh->arg;
  335. for (cnt = 0 ; cnt < argc ; cnt++)
  336. new_argv[shargc + cnt] = argv[cnt];
  337. new_argv[shargc + argc] = NULL;
  338. argv = new_argv;
  339. }
  340. lock(cur_thread->lock);
  341. put_handle(cur_thread->exec);
  342. cur_thread->exec = exec;
  343. void * stack = cur_thread->stack;
  344. void * stack_top = cur_thread->stack_top;
  345. void * tcb = cur_thread->tcb;
  346. bool user_tcb = cur_thread->user_tcb;
  347. void * frameptr = cur_thread->frameptr;
  348. cur_thread->stack = NULL;
  349. cur_thread->stack_top = NULL;
  350. cur_thread->frameptr = NULL;
  351. cur_thread->tcb = NULL;
  352. cur_thread->user_tcb = false;
  353. cur_thread->in_vm = false;
  354. unlock(cur_thread->lock);
  355. ret = do_migrate_process(&migrate_execve, exec, argv, cur_thread, envp);
  356. lock(cur_thread->lock);
  357. cur_thread->stack = stack;
  358. cur_thread->stack_top = stack_top;
  359. cur_thread->frameptr = frameptr;
  360. cur_thread->tcb = tcb;
  361. cur_thread->user_tcb = user_tcb;
  362. if (ret < 0) {
  363. cur_thread->in_vm = true;
  364. unlock(cur_thread->lock);
  365. return ret;
  366. }
  367. struct shim_handle_map * handle_map = cur_thread->handle_map;
  368. cur_thread->handle_map = NULL;
  369. unlock(cur_thread->lock);
  370. if (handle_map)
  371. put_handle_map(handle_map);
  372. if (cur_thread->dummy)
  373. switch_dummy_thread(cur_thread);
  374. try_process_exit(0, 0);
  375. return 0;
  376. }