Blame SOURCES/nmap-safe_fd_functions.patch

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