db_streams.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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_debug.h"
  27. #include "pal_error.h"
  28. #include "api.h"
  29. #include <linux/types.h>
  30. typedef __kernel_pid_t pid_t;
  31. #include <linux/stat.h>
  32. #include <linux/msg.h>
  33. #include <linux/socket.h>
  34. #include <linux/wait.h>
  35. #include <linux/in.h>
  36. #include <linux/in6.h>
  37. #include <asm/fcntl.h>
  38. #include <asm/stat.h>
  39. #include <asm/socket.h>
  40. #include <asm/poll.h>
  41. #include "enclave_pages.h"
  42. void _DkPrintConsole (const void * buf, int size)
  43. {
  44. ocall_print_string(buf, size);
  45. }
  46. bool stataccess (struct stat * stat, int acc)
  47. {
  48. unsigned int mode = stat->st_mode;
  49. if (linux_state.uid && linux_state.uid == stat->st_uid) {
  50. mode >>= 6;
  51. goto out;
  52. }
  53. if (linux_state.gid && linux_state.gid == stat->st_gid) {
  54. mode >>= 3;
  55. goto out;
  56. }
  57. if (!linux_state.uid)
  58. mode >>= 6;
  59. out:
  60. return (mode & acc);
  61. }
  62. int handle_set_cloexec (PAL_HANDLE handle, bool enable)
  63. {
  64. return -PAL_ERROR_NOTIMPLEMENTED;
  65. }
  66. /* _DkStreamUnmap for internal use. Unmap stream at certain memory address.
  67. The memory is unmapped as a whole.*/
  68. int _DkStreamUnmap (void * addr, uint64_t size)
  69. {
  70. /* Just let the kernel tell us if the mapping isn't good. */
  71. free_pages(addr, size);
  72. return 0;
  73. }
  74. #define addr_size(addr) \
  75. ({ int _size = 0; \
  76. switch (((struct sockaddr *) addr)->sa_family) { \
  77. case AF_INET: \
  78. _size = sizeof(struct sockaddr_in); break; \
  79. case AF_INET6: \
  80. _size = sizeof(struct sockaddr_in6); break; \
  81. default: break; \
  82. } _size; \
  83. })
  84. int handle_serialize (PAL_HANDLE handle, void ** data)
  85. {
  86. int hdlsz = handle_size(handle);
  87. const void * d1, * d2;
  88. int dsz1 = 0, dsz2 = 0;
  89. // ~ Check cargo PAL_HANDLE - is allowed to be sent (White List checking
  90. // of cargo type)
  91. // ~ Also, Initialize common parameter formessage passing
  92. // Channel between parent and child
  93. switch(PAL_GET_TYPE(handle)) {
  94. case pal_type_file:
  95. d1 = handle->file.realpath;
  96. dsz1 = strlen(handle->file.realpath) + 1;
  97. break;
  98. case pal_type_pipe:
  99. case pal_type_pipesrv:
  100. case pal_type_pipecli:
  101. case pal_type_pipeprv:
  102. break;
  103. case pal_type_dev:
  104. if (handle->dev.realpath) {
  105. d1 = handle->dev.realpath;
  106. dsz1 = strlen(handle->dev.realpath) + 1;
  107. }
  108. break;
  109. case pal_type_dir:
  110. if (handle->dir.realpath) {
  111. d1 = handle->dir.realpath;
  112. dsz1 = strlen(handle->dir.realpath) + 1;
  113. }
  114. break;
  115. case pal_type_tcp:
  116. case pal_type_tcpsrv:
  117. case pal_type_udp:
  118. case pal_type_udpsrv:
  119. if (handle->sock.bind) {
  120. d1 = (const void *) handle->sock.bind;
  121. dsz1 = addr_size(handle->sock.bind);
  122. }
  123. if (handle->sock.conn) {
  124. d2 = (const void *) handle->sock.conn;
  125. dsz2 = addr_size(handle->sock.conn);
  126. }
  127. break;
  128. case pal_type_gipc:
  129. case pal_type_process:
  130. break;
  131. default:
  132. return -PAL_ERROR_INVAL;
  133. }
  134. void * buffer = malloc(hdlsz + dsz1 + dsz2);
  135. if (!buffer)
  136. return -PAL_ERROR_NOMEM;
  137. memcpy(buffer, handle, hdlsz);
  138. if (dsz1)
  139. memcpy(buffer + hdlsz, d1, dsz1);
  140. if (dsz2)
  141. memcpy(buffer + hdlsz + dsz1, d2, dsz2);
  142. *data = buffer;
  143. return hdlsz + dsz1 + dsz2;
  144. }
  145. #ifndef SEEK_SET
  146. # define SEEK_SET 0
  147. #endif
  148. int handle_deserialize (PAL_HANDLE * handle, const void * data, int size)
  149. {
  150. PAL_HANDLE hdl_data = (void *) data, hdl = NULL;
  151. int hdlsz = handle_size(hdl_data), ret = -PAL_ERROR_NOMEM;
  152. data += hdlsz;
  153. size -= hdlsz;
  154. // recreate PAL_HANDLE based on type
  155. switch(PAL_GET_TYPE(hdl_data)) {
  156. case pal_type_file: {
  157. int l = strlen((const char *) data) + 1;
  158. hdl = malloc(hdlsz + l);
  159. if (!hdl)
  160. break;
  161. memcpy(hdl, hdl_data, hdlsz);
  162. memcpy((void *) hdl + hdlsz, data, l);
  163. hdl->file.realpath = (PAL_STR) hdl + hdlsz;
  164. hdl->file.stubs = (PAL_PTR) NULL;
  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 = remalloc(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(data);
  204. if (hdl_data->sock.conn)
  205. s2 = addr_size(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_gipc:
  221. case pal_type_process:
  222. hdl = remalloc(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. {
  241. struct hdl_header hdl_hdr;
  242. void * hdl_data;
  243. int ret = handle_serialize(cargo, &hdl_data);
  244. if (ret < 0)
  245. return ret;
  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(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. ret = ocall_sock_send(ch, &hdl_hdr, sizeof(struct hdl_header), NULL, 0);
  258. // Unlock is error
  259. if (ret < 0) {
  260. free(hdl_data);
  261. return ret;
  262. }
  263. // Send message
  264. ret = ocall_sock_send_fd(ch, hdl_data, hdl_hdr.data_size,
  265. fds, nfds);
  266. free(hdl_data);
  267. return (ret < 0) ? -PAL_ERROR_DENIED : 0;
  268. }
  269. /* _DkRecvHandle for internal use. Receive and return a PAL_HANDLE over the
  270. given PAL_HANDLE else return negative value. */
  271. int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE * cargo)
  272. {
  273. struct hdl_header hdl_hdr;
  274. // ~ Check connection PAL_HANDLE - is of process type for sending handle
  275. // else fail
  276. if (!IS_HANDLE_TYPE(hdl, process))
  277. return -PAL_ERROR_BADHANDLE;
  278. // ~ Initialize common parameter for message passing
  279. // Channel between parent and child
  280. int ch = hdl->process.cargo;
  281. int ret = ocall_sock_recv(ch, &hdl_hdr, sizeof(struct hdl_header), NULL,
  282. NULL);
  283. if (ret < 0 || ret < sizeof(struct hdl_header)) {
  284. if (!ret)
  285. return -PAL_ERROR_TRYAGAIN;
  286. if (ret != -PAL_ERROR_INTERRUPTED)
  287. return ret;
  288. }
  289. // initialize variables to get body
  290. void * buffer = __alloca(hdl_hdr.data_size);
  291. unsigned int nfds = 0;
  292. for (int i = 0 ; i < MAX_FDS ; i++)
  293. if (hdl_hdr.fds & (1U << i))
  294. nfds++;
  295. unsigned int * fds = __alloca(sizeof(unsigned int) * nfds);
  296. ret = ocall_sock_recv_fd(ch, buffer, hdl_hdr.data_size,
  297. fds, &nfds);
  298. if (ret < 0)
  299. return ret;
  300. PAL_HANDLE handle = NULL;
  301. ret = handle_deserialize(&handle, buffer, hdl_hdr.data_size);
  302. if (ret < 0)
  303. return ret;
  304. int n = 0;
  305. for (int i = 0 ; i < MAX_FDS ; i++)
  306. if (hdl_hdr.fds & (1U << i)) {
  307. if (n < nfds) {
  308. handle->generic.fds[i] = fds[n++];
  309. } else {
  310. HANDLE_HDR(handle)->flags &= ~(RFD(i)|WFD(i));
  311. }
  312. }
  313. *cargo = handle;
  314. return 0;
  315. }