shim_exec.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. * shim_epoll.c
  15. *
  16. * Implementation of system call "execve".
  17. */
  18. #include <shim_internal.h>
  19. #include <shim_table.h>
  20. #include <shim_thread.h>
  21. #include <shim_fs.h>
  22. #include <shim_ipc.h>
  23. #include <shim_profile.h>
  24. #include <pal.h>
  25. #include <pal_error.h>
  26. #include <errno.h>
  27. #include <linux/futex.h>
  28. #include <sys/syscall.h>
  29. #include <sys/mman.h>
  30. #include <asm/prctl.h>
  31. static int close_on_exec (struct shim_fd_handle * fd_hdl,
  32. struct shim_handle_map * map)
  33. {
  34. if (fd_hdl->flags & FD_CLOEXEC) {
  35. struct shim_handle * hdl = __detach_fd_handle(fd_hdl, NULL, map);
  36. close_handle(hdl);
  37. }
  38. return 0;
  39. }
  40. static int close_cloexec_handle (struct shim_handle_map * map)
  41. {
  42. return walk_handle_map(&close_on_exec, map);
  43. }
  44. DEFINE_PROFILE_CATEGORY(exec_rtld, exec);
  45. DEFINE_PROFILE_INTERVAL(alloc_new_stack_for_exec, exec_rtld);
  46. DEFINE_PROFILE_INTERVAL(arrange_arguments_for_exec, exec_rtld);
  47. DEFINE_PROFILE_INTERVAL(unmap_executable_for_exec, exec_rtld);
  48. DEFINE_PROFILE_INTERVAL(unmap_loaded_binaries_for_exec, exec_rtld);
  49. DEFINE_PROFILE_INTERVAL(unmap_all_vmas_for_exec, exec_rtld);
  50. DEFINE_PROFILE_INTERVAL(load_new_executable_for_exec, exec_rtld);
  51. int init_brk_from_executable (struct shim_handle * exec);
  52. struct execve_rtld_arg
  53. {
  54. void * old_stack_top;
  55. void * old_stack;
  56. void * old_stack_red;
  57. const char ** new_argp;
  58. int * new_argcp;
  59. elf_auxv_t * new_auxp;
  60. };
  61. noreturn static void __shim_do_execve_rtld (struct execve_rtld_arg * __arg)
  62. {
  63. struct execve_rtld_arg arg;
  64. memcpy(&arg, __arg, sizeof(arg));
  65. void * old_stack_top = arg.old_stack_top;
  66. void * old_stack = arg.old_stack;
  67. void * old_stack_red = arg.old_stack_red;
  68. const char ** new_argp = arg.new_argp;
  69. int * new_argcp = arg.new_argcp;
  70. elf_auxv_t * new_auxp = arg.new_auxp;
  71. struct shim_thread * cur_thread = get_cur_thread();
  72. int ret = 0;
  73. UPDATE_PROFILE_INTERVAL();
  74. DkVirtualMemoryFree(old_stack, old_stack_top - old_stack);
  75. DkVirtualMemoryFree(old_stack_red, old_stack - old_stack_red);
  76. if (bkeep_munmap(old_stack, old_stack_top - old_stack, 0) < 0 ||
  77. bkeep_munmap(old_stack_red, old_stack - old_stack_red, 0) < 0)
  78. BUG();
  79. remove_loaded_libraries();
  80. clean_link_map_list();
  81. SAVE_PROFILE_INTERVAL(unmap_loaded_binaries_for_exec);
  82. reset_brk();
  83. size_t count = DEFAULT_VMA_COUNT;
  84. struct shim_vma_val * vmas = malloc(sizeof(struct shim_vma_val) * count);
  85. if (!vmas) {
  86. ret = -ENOMEM;
  87. goto error;
  88. }
  89. retry_dump_vmas:
  90. ret = dump_all_vmas(vmas, count);
  91. if (ret == -EOVERFLOW) {
  92. struct shim_vma_val * new_vmas
  93. = malloc(sizeof(struct shim_vma_val) * count * 2);
  94. if (!new_vmas) {
  95. free(vmas);
  96. ret = -ENOMEM;
  97. goto error;
  98. }
  99. free(vmas);
  100. vmas = new_vmas;
  101. count *= 2;
  102. goto retry_dump_vmas;
  103. }
  104. if (ret < 0) {
  105. free(vmas);
  106. goto error;
  107. }
  108. count = ret;
  109. for (struct shim_vma_val * vma = vmas ; vma < vmas + count ; vma++) {
  110. /* Don't free the current stack */
  111. if (vma->addr == cur_thread->stack)
  112. continue;
  113. /* Free all the mapped VMAs */
  114. if (!(vma->flags & VMA_UNMAPPED))
  115. DkVirtualMemoryFree(vma->addr, vma->length);
  116. /* Remove the VMAs */
  117. bkeep_munmap(vma->addr, vma->length, vma->flags);
  118. }
  119. free_vma_val_array(vmas, count);
  120. SAVE_PROFILE_INTERVAL(unmap_all_vmas_for_exec);
  121. if ((ret = load_elf_object(cur_thread->exec, NULL, 0)) < 0)
  122. goto error;
  123. if ((ret = init_brk_from_executable(cur_thread->exec)) < 0)
  124. goto error;
  125. load_elf_interp(cur_thread->exec);
  126. SAVE_PROFILE_INTERVAL(load_new_executable_for_exec);
  127. cur_thread->robust_list = NULL;
  128. #ifdef PROFILE
  129. if (ENTER_TIME)
  130. SAVE_PROFILE_INTERVAL_SINCE(syscall_execve, ENTER_TIME);
  131. #endif
  132. debug("execve: start execution\n");
  133. execute_elf_object(cur_thread->exec, new_argcp, new_argp, new_auxp);
  134. /* NOTREACHED */
  135. error:
  136. debug("execve: failed %d\n", ret);
  137. shim_terminate(ret);
  138. }
  139. static int shim_do_execve_rtld (struct shim_handle * hdl, const char ** argv,
  140. const char ** envp)
  141. {
  142. BEGIN_PROFILE_INTERVAL();
  143. struct shim_thread * cur_thread = get_cur_thread();
  144. int ret;
  145. if ((ret = close_cloexec_handle(cur_thread->handle_map)) < 0)
  146. return ret;
  147. SAVE_PROFILE_INTERVAL(close_CLOEXEC_files_for_exec);
  148. void * tcb = malloc(sizeof(__libc_tcb_t));
  149. if (!tcb)
  150. return -ENOMEM;
  151. populate_tls(tcb, false);
  152. __disable_preempt(&((__libc_tcb_t *) tcb)->shim_tcb); // Temporarily disable preemption
  153. // during execve().
  154. debug("set tcb to %p\n", tcb);
  155. put_handle(cur_thread->exec);
  156. get_handle(hdl);
  157. cur_thread->exec = hdl;
  158. void * old_stack_top = cur_thread->stack_top;
  159. void * old_stack = cur_thread->stack;
  160. void * old_stack_red = cur_thread->stack_red;
  161. cur_thread->stack_top = NULL;
  162. cur_thread->stack = NULL;
  163. cur_thread->stack_red = NULL;
  164. initial_envp = NULL;
  165. int new_argc = 0;
  166. for (const char ** a = argv ; *a ; a++, new_argc++);
  167. int * new_argcp = &new_argc;
  168. const char ** new_argp;
  169. elf_auxv_t * new_auxp;
  170. if ((ret = init_stack(argv, envp, &new_argcp, &new_argp, &new_auxp)) < 0)
  171. return ret;
  172. SAVE_PROFILE_INTERVAL(alloc_new_stack_for_exec);
  173. struct execve_rtld_arg arg = {
  174. .old_stack_top = old_stack_top,
  175. .old_stack = old_stack,
  176. .old_stack_red = old_stack_red,
  177. .new_argp = new_argp,
  178. .new_argcp = new_argcp,
  179. .new_auxp = new_auxp
  180. };
  181. __SWITCH_STACK(new_argcp, &__shim_do_execve_rtld, &arg);
  182. return 0;
  183. }
  184. #include <shim_checkpoint.h>
  185. DEFINE_PROFILE_CATEGORY(exec, );
  186. DEFINE_PROFILE_INTERVAL(search_and_check_file_for_exec, exec);
  187. DEFINE_PROFILE_INTERVAL(open_file_for_exec, exec);
  188. DEFINE_PROFILE_INTERVAL(close_CLOEXEC_files_for_exec, exec);
  189. static int migrate_execve (struct shim_cp_store * cpstore,
  190. struct shim_thread * thread,
  191. struct shim_process * process, va_list ap)
  192. {
  193. struct shim_handle_map * handle_map;
  194. const char ** envp = va_arg(ap, const char **);
  195. int ret;
  196. BEGIN_PROFILE_INTERVAL();
  197. if ((ret = dup_handle_map(&handle_map, thread->handle_map)) < 0)
  198. return ret;
  199. set_handle_map(thread, handle_map);
  200. if ((ret = close_cloexec_handle(handle_map)) < 0)
  201. return ret;
  202. SAVE_PROFILE_INTERVAL(close_CLOEXEC_files_for_exec);
  203. /* Now we start to migrate bookkeeping for exec.
  204. The data we need to migrate are:
  205. 1. cur_threadrent thread
  206. 2. cur_threadrent filesystem
  207. 3. handle mapping
  208. 4. each handle */
  209. BEGIN_MIGRATION_DEF(execve,
  210. struct shim_thread * thread,
  211. struct shim_process * proc,
  212. const char ** envp)
  213. {
  214. DEFINE_MIGRATE(process, proc, sizeof(struct shim_process));
  215. DEFINE_MIGRATE(all_mounts, NULL, 0);
  216. DEFINE_MIGRATE(running_thread, thread, sizeof(struct shim_thread));
  217. DEFINE_MIGRATE(handle_map, thread->handle_map,
  218. sizeof (struct shim_handle_map));
  219. DEFINE_MIGRATE(migratable, NULL, 0);
  220. DEFINE_MIGRATE(environ, envp, 0);
  221. }
  222. END_MIGRATION_DEF(execve)
  223. return START_MIGRATE(cpstore, execve, thread, process, envp);
  224. }
  225. int shim_do_execve (const char * file, const char ** argv,
  226. const char ** envp)
  227. {
  228. struct shim_thread * cur_thread = get_cur_thread();
  229. struct shim_dentry * dent = NULL;
  230. int ret = 0, argc = 0;
  231. if (test_user_string(file))
  232. return -EFAULT;
  233. for (const char** a = argv; /* no condition*/; a++, argc++) {
  234. if (test_user_memory(a, sizeof(*a), false))
  235. return -EFAULT;
  236. if (*a == NULL)
  237. break;
  238. if (test_user_string(*a))
  239. return -EFAULT;
  240. }
  241. if (!envp)
  242. envp = initial_envp;
  243. for (const char** e = envp; /* no condition*/; e++) {
  244. if (test_user_memory(e, sizeof(*e), false))
  245. return -EFAULT;
  246. if (*e == NULL)
  247. break;
  248. if (test_user_string(*e))
  249. return -EFAULT;
  250. }
  251. BEGIN_PROFILE_INTERVAL();
  252. DEFINE_LIST(sharg);
  253. struct sharg {
  254. LIST_TYPE(sharg) list;
  255. int len;
  256. char arg[0];
  257. };
  258. DEFINE_LISTP(sharg);
  259. LISTP_TYPE(sharg) shargs;
  260. INIT_LISTP(&shargs);
  261. reopen:
  262. /* XXX: Not sure what to do here yet */
  263. assert(cur_thread);
  264. if ((ret = path_lookupat(NULL, file, LOOKUP_OPEN, &dent, NULL)) < 0)
  265. return ret;
  266. struct shim_mount * fs = dent->fs;
  267. get_dentry(dent);
  268. if (!fs->d_ops->open) {
  269. ret = -EACCES;
  270. err:
  271. put_dentry(dent);
  272. return ret;
  273. }
  274. if (fs->d_ops->mode) {
  275. __kernel_mode_t mode;
  276. if ((ret = fs->d_ops->mode(dent, &mode)) < 0)
  277. goto err;
  278. }
  279. SAVE_PROFILE_INTERVAL(search_and_check_file_for_exec);
  280. struct shim_handle * exec = NULL;
  281. if (!(exec = get_new_handle())) {
  282. ret = -ENOMEM;
  283. goto err;
  284. }
  285. set_handle_fs(exec, fs);
  286. exec->flags = O_RDONLY;
  287. exec->acc_mode = MAY_READ;
  288. ret = fs->d_ops->open(exec, dent, O_RDONLY);
  289. if (qstrempty(&exec->uri)) {
  290. put_handle(exec);
  291. return -EACCES;
  292. }
  293. size_t pathlen;
  294. char *path = dentry_get_path(dent, true, &pathlen);
  295. qstrsetstr(&exec->path, path, pathlen);
  296. if ((ret = check_elf_object(exec)) < 0 && ret != -EINVAL) {
  297. put_handle(exec);
  298. return ret;
  299. }
  300. if (ret == -EINVAL) { /* it's a shebang */
  301. LISTP_TYPE(sharg) new_shargs = LISTP_INIT;
  302. struct sharg * next = NULL;
  303. bool ended = false, started = false;
  304. char buf[80];
  305. do {
  306. ret = do_handle_read(exec, buf, 80);
  307. if (ret <= 0)
  308. break;
  309. char * s = buf, * c = buf, * e = buf + ret;
  310. if (!started) {
  311. if (ret < 2 || buf[0] != '#' || buf[1] != '!')
  312. break;
  313. s += 2;
  314. c += 2;
  315. started = true;
  316. }
  317. for (; c < e ; c++) {
  318. if (*c == ' ' || *c == '\n' || c == e - 1) {
  319. int l = (*c == ' ' || * c == '\n') ? c - s : e - s;
  320. if (next) {
  321. struct sharg * sh =
  322. __alloca(sizeof(struct sharg) + next->len + l + 1);
  323. sh->len = next->len + l;
  324. memcpy(sh->arg, next->arg, next->len);
  325. memcpy(sh->arg + next->len, s, l);
  326. sh->arg[next->len + l] = 0;
  327. next = sh;
  328. } else {
  329. next = __alloca(sizeof(struct sharg) + l + 1);
  330. next->len = l;
  331. memcpy(next->arg, s, l);
  332. next->arg[l] = 0;
  333. }
  334. if (*c == ' ' || *c == '\n') {
  335. INIT_LIST_HEAD(next, list);
  336. LISTP_ADD_TAIL(next, &new_shargs, list);
  337. next = NULL;
  338. s = c + 1;
  339. if (*c == '\n') {
  340. ended = true;
  341. break;
  342. }
  343. }
  344. }
  345. }
  346. } while (!ended);
  347. if (started) {
  348. if (next) {
  349. INIT_LIST_HEAD(next, list);
  350. LISTP_ADD_TAIL(next, &new_shargs, list);
  351. }
  352. struct sharg * first =
  353. LISTP_FIRST_ENTRY(&new_shargs, struct sharg, list);
  354. assert(first);
  355. debug("detected as script: run by %s\n", first->arg);
  356. file = first->arg;
  357. LISTP_SPLICE(&new_shargs, &shargs, list, sharg);
  358. put_handle(exec);
  359. goto reopen;
  360. }
  361. }
  362. SAVE_PROFILE_INTERVAL(open_file_for_exec);
  363. #if EXECVE_RTLD == 1
  364. if (!strcmp_static(PAL_CB(host_type), "Linux-SGX")) {
  365. int is_last = check_last_thread(cur_thread) == 0;
  366. if (is_last) {
  367. debug("execve() in the same process\n");
  368. return shim_do_execve_rtld(exec, argv, envp);
  369. }
  370. debug("execve() in a new process\n");
  371. }
  372. #endif
  373. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  374. if (!LISTP_EMPTY(&shargs)) {
  375. struct sharg * sh;
  376. int shargc = 0, cnt = 0;
  377. LISTP_FOR_EACH_ENTRY(sh, &shargs, list)
  378. shargc++;
  379. const char ** new_argv =
  380. __alloca(sizeof(const char *) * (argc + shargc + 1));
  381. LISTP_FOR_EACH_ENTRY(sh, &shargs, list)
  382. new_argv[cnt++] = sh->arg;
  383. for (cnt = 0 ; cnt < argc ; cnt++)
  384. new_argv[shargc + cnt] = argv[cnt];
  385. new_argv[shargc + argc] = NULL;
  386. argv = new_argv;
  387. }
  388. lock(&cur_thread->lock);
  389. put_handle(cur_thread->exec);
  390. cur_thread->exec = exec;
  391. void * stack = cur_thread->stack;
  392. void * stack_top = cur_thread->stack_top;
  393. __libc_tcb_t * tcb = cur_thread->tcb;
  394. bool user_tcb = cur_thread->user_tcb;
  395. void * frameptr = cur_thread->frameptr;
  396. cur_thread->stack = NULL;
  397. cur_thread->stack_top = NULL;
  398. cur_thread->frameptr = NULL;
  399. cur_thread->tcb = NULL;
  400. cur_thread->user_tcb = false;
  401. cur_thread->in_vm = false;
  402. unlock(&cur_thread->lock);
  403. ret = do_migrate_process(&migrate_execve, exec, argv, cur_thread, envp);
  404. lock(&cur_thread->lock);
  405. cur_thread->stack = stack;
  406. cur_thread->stack_top = stack_top;
  407. cur_thread->frameptr = frameptr;
  408. cur_thread->tcb = tcb;
  409. cur_thread->user_tcb = user_tcb;
  410. if (ret < 0) {
  411. cur_thread->in_vm = true;
  412. unlock(&cur_thread->lock);
  413. return ret;
  414. }
  415. struct shim_handle_map * handle_map = cur_thread->handle_map;
  416. cur_thread->handle_map = NULL;
  417. unlock(&cur_thread->lock);
  418. if (handle_map)
  419. put_handle_map(handle_map);
  420. if (cur_thread->dummy)
  421. switch_dummy_thread(cur_thread);
  422. try_process_exit(0, 0);
  423. return 0;
  424. }