b8d2c8
From 28bfe0dfd26dbc4e9917db9ad5457ab496769d24 Mon Sep 17 00:00:00 2001
b8d2c8
From: dmiller <dmiller@e0a8ed71-7df4-0310-8962-fdc924857419>
b8d2c8
Date: Thu, 7 Jan 2021 17:52:24 +0000
b8d2c8
Subject: [PATCH] Use checked versions of FD_* macros. Closes #2202
b8d2c8
b8d2c8
---
b8d2c8
 ncat/ncat_core.c   |  2 +-
b8d2c8
 ncat/ncat_listen.c | 78 +++++++++++++++++++++++-----------------------
b8d2c8
 ncat/ncat_posix.c  |  8 ++---
b8d2c8
 ncat/ncat_proxy.c  | 12 +++----
b8d2c8
 5 files changed, 53 insertions(+), 50 deletions(-)
b8d2c8
b8d2c8
diff --git a/ncat/ncat_core.c b/ncat/ncat_core.c
b8d2c8
index d1a88ac946..7c39e5d360 100644
b8d2c8
--- a/ncat/ncat_core.c
b8d2c8
+++ b/ncat/ncat_core.c
b8d2c8
@@ -431,7 +431,7 @@ int ncat_broadcast(fd_set *fds, const fd_list_t *fdlist, const char *msg, size_t
b8d2c8
 
b8d2c8
     ret = 0;
b8d2c8
     for (i = 0; i <= fdlist->fdmax; i++) {
b8d2c8
-        if (!FD_ISSET(i, fds))
b8d2c8
+        if (!checked_fd_isset(i, fds))
b8d2c8
             continue;
b8d2c8
 
b8d2c8
         fdn = get_fdinfo(fdlist, i);
b8d2c8
diff --git a/ncat/ncat_listen.c b/ncat/ncat_listen.c
b8d2c8
index 84ece94d08..e6dad13597 100644
b8d2c8
--- a/ncat/ncat_listen.c
b8d2c8
+++ b/ncat/ncat_listen.c
b8d2c8
@@ -244,10 +244,10 @@ static int ncat_listen_stream(int proto)
b8d2c8
         unblock_socket(listen_socket[num_sockets]);
b8d2c8
 
b8d2c8
         /* setup select sets and max fd */
b8d2c8
-        FD_SET(listen_socket[num_sockets], &master_readfds);
b8d2c8
+        checked_fd_set(listen_socket[num_sockets], &master_readfds);
b8d2c8
         add_fd(&client_fdlist, listen_socket[num_sockets]);
b8d2c8
 
b8d2c8
-        FD_SET(listen_socket[num_sockets], &listen_fds);
b8d2c8
+        checked_fd_set(listen_socket[num_sockets], &listen_fds);
b8d2c8
 
b8d2c8
         num_sockets++;
b8d2c8
     }
b8d2c8
@@ -296,7 +296,7 @@ static int ncat_listen_stream(int proto)
b8d2c8
             struct fdinfo *fdi = &client_fdlist.fds[i];
b8d2c8
             int cfd = fdi->fd;
b8d2c8
             /* Loop through descriptors until there's something to read */
b8d2c8
-            if (!FD_ISSET(cfd, &readfds) && !FD_ISSET(cfd, &writefds))
b8d2c8
+            if (!checked_fd_isset(cfd, &readfds) && !checked_fd_isset(cfd, &writefds))
b8d2c8
                 continue;
b8d2c8
 
b8d2c8
             if (o.debug > 1)
b8d2c8
@@ -304,27 +304,27 @@ static int ncat_listen_stream(int proto)
b8d2c8
 
b8d2c8
 #ifdef HAVE_OPENSSL
b8d2c8
             /* Is this an ssl socket pending a handshake? If so handle it. */
b8d2c8
-            if (o.ssl && FD_ISSET(cfd, &sslpending_fds)) {
b8d2c8
-                FD_CLR(cfd, &master_readfds);
b8d2c8
-                FD_CLR(cfd, &master_writefds);
b8d2c8
+            if (o.ssl && checked_fd_isset(cfd, &sslpending_fds)) {
b8d2c8
+                checked_fd_clr(cfd, &master_readfds);
b8d2c8
+                checked_fd_clr(cfd, &master_writefds);
b8d2c8
                 switch (ssl_handshake(fdi)) {
b8d2c8
                 case NCAT_SSL_HANDSHAKE_COMPLETED:
b8d2c8
                     /* Clear from sslpending_fds once ssl is established */
b8d2c8
-                    FD_CLR(cfd, &sslpending_fds);
b8d2c8
+                    checked_fd_clr(cfd, &sslpending_fds);
b8d2c8
                     post_handle_connection(*fdi);
b8d2c8
                     break;
b8d2c8
                 case NCAT_SSL_HANDSHAKE_PENDING_WRITE:
b8d2c8
-                    FD_SET(cfd, &master_writefds);
b8d2c8
+                    checked_fd_set(cfd, &master_writefds);
b8d2c8
                     break;
b8d2c8
                 case NCAT_SSL_HANDSHAKE_PENDING_READ:
b8d2c8
-                    FD_SET(cfd, &master_readfds);
b8d2c8
+                    checked_fd_set(cfd, &master_readfds);
b8d2c8
                     break;
b8d2c8
                 case NCAT_SSL_HANDSHAKE_FAILED:
b8d2c8
                 default:
b8d2c8
                     SSL_free(fdi->ssl);
b8d2c8
                     Close(fdi->fd);
b8d2c8
-                    FD_CLR(cfd, &sslpending_fds);
b8d2c8
-                    FD_CLR(cfd, &master_readfds);
b8d2c8
+                    checked_fd_clr(cfd, &sslpending_fds);
b8d2c8
+                    checked_fd_clr(cfd, &master_readfds);
b8d2c8
                     rm_fd(&client_fdlist, cfd);
b8d2c8
                     /* Since we removed this one, start loop over at the beginning.
b8d2c8
                      * Wastes a little time, but ensures correctness.
b8d2c8
@@ -339,7 +339,7 @@ static int ncat_listen_stream(int proto)
b8d2c8
                 }
b8d2c8
             } else
b8d2c8
 #endif
b8d2c8
-            if (FD_ISSET(cfd, &listen_fds)) {
b8d2c8
+            if (checked_fd_isset(cfd, &listen_fds)) {
b8d2c8
                 /* we have a new connection request */
b8d2c8
                 handle_connection(cfd);
b8d2c8
             } else if (cfd == STDIN_FILENO) {
b8d2c8
@@ -424,7 +424,7 @@ static void handle_connection(int socket_accept)
b8d2c8
         int i;
b8d2c8
         for (i = 0; i < num_listenaddrs; i++) {
b8d2c8
             Close(listen_socket[i]);
b8d2c8
-            FD_CLR(listen_socket[i], &master_readfds);
b8d2c8
+            checked_fd_clr(listen_socket[i], &master_readfds);
b8d2c8
             rm_fd(&client_fdlist, listen_socket[i]);
b8d2c8
         }
b8d2c8
     }
b8d2c8
@@ -468,9 +468,9 @@ static void handle_connection(int socket_accept)
b8d2c8
 #ifdef HAVE_OPENSSL
b8d2c8
     if (o.ssl) {
b8d2c8
         /* Add the socket to the necessary descriptor lists. */
b8d2c8
-        FD_SET(s.fd, &sslpending_fds);
b8d2c8
-        FD_SET(s.fd, &master_readfds);
b8d2c8
-        FD_SET(s.fd, &master_writefds);
b8d2c8
+        checked_fd_set(s.fd, &sslpending_fds);
b8d2c8
+        checked_fd_set(s.fd, &master_readfds);
b8d2c8
+        checked_fd_set(s.fd, &master_writefds);
b8d2c8
         /* Add it to our list of fds too for maintaining maxfd. */
b8d2c8
         if (add_fdinfo(&client_fdlist, &s) < 0)
b8d2c8
             bye("add_fdinfo() failed.");
b8d2c8
@@ -503,10 +503,10 @@ static void post_handle_connection(struct fdinfo sinfo)
b8d2c8
     } else {
b8d2c8
         /* Now that a client is connected, pay attention to stdin. */
b8d2c8
         if (!stdin_eof)
b8d2c8
-            FD_SET(STDIN_FILENO, &master_readfds);
b8d2c8
+            checked_fd_set(STDIN_FILENO, &master_readfds);
b8d2c8
         if (!o.sendonly) {
b8d2c8
             /* add to our lists */
b8d2c8
-            FD_SET(sinfo.fd, &master_readfds);
b8d2c8
+            checked_fd_set(sinfo.fd, &master_readfds);
b8d2c8
             /* add it to our list of fds for maintaining maxfd */
b8d2c8
 #ifdef HAVE_OPENSSL
b8d2c8
             /* Don't add it twice (see handle_connection above) */
b8d2c8
@@ -518,7 +518,7 @@ static void post_handle_connection(struct fdinfo sinfo)
b8d2c8
             }
b8d2c8
 #endif
b8d2c8
         }
b8d2c8
-        FD_SET(sinfo.fd, &master_broadcastfds);
b8d2c8
+        checked_fd_set(sinfo.fd, &master_broadcastfds);
b8d2c8
         if (add_fdinfo(&broadcast_fdlist, &sinfo) < 0)
b8d2c8
             bye("add_fdinfo() failed.");
b8d2c8
 
b8d2c8
@@ -543,7 +543,7 @@ int read_stdin(void)
b8d2c8
             logdebug("EOF on stdin\n");
b8d2c8
 
b8d2c8
         /* Don't close the file because that allows a socket to be fd 0. */
b8d2c8
-        FD_CLR(STDIN_FILENO, &master_readfds);
b8d2c8
+        checked_fd_clr(STDIN_FILENO, &master_readfds);
b8d2c8
         /* Buf mark that we've seen EOF so it doesn't get re-added to the
b8d2c8
            select list. */
b8d2c8
         stdin_eof = 1;
b8d2c8
@@ -596,14 +596,14 @@ int read_socket(int recv_fd)
b8d2c8
             }
b8d2c8
 #endif
b8d2c8
             close(recv_fd);
b8d2c8
-            FD_CLR(recv_fd, &master_readfds);
b8d2c8
+            checked_fd_clr(recv_fd, &master_readfds);
b8d2c8
             rm_fd(&client_fdlist, recv_fd);
b8d2c8
-            FD_CLR(recv_fd, &master_broadcastfds);
b8d2c8
+            checked_fd_clr(recv_fd, &master_broadcastfds);
b8d2c8
             rm_fd(&broadcast_fdlist, recv_fd);
b8d2c8
 
b8d2c8
             conn_inc--;
b8d2c8
             if (get_conn_count() == 0)
b8d2c8
-                FD_CLR(STDIN_FILENO, &master_readfds);
b8d2c8
+                checked_fd_clr(STDIN_FILENO, &master_readfds);
b8d2c8
 
b8d2c8
             return n;
b8d2c8
         }
b8d2c8
@@ -693,7 +693,7 @@ static int ncat_listen_dgram(int proto)
b8d2c8
                 logdebug("do_listen(\"%s\"): %s\n", inet_ntop_ez(&listenaddrs[i].storage, sizeof(listenaddrs[i].storage)), socket_strerror(socket_errno()));
b8d2c8
             continue;
b8d2c8
         }
b8d2c8
-        FD_SET(sockfd[num_sockets].fd, &listen_fds);
b8d2c8
+        checked_fd_set(sockfd[num_sockets].fd, &listen_fds);
b8d2c8
         add_fd(&listen_fdlist, sockfd[num_sockets].fd);
b8d2c8
         sockfd[num_sockets].addr = listenaddrs[i];
b8d2c8
         num_sockets++;
b8d2c8
@@ -713,14 +713,14 @@ static int ncat_listen_dgram(int proto)
b8d2c8
 
b8d2c8
         if (fdn != -1) {
b8d2c8
             /*remove socket descriptor which is burnt */
b8d2c8
-            FD_CLR(sockfd[fdn].fd, &listen_fds);
b8d2c8
+            checked_fd_clr(sockfd[fdn].fd, &listen_fds);
b8d2c8
             rm_fd(&listen_fdlist, sockfd[fdn].fd);
b8d2c8
 
b8d2c8
             /* Rebuild the udp socket which got burnt */
b8d2c8
             sockfd[fdn].fd = do_listen(SOCK_DGRAM, proto, &sockfd[fdn].addr);
b8d2c8
             if (sockfd[fdn].fd == -1)
b8d2c8
                 bye("do_listen: %s", socket_strerror(socket_errno()));
b8d2c8
-            FD_SET(sockfd[fdn].fd, &listen_fds);
b8d2c8
+            checked_fd_set(sockfd[fdn].fd, &listen_fds);
b8d2c8
             add_fd(&listen_fdlist, sockfd[fdn].fd);
b8d2c8
 
b8d2c8
         }
b8d2c8
@@ -758,7 +758,7 @@ static int ncat_listen_dgram(int proto)
b8d2c8
              */
b8d2c8
             for (i = 0; i <= listen_fdlist.fdmax && fds_ready > 0; i++) {
b8d2c8
                 /* Loop through descriptors until there is something ready */
b8d2c8
-                if (!FD_ISSET(i, &fds))
b8d2c8
+                if (!checked_fd_isset(i, &fds))
b8d2c8
                     continue;
b8d2c8
 
b8d2c8
                 /* Check each listening socket */
b8d2c8
@@ -856,8 +856,8 @@ static int ncat_listen_dgram(int proto)
b8d2c8
             continue;
b8d2c8
         }
b8d2c8
 
b8d2c8
-        FD_SET(socket_n, &read_fds);
b8d2c8
-        FD_SET(STDIN_FILENO, &read_fds);
b8d2c8
+        checked_fd_set(socket_n, &read_fds);
b8d2c8
+        checked_fd_set(STDIN_FILENO, &read_fds);
b8d2c8
         fdmax = socket_n;
b8d2c8
 
b8d2c8
         /* stdin -> socket and socket -> stdout */
b8d2c8
@@ -877,7 +877,7 @@ static int ncat_listen_dgram(int proto)
b8d2c8
             if (fds_ready == 0)
b8d2c8
                 bye("Idle timeout expired (%d ms).", o.idletimeout);
b8d2c8
 
b8d2c8
-            if (FD_ISSET(STDIN_FILENO, &fds)) {
b8d2c8
+            if (checked_fd_isset(STDIN_FILENO, &fds)) {
b8d2c8
                 nbytes = Read(STDIN_FILENO, buf, sizeof(buf));
b8d2c8
                 if (nbytes <= 0) {
b8d2c8
                     if (nbytes < 0 && o.verbose) {
b8d2c8
@@ -885,7 +885,7 @@ static int ncat_listen_dgram(int proto)
b8d2c8
                     } else if (nbytes == 0 && o.debug) {
b8d2c8
                         logdebug("EOF on stdin\n");
b8d2c8
                     }
b8d2c8
-                    FD_CLR(STDIN_FILENO, &read_fds);
b8d2c8
+                    checked_fd_clr(STDIN_FILENO, &read_fds);
b8d2c8
                     if (nbytes < 0)
b8d2c8
                         return 1;
b8d2c8
                     continue;
b8d2c8
@@ -909,7 +909,7 @@ static int ncat_listen_dgram(int proto)
b8d2c8
                     tempbuf = NULL;
b8d2c8
                 }
b8d2c8
             }
b8d2c8
-            if (FD_ISSET(socket_n, &fds)) {
b8d2c8
+            if (checked_fd_isset(socket_n, &fds)) {
b8d2c8
                 nbytes = recv(socket_n, buf, sizeof(buf), 0);
b8d2c8
                 if (nbytes < 0) {
b8d2c8
                     loguser("%s.\n", socket_strerror(socket_errno()));
b8d2c8
@@ -993,7 +993,7 @@ static void read_and_broadcast(int recv_fd)
b8d2c8
 
b8d2c8
                 /* Don't close the file because that allows a socket to be
b8d2c8
                    fd 0. */
b8d2c8
-                FD_CLR(recv_fd, &master_readfds);
b8d2c8
+                checked_fd_clr(recv_fd, &master_readfds);
b8d2c8
                 /* But mark that we've seen EOF so it doesn't get re-added to
b8d2c8
                    the select list. */
b8d2c8
                 stdin_eof = 1;
b8d2c8
@@ -1020,14 +1020,14 @@ static void read_and_broadcast(int recv_fd)
b8d2c8
                 }
b8d2c8
 #endif
b8d2c8
                 close(recv_fd);
b8d2c8
-                FD_CLR(recv_fd, &master_readfds);
b8d2c8
+                checked_fd_clr(recv_fd, &master_readfds);
b8d2c8
                 rm_fd(&client_fdlist, recv_fd);
b8d2c8
-                FD_CLR(recv_fd, &master_broadcastfds);
b8d2c8
+                checked_fd_clr(recv_fd, &master_broadcastfds);
b8d2c8
                 rm_fd(&broadcast_fdlist, recv_fd);
b8d2c8
 
b8d2c8
                 conn_inc--;
b8d2c8
                 if (conn_inc == 0)
b8d2c8
-                    FD_CLR(STDIN_FILENO, &master_readfds);
b8d2c8
+                    checked_fd_clr(STDIN_FILENO, &master_readfds);
b8d2c8
 
b8d2c8
                 if (o.chat)
b8d2c8
                     chat_announce_disconnect(recv_fd);
b8d2c8
@@ -1058,7 +1058,7 @@ static void read_and_broadcast(int recv_fd)
b8d2c8
 
b8d2c8
         /* Send to everyone except the one who sent this message. */
b8d2c8
         broadcastfds = master_broadcastfds;
b8d2c8
-        FD_CLR(recv_fd, &broadcastfds);
b8d2c8
+        checked_fd_clr(recv_fd, &broadcastfds);
b8d2c8
         ncat_broadcast(&broadcastfds, &broadcast_fdlist, outbuf, n);
b8d2c8
 
b8d2c8
         free(chatbuf);
b8d2c8
@@ -1073,7 +1073,7 @@ static void shutdown_sockets(int how)
b8d2c8
     int i;
b8d2c8
 
b8d2c8
     for (i = 0; i <= broadcast_fdlist.fdmax; i++) {
b8d2c8
-        if (!FD_ISSET(i, &master_broadcastfds))
b8d2c8
+        if (!checked_fd_isset(i, &master_broadcastfds))
b8d2c8
             continue;
b8d2c8
 
b8d2c8
         fdn = get_fdinfo(&broadcast_fdlist, i);
b8d2c8
@@ -1098,7 +1098,7 @@ static int chat_announce_connect(int fd, const union sockaddr_u *su)
b8d2c8
         union sockaddr_u tsu;
b8d2c8
         socklen_t len = sizeof(tsu.storage);
b8d2c8
 
b8d2c8
-        if (i == fd || !FD_ISSET(i, &master_broadcastfds))
b8d2c8
+        if (i == fd || !checked_fd_isset(i, &master_broadcastfds))
b8d2c8
             continue;
b8d2c8
 
b8d2c8
         if (getpeername(i, &tsu.sockaddr, &len) == -1)
b8d2c8
diff --git a/ncat/ncat_posix.c b/ncat/ncat_posix.c
b8d2c8
index b9fc3bc0b3..4f5641e4ac 100644
b8d2c8
--- a/ncat/ncat_posix.c
b8d2c8
+++ b/ncat/ncat_posix.c
b8d2c8
@@ -205,8 +205,8 @@ void netexec(struct fdinfo *info, char *cmdexec)
b8d2c8
         int r, n_r;
b8d2c8
 
b8d2c8
         FD_ZERO(&fds);
b8d2c8
-        FD_SET(info->fd, &fds);
b8d2c8
-        FD_SET(child_stdout[0], &fds);
b8d2c8
+        checked_fd_set(info->fd, &fds);
b8d2c8
+        checked_fd_set(child_stdout[0], &fds);
b8d2c8
 
b8d2c8
         r = fselect(maxfd + 1, &fds, NULL, NULL, NULL);
b8d2c8
         if (r == -1) {
b8d2c8
@@ -215,7 +215,7 @@ void netexec(struct fdinfo *info, char *cmdexec)
b8d2c8
             else
b8d2c8
                 break;
b8d2c8
         }
b8d2c8
-        if (FD_ISSET(info->fd, &fds)) {
b8d2c8
+        if (checked_fd_isset(info->fd, &fds)) {
b8d2c8
             int pending;
b8d2c8
 
b8d2c8
             do {
b8d2c8
@@ -225,7 +225,7 @@ void netexec(struct fdinfo *info, char *cmdexec)
b8d2c8
                 write_loop(child_stdin[1], buf, n_r);
b8d2c8
             } while (pending);
b8d2c8
         }
b8d2c8
-        if (FD_ISSET(child_stdout[0], &fds)) {
b8d2c8
+        if (checked_fd_isset(child_stdout[0], &fds)) {
b8d2c8
             char *crlf = NULL, *wbuf;
b8d2c8
             n_r = read(child_stdout[0], buf, sizeof(buf));
b8d2c8
             if (n_r <= 0)
b8d2c8
diff --git a/ncat/ncat_proxy.c b/ncat/ncat_proxy.c
b8d2c8
index 5ba10a61ad..befcf902a5 100644
b8d2c8
--- a/ncat/ncat_proxy.c
b8d2c8
+++ b/ncat/ncat_proxy.c
b8d2c8
@@ -166,7 +166,7 @@ int ncat_http_server(void)
b8d2c8
         unblock_socket(listen_socket[num_sockets]);
b8d2c8
 
b8d2c8
         /* setup select sets and max fd */
b8d2c8
-        FD_SET(listen_socket[num_sockets], &listen_fds);
b8d2c8
+        checked_fd_set(listen_socket[num_sockets], &listen_fds);
b8d2c8
         add_fd(&listen_fdlist, listen_socket[num_sockets]);
b8d2c8
 
b8d2c8
         num_sockets++;
b8d2c8
@@ -199,7 +199,7 @@ int ncat_http_server(void)
b8d2c8
 
b8d2c8
         for (i = 0; i <= listen_fdlist.fdmax && fds_ready > 0; i++) {
b8d2c8
             /* Loop through descriptors until there is something ready */
b8d2c8
-            if (!FD_ISSET(i, &read_fds))
b8d2c8
+            if (!checked_fd_isset(i, &read_fds))
b8d2c8
                 continue;
b8d2c8
 
b8d2c8
             /* Check each listening socket */
b8d2c8
@@ -457,8 +457,8 @@ static int handle_connect(struct socket_buffer *client_sock,
b8d2c8
 
b8d2c8
     maxfd = client_sock->fdn.fd < s ? s : client_sock->fdn.fd;
b8d2c8
     FD_ZERO(&m);
b8d2c8
-    FD_SET(client_sock->fdn.fd, &m);
b8d2c8
-    FD_SET(s, &m);
b8d2c8
+    checked_fd_set(client_sock->fdn.fd, &m);
b8d2c8
+    checked_fd_set(s, &m);
b8d2c8
 
b8d2c8
     errno = 0;
b8d2c8
 
b8d2c8
@@ -472,7 +472,7 @@ static int handle_connect(struct socket_buffer *client_sock,
b8d2c8
 
b8d2c8
         zmem(buf, sizeof(buf));
b8d2c8
 
b8d2c8
-        if (FD_ISSET(client_sock->fdn.fd, &r)) {
b8d2c8
+        if (checked_fd_isset(client_sock->fdn.fd, &r)) {
b8d2c8
             do {
b8d2c8
                 do {
b8d2c8
                     len = fdinfo_recv(&client_sock->fdn, buf, sizeof(buf));
b8d2c8
@@ -488,7 +488,7 @@ static int handle_connect(struct socket_buffer *client_sock,
b8d2c8
             } while (fdinfo_pending(&client_sock->fdn));
b8d2c8
         }
b8d2c8
 
b8d2c8
-        if (FD_ISSET(s, &r)) {
b8d2c8
+        if (checked_fd_isset(s, &r)) {
b8d2c8
             do {
b8d2c8
                 len = recv(s, buf, sizeof(buf), 0);
b8d2c8
             } while (len == -1 && socket_errno() == EINTR);