db_process.c 17 KB

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