db_process.c 16 KB

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