chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / sysdeps / mach / hurd / check_fds.c
1 /* Copyright (C) 2000 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <fcntl.h>
20 #include <paths.h>
21 #include <unistd.h>
22
23 #include <hurd.h>
24 #include <hurd/fd.h>
25
26 #include <set-hooks.h>
27
28 /* Try to get a machine dependent instruction which will make the
29    program crash.  This is used in case everything else fails.  */
30 #include <abort-instr.h>
31 #ifndef ABORT_INSTRUCTION
32 /* No such instruction is available.  */
33 # define ABORT_INSTRUCTION
34 #endif
35
36 static void
37 check_one_fd (int fd, int mode)
38 {
39   struct hurd_fd *d;
40
41   d = _hurd_fd_get (fd);
42   if (d == NULL)
43     {
44       /* This descriptor hasn't been opened.  We try to allocate the
45          descriptor and open /dev/null on it so that the SUID program
46          we are about to start does not accidently use this
47          descriptor.  */
48       d = _hurd_alloc_fd (NULL, fd);
49       if (d != NULL)
50         {
51           mach_port_t port;
52
53           port = __file_name_lookup (_PATH_DEVNULL, mode, 0);
54           if (port)
55             {
56               /* Since /dev/null isn't supposed to be a terminal, we
57                  avoid any ctty magic.  */
58               d->port.port = port;
59               d->flags = 0;
60
61               __spin_unlock (&d->port.lock);
62               return;
63             }
64         }
65       
66       /* We cannot even give an error message here since it would run
67          into the same problems.  */
68       while (1)
69         /* Try for ever and ever.  */
70         ABORT_INSTRUCTION;
71     }
72 }
73
74 static void
75 check_standard_fds (void)
76 {
77   /* Check all three standard file descriptors.  */
78   check_one_fd (STDIN_FILENO, O_RDONLY);
79   check_one_fd (STDOUT_FILENO, O_RDWR);
80   check_one_fd (STDERR_FILENO, O_RDWR);
81 }
82
83 static void
84 init_standard_fds (void)
85 {
86   /* Now that we have FDs, make sure that, if this is a SUID program,
87      FDs 0, 1 and 2 are allocated.  If necessary we'll set them up
88      ourselves.  If that's not possible we stop the program.  */
89   if (__builtin_expect (__libc_enable_secure, 0))
90     check_standard_fds ();
91
92   (void) &init_standard_fds;    /* Avoid "defined but not used" warning.  */
93 }
94 text_set_element (_hurd_fd_subinit, init_standard_fds);
95 \f
96
97 #ifndef SHARED
98 void
99 __libc_check_standard_fds (void)
100 {
101   /* We don't check the standard file descriptors here.  They will be
102      checked when we initialize the file descriptor table, as part of
103      the _hurd_fd_subinit hook.
104      
105      This function is only present to make sure that this module gets
106      linked in when part of the static libc.  */
107 }
108 #endif