db_process.c 15 KB

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