Intercept.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifdef USE_GNU_SOURCE
  28. #define _GNU_SOURCE
  29. #endif
  30. /* Name used in err msgs */
  31. char *progname = "";
  32. #include <unistd.h>
  33. #include <stdint.h>
  34. #include <pthread.h>
  35. #include <stdio.h>
  36. #include <dlfcn.h>
  37. #include <strings.h>
  38. #include <netinet/in.h>
  39. #include <sys/time.h>
  40. #include <pwd.h>
  41. #include <errno.h>
  42. #include <linux/errno.h>
  43. #include <stdarg.h>
  44. #include <netdb.h>
  45. #include <string.h>
  46. #include <stdlib.h>
  47. #include <sys/syscall.h>
  48. #include <sys/types.h>
  49. #include <sys/socket.h>
  50. #include <sys/poll.h>
  51. #include <sys/un.h>
  52. #include <arpa/inet.h>
  53. #include "Intercept.h"
  54. #include "Common.h"
  55. #ifdef CHECKS
  56. #include <sys/resource.h>
  57. #include <linux/net.h> /* for NPROTO */
  58. #define SOCK_MAX (SOCK_PACKET + 1)
  59. #define SOCK_TYPE_MASK 0xf
  60. #endif
  61. /* Global Declarations */
  62. #ifdef USE_SOCKS_DNS
  63. static int (*realresinit)(void);
  64. #endif
  65. static int (*realconnect)(CONNECT_SIG);
  66. static int (*realselect)(SELECT_SIG);
  67. static int (*realbind)(BIND_SIG);
  68. static int (*realaccept)(ACCEPT_SIG);
  69. static int (*reallisten)(LISTEN_SIG);
  70. static int (*realsocket)(SOCKET_SIG);
  71. static int (*realsetsockopt)(SETSOCKOPT_SIG);
  72. static int (*realgetsockopt)(GETSOCKOPT_SIG);
  73. static int (*realaccept4)(ACCEPT4_SIG);
  74. static long (*realsyscall)(SYSCALL_SIG);
  75. //static int (*realclone)(CLONE_SIG);
  76. //static int (*realpoll)(POLL_SIG);
  77. /* Exported Function Prototypes */
  78. void my_init(void);
  79. int connect(CONNECT_SIG);
  80. int select(SELECT_SIG);
  81. int close(CLOSE_SIG);
  82. int bind(BIND_SIG);
  83. int accept(ACCEPT_SIG);
  84. int listen(LISTEN_SIG);
  85. int socket(SOCKET_SIG);
  86. int setsockopt(SETSOCKOPT_SIG);
  87. int getsockopt(GETSOCKOPT_SIG);
  88. int accept4(ACCEPT4_SIG);
  89. long syscall(SYSCALL_SIG);
  90. //int clone(CLONE_SIG);
  91. //int poll(POLL_SIG);
  92. #ifdef USE_SOCKS_DNS
  93. int res_init(void);
  94. #endif
  95. int connect_to_service(void);
  96. int init_service_connection();
  97. void dwr(const char *fmt, ...);
  98. void load_symbols(void);
  99. void set_up_intercept();
  100. int checkpid();
  101. #define SERVICE_CONNECT_ATTEMPTS 30
  102. ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd);
  103. /* threading */
  104. pthread_mutex_t lock;
  105. pthread_mutex_t loglock;
  106. void handle_error(char *name, char *info, int err)
  107. {
  108. #ifdef ERRORS_ARE_FATAL
  109. if(err < 0) {
  110. dwr("handle_error(%s)=%d: FATAL: %s\n", name, err, info);
  111. //exit(-1);
  112. }
  113. #endif
  114. #ifdef VERBOSE
  115. //dwr("%s()=%d\n", name, err);
  116. #endif
  117. }
  118. /*------------------------------------------------------------------------------
  119. ------------------- Intercept<--->Service Comm mechanisms-----------------------
  120. ------------------------------------------------------------------------------*/
  121. static int is_initialized = 0;
  122. static int fdret_sock; // used for fd-transfers
  123. static int newfd; // used for "this_end" socket
  124. static int thispid;
  125. static char* af_sock_name = "/tmp/.ztnc_e5cd7a9e1c5311ab";
  126. /*
  127. * Check for forking
  128. */
  129. int checkpid() {
  130. if(thispid != getpid()) {
  131. printf("clone/fork detected. re-initializing this instance.\n");
  132. set_up_intercept();
  133. fdret_sock = init_service_connection();
  134. thispid = getpid();
  135. }
  136. return 0;
  137. }
  138. /*
  139. * Sends an RPC command to the service
  140. */
  141. int send_command(int rpc_fd, char *cmd)
  142. {
  143. int n_write = write(rpc_fd, cmd, BUF_SZ);
  144. if(n_write < 0){
  145. dwr("Error writing command to service (CMD = %d)\n", cmd[0]);
  146. errno = 0;
  147. return -1;
  148. }
  149. return 0;
  150. }
  151. /*
  152. * Reads a return value from the service and sets errno (if applicable)
  153. */
  154. int get_retval()
  155. {
  156. dwr("get_retval()\n");
  157. if(fdret_sock >= 0) {
  158. int retval;
  159. int sz = sizeof(char) + sizeof(retval) + sizeof(errno);
  160. char retbuf[BUF_SZ];
  161. memset(&retbuf, '\0', sz);
  162. int n_read = read(fdret_sock, &retbuf, sz);
  163. if(n_read > 0) {
  164. memcpy(&retval, &retbuf[1], sizeof(retval));
  165. memcpy(&errno, &retbuf[1+sizeof(retval)], sizeof(errno));
  166. return retval;
  167. }
  168. }
  169. dwr("unable to read return value\n");
  170. return -1;
  171. }
  172. /* Check whether the socket is mapped to the service or not. We
  173. need to know if this is a regular AF_LOCAL socket or an end of a socketpair
  174. that the service uses. We don't want to keep state in the intercept, so
  175. we simply ask the service via an RPC */
  176. int is_mapped_to_service(int sockfd)
  177. {
  178. dwr("is_mapped_to_service()\n");
  179. char cmd[BUF_SZ];
  180. memset(cmd, '\0', BUF_SZ);
  181. cmd[0] = RPC_MAP_REQ;
  182. memcpy(&cmd[1], &sockfd, sizeof(sockfd));
  183. if(send_command(fdret_sock, cmd) < 0)
  184. return -1;
  185. return get_retval();
  186. }
  187. /*------------------------------------------------------------------------------
  188. ---------- Unix-domain socket lazy initializer (for fd-transfers)--------------
  189. ------------------------------------------------------------------------------*/
  190. /* Sets up the connection pipes and sockets to the service */
  191. int init_service_connection()
  192. {
  193. dwr("init_service_connection()\n");
  194. if(!is_initialized) {
  195. struct sockaddr_un addr;
  196. int tfd = -1, attempts = 0, conn_err = -1;
  197. memset(&addr, 0, sizeof(addr));
  198. addr.sun_family = AF_UNIX;
  199. strncpy(addr.sun_path, af_sock_name, sizeof(addr.sun_path)-1);
  200. if ( (tfd = realsocket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  201. perror("socket error");
  202. exit(-1);
  203. }
  204. while(conn_err < 0 && attempts < SERVICE_CONNECT_ATTEMPTS) {
  205. conn_err = realconnect(tfd, (struct sockaddr*)&addr, sizeof(addr));
  206. if(conn_err < 0) {
  207. dwr("re-attempting connection in %ds\n", 1+attempts);
  208. sleep(1);
  209. }
  210. else {
  211. dwr("AF_UNIX connection established: %d\n", tfd);
  212. is_initialized = 1;
  213. return tfd;
  214. }
  215. attempts++;
  216. }
  217. }
  218. return -1;
  219. }
  220. /*------------------------------------------------------------------------------
  221. ------------------------ ctors and dtors (and friends)-------------------------
  222. ------------------------------------------------------------------------------*/
  223. void my_dest(void) __attribute__ ((destructor));
  224. void my_dest(void) {
  225. //dwr("closing connections to service...\n");
  226. close(fdret_sock);
  227. pthread_mutex_destroy(&lock);
  228. }
  229. void load_symbols(void)
  230. {
  231. #ifdef USE_OLD_DLSYM
  232. void *lib;
  233. #endif
  234. /* possibly add check to beginning of each method to avoid needing to cll the constructor */
  235. if(thispid == getpid()) {
  236. dwr("detected duplicate call to global ctor (pid=%d).\n", thispid);
  237. }
  238. //dwr(" -- pid = %d\n", getpid());
  239. //dwr(" -- uid = %d\n", getuid());
  240. thispid = getpid();
  241. #ifndef USE_OLD_DLSYM
  242. realconnect = dlsym(RTLD_NEXT, "connect");
  243. realbind = dlsym(RTLD_NEXT, "bind");
  244. realaccept = dlsym(RTLD_NEXT, "accept");
  245. reallisten = dlsym(RTLD_NEXT, "listen");
  246. realsocket = dlsym(RTLD_NEXT, "socket");
  247. realbind = dlsym(RTLD_NEXT, "bind");
  248. realselect = dlsym(RTLD_NEXT, "select");
  249. realsetsockopt = dlsym(RTLD_NEXT, "setsockopt");
  250. realgetsockopt = dlsym(RTLD_NEXT, "getsockopt");
  251. realaccept4 = dlsym(RTLD_NEXT, "accept4");
  252. //realclone = dlsym(RTLD_NEXT, "clone");
  253. realsyscall = dlsym(RTLD_NEXT, "syscall");
  254. //realsyscall = dlsym(RTLD_NEXT, "poll");
  255. #ifdef USE_SOCKS_DNS
  256. realresinit = dlsym(RTLD_NEXT, "res_init");
  257. #endif
  258. #else
  259. lib = dlopen(LIBCONNECT, RTLD_LAZY);
  260. realconnect = dlsym(lib, "connect");
  261. realbind = dlsym(lib, "bind");
  262. realaccept = dlsym(lib, "accept");
  263. reallisten = dlsym(lib, "listen");
  264. realsocket = dlsym(lib, "socket");
  265. realselect = dlsym(lib, "select");
  266. realsetsockopt = dlsym(lib, "setsockopt");
  267. realgetsockopt = dlsym(lib, "getsockopt");
  268. realaccept4 = dlsym(lib), "accept4");
  269. //realclone = dlsym(lib, "clone");
  270. realsyscall = dlsym(lib, "syscall");
  271. //realsyscall = dlsym(lib, "poll");
  272. #ifdef USE_SOCKS_DNS
  273. realresinit = dlsym(lib, "res_init");
  274. #endif
  275. dlclose(lib);
  276. lib = dlopen(LIBC, RTLD_LAZY);
  277. dlclose(lib);
  278. #endif
  279. }
  280. /* Private Function Prototypes */
  281. void _init(void) __attribute__ ((constructor));
  282. void _init(void) {
  283. set_up_intercept();
  284. }
  285. /* get symbols and initialize mutexes */
  286. void set_up_intercept()
  287. {
  288. load_symbols();
  289. if(pthread_mutex_init(&lock, NULL) != 0) {
  290. printf("error while initializing service call mutex\n");
  291. }
  292. if(pthread_mutex_init(&loglock, NULL) != 0) {
  293. printf("error while initializing log mutex mutex\n");
  294. }
  295. }
  296. /*------------------------------------------------------------------------------
  297. --------------------------------- setsockopt() ---------------------------------
  298. ------------------------------------------------------------------------------*/
  299. /* int socket, int level, int option_name, const void *option_value, socklen_t option_len */
  300. int setsockopt(SETSOCKOPT_SIG)
  301. {
  302. dwr("setsockopt(%d)\n", socket);
  303. /*
  304. if(is_mapped_to_service(socket) < 0) { // First, check if the service manages this
  305. return realsetsockopt(socket, level, option_name, option_value, option_len);
  306. }
  307. */
  308. //return(realsetsockopt(socket, level, option_name, option_value, option_len));
  309. if(level == SOL_IPV6 && option_name == IPV6_V6ONLY)
  310. return 0;
  311. if(level == SOL_IP && option_name == IP_TTL)
  312. return 0;
  313. if(level == IPPROTO_TCP || (level == SOL_SOCKET && option_name == SO_KEEPALIVE))
  314. return 0;
  315. /* make sure we don't touch any standard outputs */
  316. if(socket == STDIN_FILENO || socket == STDOUT_FILENO || socket == STDERR_FILENO)
  317. return(realsetsockopt(socket, level, option_name, option_value, option_len));
  318. int err = realsetsockopt(socket, level, option_name, option_value, option_len);
  319. if(err < 0){
  320. //perror("setsockopt():\n");
  321. }
  322. return 0;
  323. }
  324. /*------------------------------------------------------------------------------
  325. --------------------------------- getsockopt() ---------------------------------
  326. ------------------------------------------------------------------------------*/
  327. /* int sockfd, int level, int optname, void *optval, socklen_t *optlen */
  328. int getsockopt(GETSOCKOPT_SIG)
  329. {
  330. dwr("getsockopt(%d)\n", sockfd);
  331. /*
  332. if(is_mapped_to_service(sockfd) < 0) { // First, check if the service manages this
  333. return realgetsockopt(sockfd, level, optname, optval, optlen);
  334. }
  335. */
  336. int err = realgetsockopt(sockfd, level, optname, optval, optlen);
  337. // FIXME: this condition will need a little more intelligence later on
  338. // -- we will need to know if this fd is a local we are spoofing, or a true local
  339. if(optname == SO_TYPE)
  340. {
  341. int* val = (int*)optval;
  342. *val = 2;
  343. optval = (void*)val;
  344. }
  345. if(err < 0){
  346. //perror("setsockopt():\n");
  347. }
  348. return 0;
  349. }
  350. /*------------------------------------------------------------------------------
  351. ----------------------------------- socket() -----------------------------------
  352. ------------------------------------------------------------------------------*/
  353. /* int socket_family, int socket_type, int protocol
  354. socket() intercept function */
  355. int socket(SOCKET_SIG)
  356. {
  357. dwr("socket()*:\n");
  358. int err;
  359. #ifdef CHECKS
  360. /* Check that type makes sense */
  361. int flags = socket_type & ~SOCK_TYPE_MASK;
  362. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) {
  363. errno = EINVAL;
  364. handle_error("socket1", "", -1);
  365. return -1;
  366. }
  367. socket_type &= SOCK_TYPE_MASK;
  368. /* Check protocol is in range */
  369. if (socket_family < 0 || socket_family >= NPROTO){
  370. errno = EAFNOSUPPORT;
  371. handle_error("socket2", "", -1);
  372. return -1;
  373. }
  374. if (socket_type < 0 || socket_type >= SOCK_MAX) {
  375. errno = EINVAL;
  376. handle_error("socket3", "", -1);
  377. return -1;
  378. }
  379. /* Check that we haven't hit the soft-limit file descriptors allowed */
  380. /* FIXME: Find number of open fds
  381. struct rlimit rl;
  382. getrlimit(RLIMIT_NOFILE, &rl);
  383. if(sockfd >= rl.rlim_cur){
  384. errno = EMFILE;
  385. return -1;
  386. }
  387. */
  388. /* FIXME: detect ENFILE condition */
  389. #endif
  390. char cmd[BUF_SZ];
  391. fdret_sock = !is_initialized ? init_service_connection() : fdret_sock;
  392. if(fdret_sock < 0) {
  393. dwr("BAD service connection. exiting.\n");
  394. handle_error("socket4", "", -1);
  395. exit(-1);
  396. }
  397. if(socket_family == AF_LOCAL
  398. || socket_family == AF_NETLINK
  399. || socket_family == AF_UNIX) {
  400. int err = realsocket(socket_family, socket_type, protocol);
  401. dwr("realsocket, err = %d\n", err);
  402. handle_error("socket5", "", err);
  403. return err;
  404. }
  405. /* Assemble and send RPC */
  406. struct socket_st rpc_st;
  407. rpc_st.socket_family = socket_family;
  408. rpc_st.socket_type = socket_type;
  409. rpc_st.protocol = protocol;
  410. rpc_st.__tid = syscall(SYS_gettid);
  411. memset(cmd, '\0', BUF_SZ);
  412. cmd[0] = RPC_SOCKET;
  413. dwr("pid = %d\n", thispid);
  414. memcpy(&cmd[1], &thispid, sizeof(pid_t));
  415. memcpy(&cmd[1]+sizeof(pid_t), &rpc_st, sizeof(struct socket_st));
  416. pthread_mutex_lock(&lock);
  417. send_command(fdret_sock, cmd);
  418. /* get new fd */
  419. char rbuf[16];
  420. ssize_t sz = sock_fd_read(fdret_sock, rbuf, sizeof(rbuf), &newfd);
  421. dwr("read %d bytes (%s)\n", sz, &rbuf);
  422. if(sz > 0)
  423. {
  424. /* send our local-fd number back to service so
  425. it can complete its mapping table entry */
  426. memset(cmd, '\0', BUF_SZ);
  427. cmd[0] = RPC_MAP;
  428. memcpy(&cmd[1], &newfd, sizeof(newfd));
  429. if(newfd > -1) { // FIXME: check logic
  430. send_command(fdret_sock, cmd);
  431. pthread_mutex_unlock(&lock);
  432. errno = ERR_OK; // OK
  433. handle_error("socket6", "", newfd);
  434. return newfd;
  435. }
  436. else { // Try to read retval+errno since we RXed a bad fd
  437. dwr("Error, service sent bad fd.\n");
  438. err = get_retval();
  439. pthread_mutex_unlock(&lock);
  440. handle_error("socket7", "", -1);
  441. return err;
  442. }
  443. }
  444. else {
  445. dwr("Error while receiving new FD.\n");
  446. err = get_retval();
  447. pthread_mutex_unlock(&lock);
  448. handle_error("socket8", "", -1);
  449. return err;
  450. }
  451. }
  452. /*------------------------------------------------------------------------------
  453. ---------------------------------- connect() -----------------------------------
  454. ------------------------------------------------------------------------------*/
  455. /* int __fd, const struct sockaddr * __addr, socklen_t __len
  456. connect() intercept function */
  457. int connect(CONNECT_SIG)
  458. {
  459. dwr("connect(%d):\n", __fd);
  460. print_addr(__addr);
  461. struct sockaddr_in *connaddr;
  462. connaddr = (struct sockaddr_in *) __addr;
  463. #ifdef CHECKS
  464. /* Check that this is a valid fd */
  465. if(fcntl(__fd, F_GETFD) < 0) {
  466. errno = EBADF;
  467. handle_error("connect", "EBADF", -1);
  468. return -1;
  469. }
  470. /* Check that it is a socket */
  471. int sock_type;
  472. socklen_t sock_type_len = sizeof(sock_type);
  473. if(getsockopt(__fd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
  474. errno = ENOTSOCK;
  475. handle_error("connect", "ENOTSOCK", -1);
  476. return -1;
  477. }
  478. /* Check family */
  479. if (connaddr->sin_family < 0 || connaddr->sin_family >= NPROTO){
  480. errno = EAFNOSUPPORT;
  481. handle_error("connect", "EAFNOSUPPORT", -1);
  482. return -1;
  483. }
  484. /* FIXME: Check that address is in user space, return EFAULT ? */
  485. #endif
  486. /* make sure we don't touch any standard outputs */
  487. if(__fd == STDIN_FILENO || __fd == STDOUT_FILENO || __fd == STDERR_FILENO){
  488. if (realconnect == NULL) {
  489. handle_error("connect", "Unresolved symbol [connect]", -1);
  490. exit(-1);
  491. }
  492. return(realconnect(__fd, __addr, __len));
  493. }
  494. if(__addr != NULL && (connaddr->sin_family == AF_LOCAL
  495. || connaddr->sin_family == PF_NETLINK
  496. || connaddr->sin_family == AF_NETLINK
  497. || connaddr->sin_family == AF_UNIX)) {
  498. int err = realconnect(__fd, __addr, __len);
  499. perror("connect():");
  500. //handle_error("connect", "Cannot connect to local socket", err);
  501. return err;
  502. }
  503. /* Assemble and send RPC */
  504. int err;
  505. char cmd[BUF_SZ];
  506. memset(cmd, '\0', BUF_SZ);
  507. struct connect_st rpc_st;
  508. rpc_st.__tid = syscall(SYS_gettid);
  509. rpc_st.__fd = __fd;
  510. memcpy(&rpc_st.__addr, __addr, sizeof(struct sockaddr));
  511. memcpy(&rpc_st.__len, &__len, sizeof(socklen_t));
  512. cmd[0] = RPC_CONNECT;
  513. memcpy(&cmd[1], &rpc_st, sizeof(struct connect_st));
  514. pthread_mutex_lock(&lock);
  515. send_command(fdret_sock, cmd);
  516. /*
  517. if(sock_type && O_NONBLOCK) {
  518. //pthread_mutex_unlock(&lock);
  519. //return EINPROGRESS;
  520. }
  521. */
  522. err = get_retval();
  523. pthread_mutex_unlock(&lock);
  524. //handle_error("connect", "", err);
  525. return err;
  526. }
  527. /*------------------------------------------------------------------------------
  528. ---------------------------------- select() ------------------------------------
  529. ------------------------------------------------------------------------------*/
  530. /* int n, fd_set *readfds, fd_set *writefds,
  531. fd_set *exceptfds, struct timeval *timeout */
  532. int select(SELECT_SIG)
  533. {
  534. //dwr("select():\n");
  535. return realselect(n, readfds, writefds, exceptfds, timeout);
  536. }
  537. /*------------------------------------------------------------------------------
  538. ------------------------------------ bind() ------------------------------------
  539. ------------------------------------------------------------------------------*/
  540. /* int sockfd, const struct sockaddr *addr, socklen_t addrlen
  541. bind() intercept function */
  542. int bind(BIND_SIG)
  543. {
  544. dwr("bind(%d):\n", sockfd);
  545. print_addr(addr);
  546. #ifdef CHECKS
  547. /* Check that this is a valid fd */
  548. if(fcntl(sockfd, F_GETFD) < 0) {
  549. errno = EBADF;
  550. handle_error("bind", "EBADF", -1);
  551. return -1;
  552. }
  553. /* Check that it is a socket */
  554. int opt = -1;
  555. socklen_t opt_len;
  556. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
  557. errno = ENOTSOCK;
  558. handle_error("bind", "ENOTSOCK", -1);
  559. return -1;
  560. }
  561. #endif
  562. int err;
  563. /* make sure we don't touch any standard outputs */
  564. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
  565. return(realbind(sockfd, addr, addrlen));
  566. /* If local, just use normal syscall */
  567. struct sockaddr_in *connaddr;
  568. connaddr = (struct sockaddr_in *)addr;
  569. if(connaddr->sin_family == AF_LOCAL
  570. || connaddr->sin_family == AF_NETLINK
  571. || connaddr->sin_family == AF_UNIX) {
  572. int err = realbind(sockfd, addr, addrlen);
  573. dwr("realbind, err = %d\n", err);
  574. return err;
  575. }
  576. /* Assemble and send RPC */
  577. char cmd[BUF_SZ];
  578. struct bind_st rpc_st;
  579. rpc_st.sockfd = sockfd;
  580. rpc_st.__tid = syscall(SYS_gettid);
  581. memcpy(&rpc_st.addr, addr, sizeof(struct sockaddr));
  582. memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
  583. cmd[0]=RPC_BIND;
  584. memcpy(&cmd[1], &rpc_st, sizeof(struct bind_st));
  585. pthread_mutex_lock(&lock);
  586. send_command(fdret_sock, cmd);
  587. err = get_retval();
  588. pthread_mutex_unlock(&lock);
  589. errno = ERR_OK;
  590. handle_error("bind", "", err);
  591. return err;
  592. }
  593. /*------------------------------------------------------------------------------
  594. ----------------------------------- accept4() ----------------------------------
  595. ------------------------------------------------------------------------------*/
  596. /* int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags */
  597. int accept4(ACCEPT4_SIG)
  598. {
  599. dwr("accept4(%d):\n", sockfd);
  600. #ifdef CHECKS
  601. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) {
  602. errno = EINVAL;
  603. return -1;
  604. }
  605. #endif
  606. int newfd = accept(sockfd, addr, addrlen);
  607. if(newfd > 0) {
  608. if(flags & SOCK_CLOEXEC)
  609. fcntl(newfd, F_SETFL, FD_CLOEXEC);
  610. if(flags & SOCK_NONBLOCK)
  611. fcntl(newfd, F_SETFL, O_NONBLOCK);
  612. }
  613. handle_error("accept4", "", newfd);
  614. return newfd;
  615. }
  616. /*------------------------------------------------------------------------------
  617. ----------------------------------- accept() -----------------------------------
  618. ------------------------------------------------------------------------------*/
  619. /* int sockfd struct sockaddr *addr, socklen_t *addrlen
  620. accept() intercept function */
  621. int accept(ACCEPT_SIG)
  622. {
  623. dwr("accept(%d):\n", sockfd);
  624. #ifdef CHECKS
  625. /* Check that this is a valid fd */
  626. if(fcntl(sockfd, F_GETFD) < 0) {
  627. return -1;
  628. errno = EBADF;
  629. dwr("EBADF\n");
  630. handle_error("accept", "EBADF", -1);
  631. return -1;
  632. }
  633. /* Check that it is a socket */
  634. int opt;
  635. socklen_t opt_len;
  636. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
  637. errno = ENOTSOCK;
  638. dwr("ENOTSOCK\n");
  639. handle_error("accept", "ENOTSOCK", -1);
  640. return -1;
  641. }
  642. /* Check that this socket supports accept() */
  643. if(!(opt && (SOCK_STREAM | SOCK_SEQPACKET))) {
  644. errno = EOPNOTSUPP;
  645. dwr("EOPNOTSUPP\n");
  646. handle_error("accept", "EOPNOTSUPP", -1);
  647. return -1;
  648. }
  649. /* Check that we haven't hit the soft-limit file descriptors allowed */
  650. struct rlimit rl;
  651. getrlimit(RLIMIT_NOFILE, &rl);
  652. if(sockfd >= rl.rlim_cur){
  653. errno = EMFILE;
  654. dwr("EMFILE\n");
  655. handle_error("accept", "EMFILE", -1);
  656. return -1;
  657. }
  658. /* Check address length */
  659. if(addrlen < 0) {
  660. errno = EINVAL;
  661. dwr("EINVAL\n");
  662. handle_error("accept", "EINVAL", -1);
  663. return -1;
  664. }
  665. #endif
  666. /* redirect calls for standard I/O descriptors to kernel */
  667. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO){
  668. dwr("realaccept():\n");
  669. return(realaccept(sockfd, addr, addrlen));
  670. }
  671. if(addr)
  672. addr->sa_family = AF_INET;
  673. /* TODO: also get address info */
  674. char cmd[BUF_SZ];
  675. if(realaccept == NULL) {
  676. handle_error("accept", "Unresolved symbol [accept]", -1);
  677. return -1;
  678. }
  679. //if(opt & O_NONBLOCK)
  680. //fcntl(sockfd, F_SETFL, O_NONBLOCK);
  681. char rbuf[16], c[1];
  682. int new_conn_socket;
  683. int n = read(sockfd, c, sizeof(c)); // Read signal byte
  684. if(n > 0)
  685. {
  686. ssize_t size = sock_fd_read(fdret_sock, rbuf, sizeof(rbuf), &new_conn_socket);
  687. if(size > 0) {
  688. /* Send our local-fd number back to service so it can complete its mapping table */
  689. memset(cmd, '\0', BUF_SZ);
  690. cmd[0] = RPC_MAP;
  691. memcpy(&cmd[1], &new_conn_socket, sizeof(new_conn_socket));
  692. pthread_mutex_lock(&lock);
  693. int n_write = write(fdret_sock, cmd, BUF_SZ);
  694. if(n_write < 0) {
  695. errno = ECONNABORTED; // FIXME: Closest match, service unreachable
  696. handle_error("accept", "ECONNABORTED - Error sending perceived FD to service", -1);
  697. return -1;
  698. }
  699. pthread_mutex_unlock(&lock);
  700. errno = ERR_OK;
  701. dwr("*accept()=%d\n", new_conn_socket);
  702. handle_error("accept", "", new_conn_socket);
  703. return new_conn_socket; // OK
  704. }
  705. else {
  706. errno = ECONNABORTED; // FIXME: Closest match, service unreachable
  707. handle_error("accept", "ECONNABORTED - Error receiving new FD from service", -1);
  708. return -1;
  709. }
  710. }
  711. errno = EBADF;
  712. handle_error("accept", "EBADF - Error reading signal byte from service", -1);
  713. return -1;
  714. }
  715. /*------------------------------------------------------------------------------
  716. ------------------------------------- listen()----------------------------------
  717. ------------------------------------------------------------------------------*/
  718. /* int sockfd, int backlog
  719. listen() intercept function */
  720. int listen(LISTEN_SIG)
  721. {
  722. dwr("listen(%d):\n", sockfd);
  723. int sock_type;
  724. socklen_t sock_type_len = sizeof(sock_type);
  725. #ifdef CHECKS
  726. /* Check that this is a valid fd */
  727. if(fcntl(sockfd, F_GETFD) < 0) {
  728. errno = EBADF;
  729. handle_error("listen", "EBADF", -1);
  730. return -1;
  731. }
  732. /* Check that it is a socket */
  733. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
  734. errno = ENOTSOCK;
  735. handle_error("listen", "ENOTSOCK", -1);
  736. return -1;
  737. }
  738. /* Check that this socket supports accept() */
  739. if(!(sock_type && (SOCK_STREAM | SOCK_SEQPACKET))) {
  740. errno = EOPNOTSUPP;
  741. handle_error("listen", "EOPNOTSUPP", -1);
  742. return -1;
  743. }
  744. #endif
  745. /* make sure we don't touch any standard outputs */
  746. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
  747. return(reallisten(sockfd, backlog));
  748. if(is_mapped_to_service(sockfd) < 0) {
  749. // We now know this socket is not one of our socketpairs
  750. int err = reallisten(sockfd, backlog);
  751. dwr("reallisten()=%d\n", err);
  752. return err;
  753. }
  754. /* Assemble and send RPC */
  755. char cmd[BUF_SZ];
  756. memset(cmd, '\0', BUF_SZ);
  757. struct listen_st rpc_st;
  758. rpc_st.sockfd = sockfd;
  759. rpc_st.backlog = backlog;
  760. rpc_st.__tid = syscall(SYS_gettid);
  761. cmd[0] = RPC_LISTEN;
  762. memcpy(&cmd[1], &rpc_st, sizeof(struct listen_st));
  763. pthread_mutex_lock(&lock);
  764. send_command(fdret_sock, cmd);
  765. int err = get_retval();
  766. pthread_mutex_unlock(&lock);
  767. handle_error("listen", "", ERR_OK);
  768. return ERR_OK;
  769. }
  770. /*------------------------------------------------------------------------------
  771. -------------------------------------- clone()----------------------------------
  772. ------------------------------------------------------------------------------*/
  773. // int (*fn)(void *), void *child_stack, int flags, void *arg, ...
  774. /*
  775. int clone(CLONE_SIG)
  776. {
  777. dwr("clone()\n");
  778. return realclone(fn, child_stack, flags, arg);
  779. }
  780. */
  781. /*------------------------------------------------------------------------------
  782. -------------------------------------- poll()-----------------------------------
  783. ------------------------------------------------------------------------------*/
  784. // struct pollfd *fds, nfds_t nfds, int timeout
  785. /*
  786. int poll(POLL_SIG)
  787. {
  788. dwr("poll()\n");
  789. return realpoll(fds, nfds, timeout);
  790. //return ERESTART_RESTARTBLOCK;
  791. }
  792. */
  793. /*------------------------------------------------------------------------------
  794. ------------------------------------ syscall()----------------------------------
  795. ------------------------------------------------------------------------------*/
  796. long syscall(SYSCALL_SIG)
  797. {
  798. dwr("syscall(%u, ...):\n", number);
  799. va_list ap;
  800. uintptr_t a,b,c,d,e,f;
  801. va_start(ap, number);
  802. a=va_arg(ap, uintptr_t);
  803. b=va_arg(ap, uintptr_t);
  804. c=va_arg(ap, uintptr_t);
  805. d=va_arg(ap, uintptr_t);
  806. e=va_arg(ap, uintptr_t);
  807. f=va_arg(ap, uintptr_t);
  808. va_end(ap);
  809. #if defined(__i386__)
  810. /* TODO: Implement for 32-bit systems: syscall(__NR_socketcall, 18, args);
  811. args[0] = (unsigned long) fd;
  812. args[1] = (unsigned long) addr;
  813. args[2] = (unsigned long) addrlen;
  814. args[3] = (unsigned long) flags;
  815. */
  816. #else
  817. if(number == __NR_accept4) {
  818. int sockfd = a;
  819. struct sockaddr * addr = (struct sockaddr*)b;
  820. socklen_t * addrlen = (socklen_t*)c;
  821. int flags = d;
  822. int old_errno = errno;
  823. int err = accept4(sockfd, addr, addrlen, flags);
  824. errno = old_errno;
  825. if(err == -EBADF) {
  826. //errno = EAGAIN;
  827. err = -EAGAIN;
  828. //exit(0);
  829. }
  830. return err;
  831. }
  832. #endif
  833. return realsyscall(number,a,b,c,d,e,f);
  834. }