Back to index

glibc  2.9
Defines | Functions
check_fds.c File Reference
#include <errno.h>
#include <fcntl.h>
#include <paths.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <abort-instr.h>
#include <device-nrs.h>
#include <not-cancel.h>

Go to the source code of this file.


#define O_NOFOLLOW   0


static void check_one_fd (int fd, int mode)
void __libc_check_standard_fds (void)

Define Documentation

Definition at line 31 of file check_fds.c.

#define O_NOFOLLOW   0

Function Documentation

void __libc_check_standard_fds ( void  )

Definition at line 89 of file check_fds.c.

  /* This is really paranoid but some people actually are.  If /dev/null
     should happen to be a symlink to somewhere else and not the device
     commonly known as "/dev/null" we bail out.  We can detect this with
     the O_NOFOLLOW flag for open() but only on some system.  */
#ifndef O_NOFOLLOW
# define O_NOFOLLOW  0
  /* Check all three standard file descriptors.  */

Here is the caller graph for this function:

static void check_one_fd ( int  fd,
int  mode 
) [static]

Definition at line 41 of file check_fds.c.

  /* Note that fcntl() with this parameter is not a cancellation point.  */
  if (__builtin_expect (__libc_fcntl (fd, F_GETFD), 0) == -1
      && errno == EBADF)
      const char *name;
      dev_t dev;

      /* For writable descriptors we use /dev/full.  */
      if ((mode & O_ACCMODE) == O_WRONLY)
         name = _PATH_DEV "full";
         dev = makedev (DEV_FULL_MAJOR, DEV_FULL_MINOR);
         name = _PATH_DEVNULL;
         dev = makedev (DEV_NULL_MAJOR, DEV_NULL_MINOR);

      /* Something is wrong with this descriptor, it's probably not
        opened.  Open /dev/null so that the SUID program we are
        about to start does not accidently use this descriptor.  */
      int nullfd = open_not_cancel (name, mode, 0);

      /* We are very paranoid here.  With all means we try to ensure
        that we are actually opening the /dev/null device and nothing

        Note that the following code assumes that STDIN_FILENO,
        STDOUT_FILENO, STDERR_FILENO are the three lowest file
        decsriptor numbers, in this order.  */
      struct stat64 st;
      if (__builtin_expect (nullfd != fd, 0)
         || __builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) != 0
         || __builtin_expect (S_ISCHR (st.st_mode), 1) == 0
         || st.st_rdev != dev)
       /* We cannot even give an error message here since it would
          run into the same problems.  */
       while (1)
         /* Try for ever and ever.  */

Here is the call graph for this function:

Here is the caller graph for this function: