db_process.c 17 KB

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