sgx_socket.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "platform_api_vmcore.h"
  6. #ifndef SGX_DISABLE_WASI
  7. #define TRACE_OCALL_FAIL() os_printf("ocall %s failed!\n", __FUNCTION__)
  8. /** OCALLs prototypes **/
  9. int
  10. ocall_accept(int *p_ret, int sockfd, void *addr, uint32_t *addrlen,
  11. uint32_t addr_size);
  12. int
  13. ocall_bind(int *p_ret, int sockfd, const void *addr, uint32_t addrlen);
  14. int
  15. ocall_close(int *p_ret, int fd);
  16. int
  17. ocall_connect(int *p_ret, int sockfd, void *addr, uint32_t addrlen);
  18. int
  19. ocall_fcntl_long(int *p_ret, int fd, int cmd, long arg);
  20. int
  21. ocall_getsockname(int *p_ret, int sockfd, void *addr, uint32_t *addrlen,
  22. uint32_t addr_size);
  23. int
  24. ocall_getsockopt(int *p_ret, int sockfd, int level, int optname, void *val_buf,
  25. unsigned int val_buf_size, void *len_buf);
  26. int
  27. ocall_listen(int *p_ret, int sockfd, int backlog);
  28. int
  29. ocall_recv(int *p_ret, int sockfd, void *buf, size_t len, int flags);
  30. int
  31. ocall_recvmsg(ssize_t *p_ret, int sockfd, void *msg_buf,
  32. unsigned int msg_buf_size, int flags);
  33. int
  34. ocall_send(int *p_ret, int sockfd, const void *buf, size_t len, int flags);
  35. int
  36. ocall_sendmsg(ssize_t *p_ret, int sockfd, void *msg_buf,
  37. unsigned int msg_buf_size, int flags);
  38. int
  39. ocall_setsockopt(int *p_ret, int sockfd, int level, int optname, void *optval,
  40. unsigned int optlen);
  41. int
  42. ocall_shutdown(int *p_ret, int sockfd, int how);
  43. int
  44. ocall_socket(int *p_ret, int domain, int type, int protocol);
  45. /** OCALLs prototypes end **/
  46. /** In-enclave implementation of POSIX functions **/
  47. static bool
  48. is_little_endian()
  49. {
  50. long i = 0x01020304;
  51. unsigned char *c = (unsigned char *)&i;
  52. return (*c == 0x04) ? true : false;
  53. }
  54. static void
  55. swap32(uint8 *pData)
  56. {
  57. uint8 value = *pData;
  58. *pData = *(pData + 3);
  59. *(pData + 3) = value;
  60. value = *(pData + 1);
  61. *(pData + 1) = *(pData + 2);
  62. *(pData + 2) = value;
  63. }
  64. static void
  65. swap16(uint8 *pData)
  66. {
  67. uint8 value = *pData;
  68. *(pData) = *(pData + 1);
  69. *(pData + 1) = value;
  70. }
  71. static uint32
  72. htonl(uint32 value)
  73. {
  74. uint32 ret;
  75. if (is_little_endian()) {
  76. ret = value;
  77. swap32((uint8 *)&ret);
  78. return ret;
  79. }
  80. return value;
  81. }
  82. static uint32
  83. ntohl(uint32 value)
  84. {
  85. return htonl(value);
  86. }
  87. static uint16
  88. htons(uint16 value)
  89. {
  90. uint16 ret;
  91. if (is_little_endian()) {
  92. ret = value;
  93. swap16((uint8 *)&ret);
  94. return ret;
  95. }
  96. return value;
  97. }
  98. static uint16
  99. ntohs(uint16 value)
  100. {
  101. return htons(value);
  102. }
  103. /* Coming from musl, under MIT license */
  104. static int
  105. __inet_aton(const char *s0, struct in_addr *dest)
  106. {
  107. const char *s = s0;
  108. unsigned char *d = (void *)dest;
  109. unsigned long a[4] = { 0 };
  110. char *z;
  111. int i;
  112. for (i = 0; i < 4; i++) {
  113. a[i] = strtoul(s, &z, 0);
  114. if (z == s || (*z && *z != '.') || !isdigit(*s))
  115. return 0;
  116. if (!*z)
  117. break;
  118. s = z + 1;
  119. }
  120. if (i == 4)
  121. return 0;
  122. switch (i) {
  123. case 0:
  124. a[1] = a[0] & 0xffffff;
  125. a[0] >>= 24;
  126. case 1:
  127. a[2] = a[1] & 0xffff;
  128. a[1] >>= 16;
  129. case 2:
  130. a[3] = a[2] & 0xff;
  131. a[2] >>= 8;
  132. }
  133. for (i = 0; i < 4; i++) {
  134. if (a[i] > 255)
  135. return 0;
  136. d[i] = a[i];
  137. }
  138. return 1;
  139. }
  140. /* Coming from musl, under MIT license */
  141. static int
  142. inet_addr(const char *p)
  143. {
  144. struct in_addr a;
  145. if (!__inet_aton(p, &a))
  146. return -1;
  147. return a.s_addr;
  148. }
  149. static int
  150. inet_network(const char *p)
  151. {
  152. return ntohl(inet_addr(p));
  153. }
  154. /** In-enclave implementation of POSIX functions end **/
  155. static int
  156. textual_addr_to_sockaddr(const char *textual, int port, struct sockaddr_in *out)
  157. {
  158. assert(textual);
  159. out->sin_family = AF_INET;
  160. out->sin_port = htons(port);
  161. out->sin_addr.s_addr = inet_addr(textual);
  162. return BHT_OK;
  163. }
  164. int
  165. socket(int domain, int type, int protocol)
  166. {
  167. int ret;
  168. if (ocall_socket(&ret, domain, type, protocol) != SGX_SUCCESS) {
  169. TRACE_OCALL_FAIL();
  170. return -1;
  171. }
  172. if (ret == -1)
  173. errno = get_errno();
  174. return ret;
  175. }
  176. int
  177. getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen)
  178. {
  179. int ret;
  180. unsigned int val_buf_size = *optlen;
  181. if (ocall_getsockopt(&ret, sockfd, level, optname, optval, val_buf_size,
  182. (void *)optlen)
  183. != SGX_SUCCESS) {
  184. TRACE_OCALL_FAIL();
  185. return -1;
  186. }
  187. if (ret == -1)
  188. errno = get_errno();
  189. return ret;
  190. }
  191. ssize_t
  192. sendmsg(int sockfd, const struct msghdr *msg, int flags)
  193. {
  194. ssize_t ret;
  195. int i;
  196. char *p;
  197. struct msghdr *msg1;
  198. uint64 total_size = sizeof(struct msghdr) + (uint64)msg->msg_namelen
  199. + (uint64)msg->msg_controllen;
  200. total_size += sizeof(struct iovec) * (msg->msg_iovlen);
  201. for (i = 0; i < msg->msg_iovlen; i++) {
  202. total_size += msg->msg_iov[i].iov_len;
  203. }
  204. if (total_size >= UINT32_MAX)
  205. return -1;
  206. msg1 = BH_MALLOC((uint32)total_size);
  207. if (msg1 == NULL)
  208. return -1;
  209. p = (char *)(uintptr_t)sizeof(struct msghdr);
  210. if (msg->msg_name != NULL) {
  211. msg1->msg_name = p;
  212. memcpy((uintptr_t)p + (char *)msg1, msg->msg_name,
  213. (size_t)msg->msg_namelen);
  214. p += msg->msg_namelen;
  215. }
  216. if (msg->msg_control != NULL) {
  217. msg1->msg_control = p;
  218. memcpy((uintptr_t)p + (char *)msg1, msg->msg_control,
  219. (size_t)msg->msg_control);
  220. p += msg->msg_controllen;
  221. }
  222. if (msg->msg_iov != NULL) {
  223. msg1->msg_iov = (struct iovec *)p;
  224. p += (uintptr_t)(sizeof(struct iovec) * (msg->msg_iovlen));
  225. for (i = 0; i < msg->msg_iovlen; i++) {
  226. msg1->msg_iov[i].iov_base = p;
  227. msg1->msg_iov[i].iov_len = msg->msg_iov[i].iov_len;
  228. memcpy((uintptr_t)p + (char *)msg1, msg->msg_iov[i].iov_base,
  229. (size_t)(msg->msg_iov[i].iov_len));
  230. p += msg->msg_iov[i].iov_len;
  231. }
  232. }
  233. if (ocall_sendmsg(&ret, sockfd, (void *)msg1, (uint32)total_size, flags)
  234. != SGX_SUCCESS) {
  235. TRACE_OCALL_FAIL();
  236. return -1;
  237. }
  238. if (ret == -1)
  239. errno = get_errno();
  240. return ret;
  241. }
  242. ssize_t
  243. recvmsg(int sockfd, struct msghdr *msg, int flags)
  244. {
  245. ssize_t ret;
  246. int i;
  247. char *p;
  248. struct msghdr *msg1;
  249. uint64 total_size = sizeof(struct msghdr) + (uint64)msg->msg_namelen
  250. + (uint64)msg->msg_controllen;
  251. total_size += sizeof(struct iovec) * (msg->msg_iovlen);
  252. for (i = 0; i < msg->msg_iovlen; i++) {
  253. total_size += msg->msg_iov[i].iov_len;
  254. }
  255. if (total_size >= UINT32_MAX)
  256. return -1;
  257. msg1 = BH_MALLOC((uint32)total_size);
  258. if (msg1 == NULL)
  259. return -1;
  260. memset(msg1, 0, total_size);
  261. p = (char *)(uintptr_t)sizeof(struct msghdr);
  262. if (msg->msg_name != NULL) {
  263. msg1->msg_name = p;
  264. p += msg->msg_namelen;
  265. }
  266. if (msg->msg_control != NULL) {
  267. msg1->msg_control = p;
  268. p += msg->msg_controllen;
  269. }
  270. if (msg->msg_iov != NULL) {
  271. msg1->msg_iov = (struct iovec *)p;
  272. p += (uintptr_t)(sizeof(struct iovec) * (msg->msg_iovlen));
  273. for (i = 0; i < msg->msg_iovlen; i++) {
  274. msg1->msg_iov[i].iov_base = p;
  275. msg1->msg_iov[i].iov_len = msg->msg_iov[i].iov_len;
  276. p += msg->msg_iov[i].iov_len;
  277. }
  278. }
  279. if (ocall_recvmsg(&ret, sockfd, (void *)msg1, (uint32)total_size, flags)
  280. != SGX_SUCCESS) {
  281. TRACE_OCALL_FAIL();
  282. return -1;
  283. }
  284. p = (char *)(uintptr_t)(sizeof(struct msghdr));
  285. if (msg1->msg_name != NULL) {
  286. memcpy(msg->msg_name, (uintptr_t)p + (char *)msg1,
  287. (size_t)msg1->msg_namelen);
  288. p += msg1->msg_namelen;
  289. }
  290. if (msg1->msg_control != NULL) {
  291. memcpy(msg->msg_control, (uintptr_t)p + (char *)msg1,
  292. (size_t)msg1->msg_control);
  293. p += msg->msg_controllen;
  294. }
  295. if (msg1->msg_iov != NULL) {
  296. p += (uintptr_t)(sizeof(struct iovec) * (msg1->msg_iovlen));
  297. for (i = 0; i < msg1->msg_iovlen; i++) {
  298. memcpy(msg->msg_iov[i].iov_base, (uintptr_t)p + (char *)msg1,
  299. (size_t)(msg1->msg_iov[i].iov_len));
  300. p += msg1->msg_iov[i].iov_len;
  301. }
  302. }
  303. if (ret == -1)
  304. errno = get_errno();
  305. return ret;
  306. }
  307. int
  308. shutdown(int sockfd, int how)
  309. {
  310. int ret;
  311. if (ocall_shutdown(&ret, sockfd, how) != SGX_SUCCESS) {
  312. TRACE_OCALL_FAIL();
  313. return -1;
  314. }
  315. if (ret == -1)
  316. errno = get_errno();
  317. return ret;
  318. }
  319. int
  320. os_socket_accept(bh_socket_t server_sock, bh_socket_t *sock, void *addr,
  321. unsigned int *addrlen)
  322. {
  323. struct sockaddr addr_tmp;
  324. unsigned int len = sizeof(struct sockaddr);
  325. if (ocall_accept(sock, server_sock, &addr_tmp, &len, len) != SGX_SUCCESS) {
  326. TRACE_OCALL_FAIL();
  327. return -1;
  328. }
  329. if (*sock < 0) {
  330. errno = get_errno();
  331. return BHT_ERROR;
  332. }
  333. return BHT_OK;
  334. }
  335. int
  336. os_socket_bind(bh_socket_t socket, const char *host, int *port)
  337. {
  338. struct sockaddr_in addr;
  339. struct linger ling;
  340. unsigned int socklen;
  341. int ret;
  342. assert(host);
  343. assert(port);
  344. ling.l_onoff = 1;
  345. ling.l_linger = 0;
  346. if (ocall_fcntl_long(&ret, socket, F_SETFD, FD_CLOEXEC) != SGX_SUCCESS) {
  347. TRACE_OCALL_FAIL();
  348. return -1;
  349. }
  350. if (ret < 0) {
  351. goto fail;
  352. }
  353. if (ocall_setsockopt(&ret, socket, SOL_SOCKET, SO_LINGER, &ling,
  354. sizeof(ling))
  355. != SGX_SUCCESS) {
  356. TRACE_OCALL_FAIL();
  357. return -1;
  358. }
  359. if (ret < 0) {
  360. goto fail;
  361. }
  362. addr.sin_addr.s_addr = inet_addr(host);
  363. addr.sin_port = htons(*port);
  364. addr.sin_family = AF_INET;
  365. if (ocall_bind(&ret, socket, &addr, sizeof(addr)) != SGX_SUCCESS) {
  366. TRACE_OCALL_FAIL();
  367. return -1;
  368. }
  369. if (ret < 0) {
  370. goto fail;
  371. }
  372. socklen = sizeof(addr);
  373. if (ocall_getsockname(&ret, socket, (void *)&addr, &socklen, socklen)
  374. != SGX_SUCCESS) {
  375. TRACE_OCALL_FAIL();
  376. return -1;
  377. }
  378. if (ret == -1) {
  379. goto fail;
  380. }
  381. *port = ntohs(addr.sin_port);
  382. return BHT_OK;
  383. fail:
  384. errno = get_errno();
  385. return BHT_ERROR;
  386. }
  387. int
  388. os_socket_close(bh_socket_t socket)
  389. {
  390. int ret;
  391. if (ocall_close(&ret, socket) != SGX_SUCCESS) {
  392. TRACE_OCALL_FAIL();
  393. return -1;
  394. }
  395. if (ret == -1)
  396. errno = get_errno();
  397. return ret;
  398. }
  399. int
  400. os_socket_connect(bh_socket_t socket, const char *addr, int port)
  401. {
  402. struct sockaddr_in addr_in = { 0 };
  403. socklen_t addr_len = sizeof(struct sockaddr_in);
  404. int ret = 0;
  405. if ((ret = textual_addr_to_sockaddr(addr, port, &addr_in)) < 0) {
  406. return ret;
  407. }
  408. if (ocall_connect(&ret, socket, &addr_in, addr_len) != SGX_SUCCESS) {
  409. TRACE_OCALL_FAIL();
  410. return -1;
  411. }
  412. if (ret == -1)
  413. errno = get_errno();
  414. return ret;
  415. }
  416. int
  417. os_socket_create(bh_socket_t *sock, int tcp_or_udp)
  418. {
  419. if (!sock) {
  420. return BHT_ERROR;
  421. }
  422. if (1 == tcp_or_udp) {
  423. if (ocall_socket(sock, AF_INET, SOCK_STREAM, IPPROTO_TCP)
  424. != SGX_SUCCESS) {
  425. TRACE_OCALL_FAIL();
  426. return -1;
  427. }
  428. }
  429. else if (0 == tcp_or_udp) {
  430. if (ocall_socket(sock, AF_INET, SOCK_DGRAM, 0) != SGX_SUCCESS) {
  431. TRACE_OCALL_FAIL();
  432. return -1;
  433. }
  434. }
  435. if (*sock == -1) {
  436. errno = get_errno();
  437. return BHT_ERROR;
  438. }
  439. return BHT_OK;
  440. }
  441. int
  442. os_socket_inet_network(const char *cp, uint32 *out)
  443. {
  444. if (!cp)
  445. return BHT_ERROR;
  446. *out = inet_network(cp);
  447. return BHT_OK;
  448. }
  449. int
  450. os_socket_listen(bh_socket_t socket, int max_client)
  451. {
  452. int ret;
  453. if (ocall_listen(&ret, socket, max_client) != SGX_SUCCESS) {
  454. TRACE_OCALL_FAIL();
  455. return -1;
  456. }
  457. if (ret == -1)
  458. errno = get_errno();
  459. return ret;
  460. }
  461. int
  462. os_socket_recv(bh_socket_t socket, void *buf, unsigned int len)
  463. {
  464. int ret;
  465. if (ocall_recv(&ret, socket, buf, len, 0) != SGX_SUCCESS) {
  466. errno = ENOSYS;
  467. return -1;
  468. }
  469. if (ret == -1)
  470. errno = get_errno();
  471. return ret;
  472. }
  473. int
  474. os_socket_send(bh_socket_t socket, const void *buf, unsigned int len)
  475. {
  476. int ret;
  477. if (ocall_send(&ret, socket, buf, len, 0) != SGX_SUCCESS) {
  478. errno = ENOSYS;
  479. return -1;
  480. }
  481. if (ret == -1)
  482. errno = get_errno();
  483. return ret;
  484. }
  485. int
  486. os_socket_shutdown(bh_socket_t socket)
  487. {
  488. return shutdown(socket, O_RDWR);
  489. }
  490. #endif