sgx_socket.c 13 KB

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