shim_exec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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 shim_do_execve_rtld (struct shim_handle * hdl, const char ** argv,
  59. const char ** envp)
  60. {
  61. BEGIN_PROFILE_INTERVAL();
  62. struct shim_thread * cur_thread = get_cur_thread();
  63. int ret;
  64. if ((ret = close_cloexec_handle(cur_thread->handle_map)) < 0)
  65. return ret;
  66. SAVE_PROFILE_INTERVAL(close_CLOEXEC_files_for_exec);
  67. void * tcb = malloc(sizeof(__libc_tcb_t));
  68. if (!tcb)
  69. return -ENOMEM;
  70. populate_tls(tcb, false);
  71. debug("set tcb to %p\n", tcb);
  72. put_handle(cur_thread->exec);
  73. get_handle(hdl);
  74. cur_thread->exec = hdl;
  75. old_stack_top = cur_thread->stack_top;
  76. old_stack = cur_thread->stack;
  77. old_stack_red = cur_thread->stack_red;
  78. cur_thread->stack_top = NULL;
  79. cur_thread->stack = NULL;
  80. cur_thread->stack_red = NULL;
  81. initial_envp = NULL;
  82. new_argc = 0;
  83. for (const char ** a = argv ; *a ; a++, new_argc++);
  84. if ((ret = init_stack(argv, envp, &new_argp,
  85. REQUIRED_ELF_AUXV, &new_auxp)) < 0)
  86. return ret;
  87. SAVE_PROFILE_INTERVAL(alloc_new_stack_for_exec);
  88. switch_stack(new_argp);
  89. cur_thread = get_cur_thread();
  90. UPDATE_PROFILE_INTERVAL();
  91. DkVirtualMemoryFree(old_stack, old_stack_top - old_stack);
  92. DkVirtualMemoryFree(old_stack_red, old_stack - old_stack_red);
  93. int flags = 0;
  94. bkeep_munmap(old_stack, old_stack_top - old_stack, &flags);
  95. bkeep_munmap(old_stack_red, old_stack - old_stack_red, &flags);
  96. remove_loaded_libraries();
  97. clean_link_map_list();
  98. SAVE_PROFILE_INTERVAL(unmap_loaded_binaries_for_exec);
  99. init_brk();
  100. unmap_all_vmas();
  101. SAVE_PROFILE_INTERVAL(unmap_all_vmas_for_exec);
  102. if ((ret = load_elf_object(cur_thread->exec, NULL, 0)) < 0)
  103. shim_terminate();
  104. load_elf_interp(cur_thread->exec);
  105. SAVE_PROFILE_INTERVAL(load_new_executable_for_exec);
  106. cur_thread->robust_list = NULL;
  107. #ifdef PROFILE
  108. if (ENTER_TIME)
  109. SAVE_PROFILE_INTERVAL_SINCE(syscall_execve, ENTER_TIME);
  110. #endif
  111. debug("execve: start execution\n");
  112. execute_elf_object(cur_thread->exec, new_argc, new_argp,
  113. REQUIRED_ELF_AUXV, new_auxp);
  114. return 0;
  115. }
  116. #include <shim_checkpoint.h>
  117. DEFINE_PROFILE_CATAGORY(exec, );
  118. DEFINE_PROFILE_INTERVAL(search_and_check_file_for_exec, exec);
  119. DEFINE_PROFILE_INTERVAL(open_file_for_exec, exec);
  120. DEFINE_PROFILE_INTERVAL(close_CLOEXEC_files_for_exec, exec);
  121. static int migrate_execve (struct shim_cp_store * cpstore,
  122. struct shim_thread * thread,
  123. struct shim_process * process, va_list ap)
  124. {
  125. struct shim_handle_map * handle_map;
  126. const char ** envp = va_arg(ap, const char **);
  127. int ret;
  128. BEGIN_PROFILE_INTERVAL();
  129. if ((ret = dup_handle_map(&handle_map, thread->handle_map)) < 0)
  130. return ret;
  131. set_handle_map(thread, handle_map);
  132. if ((ret = close_cloexec_handle(handle_map)) < 0)
  133. return ret;
  134. SAVE_PROFILE_INTERVAL(close_CLOEXEC_files_for_exec);
  135. /* Now we start to migrate bookkeeping for exec.
  136. The data we need to migrate are:
  137. 1. cur_threadrent thread
  138. 2. cur_threadrent filesystem
  139. 3. handle mapping
  140. 4. each handle */
  141. BEGIN_MIGRATION_DEF(execve,
  142. struct shim_thread * thread,
  143. struct shim_process * proc,
  144. const char ** envp)
  145. {
  146. DEFINE_MIGRATE(process, proc, sizeof(struct shim_process));
  147. DEFINE_MIGRATE(all_mounts, NULL, 0);
  148. DEFINE_MIGRATE(running_thread, thread, sizeof(struct shim_thread));
  149. DEFINE_MIGRATE(handle_map, thread->handle_map,
  150. sizeof (struct shim_handle_map));
  151. DEFINE_MIGRATE(migratable, NULL, 0);
  152. DEFINE_MIGRATE(environ, envp, 0);
  153. }
  154. END_MIGRATION_DEF(execve)
  155. return START_MIGRATE(cpstore, execve, thread, process, envp);
  156. }
  157. int shim_do_execve (const char * file, const char ** argv,
  158. const char ** envp)
  159. {
  160. struct shim_thread * cur_thread = get_cur_thread();
  161. struct shim_dentry * dent = NULL;
  162. int ret = 0, argc = 0;
  163. for (const char ** a = argv ; *a ; a++, argc++);
  164. if (!envp)
  165. envp = initial_envp;
  166. BEGIN_PROFILE_INTERVAL();
  167. LIST_HEAD(shargs);
  168. struct sharg {
  169. struct list_head list;
  170. int len;
  171. char arg[0];
  172. };
  173. reopen:
  174. if ((ret = path_lookupat(NULL, file, LOOKUP_OPEN, &dent)) < 0)
  175. return ret;
  176. struct shim_mount * fs = dent->fs;
  177. get_dentry(dent);
  178. if (!fs->d_ops->open) {
  179. ret = -EACCES;
  180. err:
  181. put_dentry(dent);
  182. return ret;
  183. }
  184. if (fs->d_ops->mode) {
  185. __kernel_mode_t mode;
  186. if ((ret = fs->d_ops->mode(dent, &mode, 1)) < 0)
  187. goto err;
  188. }
  189. SAVE_PROFILE_INTERVAL(search_and_check_file_for_exec);
  190. struct shim_handle * exec = NULL;
  191. if (!(exec = get_new_handle())) {
  192. ret = -ENOMEM;
  193. goto err;
  194. }
  195. set_handle_fs(exec, fs);
  196. exec->flags = O_RDONLY;
  197. exec->acc_mode = MAY_READ;
  198. ret = fs->d_ops->open(exec, dent, O_RDONLY);
  199. if (qstrempty(&exec->uri)) {
  200. put_handle(exec);
  201. return -EACCES;
  202. }
  203. int pathlen;
  204. char *path = dentry_get_path(dent, true, &pathlen);
  205. qstrsetstr(&exec->path, path, pathlen);
  206. if ((ret = check_elf_object(exec)) < 0 && ret != -EINVAL) {
  207. put_handle(exec);
  208. return ret;
  209. }
  210. if (ret == -EINVAL) { /* it's a shebang */
  211. LIST_HEAD(new_shargs);
  212. struct sharg * next = NULL;
  213. bool ended = false, started = false;
  214. char buf[80];
  215. do {
  216. ret = do_handle_read(exec, buf, 80);
  217. if (ret <= 0)
  218. break;
  219. char * s = buf, * c = buf, * e = buf + ret;
  220. if (!started) {
  221. if (ret < 2 || buf[0] != '#' || buf[1] != '!')
  222. break;
  223. s += 2;
  224. c += 2;
  225. started = true;
  226. }
  227. for (; c < e ; c++) {
  228. if (*c == ' ' || *c == '\n' || c == e - 1) {
  229. int l = (*c == ' ' || * c == '\n') ? c - s : e - s;
  230. if (next) {
  231. struct sharg * sh =
  232. __alloca(sizeof(struct sharg) + next->len + l + 1);
  233. sh->len = next->len + l;
  234. memcpy(sh->arg, next->arg, next->len);
  235. memcpy(sh->arg + next->len, s, l);
  236. sh->arg[next->len + l] = 0;
  237. next = sh;
  238. } else {
  239. next = __alloca(sizeof(struct sharg) + l + 1);
  240. next->len = l;
  241. memcpy(next->arg, s, l);
  242. next->arg[l] = 0;
  243. }
  244. if (*c == ' ' || *c == '\n') {
  245. INIT_LIST_HEAD(&next->list);
  246. list_add_tail(&next->list, &new_shargs);
  247. next = NULL;
  248. s = c + 1;
  249. if (*c == '\n') {
  250. ended = true;
  251. break;
  252. }
  253. }
  254. }
  255. }
  256. } while (!ended);
  257. if (started) {
  258. if (next) {
  259. INIT_LIST_HEAD(&next->list);
  260. list_add_tail(&next->list, &new_shargs);
  261. }
  262. struct sharg * first =
  263. list_first_entry(&new_shargs, struct sharg, list);
  264. assert(first);
  265. debug("detected as script: run by %s\n", first->arg);
  266. file = first->arg;
  267. list_splice(&new_shargs, &shargs);
  268. put_handle(exec);
  269. goto reopen;
  270. }
  271. }
  272. SAVE_PROFILE_INTERVAL(open_file_for_exec);
  273. #if EXECVE_RTLD == 1
  274. int is_last = check_last_thread(cur_thread) == 0;
  275. if (is_last)
  276. return shim_do_execve_rtld(exec, argv, envp);
  277. #endif
  278. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  279. if (!list_empty(&shargs)) {
  280. struct sharg * sh;
  281. int shargc = 0, cnt = 0;
  282. list_for_each_entry(sh, &shargs, list)
  283. shargc++;
  284. const char ** new_argv =
  285. __alloca(sizeof(const char *) * (argc + shargc + 1));
  286. list_for_each_entry(sh, &shargs, list)
  287. new_argv[cnt++] = sh->arg;
  288. for (cnt = 0 ; cnt < argc ; cnt++)
  289. new_argv[shargc + cnt] = argv[cnt];
  290. new_argv[shargc + argc] = NULL;
  291. argv = new_argv;
  292. }
  293. lock(cur_thread->lock);
  294. put_handle(cur_thread->exec);
  295. cur_thread->exec = exec;
  296. void * stack = cur_thread->stack;
  297. void * stack_top = cur_thread->stack_top;
  298. void * tcb = cur_thread->tcb;
  299. bool user_tcb = cur_thread->user_tcb;
  300. void * frameptr = cur_thread->frameptr;
  301. cur_thread->stack = NULL;
  302. cur_thread->stack_top = NULL;
  303. cur_thread->frameptr = NULL;
  304. cur_thread->tcb = NULL;
  305. cur_thread->user_tcb = false;
  306. cur_thread->in_vm = false;
  307. unlock(cur_thread->lock);
  308. ret = do_migrate_process(&migrate_execve, exec, argv, cur_thread, envp);
  309. lock(cur_thread->lock);
  310. cur_thread->stack = stack;
  311. cur_thread->stack_top = stack_top;
  312. cur_thread->frameptr = frameptr;
  313. cur_thread->tcb = tcb;
  314. cur_thread->user_tcb = user_tcb;
  315. if (ret < 0) {
  316. cur_thread->in_vm = true;
  317. unlock(cur_thread->lock);
  318. return ret;
  319. }
  320. struct shim_handle_map * handle_map = cur_thread->handle_map;
  321. cur_thread->handle_map = NULL;
  322. unlock(cur_thread->lock);
  323. if (handle_map)
  324. put_handle_map(handle_map);
  325. if (cur_thread->dummy)
  326. switch_dummy_thread(cur_thread);
  327. try_process_exit(0);
  328. return 0;
  329. }