2004-01-12 18:55:30 +00:00
|
|
|
/*
|
2019-04-29 07:21:51 -06:00
|
|
|
* SPDX-License-Identifier: ISC
|
|
|
|
*
|
2018-11-14 13:37:45 -07:00
|
|
|
* Copyright (c) 2004-2005, 2007, 2010, 2012-2015, 2017-2018
|
2017-12-03 17:53:40 -07:00
|
|
|
* Todd C. Miller <Todd.Miller@sudo.ws>
|
2004-01-12 18:55:30 +00:00
|
|
|
*
|
2004-02-13 21:36:43 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2004-01-12 18:55:30 +00:00
|
|
|
*
|
2004-02-13 21:36:43 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2004-01-12 18:55:30 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-26 08:39:09 -06:00
|
|
|
/*
|
|
|
|
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
|
|
|
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
|
|
|
*/
|
2018-10-21 08:46:05 -06:00
|
|
|
|
2004-11-19 18:39:14 +00:00
|
|
|
#include <config.h>
|
2004-06-01 16:44:14 +00:00
|
|
|
|
2013-04-01 10:19:26 -04:00
|
|
|
#ifndef HAVE_CLOSEFROM
|
|
|
|
|
2004-01-12 18:55:30 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2004-06-01 16:44:14 +00:00
|
|
|
#include <stdio.h>
|
2015-06-19 14:29:27 -06:00
|
|
|
#include <stdlib.h>
|
2007-06-20 11:06:48 +00:00
|
|
|
#include <fcntl.h>
|
2013-12-17 14:32:24 -07:00
|
|
|
#include <limits.h>
|
2013-03-01 13:01:37 -05:00
|
|
|
#ifdef HAVE_PSTAT_GETPROC
|
|
|
|
# include <sys/pstat.h>
|
2004-06-01 16:44:14 +00:00
|
|
|
#else
|
2015-07-02 09:24:48 -06:00
|
|
|
# include <dirent.h>
|
2004-06-01 16:44:14 +00:00
|
|
|
#endif
|
2004-01-12 18:55:30 +00:00
|
|
|
|
2014-07-22 14:25:16 -06:00
|
|
|
#include "sudo_compat.h"
|
2019-10-14 10:09:30 -06:00
|
|
|
#include "sudo_util.h"
|
2017-07-27 20:51:21 -06:00
|
|
|
#include "pathnames.h"
|
2004-06-01 16:44:14 +00:00
|
|
|
|
2019-11-02 10:56:02 -06:00
|
|
|
#ifndef OPEN_MAX
|
|
|
|
# define OPEN_MAX 256
|
2015-02-19 09:59:25 -07:00
|
|
|
#endif
|
|
|
|
|
2004-01-12 18:55:30 +00:00
|
|
|
/*
|
|
|
|
* Close all file descriptors greater than or equal to lowfd.
|
2013-12-16 16:24:02 -07:00
|
|
|
* This is the expensive (fallback) method.
|
2007-06-09 11:26:43 +00:00
|
|
|
*/
|
2018-11-08 15:17:39 -07:00
|
|
|
static void
|
2010-02-27 09:23:25 -05:00
|
|
|
closefrom_fallback(int lowfd)
|
2007-06-09 11:26:43 +00:00
|
|
|
{
|
|
|
|
long fd, maxfd;
|
|
|
|
|
|
|
|
/*
|
2019-11-02 10:56:02 -06:00
|
|
|
* Fall back on sysconf(_SC_OPEN_MAX). This is equivalent to
|
|
|
|
* checking the RLIMIT_NOFILE soft limit. It is possible for
|
|
|
|
* there to be open file descriptors past this limit but there's
|
|
|
|
* not much we can do about that since the hard limit may be
|
|
|
|
* RLIM_INFINITY (LLONG_MAX or ULLONG_MAX on modern systems).
|
2007-06-09 11:26:43 +00:00
|
|
|
*/
|
|
|
|
maxfd = sysconf(_SC_OPEN_MAX);
|
2019-11-02 10:56:02 -06:00
|
|
|
if (maxfd < OPEN_MAX)
|
|
|
|
maxfd = OPEN_MAX;
|
|
|
|
|
|
|
|
/* Make sure we didn't get RLIM_INFINITY as the upper limit. */
|
|
|
|
if (maxfd > INT_MAX)
|
|
|
|
madfd = INT_MAX;
|
2007-06-09 11:26:43 +00:00
|
|
|
|
2013-08-07 15:04:58 -06:00
|
|
|
for (fd = lowfd; fd < maxfd; fd++) {
|
|
|
|
#ifdef __APPLE__
|
2013-12-16 16:24:02 -07:00
|
|
|
/* Avoid potential libdispatch crash when we close its fds. */
|
2013-12-10 16:23:21 -07:00
|
|
|
(void) fcntl((int) fd, F_SETFD, FD_CLOEXEC);
|
2013-08-07 15:04:58 -06:00
|
|
|
#else
|
2007-06-09 11:26:43 +00:00
|
|
|
(void) close((int) fd);
|
2013-08-07 15:04:58 -06:00
|
|
|
#endif
|
|
|
|
}
|
2007-06-09 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close all file descriptors greater than or equal to lowfd.
|
|
|
|
* We try the fast way first, falling back on the slow method.
|
2004-01-12 18:55:30 +00:00
|
|
|
*/
|
2013-03-01 13:01:37 -05:00
|
|
|
void
|
2014-06-26 15:51:08 -06:00
|
|
|
sudo_closefrom(int lowfd)
|
2013-03-01 13:01:37 -05:00
|
|
|
{
|
2018-11-14 13:37:45 -07:00
|
|
|
#if defined(HAVE_PSTAT_GETPROC)
|
2013-03-01 13:01:37 -05:00
|
|
|
struct pst_status pstat;
|
2018-11-14 13:37:45 -07:00
|
|
|
#elif defined(HAVE_DIRFD)
|
|
|
|
const char *path;
|
|
|
|
DIR *dirp;
|
|
|
|
#endif
|
2013-03-01 13:01:37 -05:00
|
|
|
|
2018-11-14 13:37:45 -07:00
|
|
|
/* Try the fast method first, if possible. */
|
|
|
|
#if defined(HAVE_FCNTL_CLOSEM)
|
|
|
|
if (fcntl(lowfd, F_CLOSEM, 0) != -1)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
#if defined(HAVE_PSTAT_GETPROC)
|
2019-03-06 20:13:40 -07:00
|
|
|
/*
|
|
|
|
* EOVERFLOW is not a fatal error for the fields we use.
|
|
|
|
* See the "EOVERFLOW Error" section of pstat_getvminfo(3).
|
|
|
|
*/
|
|
|
|
if (pstat_getproc(&pstat, sizeof(pstat), 0, getpid()) != -1 ||
|
|
|
|
errno == EOVERFLOW) {
|
2018-11-14 13:37:45 -07:00
|
|
|
int fd;
|
|
|
|
|
2013-03-01 13:01:37 -05:00
|
|
|
for (fd = lowfd; fd <= pstat.pst_highestfd; fd++)
|
|
|
|
(void) close(fd);
|
2018-11-14 13:37:45 -07:00
|
|
|
return;
|
2013-03-01 13:01:37 -05:00
|
|
|
}
|
|
|
|
#elif defined(HAVE_DIRFD)
|
2019-04-26 15:21:29 -06:00
|
|
|
/* Use /proc/self/fd (or /dev/fd on macOS) if it exists. */
|
|
|
|
# ifdef __APPLE__
|
2017-06-29 18:10:53 -06:00
|
|
|
path = _PATH_DEV "fd";
|
2013-12-16 16:24:02 -07:00
|
|
|
# else
|
2013-12-17 07:38:20 -07:00
|
|
|
path = "/proc/self/fd";
|
2013-12-16 16:24:02 -07:00
|
|
|
# endif
|
|
|
|
if ((dirp = opendir(path)) != NULL) {
|
|
|
|
struct dirent *dent;
|
2004-06-01 16:44:14 +00:00
|
|
|
while ((dent = readdir(dirp)) != NULL) {
|
2013-12-16 16:24:02 -07:00
|
|
|
const char *errstr;
|
2019-10-14 10:09:30 -06:00
|
|
|
int fd = sudo_strtonum(dent->d_name, lowfd, INT_MAX, &errstr);
|
2013-12-10 16:23:21 -07:00
|
|
|
if (errstr == NULL && fd != dirfd(dirp)) {
|
2013-12-16 16:24:02 -07:00
|
|
|
# ifdef __APPLE__
|
|
|
|
/* Avoid potential libdispatch crash when we close its fds. */
|
|
|
|
(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
# else
|
2013-12-10 16:23:21 -07:00
|
|
|
(void) close(fd);
|
2013-12-16 16:24:02 -07:00
|
|
|
# endif
|
2013-12-10 16:23:21 -07:00
|
|
|
}
|
2004-06-01 16:44:14 +00:00
|
|
|
}
|
2004-06-01 20:51:56 +00:00
|
|
|
(void) closedir(dirp);
|
2018-11-14 13:37:45 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_DIRFD */
|
|
|
|
|
|
|
|
/* Do things the slow way. */
|
2018-11-08 15:17:39 -07:00
|
|
|
closefrom_fallback(lowfd);
|
|
|
|
}
|
|
|
|
|
2013-04-01 10:19:26 -04:00
|
|
|
#endif /* HAVE_CLOSEFROM */
|