db_process.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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->__in.flags |= RFD(0)|WFD(1)|RFD(2)|WFD(2)|WRITEABLE(1)|WRITEABLE(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->__in.flags |= RFD(0)|WFD(1)|RFD(2)|WFD(2)|WRITEABLE(1)|WRITEABLE(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. struct pal_sec pal_sec;
  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. static int child_process (void * param)
  117. {
  118. struct proc_param * proc_param = param;
  119. int ret;
  120. INLINE_SYSCALL(close, 1, PROC_INIT_FD);
  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. _DkThreadExit();
  134. return 0;
  135. }
  136. int _DkProcessCreate (PAL_HANDLE * handle,
  137. const char * uri, int flags, const char ** args)
  138. {
  139. PAL_HANDLE exec = NULL;
  140. PAL_HANDLE parent_handle = NULL, child_handle = NULL;
  141. int ret;
  142. /* step 1: open uri and check whether it is an executable */
  143. if (uri) {
  144. if ((ret = _DkStreamOpen(&exec, uri, PAL_ACCESS_RDONLY, 0, 0, 0)) < 0)
  145. return ret;
  146. handle_set_cloexec(exec, true);
  147. }
  148. /* step 2: create parant and child process handle */
  149. struct proc_param param;
  150. ret = create_process_handle(&parent_handle, &child_handle);
  151. if (ret < 0)
  152. goto out;
  153. param.parent = parent_handle;
  154. param.exec = exec;
  155. param.manifest = pal_state.manifest_handle;
  156. /* step 3: compose process parameter */
  157. int parent_datasz = 0, exec_datasz = 0, manifest_datasz = 0;
  158. void * parent_data = NULL;
  159. void * exec_data = NULL;
  160. void * manifest_data = NULL;
  161. ret = handle_serialize(parent_handle, &parent_data);
  162. if (ret < 0)
  163. goto out;
  164. parent_datasz = ret;
  165. if (exec) {
  166. ret = handle_serialize(exec, &exec_data);
  167. if (ret < 0) {
  168. free(parent_data);
  169. goto out;
  170. }
  171. exec_datasz = ret;
  172. }
  173. if (pal_state.manifest_handle) {
  174. ret = handle_serialize(pal_state.manifest_handle, &manifest_data);
  175. if (ret < 0) {
  176. free(parent_data);
  177. free(exec_data);
  178. goto out;
  179. }
  180. manifest_datasz = ret;
  181. }
  182. unsigned int datasz = parent_datasz + exec_datasz + manifest_datasz;
  183. struct proc_args * proc_args =
  184. __alloca(sizeof(struct proc_args) + datasz);
  185. memcpy(&proc_args->pal_sec, &pal_sec, sizeof(struct pal_sec));
  186. proc_args->pal_sec.r_debug_state = NULL;
  187. proc_args->pal_sec.r_debug = NULL;
  188. proc_args->memory_quota = bsd_state.memory_quota;
  189. void * data = (void *) (proc_args + 1);
  190. memcpy(data, parent_data, parent_datasz);
  191. data += (proc_args->parent_data_size = parent_datasz);
  192. free(parent_data);
  193. if (exec_data) {
  194. memcpy(data, exec_data, exec_datasz);
  195. data += (proc_args->exec_data_size = exec_datasz);
  196. free(exec_data);
  197. } else {
  198. proc_args->exec_data_size = 0;
  199. }
  200. if (manifest_data) {
  201. memcpy(data, manifest_data, manifest_datasz);
  202. data += (proc_args->manifest_data_size = manifest_datasz);
  203. free(manifest_data);
  204. } else {
  205. proc_args->manifest_data_size = 0;
  206. }
  207. /* step 4: create a child thread which will execve in the future */
  208. /* the first arguement must be the PAL */
  209. int argc = 0;
  210. if (args)
  211. for (; args[argc] ; argc++);
  212. param.argv = __alloca(sizeof(const char *) * (argc + 2));
  213. param.argv[0] = PAL_LOADER;
  214. if (args)
  215. memcpy(&param.argv[1], args, sizeof(const char *) * argc);
  216. param.argv[argc + 1] = NULL;
  217. ret = INLINE_SYSCALL(vfork, 0);
  218. if (IS_ERR(ret)) {
  219. ret = -PAL_ERROR_DENIED;
  220. goto out;
  221. }
  222. if (!ret) {
  223. child_process(&param);
  224. return 0;
  225. }
  226. child_handle->process.pid = ret;
  227. /* step 4: send parameters over the process handle */
  228. ret = INLINE_SYSCALL(write, 3,
  229. child_handle->process.stream_out,
  230. proc_args,
  231. sizeof(struct proc_args) + datasz);
  232. if (IS_ERR(ret) ||
  233. ret < sizeof(struct proc_args) + datasz) {
  234. ret = -PAL_ERROR_DENIED;
  235. goto out;
  236. }
  237. *handle = child_handle;
  238. ret = 0;
  239. out:
  240. if (parent_handle)
  241. _DkObjectClose(parent_handle);
  242. if (ret < 0) {
  243. if (child_handle)
  244. _DkObjectClose(child_handle);
  245. if (exec)
  246. _DkObjectClose(exec);
  247. }
  248. return ret;
  249. }
  250. #define LARGE_PROC_ARGS 4096
  251. void init_child_process (PAL_HANDLE * parent_handle,
  252. PAL_HANDLE * exec_handle,
  253. PAL_HANDLE * manifest_handle)
  254. {
  255. int ret = 0;
  256. /* try to do a very large reading, so it doesn't have to be read for the
  257. second time */
  258. struct proc_args * proc_args = __alloca(sizeof(struct proc_args));
  259. struct proc_args * new_proc_args;
  260. int bytes = INLINE_SYSCALL(read, 3, PROC_INIT_FD, proc_args,
  261. sizeof(*proc_args));
  262. if (IS_ERR(bytes)) {
  263. if (ERRNO(bytes) != EBADF)
  264. init_fail(PAL_ERROR_DENIED, "communication fail with parent");
  265. /* in the first process */
  266. /* occupy PROC_INIT_FD so no one will use it */
  267. INLINE_SYSCALL(dup2, 2, 0, PROC_INIT_FD);
  268. return;
  269. }
  270. /* a child must have parent handle and an executable */
  271. if (!proc_args->parent_data_size)
  272. init_fail(PAL_ERROR_INVAL, "invalid process created");
  273. int datasz = proc_args->parent_data_size + proc_args->exec_data_size +
  274. proc_args->manifest_data_size;
  275. if (!datasz)
  276. goto no_data;
  277. new_proc_args = __alloca(sizeof(*proc_args) + datasz);
  278. memcpy(new_proc_args, proc_args, sizeof(*proc_args));
  279. proc_args = new_proc_args;
  280. void * data = (void *) (proc_args + 1);
  281. bytes = INLINE_SYSCALL(read, 3, PROC_INIT_FD, data, datasz);
  282. if (IS_ERR(bytes))
  283. init_fail(PAL_ERROR_DENIED, "communication fail with parent");
  284. /* now deserialize the parent_handle */
  285. PAL_HANDLE parent = NULL;
  286. ret = handle_deserialize(&parent, data, proc_args->parent_data_size);
  287. if (ret < 0)
  288. init_fail(-ret, "cannot deseilaize parent process handle");
  289. data += proc_args->parent_data_size;
  290. *parent_handle = parent;
  291. /* deserialize the executable handle */
  292. if (proc_args->exec_data_size) {
  293. PAL_HANDLE exec = NULL;
  294. ret = handle_deserialize(&exec, data,
  295. proc_args->exec_data_size);
  296. if (ret < 0)
  297. init_fail(-ret, "cannot deserialize executable handle");
  298. data += proc_args->exec_data_size;
  299. *exec_handle = exec;
  300. }
  301. /* deserialize the manifest handle, if there is one */
  302. if (proc_args->manifest_data_size) {
  303. PAL_HANDLE manifest = NULL;
  304. ret = handle_deserialize(&manifest, data,
  305. proc_args->manifest_data_size);
  306. if (ret < 0)
  307. init_fail(-ret, "cannot deserialize manifest handle");
  308. data += proc_args->manifest_data_size;
  309. *manifest_handle = manifest;
  310. }
  311. no_data:
  312. bsd_state.memory_quota = proc_args->memory_quota;
  313. memcpy(&pal_sec, &proc_args->pal_sec, sizeof(struct pal_sec));
  314. }
  315. void _DkProcessExit (int exitcode)
  316. {
  317. INLINE_SYSCALL(exit, 1, exitcode);
  318. }
  319. int _DkProcessSandboxCreate (const char * manifest, int flags)
  320. {
  321. return -PAL_ERROR_NOTIMPLEMENTED;
  322. }
  323. static int proc_read (PAL_HANDLE handle, int offset, int count,
  324. void * buffer)
  325. {
  326. int bytes = INLINE_SYSCALL(read, 3, handle->process.stream_in, buffer,
  327. count);
  328. if (IS_ERR(bytes))
  329. switch(ERRNO(bytes)) {
  330. case EWOULDBLOCK:
  331. return-PAL_ERROR_TRYAGAIN;
  332. case EINTR:
  333. return -PAL_ERROR_INTERRUPTED;
  334. default:
  335. return -PAL_ERROR_DENIED;
  336. }
  337. return bytes;
  338. }
  339. static int proc_write (PAL_HANDLE handle, int offset, int count,
  340. const void * buffer)
  341. {
  342. int bytes = INLINE_SYSCALL(write, 3, handle->process.stream_out, buffer,
  343. count);
  344. if (IS_ERR(bytes))
  345. switch(ERRNO(bytes)) {
  346. case EWOULDBLOCK:
  347. handle->__in.flags &= ~WRITEABLE(1);
  348. return-PAL_ERROR_TRYAGAIN;
  349. case EINTR:
  350. return -PAL_ERROR_INTERRUPTED;
  351. default:
  352. return -PAL_ERROR_DENIED;
  353. }
  354. if (bytes == count)
  355. handle->__in.flags |= WRITEABLE(1);
  356. else
  357. handle->__in.flags &= ~WRITEABLE(1);
  358. return bytes;
  359. }
  360. static int proc_close (PAL_HANDLE handle)
  361. {
  362. if (handle->process.stream_in != PAL_IDX_POISON) {
  363. INLINE_SYSCALL(close, 1, handle->process.stream_in);
  364. handle->process.stream_in = PAL_IDX_POISON;
  365. }
  366. if (handle->process.stream_out != PAL_IDX_POISON) {
  367. INLINE_SYSCALL(close, 1, handle->process.stream_out);
  368. handle->process.stream_out = PAL_IDX_POISON;
  369. }
  370. if (handle->process.cargo != PAL_IDX_POISON) {
  371. INLINE_SYSCALL(close, 1, handle->process.cargo);
  372. handle->process.cargo = PAL_IDX_POISON;
  373. }
  374. return 0;
  375. }
  376. static int proc_delete (PAL_HANDLE handle, int access)
  377. {
  378. int shutdown;
  379. switch (access) {
  380. case 0:
  381. shutdown = SHUT_RDWR;
  382. break;
  383. case PAL_DELETE_RD:
  384. shutdown = SHUT_RD;
  385. break;
  386. case PAL_DELETE_WR:
  387. shutdown = SHUT_WR;
  388. break;
  389. default:
  390. return -PAL_ERROR_INVAL;
  391. }
  392. if (access != PAL_DELETE_WR &&
  393. handle->process.stream_in != PAL_IDX_POISON) {
  394. INLINE_SYSCALL(close, 1, handle->process.stream_in);
  395. handle->process.stream_in = PAL_IDX_POISON;
  396. }
  397. if (access != PAL_DELETE_RD &&
  398. handle->process.stream_out != PAL_IDX_POISON) {
  399. INLINE_SYSCALL(close, 1, handle->process.stream_out);
  400. handle->process.stream_out = PAL_IDX_POISON;
  401. }
  402. if (handle->process.cargo != PAL_IDX_POISON)
  403. INLINE_SYSCALL(shutdown, 2, handle->process.cargo, shutdown);
  404. return 0;
  405. }
  406. static int proc_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  407. {
  408. int ret, val;
  409. if (handle->process.stream_in == PAL_IDX_POISON)
  410. return -PAL_ERROR_BADHANDLE;
  411. ret = INLINE_SYSCALL(ioctl, 3, handle->process.stream_in, FIONREAD, &val);
  412. if (IS_ERR(ret))
  413. return unix_to_pal_error(ERRNO(ret));
  414. attr->handle_type = pal_type_process;
  415. attr->nonblocking = handle->process.nonblocking;
  416. attr->disconnected = handle->__in.flags & (ERROR(0)|ERROR(1));
  417. attr->readable = !!val;
  418. attr->writeable = handle->__in.flags & WRITEABLE(1);
  419. attr->runnable = PAL_FALSE;
  420. attr->pending_size = val;
  421. return 0;
  422. }
  423. static int proc_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  424. {
  425. if (handle->process.stream_in == PAL_IDX_POISON)
  426. return -PAL_ERROR_BADHANDLE;
  427. int ret;
  428. if (attr->nonblocking != handle->process.nonblocking) {
  429. ret = INLINE_SYSCALL(fcntl, 3, handle->process.stream_in, F_SETFL,
  430. handle->process.nonblocking ? O_NONBLOCK : 0);
  431. if (IS_ERR(ret))
  432. return unix_to_pal_error(ERRNO(ret));
  433. handle->process.nonblocking = attr->nonblocking;
  434. }
  435. return 0;
  436. }
  437. struct handle_ops proc_ops = {
  438. .read = &proc_read,
  439. .write = &proc_write,
  440. .close = &proc_close,
  441. .delete = &proc_delete,
  442. .attrquerybyhdl = &proc_attrquerybyhdl,
  443. .attrsetbyhdl = &proc_attrsetbyhdl,
  444. };