db_process.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. * db_process.c
  15. *
  16. * This source file contains functions to create a child process and terminate
  17. * the running process. Child does not inherit any objects or memory from its
  18. * parent pricess. A Parent process may not modify the execution of its
  19. * children. It can wait for a child to exit using its handle. Also, parent and
  20. * child may communicate through I/O streams provided by the parent to the child
  21. * at creation.
  22. */
  23. #include "pal_defs.h"
  24. #include "pal_linux_defs.h"
  25. #include "pal.h"
  26. #include "pal_internal.h"
  27. #include "pal_linux.h"
  28. #include "pal_debug.h"
  29. #include "pal_error.h"
  30. #include "pal_security.h"
  31. #include "pal_rtld.h"
  32. #include "graphene-ipc.h"
  33. #include "api.h"
  34. #include <linux/sched.h>
  35. #include <linux/types.h>
  36. typedef __kernel_pid_t pid_t;
  37. #include <asm/fcntl.h>
  38. #include <sys/socket.h>
  39. #include <asm/errno.h>
  40. #ifndef SEEK_SET
  41. # define SEEK_SET 0
  42. #endif
  43. static inline int create_process_handle (PAL_HANDLE * parent,
  44. PAL_HANDLE * child)
  45. {
  46. PAL_HANDLE phdl = NULL, chdl = NULL;
  47. int fds[6] = { -1, -1, -1, -1, -1, -1 };
  48. int ret;
  49. if (IS_ERR((ret = INLINE_SYSCALL(pipe2, 2, &fds[0], O_CLOEXEC))) ||
  50. IS_ERR((ret = INLINE_SYSCALL(pipe2, 2, &fds[2], O_CLOEXEC))) ||
  51. IS_ERR((ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX,
  52. SOCK_STREAM|SOCK_CLOEXEC,
  53. 0, &fds[4])))) {
  54. ret = -PAL_ERROR_DENIED;
  55. goto out;
  56. }
  57. int proc_fds[2][3] = {
  58. { fds[0], fds[3], fds[4] },
  59. { fds[2], fds[1], fds[5] },
  60. };
  61. phdl = malloc(HANDLE_SIZE(process));
  62. if (!phdl) {
  63. ret = -PAL_ERROR_NOMEM;
  64. goto out;
  65. }
  66. SET_HANDLE_TYPE(phdl, process);
  67. HANDLE_HDR(phdl)->flags |= RFD(0)|WFD(1)|RFD(2)|WFD(2)|WRITABLE(1)|WRITABLE(2);
  68. phdl->process.stream_in = proc_fds[0][0];
  69. phdl->process.stream_out = proc_fds[0][1];
  70. phdl->process.cargo = proc_fds[0][2];
  71. phdl->process.pid = linux_state.pid;
  72. phdl->process.nonblocking = PAL_FALSE;
  73. chdl = malloc(HANDLE_SIZE(process));
  74. if (!chdl) {
  75. ret = -PAL_ERROR_NOMEM;
  76. goto out;
  77. }
  78. SET_HANDLE_TYPE(chdl, process);
  79. HANDLE_HDR(chdl)->flags |= RFD(0)|WFD(1)|RFD(2)|WFD(2)|WRITABLE(1)|WRITABLE(2);
  80. chdl->process.stream_in = proc_fds[1][0];
  81. chdl->process.stream_out = proc_fds[1][1];
  82. chdl->process.cargo = proc_fds[1][2];
  83. chdl->process.pid = 0; /* unknown yet */
  84. chdl->process.nonblocking = PAL_FALSE;
  85. *parent = phdl;
  86. *child = chdl;
  87. ret = 0;
  88. out:
  89. if (ret < 0) {
  90. if (phdl)
  91. _DkObjectClose(phdl);
  92. if (chdl)
  93. _DkObjectClose(chdl);
  94. for (int i = 0 ; i < 6 ; i++)
  95. if (fds[i] != -1)
  96. INLINE_SYSCALL(close, 1, fds[i]);
  97. }
  98. return ret;
  99. }
  100. struct proc_param {
  101. PAL_HANDLE parent;
  102. PAL_HANDLE exec;
  103. PAL_HANDLE manifest;
  104. const char ** argv;
  105. };
  106. struct proc_args {
  107. PAL_NUM parent_process_id;
  108. struct pal_sec pal_sec;
  109. #if PROFILING == 1
  110. unsigned long process_create_time;
  111. #endif
  112. unsigned long memory_quota;
  113. unsigned int parent_data_size;
  114. unsigned int exec_data_size;
  115. unsigned int manifest_data_size;
  116. };
  117. /*
  118. * vfork() shares stack between child and parent. Any stack modifications in
  119. * child are reflected in parent's stack. Compiler may unwittingly modify
  120. * child's stack for its own purposes and thus corrupt parent's stack
  121. * (e.g., GCC re-uses the same stack area for local vars with non-overlapping
  122. * lifetimes).
  123. * Introduce noinline function with stack area used only by child.
  124. * Make this function non-local to keep function signature.
  125. * NOTE: more tricks may be needed to prevent unexpected optimization for
  126. * future compiler.
  127. */
  128. int __attribute_noinline
  129. child_process (struct proc_param * proc_param)
  130. {
  131. int ret = ARCH_VFORK();
  132. if (ret)
  133. return ret;
  134. /* child */
  135. ret = INLINE_SYSCALL(dup2, 2, proc_param->parent->process.stream_in,
  136. PROC_INIT_FD);
  137. if (IS_ERR(ret))
  138. goto failed;
  139. if (proc_param->parent)
  140. handle_set_cloexec(proc_param->parent, false);
  141. if (proc_param->exec)
  142. handle_set_cloexec(proc_param->exec, false);
  143. if (proc_param->manifest)
  144. handle_set_cloexec(proc_param->manifest, false);
  145. INLINE_SYSCALL(execve, 3, PAL_LOADER, proc_param->argv,
  146. linux_state.environ);
  147. failed:
  148. /* fail is it gets here */
  149. return -PAL_ERROR_DENIED;
  150. }
  151. int _DkProcessCreate (PAL_HANDLE * handle, const char * uri, const char ** args)
  152. {
  153. PAL_HANDLE exec = NULL;
  154. PAL_HANDLE parent_handle = NULL, child_handle = NULL;
  155. int ret;
  156. #if PROFILING == 1
  157. unsigned long before_create = _DkSystemTimeQuery();
  158. #endif
  159. /* step 1: open uri and check whether it is an executable */
  160. if (uri) {
  161. if ((ret = _DkStreamOpen(&exec, uri, PAL_ACCESS_RDONLY, 0, 0, 0)) < 0)
  162. return ret;
  163. if (check_elf_object(exec) < 0) {
  164. ret = -PAL_ERROR_INVAL;
  165. goto out;
  166. }
  167. /* If this process creation is for fork emulation,
  168. * map address of executable is already determined.
  169. * tell its address to forked process.
  170. */
  171. size_t len;
  172. const char * file_uri = "file:";
  173. if (exec_map && exec_map->l_name &&
  174. (len = strlen(uri)) >= 5 && !memcmp(uri, file_uri, 5) &&
  175. /* skip "file:"*/
  176. strlen(exec_map->l_name) == len - 5 &&
  177. /* + 1 for lasting * NUL */
  178. !memcmp(exec_map->l_name, uri + 5, len - 5 + 1))
  179. exec->file.map_start = (PAL_PTR)exec_map->l_map_start;
  180. }
  181. /* step 2: create parant and child process handle */
  182. struct proc_param param;
  183. ret = create_process_handle(&parent_handle, &child_handle);
  184. if (ret < 0)
  185. goto out;
  186. param.parent = parent_handle;
  187. param.exec = exec;
  188. param.manifest = pal_state.manifest_handle;
  189. /* step 3: compose process parameter */
  190. size_t parent_datasz = 0, exec_datasz = 0, manifest_datasz = 0;
  191. void * parent_data = NULL;
  192. void * exec_data = NULL;
  193. void * manifest_data = NULL;
  194. ret = handle_serialize(parent_handle, &parent_data);
  195. if (ret < 0)
  196. goto out;
  197. parent_datasz = (size_t)ret;
  198. if (exec) {
  199. ret = handle_serialize(exec, &exec_data);
  200. if (ret < 0) {
  201. free(parent_data);
  202. goto out;
  203. }
  204. exec_datasz = (size_t)ret;
  205. }
  206. if (pal_state.manifest_handle) {
  207. ret = handle_serialize(pal_state.manifest_handle, &manifest_data);
  208. if (ret < 0) {
  209. free(parent_data);
  210. free(exec_data);
  211. goto out;
  212. }
  213. manifest_datasz = (size_t)ret;
  214. }
  215. size_t datasz = parent_datasz + exec_datasz + manifest_datasz;
  216. struct proc_args * proc_args = __alloca(sizeof(struct proc_args) + datasz);
  217. proc_args->parent_process_id = linux_state.parent_process_id;
  218. memcpy(&proc_args->pal_sec, &pal_sec, sizeof(struct pal_sec));
  219. proc_args->pal_sec._dl_debug_state = NULL;
  220. proc_args->pal_sec._r_debug = NULL;
  221. proc_args->memory_quota = linux_state.memory_quota;
  222. void * data = (void *) (proc_args + 1);
  223. memcpy(data, parent_data, parent_datasz);
  224. data += (proc_args->parent_data_size = parent_datasz);
  225. free(parent_data);
  226. if (exec_data) {
  227. memcpy(data, exec_data, exec_datasz);
  228. data += (proc_args->exec_data_size = exec_datasz);
  229. free(exec_data);
  230. } else {
  231. proc_args->exec_data_size = 0;
  232. }
  233. if (manifest_data) {
  234. memcpy(data, manifest_data, manifest_datasz);
  235. data += (proc_args->manifest_data_size = manifest_datasz);
  236. free(manifest_data);
  237. } else {
  238. proc_args->manifest_data_size = 0;
  239. }
  240. /* step 4: create a child thread which will execve in the future */
  241. /* the first arguement must be the PAL */
  242. int argc = 0;
  243. if (args)
  244. for (; args[argc] ; argc++);
  245. param.argv = __alloca(sizeof(const char *) * (argc + 2));
  246. param.argv[0] = PAL_LOADER;
  247. if (args)
  248. memcpy(&param.argv[1], args, sizeof(const char *) * argc);
  249. param.argv[argc + 1] = NULL;
  250. #if PROFILING == 1
  251. proc_args->process_create_time = before_create;
  252. #endif
  253. /* Child's signal handler may mess with parent's memory during vfork(),
  254. * so block signals
  255. */
  256. ret = block_async_signals(true);
  257. if (ret < 0)
  258. goto out;
  259. ret = child_process(&param);
  260. if (IS_ERR(ret)) {
  261. ret = -PAL_ERROR_DENIED;
  262. goto out;
  263. }
  264. proc_args->pal_sec.process_id = ret;
  265. child_handle->process.pid = ret;
  266. /* children unblock async signals by signal_setup() */
  267. ret = block_async_signals(false);
  268. if (ret < 0)
  269. goto out;
  270. /* step 4: send parameters over the process handle */
  271. ret = INLINE_SYSCALL(write, 3,
  272. child_handle->process.stream_out,
  273. proc_args,
  274. sizeof(struct proc_args) + datasz);
  275. if (IS_ERR(ret) || (size_t)ret < sizeof(struct proc_args) + datasz) {
  276. ret = -PAL_ERROR_DENIED;
  277. goto out;
  278. }
  279. *handle = child_handle;
  280. ret = 0;
  281. out:
  282. if (parent_handle)
  283. _DkObjectClose(parent_handle);
  284. if (exec)
  285. _DkObjectClose(exec);
  286. if (ret < 0) {
  287. if (child_handle)
  288. _DkObjectClose(child_handle);
  289. }
  290. return ret;
  291. }
  292. void init_child_process (PAL_HANDLE * parent_handle,
  293. PAL_HANDLE * exec_handle,
  294. PAL_HANDLE * manifest_handle)
  295. {
  296. int ret = 0;
  297. /* try to do a very large reading, so it doesn't have to be read for the
  298. second time */
  299. struct proc_args * proc_args = __alloca(sizeof(struct proc_args));
  300. struct proc_args * new_proc_args;
  301. int bytes = INLINE_SYSCALL(read, 3, PROC_INIT_FD, proc_args,
  302. sizeof(*proc_args));
  303. if (IS_ERR(bytes)) {
  304. if (ERRNO(bytes) != EBADF)
  305. INIT_FAIL(PAL_ERROR_DENIED, "communication fail with parent");
  306. /* in the first process */
  307. /* occupy PROC_INIT_FD so no one will use it */
  308. INLINE_SYSCALL(dup2, 2, 0, PROC_INIT_FD);
  309. return;
  310. }
  311. /* a child must have parent handle and an executable */
  312. if (!proc_args->parent_data_size)
  313. INIT_FAIL(PAL_ERROR_INVAL, "invalid process created");
  314. int datasz = proc_args->parent_data_size + proc_args->exec_data_size +
  315. proc_args->manifest_data_size;
  316. if (!datasz)
  317. goto no_data;
  318. new_proc_args = __alloca(sizeof(*proc_args) + datasz);
  319. memcpy(new_proc_args, proc_args, sizeof(*proc_args));
  320. proc_args = new_proc_args;
  321. void * data = (void *) (proc_args + 1);
  322. bytes = INLINE_SYSCALL(read, 3, PROC_INIT_FD, data, datasz);
  323. if (IS_ERR(bytes))
  324. INIT_FAIL(PAL_ERROR_DENIED, "communication fail with parent");
  325. /* now deserialize the parent_handle */
  326. PAL_HANDLE parent = NULL;
  327. ret = handle_deserialize(&parent, data, proc_args->parent_data_size);
  328. if (ret < 0)
  329. INIT_FAIL(-ret, "cannot deseilaize parent process handle");
  330. data += proc_args->parent_data_size;
  331. *parent_handle = parent;
  332. /* occupy PROC_INIT_FD so no one will use it */
  333. INLINE_SYSCALL(dup2, 2, 0, PROC_INIT_FD);
  334. /* deserialize the executable handle */
  335. if (proc_args->exec_data_size) {
  336. PAL_HANDLE exec = NULL;
  337. ret = handle_deserialize(&exec, data,
  338. proc_args->exec_data_size);
  339. if (ret < 0)
  340. INIT_FAIL(-ret, "cannot deserialize executable handle");
  341. data += proc_args->exec_data_size;
  342. *exec_handle = exec;
  343. }
  344. /* deserialize the manifest handle, if there is one */
  345. if (proc_args->manifest_data_size) {
  346. PAL_HANDLE manifest = NULL;
  347. ret = handle_deserialize(&manifest, data,
  348. proc_args->manifest_data_size);
  349. if (ret < 0)
  350. INIT_FAIL(-ret, "cannot deserialize manifest handle");
  351. data += proc_args->manifest_data_size;
  352. *manifest_handle = manifest;
  353. }
  354. no_data:
  355. linux_state.parent_process_id = proc_args->parent_process_id;
  356. linux_state.memory_quota = proc_args->memory_quota;
  357. #if PROFILING == 1
  358. pal_state.process_create_time = proc_args->process_create_time;
  359. #endif
  360. memcpy(&pal_sec, &proc_args->pal_sec, sizeof(struct pal_sec));
  361. }
  362. noreturn void _DkProcessExit (int exitcode)
  363. {
  364. INLINE_SYSCALL(exit_group, 1, exitcode);
  365. while (true) {
  366. /* nothing */;
  367. }
  368. }
  369. static int64_t proc_read (PAL_HANDLE handle, uint64_t offset, uint64_t count,
  370. void * buffer)
  371. {
  372. if (offset)
  373. return -PAL_ERROR_INVAL;
  374. int64_t bytes = INLINE_SYSCALL(read, 3, handle->process.stream_in, buffer,
  375. count);
  376. if (IS_ERR(bytes))
  377. switch(ERRNO(bytes)) {
  378. case EWOULDBLOCK:
  379. return -PAL_ERROR_TRYAGAIN;
  380. case EINTR:
  381. return -PAL_ERROR_INTERRUPTED;
  382. default:
  383. return -PAL_ERROR_DENIED;
  384. }
  385. return bytes;
  386. }
  387. static int64_t proc_write (PAL_HANDLE handle, uint64_t offset, uint64_t count,
  388. const void * buffer)
  389. {
  390. if (offset)
  391. return -PAL_ERROR_INVAL;
  392. int64_t bytes = INLINE_SYSCALL(write, 3, handle->process.stream_out, buffer,
  393. count);
  394. if (IS_ERR(bytes))
  395. switch(ERRNO(bytes)) {
  396. case EWOULDBLOCK:
  397. HANDLE_HDR(handle)->flags &= ~WRITABLE(1);
  398. return -PAL_ERROR_TRYAGAIN;
  399. case EINTR:
  400. return -PAL_ERROR_INTERRUPTED;
  401. default:
  402. return -PAL_ERROR_DENIED;
  403. }
  404. assert(!IS_ERR(bytes));
  405. if ((size_t)bytes == count)
  406. HANDLE_HDR(handle)->flags |= WRITABLE(1);
  407. else
  408. HANDLE_HDR(handle)->flags &= ~WRITABLE(1);
  409. return bytes;
  410. }
  411. static int proc_close (PAL_HANDLE handle)
  412. {
  413. if (handle->process.stream_in != PAL_IDX_POISON) {
  414. INLINE_SYSCALL(close, 1, handle->process.stream_in);
  415. handle->process.stream_in = PAL_IDX_POISON;
  416. }
  417. if (handle->process.stream_out != PAL_IDX_POISON) {
  418. INLINE_SYSCALL(close, 1, handle->process.stream_out);
  419. handle->process.stream_out = PAL_IDX_POISON;
  420. }
  421. if (handle->process.cargo != PAL_IDX_POISON) {
  422. INLINE_SYSCALL(close, 1, handle->process.cargo);
  423. handle->process.cargo = PAL_IDX_POISON;
  424. }
  425. return 0;
  426. }
  427. static int proc_delete (PAL_HANDLE handle, int access)
  428. {
  429. int shutdown;
  430. switch (access) {
  431. case 0:
  432. shutdown = SHUT_RDWR;
  433. break;
  434. case PAL_DELETE_RD:
  435. shutdown = SHUT_RD;
  436. break;
  437. case PAL_DELETE_WR:
  438. shutdown = SHUT_WR;
  439. break;
  440. default:
  441. return -PAL_ERROR_INVAL;
  442. }
  443. if (access != PAL_DELETE_WR &&
  444. handle->process.stream_in != PAL_IDX_POISON) {
  445. INLINE_SYSCALL(close, 1, handle->process.stream_in);
  446. handle->process.stream_in = PAL_IDX_POISON;
  447. }
  448. if (access != PAL_DELETE_RD &&
  449. handle->process.stream_out != PAL_IDX_POISON) {
  450. INLINE_SYSCALL(close, 1, handle->process.stream_out);
  451. handle->process.stream_out = PAL_IDX_POISON;
  452. }
  453. if (handle->process.cargo != PAL_IDX_POISON)
  454. INLINE_SYSCALL(shutdown, 2, handle->process.cargo, shutdown);
  455. return 0;
  456. }
  457. #ifndef FIONREAD
  458. # define FIONREAD 0x541B
  459. #endif
  460. static int proc_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  461. {
  462. int ret, val;
  463. if (handle->process.stream_in == PAL_IDX_POISON)
  464. return -PAL_ERROR_BADHANDLE;
  465. ret = INLINE_SYSCALL(ioctl, 3, handle->process.stream_in, FIONREAD, &val);
  466. if (IS_ERR(ret))
  467. return unix_to_pal_error(ERRNO(ret));
  468. attr->handle_type = pal_type_process;
  469. attr->nonblocking = handle->process.nonblocking;
  470. attr->disconnected = HANDLE_HDR(handle)->flags & (ERROR(0)|ERROR(1));
  471. attr->readable = !!val;
  472. attr->writable = HANDLE_HDR(handle)->flags & WRITABLE(1);
  473. attr->runnable = PAL_FALSE;
  474. attr->pending_size = val;
  475. return 0;
  476. }
  477. static int proc_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  478. {
  479. if (handle->process.stream_in == PAL_IDX_POISON)
  480. return -PAL_ERROR_BADHANDLE;
  481. int ret;
  482. if (attr->nonblocking != handle->process.nonblocking) {
  483. ret = INLINE_SYSCALL(fcntl, 3, handle->process.stream_in, F_SETFL,
  484. handle->process.nonblocking ? O_NONBLOCK : 0);
  485. if (IS_ERR(ret))
  486. return unix_to_pal_error(ERRNO(ret));
  487. handle->process.nonblocking = attr->nonblocking;
  488. }
  489. return 0;
  490. }
  491. struct handle_ops proc_ops = {
  492. .read = &proc_read,
  493. .write = &proc_write,
  494. .close = &proc_close,
  495. .delete = &proc_delete,
  496. .attrquerybyhdl = &proc_attrquerybyhdl,
  497. .attrsetbyhdl = &proc_attrsetbyhdl,
  498. };