db_streams.c 12 KB

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