db_streams.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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_stream.c
  15. *
  16. * This file contains APIs to open, read, write and get attribute of
  17. * streams.
  18. */
  19. #include <linux/types.h>
  20. #include "api.h"
  21. #include "pal.h"
  22. #include "pal_debug.h"
  23. #include "pal_defs.h"
  24. #include "pal_error.h"
  25. #include "pal_internal.h"
  26. #include "pal_linux.h"
  27. #include "pal_linux_defs.h"
  28. typedef __kernel_pid_t pid_t;
  29. #include <asm/errno.h>
  30. #include <asm/fcntl.h>
  31. #include <asm/poll.h>
  32. #include <asm/socket.h>
  33. #include <linux/msg.h>
  34. #include <linux/socket.h>
  35. #include <linux/wait.h>
  36. #include <netinet/in.h>
  37. #include <sys/signal.h>
  38. #include <sys/socket.h>
  39. void _DkPrintConsole(const void* buf, int size) {
  40. INLINE_SYSCALL(write, 3, 2, buf, size);
  41. }
  42. bool stataccess(struct stat* stat, int acc) {
  43. mode_t mode = stat->st_mode;
  44. if (linux_state.uid && linux_state.uid == stat->st_uid) {
  45. mode >>= 6;
  46. goto out;
  47. }
  48. if (linux_state.gid && linux_state.gid == stat->st_gid) {
  49. mode >>= 3;
  50. goto out;
  51. }
  52. if (!linux_state.uid)
  53. mode >>= 6;
  54. out:
  55. return (mode & acc);
  56. }
  57. int handle_set_cloexec(PAL_HANDLE handle, bool enable) {
  58. for (int i = 0; i < MAX_FDS; i++)
  59. if (HANDLE_HDR(handle)->flags & (RFD(i) | WFD(i))) {
  60. long flags = enable ? FD_CLOEXEC : 0;
  61. int ret = INLINE_SYSCALL(fcntl, 3, handle->generic.fds[i], F_SETFD, flags);
  62. if (IS_ERR(ret) && ERRNO(ret) != EBADF)
  63. return -PAL_ERROR_DENIED;
  64. }
  65. return 0;
  66. }
  67. /* _DkStreamUnmap for internal use. Unmap stream at certain memory address.
  68. The memory is unmapped as a whole.*/
  69. int _DkStreamUnmap(void* addr, uint64_t size) {
  70. /* Just let the kernel tell us if the mapping isn't good. */
  71. int ret = INLINE_SYSCALL(munmap, 2, addr, size);
  72. if (IS_ERR(ret))
  73. return -PAL_ERROR_DENIED;
  74. return 0;
  75. }
  76. static size_t addr_size(const struct sockaddr* addr) {
  77. switch (addr->sa_family) {
  78. case AF_INET:
  79. return sizeof(struct sockaddr_in);
  80. case AF_INET6:
  81. return sizeof(struct sockaddr_in6);
  82. default:
  83. return 0;
  84. }
  85. }
  86. int handle_serialize(PAL_HANDLE handle, void** data) {
  87. int hdlsz = handle_size(handle);
  88. const void* d1;
  89. const void* d2;
  90. int dsz1 = 0, dsz2 = 0;
  91. // ~ Check cargo PAL_HANDLE - is allowed to be sent (White List checking
  92. // of cargo type)
  93. // ~ Also, Initialize common parameter formessage passing
  94. // Channel between parent and child
  95. switch (PAL_GET_TYPE(handle)) {
  96. case pal_type_file:
  97. d1 = handle->file.realpath;
  98. dsz1 = strlen(handle->file.realpath) + 1;
  99. break;
  100. case pal_type_pipe:
  101. case pal_type_pipesrv:
  102. case pal_type_pipecli:
  103. case pal_type_pipeprv:
  104. break;
  105. case pal_type_dev:
  106. if (handle->dev.realpath) {
  107. d1 = handle->dev.realpath;
  108. dsz1 = strlen(handle->dev.realpath) + 1;
  109. }
  110. break;
  111. case pal_type_dir:
  112. if (handle->dir.realpath) {
  113. d1 = handle->dir.realpath;
  114. dsz1 = strlen(handle->dir.realpath) + 1;
  115. }
  116. break;
  117. case pal_type_tcp:
  118. case pal_type_tcpsrv:
  119. case pal_type_udp:
  120. case pal_type_udpsrv:
  121. if (handle->sock.bind) {
  122. d1 = (const void*)handle->sock.bind;
  123. dsz1 = addr_size(handle->sock.bind);
  124. }
  125. if (handle->sock.conn) {
  126. d2 = (const void*)handle->sock.conn;
  127. dsz2 = addr_size(handle->sock.conn);
  128. }
  129. break;
  130. case pal_type_process:
  131. case pal_type_eventfd:
  132. break;
  133. default:
  134. return -PAL_ERROR_INVAL;
  135. }
  136. void* buffer = malloc(hdlsz + dsz1 + dsz2);
  137. if (!buffer)
  138. return -PAL_ERROR_NOMEM;
  139. memcpy(buffer, handle, hdlsz);
  140. if (dsz1)
  141. memcpy(buffer + hdlsz, d1, dsz1);
  142. if (dsz2)
  143. memcpy(buffer + hdlsz + dsz1, d2, dsz2);
  144. *data = buffer;
  145. return hdlsz + dsz1 + dsz2;
  146. }
  147. int handle_deserialize(PAL_HANDLE* handle, const void* data, int size) {
  148. PAL_HANDLE hdl_data = (void*)data, hdl = NULL;
  149. int hdlsz = handle_size(hdl_data), ret = -PAL_ERROR_NOMEM;
  150. data += hdlsz;
  151. size -= hdlsz;
  152. // recreate PAL_HANDLE based on type
  153. switch (PAL_GET_TYPE(hdl_data)) {
  154. case pal_type_file: {
  155. int l = strlen((const char*)data) + 1;
  156. hdl = malloc(hdlsz + l);
  157. if (!hdl)
  158. break;
  159. memcpy(hdl, hdl_data, hdlsz);
  160. memcpy((void*)hdl + hdlsz, data, l);
  161. hdl->file.realpath = (void*)hdl + hdlsz;
  162. break;
  163. }
  164. case pal_type_pipe:
  165. case pal_type_pipesrv:
  166. case pal_type_pipecli:
  167. case pal_type_pipeprv:
  168. hdl = malloc_copy(hdl_data, hdlsz);
  169. break;
  170. case pal_type_dev: {
  171. int l = hdl_data->dev.realpath ? strlen((const char*)data) + 1 : 0;
  172. hdl = malloc(hdlsz + l);
  173. if (!hdl)
  174. break;
  175. memcpy(hdl, hdl_data, hdlsz);
  176. if (l) {
  177. memcpy((void*)hdl + hdlsz, data, l);
  178. hdl->dev.realpath = (void*)hdl + hdlsz;
  179. }
  180. break;
  181. }
  182. case pal_type_dir: {
  183. int l = hdl_data->dir.realpath ? strlen((const char*)data) + 1 : 0;
  184. hdl = malloc(hdlsz + l);
  185. if (!hdl)
  186. break;
  187. memcpy(hdl, hdl_data, hdlsz);
  188. if (l) {
  189. memcpy((void*)hdl + hdlsz, data, l);
  190. hdl->dir.realpath = (void*)hdl + hdlsz;
  191. }
  192. break;
  193. }
  194. case pal_type_tcp:
  195. case pal_type_tcpsrv:
  196. case pal_type_udp:
  197. case pal_type_udpsrv: {
  198. int s1 = 0, s2 = 0;
  199. if (hdl_data->sock.bind)
  200. s1 = addr_size((const struct sockaddr*)data);
  201. if (hdl_data->sock.conn)
  202. s2 = addr_size((const struct sockaddr*)(data + s1));
  203. hdl = malloc(hdlsz + s1 + s2);
  204. if (!hdl)
  205. break;
  206. memcpy(hdl, hdl_data, hdlsz);
  207. if (s1) {
  208. memcpy((void*)hdl + hdlsz, data, s1);
  209. hdl->sock.bind = (PAL_PTR)hdl + hdlsz;
  210. }
  211. if (s2) {
  212. memcpy((void*)hdl + hdlsz + s1, data + s1, s2);
  213. hdl->sock.conn = (PAL_PTR)hdl + hdlsz + s2;
  214. }
  215. break;
  216. }
  217. case pal_type_process:
  218. case pal_type_eventfd:
  219. hdl = malloc_copy(hdl_data, hdlsz);
  220. break;
  221. default:
  222. return -PAL_ERROR_BADHANDLE;
  223. }
  224. if (!hdl)
  225. return ret;
  226. *handle = hdl;
  227. return 0;
  228. }
  229. // Header for DkSendHandle and DkRecvHandle
  230. struct hdl_header {
  231. unsigned short fds : (MAX_FDS);
  232. unsigned short data_size : (16 - (MAX_FDS));
  233. };
  234. /* _DkSendHandle for internal use. Send a Pal Handle over the given
  235. process handle. Return 1 if success else return negative error code */
  236. int _DkSendHandle(PAL_HANDLE hdl, PAL_HANDLE cargo) {
  237. struct hdl_header hdl_hdr;
  238. void* hdl_data;
  239. int ret = handle_serialize(cargo, &hdl_data);
  240. if (ret < 0)
  241. return ret;
  242. hdl_hdr.fds = 0;
  243. hdl_hdr.data_size = ret;
  244. int fds[MAX_FDS];
  245. int nfds = 0;
  246. for (int i = 0; i < MAX_FDS; i++)
  247. if (HANDLE_HDR(cargo)->flags & (RFD(i) | WFD(i))) {
  248. hdl_hdr.fds |= 1U << i;
  249. fds[nfds++] = cargo->generic.fds[i];
  250. }
  251. // ~ Initialize common parameter formessage passing
  252. // Channel between parent and child
  253. int ch = hdl->process.cargo;
  254. // Declare variables required for sending the message
  255. struct msghdr hdr; // message header
  256. struct cmsghdr* chdr; // control message header
  257. struct iovec iov[1]; // IO Vector
  258. iov[0].iov_base = &hdl_hdr;
  259. iov[0].iov_len = sizeof(struct hdl_header);
  260. hdr.msg_name = NULL;
  261. hdr.msg_namelen = 0;
  262. hdr.msg_iov = iov;
  263. hdr.msg_iovlen = 1;
  264. hdr.msg_control = NULL;
  265. hdr.msg_controllen = 0;
  266. hdr.msg_flags = 0;
  267. ret = INLINE_SYSCALL(sendmsg, 3, ch, &hdr, MSG_NOSIGNAL);
  268. // Unlock is error
  269. if (IS_ERR(ret)) {
  270. free(hdl_data);
  271. return -PAL_ERROR_DENIED;
  272. }
  273. /* Message Body Composition:
  274. IOVEC[0]: PAL_HANDLE
  275. IOVEC[1..n]: Additional handle member follow
  276. Control Message: file descriptors */
  277. // Control message buffer with added space for 2 fds (ie. max size
  278. // that it will have)
  279. char cbuf[sizeof(struct cmsghdr) + MAX_FDS * sizeof(int)];
  280. // Initialize iovec[0] with struct PAL_HANDLE
  281. iov[0].iov_base = hdl_data;
  282. iov[0].iov_len = hdl_hdr.data_size;
  283. hdr.msg_iov = iov;
  284. hdr.msg_iovlen = 1;
  285. hdr.msg_control = cbuf; // Control Message Buffer
  286. hdr.msg_controllen = sizeof(cbuf);
  287. // Fill control message infomation for the file descriptors
  288. // Check hdr.msg_controllen >= sizeof(struct cmsghdr) to point to
  289. // cbuf, which is redundant based on the above code as we have
  290. // statically allocated memory.
  291. // or (struct cmsghdr*) cbuf
  292. chdr = CMSG_FIRSTHDR(&hdr); // Pointer to msg_control
  293. chdr->cmsg_level = SOL_SOCKET; // Originating Protocol
  294. chdr->cmsg_type = SCM_RIGHTS; // Protocol Specific Type
  295. // Length of control message = sizeof(struct cmsghdr) + nfds
  296. chdr->cmsg_len = CMSG_LEN(sizeof(int) * nfds);
  297. // Copy the fds below control header
  298. memcpy(CMSG_DATA(chdr), fds, sizeof(int) * nfds);
  299. // Also, Update main header with control message length (duplicate)
  300. hdr.msg_controllen = chdr->cmsg_len;
  301. // Send message
  302. ret = INLINE_SYSCALL(sendmsg, 3, ch, &hdr, 0);
  303. free(hdl_data);
  304. return IS_ERR(ret) ? -PAL_ERROR_DENIED : 0;
  305. }
  306. /* _DkRecvHandle for internal use. Receive and return a PAL_HANDLE over the
  307. given PAL_HANDLE else return negative value. */
  308. int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE* cargo) {
  309. struct hdl_header hdl_hdr;
  310. // ~ Check connection PAL_HANDLE - is of process type for sending handle
  311. // else fail
  312. if (!IS_HANDLE_TYPE(hdl, process))
  313. return -PAL_ERROR_BADHANDLE;
  314. // ~ Initialize common parameter for message passing
  315. // Channel between parent and child
  316. int ch = hdl->process.cargo;
  317. struct msghdr hdr;
  318. struct iovec iov[1];
  319. iov[0].iov_base = &hdl_hdr;
  320. iov[0].iov_len = sizeof(struct hdl_header);
  321. hdr.msg_name = NULL;
  322. hdr.msg_namelen = 0;
  323. hdr.msg_iov = iov;
  324. hdr.msg_iovlen = 1;
  325. hdr.msg_control = NULL;
  326. hdr.msg_controllen = 0;
  327. hdr.msg_flags = 0;
  328. int ret = INLINE_SYSCALL(recvmsg, 3, ch, &hdr, 0);
  329. if (IS_ERR(ret))
  330. return unix_to_pal_error(ERRNO(ret));
  331. if ((size_t)ret < sizeof(struct hdl_header)) {
  332. /*
  333. * This code block is just in case to cover all the possibilities.
  334. * We know that the file descriptor is an unix domain socket with
  335. * blocking mode and that the sender, _DkSendHandle() above, sends the
  336. * header with single sendmsg syscall which transfers message atomically.
  337. *
  338. * read size == 0: return error for the caller to try again.
  339. * It should result in EINTR.
  340. *
  341. * read size > 0: return error for the caller to give up this file
  342. * descriptor.
  343. * If the header can't be send atomically for some
  344. * reason, the sender should get EMSGSIZE.
  345. */
  346. if (!ret)
  347. return -PAL_ERROR_TRYAGAIN;
  348. return -PAL_ERROR_DENIED;
  349. }
  350. // initialize variables to get body
  351. void* buffer = __alloca(hdl_hdr.data_size);
  352. size_t nfds = 0;
  353. for (int i = 0; i < MAX_FDS; i++)
  354. if (hdl_hdr.fds & (1U << i))
  355. nfds++;
  356. // receive PAL_HANDLE contents in the body
  357. char cbuf[sizeof(struct cmsghdr) + nfds * sizeof(int)];
  358. // initialize iovec[0] with struct PAL_HANDLE
  359. iov[0].iov_base = buffer;
  360. iov[0].iov_len = hdl_hdr.data_size;
  361. // set message header values
  362. hdr.msg_iov = iov;
  363. hdr.msg_iovlen = 1;
  364. hdr.msg_control = cbuf;
  365. hdr.msg_controllen = sizeof(struct cmsghdr) + sizeof(int) * nfds;
  366. ret = INLINE_SYSCALL(recvmsg, 3, ch, &hdr, 0);
  367. // if error was returned
  368. if (IS_ERR(ret) && ERRNO(ret) != EINTR && ERRNO(ret) != ERESTART)
  369. return -ERRNO(ret);
  370. struct cmsghdr* chdr = CMSG_FIRSTHDR(&hdr);
  371. if (!chdr || chdr->cmsg_type != SCM_RIGHTS)
  372. return -PAL_ERROR_INVAL;
  373. PAL_HANDLE handle = NULL;
  374. ret = handle_deserialize(&handle, buffer, hdl_hdr.data_size);
  375. if (ret < 0)
  376. return ret;
  377. int total_fds = (hdr.msg_controllen - sizeof(struct cmsghdr)) / sizeof(int);
  378. int n = 0;
  379. for (int i = 0; i < MAX_FDS; i++)
  380. if (hdl_hdr.fds & (1U << i)) {
  381. if (n < total_fds) {
  382. handle->generic.fds[i] = ((int*)CMSG_DATA(chdr))[n++];
  383. } else {
  384. HANDLE_HDR(handle)->flags &= ~(RFD(i) | WFD(i));
  385. }
  386. }
  387. *cargo = handle;
  388. return 0;
  389. }