shim_exec.c 16 KB

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