db_streams.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_stream.c
  17. *
  18. * This file contains APIs to open, read, write and get attribute of
  19. * streams.
  20. */
  21. #include "pal_defs.h"
  22. #include "pal_linux_defs.h"
  23. #include "pal.h"
  24. #include "pal_internal.h"
  25. #include "pal_linux.h"
  26. #include "pal_linux_error.h"
  27. #include "pal_debug.h"
  28. #include "pal_error.h"
  29. #include "api.h"
  30. #include <linux/types.h>
  31. typedef __kernel_pid_t pid_t;
  32. #include <linux/stat.h>
  33. #include <linux/msg.h>
  34. #include <linux/socket.h>
  35. #include <linux/wait.h>
  36. #include <linux/in.h>
  37. #include <linux/in6.h>
  38. #include <asm/fcntl.h>
  39. #include <asm/stat.h>
  40. #include <asm/socket.h>
  41. #include <asm/poll.h>
  42. #include "enclave_pages.h"
  43. void _DkPrintConsole (const void * buf, int size)
  44. {
  45. ocall_print_string(buf, size);
  46. }
  47. bool stataccess (struct stat * stat, int acc)
  48. {
  49. unsigned int mode = stat->st_mode;
  50. if (linux_state.uid && linux_state.uid == stat->st_uid) {
  51. mode >>= 6;
  52. goto out;
  53. }
  54. if (linux_state.gid && linux_state.gid == stat->st_gid) {
  55. mode >>= 3;
  56. goto out;
  57. }
  58. if (!linux_state.uid)
  59. mode >>= 6;
  60. out:
  61. return (mode & acc);
  62. }
  63. /* _DkStreamUnmap for internal use. Unmap stream at certain memory address.
  64. The memory is unmapped as a whole.*/
  65. int _DkStreamUnmap (void * addr, uint64_t size)
  66. {
  67. /* Just let the kernel tell us if the mapping isn't good. */
  68. free_pages(addr, size);
  69. return 0;
  70. }
  71. static size_t addr_size(const struct sockaddr* addr) {
  72. switch (addr->sa_family) {
  73. case AF_INET:
  74. return sizeof(struct sockaddr_in);
  75. case AF_INET6:
  76. return sizeof(struct sockaddr_in6);
  77. default:
  78. return 0;
  79. }
  80. }
  81. int handle_serialize (PAL_HANDLE handle, void ** data)
  82. {
  83. int hdlsz = handle_size(handle);
  84. const void * d1, * d2;
  85. int dsz1 = 0, dsz2 = 0;
  86. // ~ Check cargo PAL_HANDLE - is allowed to be sent (White List checking
  87. // of cargo type)
  88. // ~ Also, Initialize common parameter formessage passing
  89. // Channel between parent and child
  90. switch(PAL_GET_TYPE(handle)) {
  91. case pal_type_file:
  92. d1 = handle->file.realpath;
  93. dsz1 = strlen(handle->file.realpath) + 1;
  94. break;
  95. case pal_type_pipe:
  96. case pal_type_pipesrv:
  97. case pal_type_pipecli:
  98. case pal_type_pipeprv:
  99. break;
  100. case pal_type_dev:
  101. if (handle->dev.realpath) {
  102. d1 = handle->dev.realpath;
  103. dsz1 = strlen(handle->dev.realpath) + 1;
  104. }
  105. break;
  106. case pal_type_dir:
  107. if (handle->dir.realpath) {
  108. d1 = handle->dir.realpath;
  109. dsz1 = strlen(handle->dir.realpath) + 1;
  110. }
  111. break;
  112. case pal_type_tcp:
  113. case pal_type_tcpsrv:
  114. case pal_type_udp:
  115. case pal_type_udpsrv:
  116. if (handle->sock.bind) {
  117. d1 = (const void *) handle->sock.bind;
  118. dsz1 = addr_size(handle->sock.bind);
  119. }
  120. if (handle->sock.conn) {
  121. d2 = (const void *) handle->sock.conn;
  122. dsz2 = addr_size(handle->sock.conn);
  123. }
  124. break;
  125. case pal_type_gipc:
  126. case pal_type_process:
  127. break;
  128. default:
  129. return -PAL_ERROR_INVAL;
  130. }
  131. void * buffer = malloc(hdlsz + dsz1 + dsz2);
  132. if (!buffer)
  133. return -PAL_ERROR_NOMEM;
  134. memcpy(buffer, handle, hdlsz);
  135. if (dsz1)
  136. memcpy(buffer + hdlsz, d1, dsz1);
  137. if (dsz2)
  138. memcpy(buffer + hdlsz + dsz1, d2, dsz2);
  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. {
  147. PAL_HANDLE hdl_data = (void *) data, hdl = NULL;
  148. int hdlsz = handle_size(hdl_data), ret = -PAL_ERROR_NOMEM;
  149. data += hdlsz;
  150. size -= hdlsz;
  151. // recreate PAL_HANDLE based on type
  152. switch(PAL_GET_TYPE(hdl_data)) {
  153. case pal_type_file: {
  154. int l = strlen((const char *) data) + 1;
  155. hdl = malloc(hdlsz + l);
  156. if (!hdl)
  157. break;
  158. memcpy(hdl, hdl_data, hdlsz);
  159. memcpy((void *) hdl + hdlsz, data, l);
  160. hdl->file.realpath = (PAL_STR) hdl + hdlsz;
  161. hdl->file.stubs = (PAL_PTR) NULL;
  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(data);
  201. if (hdl_data->sock.conn)
  202. s2 = addr_size(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_gipc:
  218. case pal_type_process:
  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. {
  238. struct hdl_header hdl_hdr;
  239. void * hdl_data;
  240. int ret = handle_serialize(cargo, &hdl_data);
  241. if (ret < 0)
  242. return ret;
  243. hdl_hdr.fds = 0;
  244. hdl_hdr.data_size = ret;
  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(1))) {
  249. hdl_hdr.fds |= 1U << i;
  250. fds[nfds++] = cargo->generic.fds[i];
  251. }
  252. // ~ Initialize common parameter formessage passing
  253. // Channel between parent and child
  254. int ch = hdl->process.cargo;
  255. ret = ocall_sock_send(ch, &hdl_hdr, sizeof(struct hdl_header), NULL, 0);
  256. // Unlock is error
  257. if (IS_ERR(ret)) {
  258. free(hdl_data);
  259. return unix_to_pal_error(ERRNO(ret));
  260. }
  261. // Send message
  262. ret = ocall_sock_send_fd(ch, hdl_data, hdl_hdr.data_size,
  263. fds, nfds);
  264. free(hdl_data);
  265. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
  266. }
  267. /* _DkRecvHandle for internal use. Receive and return a PAL_HANDLE over the
  268. given PAL_HANDLE else return negative value. */
  269. int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE * cargo)
  270. {
  271. struct hdl_header hdl_hdr;
  272. // ~ Check connection PAL_HANDLE - is of process type for sending handle
  273. // else fail
  274. if (!IS_HANDLE_TYPE(hdl, process))
  275. return -PAL_ERROR_BADHANDLE;
  276. // ~ Initialize common parameter for message passing
  277. // Channel between parent and child
  278. int ch = hdl->process.cargo;
  279. int ret = ocall_sock_recv(ch, &hdl_hdr, sizeof(struct hdl_header), NULL,
  280. NULL);
  281. if (IS_ERR(ret))
  282. return unix_to_pal_error(ERRNO(ret));
  283. if (ret < sizeof(struct hdl_header)) {
  284. /*
  285. * This code block is just in case to cover all the possibilities
  286. * to shield Iago attack.
  287. * We know that the file descriptor is an unix domain socket with
  288. * blocking mode and that the sender, _DkSendHandle() above, sends the
  289. * header with single sendto syscall by ocall_sock_send() which
  290. * transfers a message atomically.
  291. *
  292. * read size == 0: return error for the caller to try again.
  293. * It should result in EINTR.
  294. *
  295. * read size > 0: return error for the caller to give up this file
  296. * descriptor.
  297. * If the header can't be send atomically for some
  298. * reason, the sender should get EMSGSIZE.
  299. */
  300. if (!ret)
  301. return -PAL_ERROR_TRYAGAIN;
  302. return -PAL_ERROR_DENIED;
  303. }
  304. // initialize variables to get body
  305. void * buffer = __alloca(hdl_hdr.data_size);
  306. unsigned int nfds = 0;
  307. for (int i = 0 ; i < MAX_FDS ; i++)
  308. if (hdl_hdr.fds & (1U << i))
  309. nfds++;
  310. unsigned int * fds = __alloca(sizeof(unsigned int) * nfds);
  311. ret = ocall_sock_recv_fd(ch, buffer, hdl_hdr.data_size,
  312. fds, &nfds);
  313. if (IS_ERR(ret))
  314. return unix_to_pal_error(ERRNO(ret));
  315. PAL_HANDLE handle = NULL;
  316. ret = handle_deserialize(&handle, buffer, hdl_hdr.data_size);
  317. if (ret < 0)
  318. return ret;
  319. int n = 0;
  320. for (int i = 0 ; i < MAX_FDS ; i++)
  321. if (hdl_hdr.fds & (1U << i)) {
  322. if (n < nfds) {
  323. handle->generic.fds[i] = fds[n++];
  324. } else {
  325. HANDLE_HDR(handle)->flags &= ~(RFD(i)|WFD(i));
  326. }
  327. }
  328. *cargo = handle;
  329. return 0;
  330. }