From: Todd C. Miller <Todd.Miller@courtesan.com>
Date: Tue, 23 Jun 2015 22:37:00 +0000 (-0600)
Subject: Use our own bitmap macros instead of borrowing the ones from select.
X-Git-Tag: SUDO_1_8_14^2~54
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a76b93e23a1cc43bb03f2016b464885a57e5bff5;p=sudo

Use our own bitmap macros instead of borrowing the ones from select.
---

diff --git a/include/sudo_compat.h b/include/sudo_compat.h
index c41cc0740..6d9dae404 100644
--- a/include/sudo_compat.h
+++ b/include/sudo_compat.h
@@ -297,6 +297,15 @@ extern int errno;
 # define WCOREDUMP(x)	((x) & 0x80)
 #endif
 
+/* Number of bits in a byte. */
+#ifndef NBBY
+# ifdef __NBBY
+#  define NBBY __NBBY
+# else
+#  define NBBY 8
+# endif
+#endif
+
 #ifndef HAVE_SETEUID
 #  if defined(HAVE_SETRESUID)
 #    define seteuid(u)	setresuid(-1, (u), -1)
diff --git a/include/sudo_debug.h b/include/sudo_debug.h
index 15494ec68..70619f1c9 100644
--- a/include/sudo_debug.h
+++ b/include/sudo_debug.h
@@ -25,21 +25,6 @@
 #endif
 #include "sudo_queue.h"
 
-/* Number of bits in a byte. */
-#ifndef NBBY
-# ifdef __NBBY
-#  define NBBY __NBBY
-# else
-#  define NBBY 8
-# endif
-#endif
-
-/* Bit map macros. */
-#define sudo_setbit(_a, _i)	((_a)[(_i) / NBBY] |= 1 << ((_i) % NBBY))
-#define sudo_clrbit(_a, _i)	((_a)[(_i) / NBBY] &= ~(1<<((_i) % NBBY)))
-#define sudo_isset(_a, _i)	((_a)[(_i) / NBBY] & (1<<((_i) % NBBY)))
-#define sudo_isclr(_a, _i)	(((_a)[(_i) / NBBY] & (1<<((_i) % NBBY))) == 0)
- 
 /*
  * List of debug files and flags for use in registration.
  */
diff --git a/include/sudo_util.h b/include/sudo_util.h
index f49e4d69b..89c9f89f5 100644
--- a/include/sudo_util.h
+++ b/include/sudo_util.h
@@ -132,6 +132,12 @@
 # define mtim_get(_x, _y)	do { (_y).tv_sec = (_x)->st_mtime; (_y).tv_nsec = 0; } while (0)
 #endif /* HAVE_ST_MTIM */
 
+/* Bit map macros. */
+#define sudo_setbit(_a, _i)	((_a)[(_i) / NBBY] |= 1 << ((_i) % NBBY))
+#define sudo_clrbit(_a, _i)	((_a)[(_i) / NBBY] &= ~(1<<((_i) % NBBY)))
+#define sudo_isset(_a, _i)	((_a)[(_i) / NBBY] & (1<<((_i) % NBBY)))
+#define sudo_isclr(_a, _i)	(((_a)[(_i) / NBBY] & (1<<((_i) % NBBY))) == 0)
+
 /*
  * Macros to quiet gcc's warn_unused_result attribute.
  */
diff --git a/src/preserve_fds.c b/src/preserve_fds.c
index d0ab439e8..00c73e29d 100644
--- a/src/preserve_fds.c
+++ b/src/preserve_fds.c
@@ -16,13 +16,6 @@
 
 #include <config.h>
 
-#include <sys/param.h>		/* for howmany() on Linux */
-#ifdef HAVE_SYS_SYSMACROS_H
-# include <sys/sysmacros.h>	/* for howmany() on Solaris */
-#endif /* HAVE_SYS_SYSMACROS_H */
-#ifdef HAVE_SYS_SELECT_H
-# include <sys/select.h>	/* for FD_* macros */
-#endif /* HAVE_SYS_SELECT_H */
 #include <stdio.h>
 #include <stdlib.h>
 #ifdef HAVE_STRING_H
@@ -93,7 +86,7 @@ closefrom_except(int startfd, struct preserved_fd_list *pfds)
 {
     int fd, lastfd = -1;
     struct preserved_fd *pfd, *pfd_next;
-    fd_set *fdsp;
+    unsigned char *fdbits;
     debug_decl(closefrom_except, SUDO_DEBUG_UTIL)
 
     /* First, relocate preserved fds to be as contiguous as possible.  */
@@ -132,18 +125,18 @@ closefrom_except(int startfd, struct preserved_fd_list *pfds)
     }
 
     /* Create bitmap of preserved (relocated) fds.  */
-    fdsp = calloc(howmany(lastfd + 1, NFDBITS), sizeof(fd_mask));
-    if (fdsp == NULL)
+    fdbits = calloc((lastfd + NBBY) / NBBY, 1);
+    if (fdbits == NULL)
 	sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
     TAILQ_FOREACH(pfd, pfds, entries) {
-	FD_SET(pfd->lowfd, fdsp);
+	sudo_setbit(fdbits, pfd->lowfd);
     }
 
     /*
      * Close any unpreserved fds [startfd,lastfd]
      */
     for (fd = startfd; fd <= lastfd; fd++) {
-	if (!FD_ISSET(fd, fdsp)) {
+	if (!sudo_isset(fdbits, fd)) {
 	    sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
 		"closing fd %d", fd);
 #ifdef __APPLE__
@@ -154,7 +147,7 @@ closefrom_except(int startfd, struct preserved_fd_list *pfds)
 #endif
 	}
     }
-    free(fdsp);
+    free(fdbits);
 
     /* Let closefrom() do the rest for us. */
     if (lastfd + 1 > startfd)