db_streams.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. #ifndef SEEK_SET
  143. #define SEEK_SET 0
  144. #endif
  145. int handle_deserialize(PAL_HANDLE* handle, const void* data, int size) {
  146. PAL_HANDLE hdl_data = (void*)data, hdl = NULL;
  147. int hdlsz = handle_size(hdl_data), ret = -PAL_ERROR_NOMEM;
  148. data += hdlsz;
  149. size -= hdlsz;
  150. // recreate PAL_HANDLE based on type
  151. switch (PAL_GET_TYPE(hdl_data)) {
  152. case pal_type_file: {
  153. int l = strlen((const char*)data) + 1;
  154. hdl = malloc(hdlsz + l);
  155. if (!hdl)
  156. break;
  157. memcpy(hdl, hdl_data, hdlsz);
  158. memcpy((void*)hdl + hdlsz, data, l);
  159. hdl->file.realpath = (PAL_STR)hdl + hdlsz;
  160. hdl->file.stubs = (PAL_PTR)NULL;
  161. break;
  162. }
  163. case pal_type_pipe:
  164. case pal_type_pipesrv:
  165. case pal_type_pipecli:
  166. case pal_type_pipeprv:
  167. hdl = malloc_copy(hdl_data, hdlsz);
  168. break;
  169. case pal_type_dev: {
  170. int l = hdl_data->dev.realpath ? strlen((const char*)data) + 1 : 0;
  171. hdl = malloc(hdlsz + l);
  172. if (!hdl)
  173. break;
  174. memcpy(hdl, hdl_data, hdlsz);
  175. if (l) {
  176. memcpy((void*)hdl + hdlsz, data, l);
  177. hdl->dev.realpath = (void*)hdl + hdlsz;
  178. }
  179. break;
  180. }
  181. case pal_type_dir: {
  182. int l = hdl_data->dir.realpath ? strlen((const char*)data) + 1 : 0;
  183. hdl = malloc(hdlsz + l);
  184. if (!hdl)
  185. break;
  186. memcpy(hdl, hdl_data, hdlsz);
  187. if (l) {
  188. memcpy((void*)hdl + hdlsz, data, l);
  189. hdl->dir.realpath = (void*)hdl + hdlsz;
  190. }
  191. break;
  192. }
  193. case pal_type_tcp:
  194. case pal_type_tcpsrv:
  195. case pal_type_udp:
  196. case pal_type_udpsrv: {
  197. int s1 = 0, s2 = 0;
  198. if (hdl_data->sock.bind)
  199. s1 = addr_size(data);
  200. if (hdl_data->sock.conn)
  201. s2 = addr_size(data + s1);
  202. hdl = malloc(hdlsz + s1 + s2);
  203. if (!hdl)
  204. break;
  205. memcpy(hdl, hdl_data, hdlsz);
  206. if (s1) {
  207. memcpy((void*)hdl + hdlsz, data, s1);
  208. hdl->sock.bind = (PAL_PTR)hdl + hdlsz;
  209. }
  210. if (s2) {
  211. memcpy((void*)hdl + hdlsz + s1, data + s1, s2);
  212. hdl->sock.conn = (PAL_PTR)hdl + hdlsz + s2;
  213. }
  214. break;
  215. }
  216. case pal_type_process:
  217. case pal_type_eventfd:
  218. hdl = malloc_copy(hdl_data, hdlsz);
  219. break;
  220. default:
  221. return -PAL_ERROR_BADHANDLE;
  222. }
  223. if (!hdl)
  224. return ret;
  225. if (PAL_GET_TYPE(hdl) == pal_type_process) {
  226. /* must not have leaked session key and SSL context, verify */
  227. static PAL_SESSION_KEY zero_session_key;
  228. __UNUSED(zero_session_key); /* otherwise GCC with Release build complains */
  229. assert(memcmp(hdl->process.session_key, zero_session_key, sizeof(zero_session_key)) == 0);
  230. assert(hdl->process.ssl_ctx == 0);
  231. }
  232. *handle = hdl;
  233. return 0;
  234. }
  235. // Header for DkSendHandle and DkRecvHandle
  236. struct hdl_header {
  237. unsigned short fds : (MAX_FDS);
  238. unsigned short data_size : (16 - (MAX_FDS));
  239. };
  240. /* _DkSendHandle for internal use. Send a Pal Handle over the given
  241. process handle. Return 1 if success else return negative error code */
  242. int _DkSendHandle(PAL_HANDLE hdl, PAL_HANDLE cargo) {
  243. struct hdl_header hdl_hdr;
  244. void* hdl_data;
  245. int ret = handle_serialize(cargo, &hdl_data);
  246. if (ret < 0)
  247. return ret;
  248. hdl_hdr.fds = 0;
  249. hdl_hdr.data_size = ret;
  250. unsigned int fds[MAX_FDS];
  251. unsigned int nfds = 0;
  252. for (int i = 0; i < MAX_FDS; i++)
  253. if (HANDLE_HDR(cargo)->flags & (RFD(i) | WFD(i))) {
  254. hdl_hdr.fds |= 1U << i;
  255. fds[nfds++] = cargo->generic.fds[i];
  256. }
  257. int ch = hdl->process.cargo;
  258. ret = ocall_send(ch, &hdl_hdr, sizeof(struct hdl_header), NULL, 0, NULL, 0);
  259. if (IS_ERR(ret)) {
  260. free(hdl_data);
  261. return unix_to_pal_error(ERRNO(ret));
  262. }
  263. uint64_t fds_size = nfds * sizeof(unsigned int);
  264. char cbuf[sizeof(struct cmsghdr) + fds_size];
  265. struct cmsghdr* chdr = (struct cmsghdr*)cbuf;
  266. chdr->cmsg_level = SOL_SOCKET;
  267. chdr->cmsg_type = SCM_RIGHTS;
  268. chdr->cmsg_len = CMSG_LEN(fds_size);
  269. memcpy(CMSG_DATA(chdr), fds, fds_size);
  270. ret = ocall_send(ch, hdl_data, hdl_hdr.data_size, NULL, 0, chdr, chdr->cmsg_len);
  271. free(hdl_data);
  272. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
  273. }
  274. /* _DkRecvHandle for internal use. Receive and return a PAL_HANDLE over the
  275. given PAL_HANDLE else return negative value. */
  276. int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE* cargo) {
  277. struct hdl_header hdl_hdr;
  278. if (!IS_HANDLE_TYPE(hdl, process))
  279. return -PAL_ERROR_BADHANDLE;
  280. int ch = hdl->process.cargo;
  281. int ret = ocall_recv(ch, &hdl_hdr, sizeof(struct hdl_header), NULL, NULL, NULL, NULL);
  282. if (IS_ERR(ret))
  283. return unix_to_pal_error(ERRNO(ret));
  284. if ((size_t)ret < sizeof(struct hdl_header)) {
  285. /*
  286. * This code block is just in case to cover all the possibilities
  287. * to shield Iago attack.
  288. * We know that the file descriptor is an unix domain socket with
  289. * blocking mode and that the sender, _DkSendHandle() above, sends the
  290. * header with single sendmsg syscall by ocall_send() which
  291. * transfers a message atomically.
  292. *
  293. * read size == 0: return error for the caller to try again.
  294. * It should result in EINTR.
  295. *
  296. * read size > 0: return error for the caller to give up this file
  297. * descriptor.
  298. * If the header can't be send atomically for some
  299. * reason, the sender should get EMSGSIZE.
  300. */
  301. if (!ret)
  302. return -PAL_ERROR_TRYAGAIN;
  303. return -PAL_ERROR_DENIED;
  304. }
  305. uint32_t nfds = 0;
  306. for (int i = 0; i < MAX_FDS; i++)
  307. if (hdl_hdr.fds & (1U << i))
  308. nfds++;
  309. uint64_t fds_size = nfds * sizeof(unsigned int);
  310. uint64_t cbuf_size = sizeof(struct cmsghdr) + fds_size;
  311. char buffer[hdl_hdr.data_size];
  312. char cbuf[cbuf_size];
  313. ret = ocall_recv(ch, buffer, hdl_hdr.data_size, NULL, NULL, cbuf, &cbuf_size);
  314. if (IS_ERR(ret))
  315. return unix_to_pal_error(ERRNO(ret));
  316. nfds = 0;
  317. uint32_t fds[fds_size];
  318. struct cmsghdr* chdr = (struct cmsghdr*)cbuf;
  319. if (chdr->cmsg_type == SCM_RIGHTS) {
  320. nfds = (chdr->cmsg_len - sizeof(struct cmsghdr)) / sizeof(int);
  321. memcpy(fds, CMSG_DATA(chdr), nfds * sizeof(int));
  322. }
  323. PAL_HANDLE handle = NULL;
  324. ret = handle_deserialize(&handle, buffer, hdl_hdr.data_size);
  325. if (ret < 0)
  326. return ret;
  327. uint32_t n = 0;
  328. for (uint32_t i = 0; i < MAX_FDS; i++)
  329. if (hdl_hdr.fds & (1U << i)) {
  330. if (n < nfds) {
  331. handle->generic.fds[i] = fds[n++];
  332. } else {
  333. HANDLE_HDR(handle)->flags &= ~(RFD(i) | WFD(i));
  334. }
  335. }
  336. *cargo = handle;
  337. return 0;
  338. }