1 /* libio vtable validation.
2    Copyright (C) 2016-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <dlfcn.h>
20 #include <libioP.h>
21 #include <stdio.h>
22 #include <ldsodefs.h>
23 
24 #ifdef SHARED
25 
26 void (*IO_accept_foreign_vtables) (void) attribute_hidden;
27 
28 /* Used to detected multiple libcs.  */
29 extern struct dl_open_hook *_dl_open_hook;
30 libc_hidden_proto (_dl_open_hook);
31 
32 #else  /* !SHARED */
33 
34 /* Used to check whether static dlopen support is needed.  */
35 # pragma weak __dlopen
36 
37 #endif
38 
39 void attribute_hidden
_IO_vtable_check(void)40 _IO_vtable_check (void)
41 {
42 #ifdef SHARED
43   /* Honor the compatibility flag.  */
44   void (*flag) (void) = atomic_load_relaxed (&IO_accept_foreign_vtables);
45 #ifdef PTR_DEMANGLE
46   PTR_DEMANGLE (flag);
47 #endif
48   if (flag == &_IO_vtable_check)
49     return;
50 
51   /* In case this libc copy is in a non-default namespace, we always
52      need to accept foreign vtables because there is always a
53      possibility that FILE * objects are passed across the linking
54      boundary.  */
55   {
56     Dl_info di;
57     struct link_map *l;
58     if (!rtld_active ()
59         || (_dl_addr (_IO_vtable_check, &di, &l, NULL) != 0
60             && l->l_ns != LM_ID_BASE))
61       return;
62   }
63 
64 #else /* !SHARED */
65   /* We cannot perform vtable validation in the static dlopen case
66      because FILE * handles might be passed back and forth across the
67      boundary.  Therefore, we disable checking in this case.  */
68   if (__dlopen != NULL)
69     return;
70 #endif
71 
72   __libc_fatal ("Fatal error: glibc detected an invalid stdio handle\n");
73 }
74 
75 /* Some variants of libstdc++ interpose _IO_2_1_stdin_ etc. and
76    install their own vtables directly, without calling _IO_init or
77    other functions.  Detect this by looking at the vtables values
78    during startup, and disable vtable validation in this case.  */
79 #ifdef SHARED
80 __attribute__ ((constructor))
81 static void
check_stdfiles_vtables(void)82 check_stdfiles_vtables (void)
83 {
84   if (_IO_2_1_stdin_.vtable != &_IO_file_jumps
85       || _IO_2_1_stdout_.vtable != &_IO_file_jumps
86       || _IO_2_1_stderr_.vtable != &_IO_file_jumps)
87     IO_set_accept_foreign_vtables (&_IO_vtable_check);
88 }
89 #endif
90