db_streams.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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 _DkStreamFile (PAL_HANDLE hdl, PAL_HANDLE * file)
  63. {
  64. if (IS_HANDLE_TYPE(hdl, file)) {
  65. _DkObjectReference(hdl);
  66. *file = hdl;
  67. return 0;
  68. }
  69. /* for other types of handles, try to save it to a local file */
  70. return -PAL_ERROR_NOTIMPLEMENTED;
  71. }
  72. int handle_set_cloexec (PAL_HANDLE handle, bool enable)
  73. {
  74. return -PAL_ERROR_NOTIMPLEMENTED;
  75. }
  76. /* _DkStreamUnmap for internal use. Unmap stream at certain memory address.
  77. The memory is unmapped as a whole.*/
  78. int _DkStreamUnmap (void * addr, uint64_t size)
  79. {
  80. /* Just let the kernel tell us if the mapping isn't good. */
  81. free_pages(addr, size);
  82. return 0;
  83. }
  84. #define addr_size(addr) \
  85. ({ int _size = 0; \
  86. switch (((struct sockaddr *) addr)->sa_family) { \
  87. case AF_INET: \
  88. _size = sizeof(struct sockaddr_in); break; \
  89. case AF_INET6: \
  90. _size = sizeof(struct sockaddr_in6); break; \
  91. default: break; \
  92. } _size; \
  93. })
  94. int handle_serialize (PAL_HANDLE handle, void ** data)
  95. {
  96. int hdlsz = handle_size(handle);
  97. const void * d1, * d2;
  98. int dsz1 = 0, dsz2 = 0;
  99. // ~ Check cargo PAL_HANDLE - is allowed to be sent (White List checking
  100. // of cargo type)
  101. // ~ Also, Initialize common parameter formessage passing
  102. // Channel between parent and child
  103. switch(PAL_GET_TYPE(handle)) {
  104. case pal_type_file:
  105. d1 = handle->file.realpath;
  106. dsz1 = strlen(handle->file.realpath) + 1;
  107. break;
  108. case pal_type_pipe:
  109. case pal_type_pipesrv:
  110. case pal_type_pipecli:
  111. case pal_type_pipeprv:
  112. break;
  113. case pal_type_dev:
  114. if (handle->dev.realpath) {
  115. d1 = handle->dev.realpath;
  116. dsz1 = strlen(handle->dev.realpath) + 1;
  117. }
  118. break;
  119. case pal_type_dir:
  120. if (handle->dir.realpath) {
  121. d1 = handle->dir.realpath;
  122. dsz1 = strlen(handle->dir.realpath) + 1;
  123. }
  124. break;
  125. case pal_type_tcp:
  126. case pal_type_tcpsrv:
  127. case pal_type_udp:
  128. case pal_type_udpsrv:
  129. if (handle->sock.bind) {
  130. d1 = (const void *) handle->sock.bind;
  131. dsz1 = addr_size(handle->sock.bind);
  132. }
  133. if (handle->sock.conn) {
  134. d2 = (const void *) handle->sock.conn;
  135. dsz2 = addr_size(handle->sock.conn);
  136. }
  137. break;
  138. case pal_type_gipc:
  139. case pal_type_process:
  140. break;
  141. default:
  142. return -PAL_ERROR_INVAL;
  143. }
  144. void * buffer = malloc(hdlsz + dsz1 + dsz2);
  145. if (!buffer)
  146. return -PAL_ERROR_NOMEM;
  147. memcpy(buffer, handle, hdlsz);
  148. if (dsz1)
  149. memcpy(buffer + hdlsz, d1, dsz1);
  150. if (dsz2)
  151. memcpy(buffer + hdlsz + dsz1, d2, dsz2);
  152. *data = buffer;
  153. return hdlsz + dsz1 + dsz2;
  154. }
  155. #ifndef SEEK_SET
  156. # define SEEK_SET 0
  157. #endif
  158. int handle_deserialize (PAL_HANDLE * handle, const void * data, int size)
  159. {
  160. PAL_HANDLE hdl_data = (void *) data, hdl = NULL;
  161. int hdlsz = handle_size(hdl_data), ret = -PAL_ERROR_NOMEM;
  162. data += hdlsz;
  163. size -= hdlsz;
  164. // recreate PAL_HANDLE based on type
  165. switch(PAL_GET_TYPE(hdl_data)) {
  166. case pal_type_file: {
  167. int l = strlen((const char *) data) + 1;
  168. hdl = malloc(hdlsz + l);
  169. if (!hdl)
  170. break;
  171. memcpy(hdl, hdl_data, hdlsz);
  172. memcpy((void *) hdl + hdlsz, data, l);
  173. hdl->file.realpath = (PAL_STR) hdl + hdlsz;
  174. hdl->file.stubs = (PAL_PTR) NULL;
  175. break;
  176. }
  177. case pal_type_pipe:
  178. case pal_type_pipesrv:
  179. case pal_type_pipecli:
  180. case pal_type_pipeprv:
  181. hdl = remalloc(hdl_data, hdlsz);
  182. break;
  183. case pal_type_dev: {
  184. int l = hdl_data->dev.realpath ? strlen((const char *) data) + 1 : 0;
  185. hdl = malloc(hdlsz + l);
  186. if (!hdl)
  187. break;
  188. memcpy(hdl, hdl_data, hdlsz);
  189. if (l) {
  190. memcpy((void *) hdl + hdlsz, data, l);
  191. hdl->dev.realpath = (void *) hdl + hdlsz;
  192. }
  193. break;
  194. }
  195. case pal_type_dir: {
  196. int l = hdl_data->dir.realpath ? strlen((const char *) data) + 1 : 0;
  197. hdl = malloc(hdlsz + l);
  198. if (!hdl)
  199. break;
  200. memcpy(hdl, hdl_data, hdlsz);
  201. if (l) {
  202. memcpy((void *) hdl + hdlsz, data, l);
  203. hdl->dir.realpath = (void *) hdl + hdlsz;
  204. }
  205. break;
  206. }
  207. case pal_type_tcp:
  208. case pal_type_tcpsrv:
  209. case pal_type_udp:
  210. case pal_type_udpsrv: {
  211. int s1 = 0, s2 = 0;
  212. if (hdl_data->sock.bind)
  213. s1 = addr_size(data);
  214. if (hdl_data->sock.conn)
  215. s2 = addr_size(data + s1);
  216. hdl = malloc(hdlsz + s1 + s2);
  217. if (!hdl)
  218. break;
  219. memcpy(hdl, hdl_data, hdlsz);
  220. if (s1) {
  221. memcpy((void *) hdl + hdlsz, data, s1);
  222. hdl->sock.bind = (PAL_PTR) hdl + hdlsz;
  223. }
  224. if (s2) {
  225. memcpy((void *) hdl + hdlsz + s1, data + s1, s2);
  226. hdl->sock.conn = (PAL_PTR) hdl + hdlsz + s2;
  227. }
  228. break;
  229. }
  230. case pal_type_gipc:
  231. case pal_type_process:
  232. hdl = remalloc(hdl_data, hdlsz);
  233. break;
  234. default :
  235. return -PAL_ERROR_BADHANDLE;
  236. }
  237. if (!hdl)
  238. return ret;
  239. *handle = hdl;
  240. return 0;
  241. }
  242. // Header for DkSendHandle and DkRecvHandle
  243. struct hdl_header {
  244. unsigned short fds:(MAX_FDS);
  245. unsigned short data_size:(16-(MAX_FDS));
  246. };
  247. /* _DkSendHandle for internal use. Send a Pal Handle over the given
  248. process handle. Return 1 if success else return negative error code */
  249. int _DkSendHandle (PAL_HANDLE hdl, PAL_HANDLE cargo)
  250. {
  251. struct hdl_header hdl_hdr;
  252. void * hdl_data;
  253. int ret = handle_serialize(cargo, &hdl_data);
  254. if (ret < 0)
  255. return ret;
  256. hdl_hdr.data_size = ret;
  257. unsigned int fds[MAX_FDS];
  258. unsigned int nfds = 0;
  259. for (int i = 0 ; i < MAX_FDS ; i++)
  260. if (HANDLE_HDR(cargo)->flags & (RFD(i)|WFD(1))) {
  261. hdl_hdr.fds |= 1U << i;
  262. fds[nfds++] = HANDLE_HDR(cargo)->fds[i];
  263. }
  264. // ~ Initialize common parameter formessage passing
  265. // Channel between parent and child
  266. int ch = hdl->process.cargo;
  267. ret = ocall_sock_send(ch, &hdl_hdr, sizeof(struct hdl_header), NULL, 0);
  268. // Unlock is error
  269. if (ret < 0) {
  270. free(hdl_data);
  271. return ret;
  272. }
  273. // Send message
  274. ret = ocall_sock_send_fd(ch, hdl_data, hdl_hdr.data_size,
  275. fds, nfds);
  276. free(hdl_data);
  277. return (ret < 0) ? -PAL_ERROR_DENIED : 0;
  278. }
  279. /* _DkRecvHandle for internal use. Receive and return a PAL_HANDLE over the
  280. given PAL_HANDLE else return negative value. */
  281. int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE * cargo)
  282. {
  283. struct hdl_header hdl_hdr;
  284. // ~ Check connection PAL_HANDLE - is of process type for sending handle
  285. // else fail
  286. if (!IS_HANDLE_TYPE(hdl, process))
  287. return -PAL_ERROR_BADHANDLE;
  288. // ~ Initialize common parameter for message passing
  289. // Channel between parent and child
  290. int ch = hdl->process.cargo;
  291. int ret = ocall_sock_recv(ch, &hdl_hdr, sizeof(struct hdl_header), NULL,
  292. NULL);
  293. if (ret < 0 || ret < sizeof(struct hdl_header)) {
  294. if (!ret)
  295. return -PAL_ERROR_TRYAGAIN;
  296. if (ret != -PAL_ERROR_INTERRUPTED)
  297. return ret;
  298. }
  299. // initialize variables to get body
  300. void * buffer = __alloca(hdl_hdr.data_size);
  301. unsigned int nfds = 0;
  302. for (int i = 0 ; i < MAX_FDS ; i++)
  303. if (hdl_hdr.fds & (1U << i))
  304. nfds++;
  305. unsigned int * fds = __alloca(sizeof(unsigned int) * nfds);
  306. ret = ocall_sock_recv_fd(ch, buffer, hdl_hdr.data_size,
  307. fds, &nfds);
  308. if (ret < 0)
  309. return ret;
  310. PAL_HANDLE handle = NULL;
  311. ret = handle_deserialize(&handle, buffer, hdl_hdr.data_size);
  312. if (ret < 0)
  313. return ret;
  314. int n = 0;
  315. for (int i = 0 ; i < MAX_FDS ; i++)
  316. if (hdl_hdr.fds & (1U << i)) {
  317. if (n < nfds) {
  318. HANDLE_HDR(handle)->fds[i] = fds[n++];
  319. } else {
  320. HANDLE_HDR(handle)->flags &= ~(RFD(i)|WFD(i));
  321. }
  322. }
  323. *cargo = handle;
  324. return 0;
  325. }