db_process.c 18 KB

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