db_streams.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. #ifndef SEEK_SET
  148. #define SEEK_SET 0
  149. #endif
  150. int handle_deserialize(PAL_HANDLE* handle, const void* data, int size) {
  151. PAL_HANDLE hdl_data = (void*)data, hdl = NULL;
  152. int hdlsz = handle_size(hdl_data), ret = -PAL_ERROR_NOMEM;
  153. data += hdlsz;
  154. size -= hdlsz;
  155. // recreate PAL_HANDLE based on type
  156. switch (PAL_GET_TYPE(hdl_data)) {
  157. case pal_type_file: {
  158. int l = strlen((const char*)data) + 1;
  159. hdl = malloc(hdlsz + l);
  160. if (!hdl)
  161. break;
  162. memcpy(hdl, hdl_data, hdlsz);
  163. memcpy((void*)hdl + hdlsz, data, l);
  164. hdl->file.realpath = (void*)hdl + hdlsz;
  165. break;
  166. }
  167. case pal_type_pipe:
  168. case pal_type_pipesrv:
  169. case pal_type_pipecli:
  170. case pal_type_pipeprv:
  171. hdl = malloc_copy(hdl_data, hdlsz);
  172. break;
  173. case pal_type_dev: {
  174. int l = hdl_data->dev.realpath ? strlen((const char*)data) + 1 : 0;
  175. hdl = malloc(hdlsz + l);
  176. if (!hdl)
  177. break;
  178. memcpy(hdl, hdl_data, hdlsz);
  179. if (l) {
  180. memcpy((void*)hdl + hdlsz, data, l);
  181. hdl->dev.realpath = (void*)hdl + hdlsz;
  182. }
  183. break;
  184. }
  185. case pal_type_dir: {
  186. int l = hdl_data->dir.realpath ? strlen((const char*)data) + 1 : 0;
  187. hdl = malloc(hdlsz + l);
  188. if (!hdl)
  189. break;
  190. memcpy(hdl, hdl_data, hdlsz);
  191. if (l) {
  192. memcpy((void*)hdl + hdlsz, data, l);
  193. hdl->dir.realpath = (void*)hdl + hdlsz;
  194. }
  195. break;
  196. }
  197. case pal_type_tcp:
  198. case pal_type_tcpsrv:
  199. case pal_type_udp:
  200. case pal_type_udpsrv: {
  201. int s1 = 0, s2 = 0;
  202. if (hdl_data->sock.bind)
  203. s1 = addr_size((const struct sockaddr*)data);
  204. if (hdl_data->sock.conn)
  205. s2 = addr_size((const struct sockaddr*)(data + s1));
  206. hdl = malloc(hdlsz + s1 + s2);
  207. if (!hdl)
  208. break;
  209. memcpy(hdl, hdl_data, hdlsz);
  210. if (s1) {
  211. memcpy((void*)hdl + hdlsz, data, s1);
  212. hdl->sock.bind = (PAL_PTR)hdl + hdlsz;
  213. }
  214. if (s2) {
  215. memcpy((void*)hdl + hdlsz + s1, data + s1, s2);
  216. hdl->sock.conn = (PAL_PTR)hdl + hdlsz + s2;
  217. }
  218. break;
  219. }
  220. case pal_type_process:
  221. case pal_type_eventfd:
  222. hdl = malloc_copy(hdl_data, hdlsz);
  223. break;
  224. default:
  225. return -PAL_ERROR_BADHANDLE;
  226. }
  227. if (!hdl)
  228. return ret;
  229. *handle = hdl;
  230. return 0;
  231. }
  232. // Header for DkSendHandle and DkRecvHandle
  233. struct hdl_header {
  234. unsigned short fds : (MAX_FDS);
  235. unsigned short data_size : (16 - (MAX_FDS));
  236. };
  237. /* _DkSendHandle for internal use. Send a Pal Handle over the given
  238. process handle. Return 1 if success else return negative error code */
  239. int _DkSendHandle(PAL_HANDLE hdl, PAL_HANDLE cargo) {
  240. struct hdl_header hdl_hdr;
  241. void* hdl_data;
  242. int ret = handle_serialize(cargo, &hdl_data);
  243. if (ret < 0)
  244. return ret;
  245. hdl_hdr.fds = 0;
  246. hdl_hdr.data_size = ret;
  247. int fds[MAX_FDS];
  248. int nfds = 0;
  249. for (int i = 0; i < MAX_FDS; i++)
  250. if (HANDLE_HDR(cargo)->flags & (RFD(i) | WFD(1))) {
  251. hdl_hdr.fds |= 1U << i;
  252. fds[nfds++] = cargo->generic.fds[i];
  253. }
  254. // ~ Initialize common parameter formessage passing
  255. // Channel between parent and child
  256. int ch = hdl->process.cargo;
  257. // Declare variables required for sending the message
  258. struct msghdr hdr; // message header
  259. struct cmsghdr* chdr; // control message header
  260. struct iovec iov[1]; // IO Vector
  261. iov[0].iov_base = &hdl_hdr;
  262. iov[0].iov_len = sizeof(struct hdl_header);
  263. hdr.msg_name = NULL;
  264. hdr.msg_namelen = 0;
  265. hdr.msg_iov = iov;
  266. hdr.msg_iovlen = 1;
  267. hdr.msg_control = NULL;
  268. hdr.msg_controllen = 0;
  269. hdr.msg_flags = 0;
  270. ret = INLINE_SYSCALL(sendmsg, 3, ch, &hdr, MSG_NOSIGNAL);
  271. // Unlock is error
  272. if (IS_ERR(ret)) {
  273. free(hdl_data);
  274. return -PAL_ERROR_DENIED;
  275. }
  276. /* Message Body Composition:
  277. IOVEC[0]: PAL_HANDLE
  278. IOVEC[1..n]: Additional handle member follow
  279. Control Message: file descriptors */
  280. // Control message buffer with added space for 2 fds (ie. max size
  281. // that it will have)
  282. char cbuf[sizeof(struct cmsghdr) + MAX_FDS * sizeof(int)];
  283. // Initialize iovec[0] with struct PAL_HANDLE
  284. iov[0].iov_base = hdl_data;
  285. iov[0].iov_len = hdl_hdr.data_size;
  286. hdr.msg_iov = iov;
  287. hdr.msg_iovlen = 1;
  288. hdr.msg_control = cbuf; // Control Message Buffer
  289. hdr.msg_controllen = sizeof(cbuf);
  290. // Fill control message infomation for the file descriptors
  291. // Check hdr.msg_controllen >= sizeof(struct cmsghdr) to point to
  292. // cbuf, which is redundant based on the above code as we have
  293. // statically allocated memory.
  294. // or (struct cmsghdr*) cbuf
  295. chdr = CMSG_FIRSTHDR(&hdr); // Pointer to msg_control
  296. chdr->cmsg_level = SOL_SOCKET; // Originating Protocol
  297. chdr->cmsg_type = SCM_RIGHTS; // Protocol Specific Type
  298. // Length of control message = sizeof(struct cmsghdr) + nfds
  299. chdr->cmsg_len = CMSG_LEN(sizeof(int) * nfds);
  300. // Copy the fds below control header
  301. memcpy(CMSG_DATA(chdr), fds, sizeof(int) * nfds);
  302. // Also, Update main header with control message length (duplicate)
  303. hdr.msg_controllen = chdr->cmsg_len;
  304. // Send message
  305. ret = INLINE_SYSCALL(sendmsg, 3, ch, &hdr, 0);
  306. free(hdl_data);
  307. return IS_ERR(ret) ? -PAL_ERROR_DENIED : 0;
  308. }
  309. /* _DkRecvHandle for internal use. Receive and return a PAL_HANDLE over the
  310. given PAL_HANDLE else return negative value. */
  311. int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE* cargo) {
  312. struct hdl_header hdl_hdr;
  313. // ~ Check connection PAL_HANDLE - is of process type for sending handle
  314. // else fail
  315. if (!IS_HANDLE_TYPE(hdl, process))
  316. return -PAL_ERROR_BADHANDLE;
  317. // ~ Initialize common parameter for message passing
  318. // Channel between parent and child
  319. int ch = hdl->process.cargo;
  320. struct msghdr hdr;
  321. struct iovec iov[1];
  322. iov[0].iov_base = &hdl_hdr;
  323. iov[0].iov_len = sizeof(struct hdl_header);
  324. hdr.msg_name = NULL;
  325. hdr.msg_namelen = 0;
  326. hdr.msg_iov = iov;
  327. hdr.msg_iovlen = 1;
  328. hdr.msg_control = NULL;
  329. hdr.msg_controllen = 0;
  330. hdr.msg_flags = 0;
  331. int ret = INLINE_SYSCALL(recvmsg, 3, ch, &hdr, 0);
  332. if (IS_ERR(ret))
  333. return unix_to_pal_error(ERRNO(ret));
  334. if ((size_t)ret < sizeof(struct hdl_header)) {
  335. /*
  336. * This code block is just in case to cover all the possibilities.
  337. * We know that the file descriptor is an unix domain socket with
  338. * blocking mode and that the sender, _DkSendHandle() above, sends the
  339. * header with single sendmsg syscall which transfers message atomically.
  340. *
  341. * read size == 0: return error for the caller to try again.
  342. * It should result in EINTR.
  343. *
  344. * read size > 0: return error for the caller to give up this file
  345. * descriptor.
  346. * If the header can't be send atomically for some
  347. * reason, the sender should get EMSGSIZE.
  348. */
  349. if (!ret)
  350. return -PAL_ERROR_TRYAGAIN;
  351. return -PAL_ERROR_DENIED;
  352. }
  353. // initialize variables to get body
  354. void* buffer = __alloca(hdl_hdr.data_size);
  355. size_t nfds = 0;
  356. for (int i = 0; i < MAX_FDS; i++)
  357. if (hdl_hdr.fds & (1U << i))
  358. nfds++;
  359. // receive PAL_HANDLE contents in the body
  360. char cbuf[sizeof(struct cmsghdr) + nfds * sizeof(int)];
  361. // initialize iovec[0] with struct PAL_HANDLE
  362. iov[0].iov_base = buffer;
  363. iov[0].iov_len = hdl_hdr.data_size;
  364. // set message header values
  365. hdr.msg_iov = iov;
  366. hdr.msg_iovlen = 1;
  367. hdr.msg_control = cbuf;
  368. hdr.msg_controllen = sizeof(struct cmsghdr) + sizeof(int) * nfds;
  369. ret = INLINE_SYSCALL(recvmsg, 3, ch, &hdr, 0);
  370. // if error was returned
  371. if (IS_ERR(ret) && ERRNO(ret) != EINTR && ERRNO(ret) != ERESTART)
  372. return -ERRNO(ret);
  373. struct cmsghdr* chdr = CMSG_FIRSTHDR(&hdr);
  374. if (!chdr || chdr->cmsg_type != SCM_RIGHTS)
  375. return -PAL_ERROR_INVAL;
  376. PAL_HANDLE handle = NULL;
  377. ret = handle_deserialize(&handle, buffer, hdl_hdr.data_size);
  378. if (ret < 0)
  379. return ret;
  380. int total_fds = (hdr.msg_controllen - sizeof(struct cmsghdr)) / sizeof(int);
  381. int n = 0;
  382. for (int i = 0; i < MAX_FDS; i++)
  383. if (hdl_hdr.fds & (1U << i)) {
  384. if (n < total_fds) {
  385. handle->generic.fds[i] = ((int*)CMSG_DATA(chdr))[n++];
  386. } else {
  387. HANDLE_HDR(handle)->flags &= ~(RFD(i) | WFD(i));
  388. }
  389. }
  390. *cargo = handle;
  391. return 0;
  392. }