db_process.c 14 KB

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