db_process.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. * 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. int _DkProcessCreate (PAL_HANDLE * handle,
  46. const char * uri, int flags, const char ** args)
  47. {
  48. int ret, rete;
  49. const char * manifest_uri = pal_config.manifest;
  50. PAL_HANDLE manifest = pal_config.manifest_handle;
  51. int manifest_fd = -1;
  52. const char * exec_uri = NULL;
  53. PAL_HANDLE exec = NULL;
  54. int exec_fd = -1;
  55. bool noexec = false;
  56. if (uri) {
  57. exec_uri = uri;
  58. if ((ret = _DkStreamOpen(&exec, uri, PAL_ACCESS_RDONLY, 0, 0, 0)) < 0)
  59. return ret;
  60. if (check_elf_object(exec) < 0) {
  61. manifest = exec;
  62. manifest_uri = uri;
  63. exec = NULL;
  64. exec_uri = NULL;
  65. }
  66. exec_fd = exec->file.fd;
  67. INLINE_SYSCALL(fcntl, 3, exec_fd, F_SETFD, 0);
  68. } else {
  69. noexec = true;
  70. }
  71. if (manifest) {
  72. manifest_fd = manifest->file.fd;
  73. INLINE_SYSCALL(fcntl, 3, manifest_fd, F_SETFD, 0);
  74. }
  75. int fds[6] = { -1, -1, -1, -1, -1, -1 };
  76. if (IS_ERR((ret = INLINE_SYSCALL(pipe2, 2, &fds[0], 0))) ||
  77. IS_ERR((ret = INLINE_SYSCALL(pipe2, 2, &fds[2], 0))) ||
  78. IS_ERR((ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX, SOCK_STREAM,
  79. 0, &fds[4])))) {
  80. ret = -PAL_ERROR_DENIED;
  81. goto out;
  82. }
  83. int proc_fds[2][3] = {
  84. { fds[0], fds[3], fds[4] },
  85. { fds[2], fds[1], fds[5] },
  86. };
  87. int argc = 0;
  88. if (args) for (; args[argc] ; argc++);
  89. const char ** argv = __alloca(sizeof(const char *) * (argc + 2));
  90. argv[0] = PAL_LOADER;
  91. if (args) memcpy(&argv[1], args, sizeof(const char *) * argc);
  92. argv[argc + 1] = NULL;
  93. ret = ARCH_VFORK();
  94. if (IS_ERR(ret)) {
  95. ret = -PAL_ERROR_DENIED;
  96. goto out;
  97. }
  98. if (!ret) {
  99. for (int i = 0 ; i < 3 ; i++)
  100. INLINE_SYSCALL(close, 1, proc_fds[1][i]);
  101. INLINE_SYSCALL(close, 1, PROC_INIT_FD);
  102. rete = INLINE_SYSCALL(dup2, 2, proc_fds[0][0], PROC_INIT_FD);
  103. if (IS_ERR(rete))
  104. goto out_child;
  105. if (manifest_fd >= 0)
  106. INLINE_SYSCALL(fcntl, 3, manifest_fd, F_SETFD, 0);
  107. rete = INLINE_SYSCALL(execve, 3, PAL_LOADER, argv, NULL);
  108. /* shouldn't get to here */
  109. printf("unexpected failure of new process\n");
  110. out_child:
  111. asm("hlt");
  112. return 0;
  113. }
  114. if (IS_ERR(rete)) {
  115. ret = -PAL_ERROR_DENIED;
  116. goto out;
  117. }
  118. for (int i = 0 ; i < 3 ; i++)
  119. INLINE_SYSCALL(close, 1, proc_fds[0][i]);
  120. int pipe_in = proc_fds[1][0], pipe_out = proc_fds[1][1];
  121. unsigned short data_size = 0;
  122. unsigned short exec_uri_offset = 0, manifest_uri_offset = 0;
  123. if (exec_uri) {
  124. int len = strlen(exec_uri);
  125. exec_uri_offset = data_size;
  126. data_size += len + 1;
  127. }
  128. if (manifest_fd >= 0) {
  129. int len = strlen(manifest_uri);
  130. manifest_uri_offset = data_size;
  131. data_size += len + 1;
  132. }
  133. struct pal_proc_args * proc_args = __alloca(sizeof(struct pal_proc_args) +
  134. data_size);
  135. void * data = ((void *) proc_args) + sizeof(struct pal_proc_args);
  136. memset(proc_args, 0, sizeof(struct pal_proc_args));
  137. memcpy(&proc_args->pal_sec_info, &pal_sec_info, sizeof(struct pal_sec_info));
  138. proc_args->pal_sec_info._dl_debug_state = NULL;
  139. proc_args->pal_sec_info._r_debug = NULL;
  140. proc_args->proc_fds[0] = proc_fds[0][0];
  141. proc_args->proc_fds[1] = proc_fds[0][1];
  142. proc_args->proc_fds[2] = proc_fds[0][2];
  143. proc_args->parent_pid = pal_linux_config.pid;
  144. proc_args->exec_fd = (exec_fd == -1) ? PAL_IDX_POISON : exec_fd;
  145. proc_args->noexec = noexec;
  146. proc_args->manifest_fd = (manifest_fd == -1) ? PAL_IDX_POISON : manifest_fd;
  147. if (exec_uri)
  148. memcpy(data + (proc_args->exec_uri_offset = exec_uri_offset),
  149. exec_uri, strlen(exec_uri) + 1);
  150. if (manifest_uri)
  151. memcpy(data + (proc_args->manifest_uri_offset = manifest_uri_offset),
  152. manifest_uri, strlen(manifest_uri) + 1);
  153. proc_args->data_size = data_size;
  154. ret = INLINE_SYSCALL(write, 3, pipe_out, proc_args,
  155. sizeof(struct pal_proc_args) + data_size);
  156. if (IS_ERR(ret) || ret < sizeof(struct pal_proc_args) + data_size) {
  157. ret = -PAL_ERROR_DENIED;
  158. goto out;
  159. }
  160. ret = INLINE_SYSCALL(read, 3, pipe_in, &rete, sizeof(int));
  161. if (IS_ERR(ret) || ret < sizeof(int)) {
  162. ret = -PAL_ERROR_DENIED;
  163. goto out;
  164. }
  165. if (rete < 0) {
  166. ret = rete;
  167. goto out;
  168. }
  169. for (int i = 0 ; i < 3 ; i++)
  170. INLINE_SYSCALL(fcntl, 3, proc_fds[1][i], F_SETFD, FD_CLOEXEC);
  171. int pid = ret;
  172. PAL_HANDLE hdl = malloc(HANDLE_SIZE(process));
  173. SET_HANDLE_TYPE(hdl, process);
  174. hdl->__in.flags |= RFD(0)|WFD(1)|RFD(2)|WFD(2)|WRITEABLE(1)|WRITEABLE(2);
  175. hdl->process.stream_in = proc_fds[1][0];
  176. hdl->process.stream_out = proc_fds[1][1];
  177. hdl->process.cargo = proc_fds[1][2];
  178. hdl->process.pid = pid;
  179. hdl->process.nonblocking = PAL_FALSE;
  180. *handle = hdl;
  181. ret = 0;
  182. out:
  183. if (ret < 0) {
  184. for (int i = 0 ; i < 6 ; i++)
  185. if (fds[i] >= 0)
  186. INLINE_SYSCALL(close, 1, fds[i]);
  187. }
  188. return ret;
  189. }
  190. int init_child_process (struct pal_proc_args * proc_args, void * proc_data)
  191. {
  192. memcpy(&pal_sec_info, &proc_args->pal_sec_info, sizeof(pal_sec_info));
  193. PAL_HANDLE parent = malloc(HANDLE_SIZE(process));
  194. SET_HANDLE_TYPE(parent, process);
  195. parent->__in.flags |= RFD(0)|WFD(1)|RFD(2)|WFD(2)|WRITEABLE(1)|WRITEABLE(2);
  196. parent->process.stream_in = proc_args->proc_fds[0];
  197. parent->process.stream_out = proc_args->proc_fds[1];
  198. parent->process.cargo = proc_args->proc_fds[2];
  199. parent->process.pid = proc_args->parent_pid;
  200. parent->process.nonblocking = PAL_FALSE;
  201. __pal_control.parent_process = parent;
  202. if (proc_args->exec_fd != PAL_IDX_POISON) {
  203. char * uri = (char *) proc_data + proc_args->exec_uri_offset;
  204. char * exec_uri = remalloc(uri, strlen(uri) + 1);
  205. INLINE_SYSCALL(lseek, 3, proc_args->exec_fd, 0, SEEK_SET);
  206. PAL_HANDLE exec = malloc(HANDLE_SIZE(file));
  207. SET_HANDLE_TYPE(exec, file);
  208. exec->__in.flags |= RFD(0);
  209. exec->file.fd = proc_args->exec_fd;
  210. exec->file.offset = 0;
  211. exec->file.append = PAL_FALSE;
  212. exec->file.pass = PAL_FALSE;
  213. exec->file.realpath = remalloc(exec_uri + 5, strlen(exec_uri + 5) + 1);
  214. pal_config.exec = exec_uri;
  215. pal_config.exec_handle = exec;
  216. } else {
  217. pal_linux_config.noexec = proc_args->noexec;
  218. }
  219. if (proc_args->manifest_fd != PAL_IDX_POISON) {
  220. char * uri = (char *) proc_data + proc_args->manifest_uri_offset;
  221. char * manifest_uri = remalloc(uri, strlen(uri) + 1);
  222. INLINE_SYSCALL(lseek, 3, proc_args->manifest_fd, 0, SEEK_SET);
  223. PAL_HANDLE manifest = malloc(HANDLE_SIZE(file));
  224. SET_HANDLE_TYPE(manifest, file);
  225. manifest->__in.flags |= RFD(0);
  226. manifest->file.fd = proc_args->manifest_fd;
  227. manifest->file.offset = 0;
  228. manifest->file.append = PAL_FALSE;
  229. manifest->file.pass = PAL_FALSE;
  230. manifest->file.realpath = remalloc(manifest_uri + 5,
  231. strlen(manifest_uri + 5) + 1);
  232. pal_config.manifest = manifest_uri;
  233. pal_config.manifest_handle = manifest;
  234. }
  235. int child_status = 0;
  236. int ret = INLINE_SYSCALL(write, 3, proc_args->proc_fds[1], &child_status,
  237. sizeof(int));
  238. if (IS_ERR(ret))
  239. return -PAL_ERROR_DENIED;
  240. return 0;
  241. }
  242. void _DkProcessExit (int exitcode)
  243. {
  244. if (__pal_control.parent_process)
  245. _DkObjectClose(__pal_control.parent_process);
  246. if (__pal_control.manifest_handle)
  247. _DkObjectClose(__pal_control.manifest_handle);
  248. INLINE_SYSCALL(exit_group, 1, exitcode);
  249. }
  250. int ioctl_set_graphene (struct config_store * config, int ndefault,
  251. const struct graphene_user_policy * default_policies);
  252. static int set_graphene_task (const char * uri, int flags)
  253. {
  254. PAL_HANDLE handle = NULL;
  255. int ret;
  256. if ((ret = _DkStreamOpen(&handle, uri, PAL_ACCESS_RDONLY, 0, 0, 0)) < 0)
  257. return ret;
  258. PAL_STREAM_ATTR attr;
  259. if ((ret = _DkStreamAttributesQuerybyHandle(handle, &attr)) < 0)
  260. goto out;
  261. void * addr = NULL;
  262. size_t size = attr.size;
  263. if ((ret = _DkStreamMap(handle, &addr, PAL_PROT_READ, 0,
  264. ALLOC_ALIGNUP(size))) < 0)
  265. goto out;
  266. struct config_store cfg;
  267. cfg.raw_data = addr;
  268. cfg.raw_size = size;
  269. cfg.malloc = malloc;
  270. cfg.free = free;
  271. if ((ret = read_config(&cfg, NULL, NULL)) < 0)
  272. goto out_mem;
  273. const char * manifest = uri;
  274. struct graphene_user_policy manifest_policy;
  275. if (!memcmp(manifest, "file:", 5)) {
  276. manifest_policy.type = GRAPHENE_FS_PATH | GRAPHENE_FS_READ;
  277. manifest_policy.value = manifest + 5;
  278. } else {
  279. manifest_policy.type = 0;
  280. }
  281. if (flags & PAL_SANDBOX_PIPE) {
  282. do {
  283. getrand(&pal_sec_info.mcast_port, sizeof(unsigned short));
  284. } while (pal_sec_info.mcast_port < 1024);
  285. }
  286. struct graphene_net_policy mcast_rules[2];
  287. memset(mcast_rules, 0, sizeof(struct graphene_net_policy) * 2);
  288. mcast_rules[0].family = AF_INET;
  289. mcast_rules[0].local.port_begin = pal_sec_info.mcast_port;
  290. mcast_rules[0].local.port_end = pal_sec_info.mcast_port;
  291. mcast_rules[0].peer.port_begin = 0;
  292. mcast_rules[0].peer.port_end = 65535;
  293. mcast_rules[1].family = AF_INET;
  294. mcast_rules[1].local.port_begin = 0;
  295. mcast_rules[1].local.port_end = 65535;
  296. inet_pton(AF_INET, MCAST_GROUP, &mcast_rules[1].peer.addr);
  297. mcast_rules[1].peer.port_begin = pal_sec_info.mcast_port;
  298. mcast_rules[1].peer.port_end = pal_sec_info.mcast_port;
  299. if (flags & PAL_SANDBOX_PIPE) {
  300. char pipe_root[sizeof(GRAPHENE_PIPEDIR) + 20];
  301. char pipe_prefix[9];
  302. int sandboxid;
  303. snprintf(pipe_root,
  304. sizeof(GRAPHENE_PIPEDIR) + 20, GRAPHENE_PIPEDIR "/%08x",
  305. pal_sec_info.domain_id);
  306. getrand(&sandboxid, sizeof(int));
  307. snprintf(pipe_prefix, 9, "%08x", sandboxid);
  308. struct graphene_user_policy default_policies[] = {
  309. { .type = GRAPHENE_UNIX_ROOT, .value = pipe_root, },
  310. { .type = GRAPHENE_UNIX_PREFIX, .value = pipe_prefix, },
  311. { .type = GRAPHENE_NET_RULE, .value = &mcast_rules[0], },
  312. { .type = GRAPHENE_NET_RULE, .value = &mcast_rules[1], },
  313. manifest_policy,
  314. };
  315. ret = ioctl_set_graphene(&cfg, manifest_policy.type ? 5 : 4,
  316. default_policies);
  317. if (ret < 0)
  318. goto out_mem;
  319. memcpy(&pal_sec_info.pipe_prefix, pipe_prefix, 9);
  320. } else {
  321. const struct graphene_user_policy default_policies[] = {
  322. { .type = GRAPHENE_NET_RULE, .value = &mcast_rules[0], },
  323. { .type = GRAPHENE_NET_RULE, .value = &mcast_rules[1], },
  324. manifest_policy,
  325. };
  326. ret = ioctl_set_graphene(&cfg, manifest_policy.type ? 3 : 2,
  327. default_policies);
  328. if (ret < 0)
  329. goto out_mem;
  330. }
  331. pal_config.manifest = manifest;
  332. _DkObjectClose(pal_config.manifest_handle);
  333. pal_config.manifest_handle = handle;
  334. free_config(&cfg);
  335. out_mem:
  336. _DkStreamUnmap(cfg.raw_data, ALLOC_ALIGNUP(cfg.raw_size));
  337. out:
  338. DkObjectClose(handle);
  339. return ret;
  340. }
  341. int _DkProcessSandboxCreate (const char * manifest, int flags)
  342. {
  343. return set_graphene_task(manifest, flags);
  344. }
  345. static int proc_read (PAL_HANDLE handle, int offset, int count,
  346. void * buffer)
  347. {
  348. int bytes = INLINE_SYSCALL(read, 3, handle->process.stream_in, buffer,
  349. count);
  350. if (IS_ERR(bytes))
  351. switch(ERRNO(bytes)) {
  352. case EWOULDBLOCK:
  353. return-PAL_ERROR_TRYAGAIN;
  354. case EINTR:
  355. return -PAL_ERROR_INTERRUPTED;
  356. default:
  357. return -PAL_ERROR_DENIED;
  358. }
  359. return bytes;
  360. }
  361. static int proc_write (PAL_HANDLE handle, int offset, int count,
  362. const void * buffer)
  363. {
  364. int bytes = INLINE_SYSCALL(write, 3, handle->process.stream_out, buffer,
  365. count);
  366. if (IS_ERR(bytes))
  367. switch(ERRNO(bytes)) {
  368. case EWOULDBLOCK:
  369. handle->__in.flags &= ~WRITEABLE(1);
  370. return-PAL_ERROR_TRYAGAIN;
  371. case EINTR:
  372. return -PAL_ERROR_INTERRUPTED;
  373. default:
  374. return -PAL_ERROR_DENIED;
  375. }
  376. if (bytes == count)
  377. handle->__in.flags |= WRITEABLE(1);
  378. else
  379. handle->__in.flags &= ~WRITEABLE(1);
  380. return bytes;
  381. }
  382. static int proc_close (PAL_HANDLE handle)
  383. {
  384. if (handle->process.stream_in != PAL_IDX_POISON) {
  385. INLINE_SYSCALL(close, 1, handle->process.stream_in);
  386. handle->process.stream_in = PAL_IDX_POISON;
  387. }
  388. if (handle->process.stream_out != PAL_IDX_POISON) {
  389. INLINE_SYSCALL(close, 1, handle->process.stream_out);
  390. handle->process.stream_out = PAL_IDX_POISON;
  391. }
  392. if (handle->process.cargo != PAL_IDX_POISON) {
  393. INLINE_SYSCALL(close, 1, handle->process.cargo);
  394. handle->process.cargo = PAL_IDX_POISON;
  395. }
  396. return 0;
  397. }
  398. static int proc_delete (PAL_HANDLE handle, int access)
  399. {
  400. int shutdown;
  401. switch (access) {
  402. case 0:
  403. shutdown = SHUT_RDWR;
  404. break;
  405. case PAL_DELETE_RD:
  406. shutdown = SHUT_RD;
  407. break;
  408. case PAL_DELETE_WR:
  409. shutdown = SHUT_WR;
  410. break;
  411. default:
  412. return -PAL_ERROR_INVAL;
  413. }
  414. if (access != PAL_DELETE_WR &&
  415. handle->process.stream_in != PAL_IDX_POISON) {
  416. INLINE_SYSCALL(close, 1, handle->process.stream_in);
  417. handle->process.stream_in = PAL_IDX_POISON;
  418. }
  419. if (access != PAL_DELETE_RD &&
  420. handle->process.stream_out != PAL_IDX_POISON) {
  421. INLINE_SYSCALL(close, 1, handle->process.stream_out);
  422. handle->process.stream_out = PAL_IDX_POISON;
  423. }
  424. if (handle->process.cargo != PAL_IDX_POISON)
  425. INLINE_SYSCALL(shutdown, 2, handle->process.cargo, shutdown);
  426. return 0;
  427. }
  428. #ifndef FIONREAD
  429. # define FIONREAD 0x541B
  430. #endif
  431. static int proc_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  432. {
  433. int ret, val;
  434. if (handle->process.stream_in == PAL_IDX_POISON)
  435. return -PAL_ERROR_BADHANDLE;
  436. memset(attr, 0, sizeof(PAL_STREAM_ATTR));
  437. ret = INLINE_SYSCALL(ioctl, 3, handle->process.stream_in, FIONREAD, &val);
  438. if (!IS_ERR(ret))
  439. attr->size = val;
  440. attr->disconnected = handle->__in.flags & (ERROR(0)|ERROR(1));
  441. attr->readable = (attr->size > 0);
  442. attr->writeable = handle->__in.flags & WRITEABLE(1);
  443. attr->nonblocking = handle->process.nonblocking;
  444. return 0;
  445. }
  446. static int proc_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  447. {
  448. if (handle->process.stream_in == PAL_IDX_POISON)
  449. return -PAL_ERROR_BADHANDLE;
  450. int ret;
  451. if (attr->nonblocking != handle->process.nonblocking) {
  452. ret = INLINE_SYSCALL(fcntl, 3, handle->process.stream_in, F_SETFL,
  453. handle->process.nonblocking ? O_NONBLOCK : 0);
  454. if (IS_ERR(ret))
  455. return unix_to_pal_error(ERRNO(ret));
  456. handle->process.nonblocking = attr->nonblocking;
  457. }
  458. return 0;
  459. }
  460. struct handle_ops proc_ops = {
  461. .read = &proc_read,
  462. .write = &proc_write,
  463. .close = &proc_close,
  464. .delete = &proc_delete,
  465. .attrquerybyhdl = &proc_attrquerybyhdl,
  466. .attrsetbyhdl = &proc_attrsetbyhdl,
  467. };