db_process.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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_freebsd_defs.h"
  27. #include "pal.h"
  28. #include "pal_internal.h"
  29. #include "pal_freebsd.h"
  30. #include "pal_debug.h"
  31. #include "pal_error.h"
  32. #include "pal_security.h"
  33. #include "api.h"
  34. #include <sched.h>
  35. #include <sys/types.h>
  36. typedef __kernel_pid_t pid_t;
  37. #include <fcntl.h>
  38. #include <sys/socket.h>
  39. #include <errno.h>
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #include <sys/filio.h>
  43. #ifndef SEEK_SET
  44. # define SEEK_SET 0
  45. #endif
  46. static inline int create_process_handle (PAL_HANDLE * parent,
  47. PAL_HANDLE * child)
  48. {
  49. PAL_HANDLE phdl = NULL, chdl = NULL;
  50. int fds[6] = { -1, -1, -1, -1, -1, -1 };
  51. int ret;
  52. if (IS_ERR((ret = INLINE_SYSCALL(pipe2, 2, &fds[0], O_CLOEXEC))) ||
  53. IS_ERR((ret = INLINE_SYSCALL(pipe2, 2, &fds[2], O_CLOEXEC))) ||
  54. IS_ERR((ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX,
  55. SOCK_STREAM|SOCK_CLOEXEC,
  56. 0, &fds[4])))) {
  57. ret = -PAL_ERROR_DENIED;
  58. goto out;
  59. }
  60. int proc_fds[2][3] = {
  61. { fds[0], fds[3], fds[4] },
  62. { fds[2], fds[1], fds[5] },
  63. };
  64. phdl = malloc(HANDLE_SIZE(process));
  65. if (!phdl) {
  66. ret = -PAL_ERROR_NOMEM;
  67. goto out;
  68. }
  69. SET_HANDLE_TYPE(phdl, process);
  70. phdl->hdr.flags |= RFD(0)|WFD(1)|RFD(2)|WFD(2)|WRITABLE(1)|WRITABLE(2);
  71. phdl->process.stream_in = proc_fds[0][0];
  72. phdl->process.stream_out = proc_fds[0][1];
  73. phdl->process.cargo = proc_fds[0][2];
  74. phdl->process.pid = bsd_state.pid;
  75. phdl->process.nonblocking = PAL_FALSE;
  76. chdl = malloc(HANDLE_SIZE(process));
  77. if (!chdl) {
  78. ret = -PAL_ERROR_NOMEM;
  79. goto out;
  80. }
  81. SET_HANDLE_TYPE(chdl, process);
  82. chdl->hdr.flags |= RFD(0)|WFD(1)|RFD(2)|WFD(2)|WRITABLE(1)|WRITABLE(2);
  83. chdl->process.stream_in = proc_fds[1][0];
  84. chdl->process.stream_out = proc_fds[1][1];
  85. chdl->process.cargo = proc_fds[1][2];
  86. chdl->process.pid = 0; /* unknown yet */
  87. chdl->process.nonblocking = PAL_FALSE;
  88. *parent = phdl;
  89. *child = chdl;
  90. ret = 0;
  91. out:
  92. if (ret < 0) {
  93. if (phdl)
  94. _DkObjectClose(phdl);
  95. if (chdl)
  96. _DkObjectClose(chdl);
  97. for (int i = 0 ; i < 6 ; i++)
  98. if (fds[i] != -1)
  99. INLINE_SYSCALL(close, 1, fds[i]);
  100. }
  101. return ret;
  102. }
  103. struct proc_param {
  104. PAL_HANDLE parent;
  105. PAL_HANDLE exec;
  106. PAL_HANDLE manifest;
  107. const char ** argv;
  108. };
  109. struct proc_args {
  110. PAL_NUM parent_process_id;
  111. struct pal_sec pal_sec;
  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. static int child_process (void * param)
  118. {
  119. struct proc_param * proc_param = param;
  120. int ret;
  121. ret = INLINE_SYSCALL(dup2, 2, proc_param->parent->process.stream_in,
  122. PROC_INIT_FD);
  123. if (IS_ERR(ret))
  124. goto failed;
  125. handle_set_cloexec(proc_param->parent, false);
  126. if (proc_param->exec)
  127. handle_set_cloexec(proc_param->exec, false);
  128. if (proc_param->manifest)
  129. handle_set_cloexec(proc_param->manifest, false);
  130. INLINE_SYSCALL(execve, 3, PAL_LOADER, proc_param->argv, NULL);
  131. failed:
  132. /* fail is it gets here */
  133. return -PAL_ERROR_DENIED;
  134. }
  135. int _DkProcessCreate (PAL_HANDLE * handle, const char * uri, const char ** args)
  136. {
  137. PAL_HANDLE exec = NULL;
  138. PAL_HANDLE parent_handle = NULL, child_handle = NULL;
  139. int ret;
  140. /* step 1: open uri and check whether it is an executable */
  141. if (uri) {
  142. if ((ret = _DkStreamOpen(&exec, uri, PAL_ACCESS_RDONLY, 0, 0, 0)) < 0)
  143. return ret;
  144. handle_set_cloexec(exec, true);
  145. }
  146. /* step 2: create parant and child process handle */
  147. struct proc_param param;
  148. ret = create_process_handle(&parent_handle, &child_handle);
  149. if (ret < 0)
  150. goto out;
  151. param.parent = parent_handle;
  152. param.exec = exec;
  153. param.manifest = pal_state.manifest_handle;
  154. /* step 3: compose process parameter */
  155. int parent_datasz = 0, exec_datasz = 0, manifest_datasz = 0;
  156. void * parent_data = NULL;
  157. void * exec_data = NULL;
  158. void * manifest_data = NULL;
  159. ret = handle_serialize(parent_handle, &parent_data);
  160. if (ret < 0)
  161. goto out;
  162. parent_datasz = ret;
  163. if (exec) {
  164. ret = handle_serialize(exec, &exec_data);
  165. if (ret < 0) {
  166. free(parent_data);
  167. goto out;
  168. }
  169. exec_datasz = ret;
  170. }
  171. if (pal_state.manifest_handle) {
  172. ret = handle_serialize(pal_state.manifest_handle, &manifest_data);
  173. if (ret < 0) {
  174. free(parent_data);
  175. free(exec_data);
  176. goto out;
  177. }
  178. manifest_datasz = ret;
  179. }
  180. unsigned int datasz = parent_datasz + exec_datasz + manifest_datasz;
  181. struct proc_args * proc_args =
  182. __alloca(sizeof(struct proc_args) + datasz);
  183. proc_args->parent_process_id = bsd_state.parent_pid;
  184. memcpy(&proc_args->pal_sec, &pal_sec, sizeof(struct pal_sec));
  185. proc_args->pal_sec.r_debug_state = NULL;
  186. proc_args->pal_sec.r_debug = NULL;
  187. proc_args->memory_quota = bsd_state.memory_quota;
  188. void * data = (void *) (proc_args + 1);
  189. memcpy(data, parent_data, parent_datasz);
  190. data += (proc_args->parent_data_size = parent_datasz);
  191. free(parent_data);
  192. if (exec_data) {
  193. memcpy(data, exec_data, exec_datasz);
  194. data += (proc_args->exec_data_size = exec_datasz);
  195. free(exec_data);
  196. } else {
  197. proc_args->exec_data_size = 0;
  198. }
  199. if (manifest_data) {
  200. memcpy(data, manifest_data, manifest_datasz);
  201. data += (proc_args->manifest_data_size = manifest_datasz);
  202. free(manifest_data);
  203. } else {
  204. proc_args->manifest_data_size = 0;
  205. }
  206. /* step 4: create a child thread which will execve in the future */
  207. /* the first arguement must be the PAL */
  208. int argc = 0;
  209. if (args)
  210. for (; args[argc] ; argc++);
  211. param.argv = __alloca(sizeof(const char *) * (argc + 2));
  212. param.argv[0] = PAL_LOADER;
  213. if (args)
  214. memcpy(&param.argv[1], args, sizeof(const char *) * argc);
  215. param.argv[argc + 1] = NULL;
  216. ret = INLINE_SYSCALL(vfork, 0);
  217. if (IS_ERR(ret)) {
  218. ret = -PAL_ERROR_DENIED;
  219. goto out;
  220. }
  221. if (!ret) {
  222. ret = child_process(&param);
  223. goto out; /* if child_process returned, there was a failure */
  224. }
  225. child_handle->process.pid = ret;
  226. /* step 4: send parameters over the process handle */
  227. ret = INLINE_SYSCALL(write, 3,
  228. child_handle->process.stream_out,
  229. proc_args,
  230. sizeof(struct proc_args) + datasz);
  231. if (IS_ERR(ret) ||
  232. ret < sizeof(struct proc_args) + datasz) {
  233. ret = -PAL_ERROR_DENIED;
  234. goto out;
  235. }
  236. *handle = child_handle;
  237. ret = 0;
  238. out:
  239. if (parent_handle)
  240. _DkObjectClose(parent_handle);
  241. if (ret < 0) {
  242. if (child_handle)
  243. _DkObjectClose(child_handle);
  244. if (exec)
  245. _DkObjectClose(exec);
  246. }
  247. return ret;
  248. }
  249. #define LARGE_PROC_ARGS 4096
  250. void init_child_process (PAL_HANDLE * parent_handle,
  251. PAL_HANDLE * exec_handle,
  252. PAL_HANDLE * manifest_handle)
  253. {
  254. int ret = 0;
  255. /* try to do a very large reading, so it doesn't have to be read for the
  256. second time */
  257. struct proc_args * proc_args = __alloca(sizeof(struct proc_args));
  258. struct proc_args * new_proc_args;
  259. int bytes = INLINE_SYSCALL(read, 3, PROC_INIT_FD, proc_args,
  260. sizeof(*proc_args));
  261. if (IS_ERR(bytes)) {
  262. if (ERRNO(bytes) != EBADF)
  263. INIT_FAIL(PAL_ERROR_DENIED, "communication fail with parent");
  264. /* in the first process */
  265. /* occupy PROC_INIT_FD so no one will use it */
  266. INLINE_SYSCALL(dup2, 2, 0, PROC_INIT_FD);
  267. return;
  268. }
  269. /* a child must have parent handle and an executable */
  270. if (!proc_args->parent_data_size)
  271. INIT_FAIL(PAL_ERROR_INVAL, "invalid process created");
  272. int datasz = proc_args->parent_data_size + proc_args->exec_data_size +
  273. proc_args->manifest_data_size;
  274. if (!datasz)
  275. goto no_data;
  276. new_proc_args = __alloca(sizeof(*proc_args) + datasz);
  277. memcpy(new_proc_args, proc_args, sizeof(*proc_args));
  278. proc_args = new_proc_args;
  279. void * data = (void *) (proc_args + 1);
  280. bytes = INLINE_SYSCALL(read, 3, PROC_INIT_FD, data, datasz);
  281. if (IS_ERR(bytes))
  282. INIT_FAIL(PAL_ERROR_DENIED, "communication fail with parent");
  283. /* now deserialize the parent_handle */
  284. PAL_HANDLE parent = NULL;
  285. ret = handle_deserialize(&parent, data, proc_args->parent_data_size);
  286. if (ret < 0)
  287. INIT_FAIL(-ret, "cannot deseilaize parent process handle");
  288. data += proc_args->parent_data_size;
  289. *parent_handle = parent;
  290. /* deserialize the executable handle */
  291. if (proc_args->exec_data_size) {
  292. PAL_HANDLE exec = NULL;
  293. ret = handle_deserialize(&exec, data,
  294. proc_args->exec_data_size);
  295. if (ret < 0)
  296. INIT_FAIL(-ret, "cannot deserialize executable handle");
  297. data += proc_args->exec_data_size;
  298. *exec_handle = exec;
  299. }
  300. /* deserialize the manifest handle, if there is one */
  301. if (proc_args->manifest_data_size) {
  302. PAL_HANDLE manifest = NULL;
  303. ret = handle_deserialize(&manifest, data,
  304. proc_args->manifest_data_size);
  305. if (ret < 0)
  306. INIT_FAIL(-ret, "cannot deserialize manifest handle");
  307. data += proc_args->manifest_data_size;
  308. *manifest_handle = manifest;
  309. }
  310. no_data:
  311. bsd_state.memory_quota = proc_args->memory_quota;
  312. memcpy(&pal_sec, &proc_args->pal_sec, sizeof(struct pal_sec));
  313. }
  314. noreturn void _DkProcessExit (int exitcode)
  315. {
  316. INLINE_SYSCALL(exit, 1, exitcode);
  317. }
  318. int _DkProcessSandboxCreate (const char * manifest, int flags)
  319. {
  320. PAL_HANDLE handle = NULL;
  321. _DkStreamOpen(&handle, manifest, PAL_ACCESS_RDONLY, 0, 0, 0);
  322. pal_state.manifest_handle = handle;
  323. pal_state.manifest = manifest;
  324. return -PAL_ERROR_NOTIMPLEMENTED;
  325. }
  326. static int proc_read (PAL_HANDLE handle, int offset, int count,
  327. void * buffer)
  328. {
  329. int bytes = INLINE_SYSCALL(read, 3, handle->process.stream_in, buffer,
  330. count);
  331. if (IS_ERR(bytes))
  332. switch(ERRNO(bytes)) {
  333. case EWOULDBLOCK:
  334. return-PAL_ERROR_TRYAGAIN;
  335. case EINTR:
  336. return -PAL_ERROR_INTERRUPTED;
  337. default:
  338. return -PAL_ERROR_DENIED;
  339. }
  340. return bytes;
  341. }
  342. static int proc_write (PAL_HANDLE handle, int offset, int count,
  343. const void * buffer)
  344. {
  345. int bytes = INLINE_SYSCALL(write, 3, handle->process.stream_out, buffer,
  346. count);
  347. if (IS_ERR(bytes))
  348. switch(ERRNO(bytes)) {
  349. case EWOULDBLOCK:
  350. handle->hdr.flags &= ~WRITABLE(1);
  351. return-PAL_ERROR_TRYAGAIN;
  352. case EINTR:
  353. return -PAL_ERROR_INTERRUPTED;
  354. default:
  355. return -PAL_ERROR_DENIED;
  356. }
  357. if (bytes == count)
  358. handle->hdr.flags |= WRITABLE(1);
  359. else
  360. handle->hdr.flags &= ~WRITABLE(1);
  361. return bytes;
  362. }
  363. static int proc_close (PAL_HANDLE handle)
  364. {
  365. if (handle->process.stream_in != PAL_IDX_POISON) {
  366. INLINE_SYSCALL(close, 1, handle->process.stream_in);
  367. handle->process.stream_in = PAL_IDX_POISON;
  368. }
  369. if (handle->process.stream_out != PAL_IDX_POISON) {
  370. INLINE_SYSCALL(close, 1, handle->process.stream_out);
  371. handle->process.stream_out = PAL_IDX_POISON;
  372. }
  373. if (handle->process.cargo != PAL_IDX_POISON) {
  374. INLINE_SYSCALL(close, 1, handle->process.cargo);
  375. handle->process.cargo = PAL_IDX_POISON;
  376. }
  377. return 0;
  378. }
  379. static int proc_delete (PAL_HANDLE handle, int access)
  380. {
  381. int shutdown;
  382. switch (access) {
  383. case 0:
  384. shutdown = SHUT_RDWR;
  385. break;
  386. case PAL_DELETE_RD:
  387. shutdown = SHUT_RD;
  388. break;
  389. case PAL_DELETE_WR:
  390. shutdown = SHUT_WR;
  391. break;
  392. default:
  393. return -PAL_ERROR_INVAL;
  394. }
  395. if (access != PAL_DELETE_WR &&
  396. handle->process.stream_in != PAL_IDX_POISON) {
  397. INLINE_SYSCALL(close, 1, handle->process.stream_in);
  398. handle->process.stream_in = PAL_IDX_POISON;
  399. }
  400. if (access != PAL_DELETE_RD &&
  401. handle->process.stream_out != PAL_IDX_POISON) {
  402. INLINE_SYSCALL(close, 1, handle->process.stream_out);
  403. handle->process.stream_out = PAL_IDX_POISON;
  404. }
  405. if (handle->process.cargo != PAL_IDX_POISON)
  406. INLINE_SYSCALL(shutdown, 2, handle->process.cargo, shutdown);
  407. return 0;
  408. }
  409. static int proc_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  410. {
  411. int ret, val;
  412. if (handle->process.stream_in == PAL_IDX_POISON)
  413. return -PAL_ERROR_BADHANDLE;
  414. ret = INLINE_SYSCALL(ioctl, 3, handle->process.stream_in, FIONREAD, &val);
  415. if (IS_ERR(ret))
  416. return unix_to_pal_error(ERRNO(ret));
  417. attr->handle_type = pal_type_process;
  418. attr->nonblocking = handle->process.nonblocking;
  419. attr->disconnected = handle->hdr.flags & (ERROR(0)|ERROR(1));
  420. attr->readable = !!val;
  421. attr->writable = handle->hdr.flags & WRITABLE(1);
  422. attr->runnable = PAL_FALSE;
  423. attr->pending_size = val;
  424. return 0;
  425. }
  426. static int proc_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  427. {
  428. if (handle->process.stream_in == PAL_IDX_POISON)
  429. return -PAL_ERROR_BADHANDLE;
  430. int ret;
  431. if (attr->nonblocking != handle->process.nonblocking) {
  432. ret = INLINE_SYSCALL(fcntl, 3, handle->process.stream_in, F_SETFL,
  433. handle->process.nonblocking ? O_NONBLOCK : 0);
  434. if (IS_ERR(ret))
  435. return unix_to_pal_error(ERRNO(ret));
  436. handle->process.nonblocking = attr->nonblocking;
  437. }
  438. return 0;
  439. }
  440. struct handle_ops proc_ops = {
  441. .read = &proc_read,
  442. .write = &proc_write,
  443. .close = &proc_close,
  444. .delete = &proc_delete,
  445. .attrquerybyhdl = &proc_attrquerybyhdl,
  446. .attrsetbyhdl = &proc_attrsetbyhdl,
  447. };