|
Packit |
6c4009 |
/* Copyright (C) 2000-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <fcntl.h>
|
|
Packit |
6c4009 |
#include <paths.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
#include <sys/stat.h>
|
|
Packit |
6c4009 |
#include <sys/sysmacros.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Try to get a machine dependent instruction which will make the
|
|
Packit |
6c4009 |
program crash. This is used in case everything else fails. */
|
|
Packit |
6c4009 |
#include <abort-instr.h>
|
|
Packit |
6c4009 |
#ifndef ABORT_INSTRUCTION
|
|
Packit |
6c4009 |
/* No such instruction is available. */
|
|
Packit |
6c4009 |
# define ABORT_INSTRUCTION
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <device-nrs.h>
|
|
Packit |
6c4009 |
#include <not-cancel.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Should other OSes (e.g., Hurd) have different versions which can
|
|
Packit |
6c4009 |
be written in a better way? */
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
check_one_fd (int fd, int mode)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (__builtin_expect (__fcntl64_nocancel (fd, F_GETFD), 0) == -1
|
|
Packit |
6c4009 |
&& errno == EBADF)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
const char *name;
|
|
Packit |
6c4009 |
dev_t dev;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* For writable descriptors we use /dev/full. */
|
|
Packit |
6c4009 |
if ((mode & O_ACCMODE) == O_WRONLY)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
name = _PATH_DEV "full";
|
|
Packit |
6c4009 |
dev = __gnu_dev_makedev (DEV_FULL_MAJOR, DEV_FULL_MINOR);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
name = _PATH_DEVNULL;
|
|
Packit |
6c4009 |
dev = __gnu_dev_makedev (DEV_NULL_MAJOR, DEV_NULL_MINOR);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Something is wrong with this descriptor, it's probably not
|
|
Packit |
6c4009 |
opened. Open /dev/null so that the SUID program we are
|
|
Packit |
6c4009 |
about to start does not accidentally use this descriptor. */
|
|
Packit |
6c4009 |
int nullfd = __open_nocancel (name, mode, 0);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* We are very paranoid here. With all means we try to ensure
|
|
Packit |
6c4009 |
that we are actually opening the /dev/null device and nothing
|
|
Packit |
6c4009 |
else.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
Note that the following code assumes that STDIN_FILENO,
|
|
Packit |
6c4009 |
STDOUT_FILENO, STDERR_FILENO are the three lowest file
|
|
Packit |
6c4009 |
decsriptor numbers, in this order. */
|
|
Packit |
6c4009 |
struct stat64 st;
|
|
Packit |
6c4009 |
if (__builtin_expect (nullfd != fd, 0)
|
|
Packit |
6c4009 |
|| __builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) != 0
|
|
Packit |
6c4009 |
|| __builtin_expect (S_ISCHR (st.st_mode), 1) == 0
|
|
Packit |
6c4009 |
|| st.st_rdev != dev)
|
|
Packit |
6c4009 |
/* We cannot even give an error message here since it would
|
|
Packit |
6c4009 |
run into the same problems. */
|
|
Packit |
6c4009 |
while (1)
|
|
Packit |
6c4009 |
/* Try for ever and ever. */
|
|
Packit |
6c4009 |
ABORT_INSTRUCTION;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
void
|
|
Packit |
6c4009 |
__libc_check_standard_fds (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Check all three standard file descriptors. The O_NOFOLLOW flag
|
|
Packit |
6c4009 |
is really paranoid but some people actually are. If /dev/null
|
|
Packit |
6c4009 |
should happen to be a symlink to somewhere else and not the
|
|
Packit |
6c4009 |
device commonly known as "/dev/null" we bail out. */
|
|
Packit |
6c4009 |
check_one_fd (STDIN_FILENO, O_WRONLY | O_NOFOLLOW);
|
|
Packit |
6c4009 |
check_one_fd (STDOUT_FILENO, O_RDONLY | O_NOFOLLOW);
|
|
Packit |
6c4009 |
check_one_fd (STDERR_FILENO, O_RDONLY | O_NOFOLLOW);
|
|
Packit |
6c4009 |
}
|