2004-01-12 18:55:30 +00:00
|
|
|
/*
|
2015-02-19 09:59:25 -07:00
|
|
|
* Copyright (c) 2004-2005, 2007, 2010, 2012-2015
|
2008-11-09 14:13:13 +00:00
|
|
|
* Todd C. Miller <Todd.Miller@courtesan.com>
|
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
|
|
|
*/
|
|
|
|
|
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"
|
2004-06-01 16:44:14 +00:00
|
|
|
|
2015-02-19 09:59:25 -07:00
|
|
|
#ifndef _POSIX_OPEN_MAX
|
|
|
|
# define _POSIX_OPEN_MAX 20
|
|
|
|
#endif
|
|
|
|
|
2013-12-16 16:24:02 -07:00
|
|
|
#if defined(HAVE_FCNTL_CLOSEM) && !defined(HAVE_DIRFD)
|
2014-06-26 15:51:08 -06:00
|
|
|
# define sudo_closefrom closefrom_fallback
|
2007-06-09 11:26:43 +00: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
|
|
|
*/
|
|
|
|
void
|
2010-02-27 09:23:25 -05:00
|
|
|
closefrom_fallback(int lowfd)
|
2007-06-09 11:26:43 +00:00
|
|
|
{
|
|
|
|
long fd, maxfd;
|
|
|
|
|
|
|
|
/*
|
2015-02-19 09:59:25 -07:00
|
|
|
* Fall back on sysconf(_SC_OPEN_MAX). We avoid checking
|
2007-06-09 11:26:43 +00:00
|
|
|
* resource limits since it is possible to open a file descriptor
|
|
|
|
* and then drop the rlimit such that it is below the open fd.
|
|
|
|
*/
|
|
|
|
maxfd = sysconf(_SC_OPEN_MAX);
|
|
|
|
if (maxfd < 0)
|
2015-02-19 09:59:25 -07:00
|
|
|
maxfd = _POSIX_OPEN_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
|
|
|
#if defined(HAVE_FCNTL_CLOSEM)
|
2006-08-17 15:26:54 +00:00
|
|
|
void
|
2014-06-26 15:51:08 -06:00
|
|
|
sudo_closefrom(int lowfd)
|
2006-08-17 15:26:54 +00:00
|
|
|
{
|
2007-06-09 11:26:43 +00:00
|
|
|
if (fcntl(lowfd, F_CLOSEM, 0) == -1)
|
|
|
|
closefrom_fallback(lowfd);
|
2006-08-17 15:26:54 +00:00
|
|
|
}
|
2013-03-01 13:01:37 -05:00
|
|
|
#elif defined(HAVE_PSTAT_GETPROC)
|
|
|
|
void
|
2014-06-26 15:51:08 -06:00
|
|
|
sudo_closefrom(int lowfd)
|
2013-03-01 13:01:37 -05:00
|
|
|
{
|
|
|
|
struct pst_status pstat;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (pstat_getproc(&pstat, sizeof(pstat), 0, getpid()) != -1) {
|
|
|
|
for (fd = lowfd; fd <= pstat.pst_highestfd; fd++)
|
|
|
|
(void) close(fd);
|
|
|
|
} else {
|
|
|
|
closefrom_fallback(lowfd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#elif defined(HAVE_DIRFD)
|
2004-01-12 18:55:30 +00:00
|
|
|
void
|
2014-06-26 15:51:08 -06:00
|
|
|
sudo_closefrom(int lowfd)
|
2004-01-12 18:55:30 +00:00
|
|
|
{
|
2013-12-17 07:38:20 -07:00
|
|
|
const char *path;
|
2004-06-01 16:44:14 +00:00
|
|
|
DIR *dirp;
|
2004-01-12 18:55:30 +00:00
|
|
|
|
2013-12-17 07:38:20 -07:00
|
|
|
/* Use /proc/self/fd (or /dev/fd on FreeBSD) if it exists. */
|
2013-12-16 16:24:02 -07:00
|
|
|
# if defined(__FreeBSD__) || defined(__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;
|
|
|
|
int fd = 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);
|
|
|
|
} else
|
2007-06-09 11:26:43 +00:00
|
|
|
closefrom_fallback(lowfd);
|
2004-01-12 18:55:30 +00:00
|
|
|
}
|
2007-06-09 11:26:43 +00:00
|
|
|
#endif /* HAVE_FCNTL_CLOSEM */
|
2013-04-01 10:19:26 -04:00
|
|
|
#endif /* HAVE_CLOSEFROM */
|