db_streams.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. #include "pal_linux_error.h"
  29. typedef __kernel_pid_t pid_t;
  30. #include <asm/fcntl.h>
  31. #include <asm/poll.h>
  32. #include <asm/socket.h>
  33. #include <asm/stat.h>
  34. #include <linux/in.h>
  35. #include <linux/in6.h>
  36. #include <linux/msg.h>
  37. #include <linux/socket.h>
  38. #include <linux/stat.h>
  39. #include <linux/wait.h>
  40. #include "enclave_pages.h"
  41. void _DkPrintConsole(const void* buf, int size) {
  42. ocall_write(2 /*stderr*/, buf, size);
  43. }
  44. bool stataccess(struct stat* stat, int acc) {
  45. unsigned int mode = stat->st_mode;
  46. if (linux_state.uid && linux_state.uid == stat->st_uid) {
  47. mode >>= 6;
  48. goto out;
  49. }
  50. if (linux_state.gid && linux_state.gid == stat->st_gid) {
  51. mode >>= 3;
  52. goto out;
  53. }
  54. if (!linux_state.uid)
  55. mode >>= 6;
  56. out:
  57. return (mode & acc);
  58. }
  59. /* _DkStreamUnmap for internal use. Unmap stream at certain memory address.
  60. The memory is unmapped as a whole.*/
  61. int _DkStreamUnmap(void* addr, uint64_t size) {
  62. /* Just let the kernel tell us if the mapping isn't good. */
  63. free_pages(addr, size);
  64. return 0;
  65. }
  66. static size_t addr_size(const struct sockaddr* addr) {
  67. switch (addr->sa_family) {
  68. case AF_INET:
  69. return sizeof(struct sockaddr_in);
  70. case AF_INET6:
  71. return sizeof(struct sockaddr_in6);
  72. default:
  73. return 0;
  74. }
  75. }
  76. int handle_serialize(PAL_HANDLE handle, void** data) {
  77. int hdlsz = handle_size(handle);
  78. const void* d1;
  79. const void* d2;
  80. int dsz1 = 0, dsz2 = 0;
  81. // ~ Check cargo PAL_HANDLE - is allowed to be sent (White List checking
  82. // of cargo type)
  83. // ~ Also, Initialize common parameter formessage passing
  84. // Channel between parent and child
  85. switch (PAL_GET_TYPE(handle)) {
  86. case pal_type_file:
  87. d1 = handle->file.realpath;
  88. dsz1 = strlen(handle->file.realpath) + 1;
  89. break;
  90. case pal_type_pipe:
  91. case pal_type_pipesrv:
  92. case pal_type_pipecli:
  93. case pal_type_pipeprv:
  94. break;
  95. case pal_type_dev:
  96. if (handle->dev.realpath) {
  97. d1 = handle->dev.realpath;
  98. dsz1 = strlen(handle->dev.realpath) + 1;
  99. }
  100. break;
  101. case pal_type_dir:
  102. if (handle->dir.realpath) {
  103. d1 = handle->dir.realpath;
  104. dsz1 = strlen(handle->dir.realpath) + 1;
  105. }
  106. break;
  107. case pal_type_tcp:
  108. case pal_type_tcpsrv:
  109. case pal_type_udp:
  110. case pal_type_udpsrv:
  111. if (handle->sock.bind) {
  112. d1 = (const void*)handle->sock.bind;
  113. dsz1 = addr_size(handle->sock.bind);
  114. }
  115. if (handle->sock.conn) {
  116. d2 = (const void*)handle->sock.conn;
  117. dsz2 = addr_size(handle->sock.conn);
  118. }
  119. break;
  120. case pal_type_process:
  121. case pal_type_eventfd:
  122. break;
  123. default:
  124. return -PAL_ERROR_INVAL;
  125. }
  126. void* buffer = malloc(hdlsz + dsz1 + dsz2);
  127. if (!buffer)
  128. return -PAL_ERROR_NOMEM;
  129. memcpy(buffer, handle, hdlsz);
  130. if (dsz1)
  131. memcpy(buffer + hdlsz, d1, dsz1);
  132. if (dsz2)
  133. memcpy(buffer + hdlsz + dsz1, d2, dsz2);
  134. if (PAL_GET_TYPE(handle) == pal_type_process) {
  135. /* must not leak session key and SSL context -> zero them */
  136. memset(buffer + offsetof(struct pal_handle, process.session_key), 0, sizeof(handle->process.session_key));
  137. memset(buffer + offsetof(struct pal_handle, process.ssl_ctx), 0, sizeof(handle->process.ssl_ctx));
  138. }
  139. *data = buffer;
  140. return hdlsz + dsz1 + dsz2;
  141. }
  142. int handle_deserialize(PAL_HANDLE* handle, const void* data, int size) {
  143. PAL_HANDLE hdl_data = (void*)data, hdl = NULL;
  144. int hdlsz = handle_size(hdl_data), ret = -PAL_ERROR_NOMEM;
  145. data += hdlsz;
  146. size -= hdlsz;
  147. // recreate PAL_HANDLE based on type
  148. switch (PAL_GET_TYPE(hdl_data)) {
  149. case pal_type_file: {
  150. int l = strlen((const char*)data) + 1;
  151. hdl = malloc(hdlsz + l);
  152. if (!hdl)
  153. break;
  154. memcpy(hdl, hdl_data, hdlsz);
  155. memcpy((void*)hdl + hdlsz, data, l);
  156. hdl->file.realpath = (PAL_STR)hdl + hdlsz;
  157. hdl->file.stubs = (PAL_PTR)NULL;
  158. break;
  159. }
  160. case pal_type_pipe:
  161. case pal_type_pipesrv:
  162. case pal_type_pipecli:
  163. case pal_type_pipeprv:
  164. hdl = malloc_copy(hdl_data, hdlsz);
  165. break;
  166. case pal_type_dev: {
  167. int l = hdl_data->dev.realpath ? strlen((const char*)data) + 1 : 0;
  168. hdl = malloc(hdlsz + l);
  169. if (!hdl)
  170. break;
  171. memcpy(hdl, hdl_data, hdlsz);
  172. if (l) {
  173. memcpy((void*)hdl + hdlsz, data, l);
  174. hdl->dev.realpath = (void*)hdl + hdlsz;
  175. }
  176. break;
  177. }
  178. case pal_type_dir: {
  179. int l = hdl_data->dir.realpath ? strlen((const char*)data) + 1 : 0;
  180. hdl = malloc(hdlsz + l);
  181. if (!hdl)
  182. break;
  183. memcpy(hdl, hdl_data, hdlsz);
  184. if (l) {
  185. memcpy((void*)hdl + hdlsz, data, l);
  186. hdl->dir.realpath = (void*)hdl + hdlsz;
  187. }
  188. break;
  189. }
  190. case pal_type_tcp:
  191. case pal_type_tcpsrv:
  192. case pal_type_udp:
  193. case pal_type_udpsrv: {
  194. int s1 = 0, s2 = 0;
  195. if (hdl_data->sock.bind)
  196. s1 = addr_size(data);
  197. if (hdl_data->sock.conn)
  198. s2 = addr_size(data + s1);
  199. hdl = malloc(hdlsz + s1 + s2);
  200. if (!hdl)
  201. break;
  202. memcpy(hdl, hdl_data, hdlsz);
  203. if (s1) {
  204. memcpy((void*)hdl + hdlsz, data, s1);
  205. hdl->sock.bind = (PAL_PTR)hdl + hdlsz;
  206. }
  207. if (s2) {
  208. memcpy((void*)hdl + hdlsz + s1, data + s1, s2);
  209. hdl->sock.conn = (PAL_PTR)hdl + hdlsz + s2;
  210. }
  211. break;
  212. }
  213. case pal_type_process:
  214. case pal_type_eventfd:
  215. hdl = malloc_copy(hdl_data, hdlsz);
  216. break;
  217. default:
  218. return -PAL_ERROR_BADHANDLE;
  219. }
  220. if (!hdl)
  221. return ret;
  222. if (PAL_GET_TYPE(hdl) == pal_type_process) {
  223. /* must not have leaked session key and SSL context, verify */
  224. static PAL_SESSION_KEY zero_session_key;
  225. __UNUSED(zero_session_key); /* otherwise GCC with Release build complains */
  226. assert(memcmp(hdl->process.session_key, zero_session_key, sizeof(zero_session_key)) == 0);
  227. assert(hdl->process.ssl_ctx == 0);
  228. }
  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. unsigned int fds[MAX_FDS];
  248. unsigned int nfds = 0;
  249. for (int i = 0; i < MAX_FDS; i++)
  250. if (HANDLE_HDR(cargo)->flags & (RFD(i) | WFD(i))) {
  251. hdl_hdr.fds |= 1U << i;
  252. fds[nfds++] = cargo->generic.fds[i];
  253. }
  254. int ch = hdl->process.cargo;
  255. ret = ocall_send(ch, &hdl_hdr, sizeof(struct hdl_header), NULL, 0, NULL, 0);
  256. if (IS_ERR(ret)) {
  257. free(hdl_data);
  258. return unix_to_pal_error(ERRNO(ret));
  259. }
  260. uint64_t fds_size = nfds * sizeof(unsigned int);
  261. char cbuf[sizeof(struct cmsghdr) + fds_size];
  262. struct cmsghdr* chdr = (struct cmsghdr*)cbuf;
  263. chdr->cmsg_level = SOL_SOCKET;
  264. chdr->cmsg_type = SCM_RIGHTS;
  265. chdr->cmsg_len = CMSG_LEN(fds_size);
  266. memcpy(CMSG_DATA(chdr), fds, fds_size);
  267. ret = ocall_send(ch, hdl_data, hdl_hdr.data_size, NULL, 0, chdr, chdr->cmsg_len);
  268. free(hdl_data);
  269. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
  270. }
  271. /* _DkRecvHandle for internal use. Receive and return a PAL_HANDLE over the
  272. given PAL_HANDLE else return negative value. */
  273. int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE* cargo) {
  274. struct hdl_header hdl_hdr;
  275. if (!IS_HANDLE_TYPE(hdl, process))
  276. return -PAL_ERROR_BADHANDLE;
  277. int ch = hdl->process.cargo;
  278. int ret = ocall_recv(ch, &hdl_hdr, sizeof(struct hdl_header), NULL, NULL, NULL, NULL);
  279. if (IS_ERR(ret))
  280. return unix_to_pal_error(ERRNO(ret));
  281. if ((size_t)ret < sizeof(struct hdl_header)) {
  282. /*
  283. * This code block is just in case to cover all the possibilities
  284. * to shield Iago attack.
  285. * We know that the file descriptor is an unix domain socket with
  286. * blocking mode and that the sender, _DkSendHandle() above, sends the
  287. * header with single sendmsg syscall by ocall_send() which
  288. * transfers a message atomically.
  289. *
  290. * read size == 0: return error for the caller to try again.
  291. * It should result in EINTR.
  292. *
  293. * read size > 0: return error for the caller to give up this file
  294. * descriptor.
  295. * If the header can't be send atomically for some
  296. * reason, the sender should get EMSGSIZE.
  297. */
  298. if (!ret)
  299. return -PAL_ERROR_TRYAGAIN;
  300. return -PAL_ERROR_DENIED;
  301. }
  302. uint32_t nfds = 0;
  303. for (int i = 0; i < MAX_FDS; i++)
  304. if (hdl_hdr.fds & (1U << i))
  305. nfds++;
  306. uint64_t fds_size = nfds * sizeof(unsigned int);
  307. uint64_t cbuf_size = sizeof(struct cmsghdr) + fds_size;
  308. char buffer[hdl_hdr.data_size];
  309. char cbuf[cbuf_size];
  310. ret = ocall_recv(ch, buffer, hdl_hdr.data_size, NULL, NULL, cbuf, &cbuf_size);
  311. if (IS_ERR(ret))
  312. return unix_to_pal_error(ERRNO(ret));
  313. nfds = 0;
  314. uint32_t fds[fds_size];
  315. struct cmsghdr* chdr = (struct cmsghdr*)cbuf;
  316. if (chdr->cmsg_type == SCM_RIGHTS) {
  317. nfds = (chdr->cmsg_len - sizeof(struct cmsghdr)) / sizeof(int);
  318. memcpy(fds, CMSG_DATA(chdr), nfds * sizeof(int));
  319. }
  320. PAL_HANDLE handle = NULL;
  321. ret = handle_deserialize(&handle, buffer, hdl_hdr.data_size);
  322. if (ret < 0)
  323. return ret;
  324. uint32_t n = 0;
  325. for (uint32_t i = 0; i < MAX_FDS; i++)
  326. if (hdl_hdr.fds & (1U << i)) {
  327. if (n < nfds) {
  328. handle->generic.fds[i] = fds[n++];
  329. } else {
  330. HANDLE_HDR(handle)->flags &= ~(RFD(i) | WFD(i));
  331. }
  332. }
  333. *cargo = handle;
  334. return 0;
  335. }