2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-31 14:25:15 +00:00

Make sure stdin, stdout and stderr are open and dup them to /dev/null

if not.
This commit is contained in:
Todd C. Miller
2004-12-10 00:26:22 +00:00
parent 1936aeb299
commit 07d74adfba

18
sudo.c
View File

@@ -965,6 +965,7 @@ open_sudoers(sudoers, keepopen)
static void
initial_setup()
{
int miss[3], devnull = -1;
#if defined(RLIMIT_CORE) && !defined(SUDO_DEVEL)
struct rlimit rl;
@@ -977,6 +978,23 @@ initial_setup()
(void) setrlimit(RLIMIT_CORE, &rl);
#endif /* RLIMIT_CORE && !SUDO_DEVEL */
/*
* stdin, stdout and stderr must be open; set them to /dev/null
* if they are closed and close all other fds.
*/
miss[STDIN_FILENO] = fcntl(STDIN_FILENO, F_GETFL, 0) != 0;
miss[STDOUT_FILENO] = fcntl(STDOUT_FILENO, F_GETFL, 0) != 0;
miss[STDERR_FILENO] = fcntl(STDERR_FILENO, F_GETFL, 0) != 0;
if ((miss[STDIN_FILENO] || miss[STDOUT_FILENO] || miss[STDERR_FILENO])) {
if ((devnull = open(_PATH_DEVNULL, O_RDWR, 0644)) != -1) {
if (miss[STDIN_FILENO])
(void) dup2(devnull, STDIN_FILENO);
if (miss[STDOUT_FILENO])
(void) dup2(devnull, STDOUT_FILENO);
if (miss[STDERR_FILENO])
(void) dup2(devnull, STDERR_FILENO);
}
}
closefrom(STDERR_FILENO + 1);
}