/*1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2004-2005, 2007, 2010, 2012-2015, 2017-20224* Todd C. Miller <[email protected]>5*6* Permission to use, copy, modify, and distribute this software for any7* purpose with or without fee is hereby granted, provided that the above8* copyright notice and this permission notice appear in all copies.9*10* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES11* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF12* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR13* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES14* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN15* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF16* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.17*/1819#include <config.h>2021#ifndef HAVE_CLOSEFROM2223#include <errno.h>24#include <fcntl.h>25#include <limits.h>26#include <stdlib.h>27#include <unistd.h>28#ifdef HAVE_PSTAT_GETPROC29# include <sys/pstat.h>30#else31# include <dirent.h>32#endif33#ifdef HAVE_LIBPROC_H34# include <libproc.h>35#endif36#ifdef HAVE_LINUX_CLOSE_RANGE_H37# include <linux/close_range.h>38#endif3940#include <sudo_compat.h>41#include <sudo_util.h>42#include <pathnames.h>4344#ifndef OPEN_MAX45# define OPEN_MAX 25646#endif4748/* Avoid potential libdispatch crash on macOS when we close its fds. */49#ifdef __APPLE__50# define closefrom_close(x) fcntl((x), F_SETFD, FD_CLOEXEC)51#else52# define closefrom_close(x) close(x)53#endif5455/*56* Close all file descriptors greater than or equal to lowfd.57* This is the expensive (fallback) method.58*/59static void60closefrom_fallback(int lowfd)61{62long fd, maxfd;6364/*65* Fall back on sysconf(_SC_OPEN_MAX). This is equivalent to66* checking the RLIMIT_NOFILE soft limit. It is possible for67* there to be open file descriptors past this limit but there's68* not much we can do about that since the hard limit may be69* RLIM_INFINITY (LLONG_MAX or ULLONG_MAX on modern systems).70*/71maxfd = sysconf(_SC_OPEN_MAX);72if (maxfd < OPEN_MAX)73maxfd = OPEN_MAX;7475/* Make sure we didn't get RLIM_INFINITY as the upper limit. */76if (maxfd > INT_MAX)77maxfd = INT_MAX;7879for (fd = lowfd; fd < maxfd; fd++) {80(void)closefrom_close((int)fd);81}82}8384/*85* Close all file descriptors greater than or equal to lowfd.86* We try the fast way first, falling back on the slow method.87*/88void89sudo_closefrom(int lowfd)90{91#if defined(HAVE_PSTAT_GETPROC)92struct pst_status pst;93#elif defined(HAVE_DIRFD)94const char *path;95DIR *dirp;96#endif97#if defined(HAVE_PROC_PIDINFO)98struct proc_fdinfo *buf = NULL;99const pid_t pid = getpid();100int i, n, len;101#endif102103/* Try the fast method first, if possible. */104#if defined(HAVE_FCNTL_CLOSEM)105if (fcntl(lowfd, F_CLOSEM, 0) != -1)106return;107#elif defined(HAVE_CLOSE_RANGE)108if (close_range((unsigned int)lowfd, ~0U, 0) != -1)109return;110#elif defined(HAVE_PROC_PIDINFO)111len = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0);112switch (len) {113case 0:114/* No open files. */115return;116case -1:117/* Fall back on other methods. */118break;119default:120/* Allocate space for 4 extra fds to leave some wiggle room. */121buf = malloc(len + (PROC_PIDLISTFD_SIZE * 4));122if (buf == NULL)123break;124n = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, buf, len);125if (n == -1 || n > len) {126free(buf);127break;128}129n /= PROC_PIDLISTFD_SIZE;130for (i = 0; i < n; i++) {131if (buf[i].proc_fd >= lowfd) {132(void)closefrom_close(buf[i].proc_fd);133}134}135free(buf);136return;137}138#endif /* HAVE_PROC_PIDINFO */139#if defined(HAVE_PSTAT_GETPROC)140/*141* EOVERFLOW is not a fatal error for the fields we use.142* See the "EOVERFLOW Error" section of pstat_getvminfo(3).143*/144if (pstat_getproc(&pst, sizeof(pst), 0, getpid()) != -1 ||145errno == EOVERFLOW) {146int fd;147148for (fd = lowfd; fd <= pst.pst_highestfd; fd++)149(void)closefrom_close(fd);150return;151}152#elif defined(HAVE_DIRFD)153/* Use /proc/self/fd (or /dev/fd on macOS) if it exists. */154# ifdef __APPLE__155path = _PATH_DEV "fd";156# else157path = "/proc/self/fd";158# endif159if ((dirp = opendir(path)) != NULL) {160struct dirent *dent;161while ((dent = readdir(dirp)) != NULL) {162const char *errstr;163int fd = (int)sudo_strtonum(dent->d_name, lowfd, INT_MAX, &errstr);164if (errstr == NULL && fd != dirfd(dirp)) {165(void)closefrom_close(fd);166}167}168(void)closedir(dirp);169return;170}171#endif /* HAVE_DIRFD */172173/* Do things the slow way. */174closefrom_fallback(lowfd);175}176177#endif /* HAVE_CLOSEFROM */178179180