shim_exec.c 14 KB

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