Update libc.pot for 2.37 release.
[glibc.git] / libio / vtables.c
blob5d5906a818d6304c7110cd7e2c41865cc1745847
1 /* libio vtable validation.
2 Copyright (C) 2016-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <dlfcn.h>
20 #include <libioP.h>
21 #include <stdio.h>
22 #include <ldsodefs.h>
23 #include <pointer_guard.h>
25 #ifdef SHARED
27 void (*IO_accept_foreign_vtables) (void) attribute_hidden;
29 #else /* !SHARED */
31 /* Used to check whether static dlopen support is needed. */
32 # pragma weak __dlopen
34 #endif
36 void attribute_hidden
37 _IO_vtable_check (void)
39 #ifdef SHARED
40 /* Honor the compatibility flag. */
41 void (*flag) (void) = atomic_load_relaxed (&IO_accept_foreign_vtables);
42 PTR_DEMANGLE (flag);
43 if (flag == &_IO_vtable_check)
44 return;
46 /* In case this libc copy is in a non-default namespace, we always
47 need to accept foreign vtables because there is always a
48 possibility that FILE * objects are passed across the linking
49 boundary. */
51 Dl_info di;
52 struct link_map *l;
53 if (!rtld_active ()
54 || (_dl_addr (_IO_vtable_check, &di, &l, NULL) != 0
55 && l->l_ns != LM_ID_BASE))
56 return;
59 #else /* !SHARED */
60 /* We cannot perform vtable validation in the static dlopen case
61 because FILE * handles might be passed back and forth across the
62 boundary. Therefore, we disable checking in this case. */
63 if (__dlopen != NULL)
64 return;
65 #endif
67 __libc_fatal ("Fatal error: glibc detected an invalid stdio handle\n");
70 /* Some variants of libstdc++ interpose _IO_2_1_stdin_ etc. and
71 install their own vtables directly, without calling _IO_init or
72 other functions. Detect this by looking at the vtables values
73 during startup, and disable vtable validation in this case. */
74 #ifdef SHARED
75 __attribute__ ((constructor))
76 static void
77 check_stdfiles_vtables (void)
79 if (_IO_2_1_stdin_.vtable != &_IO_file_jumps
80 || _IO_2_1_stdout_.vtable != &_IO_file_jumps
81 || _IO_2_1_stderr_.vtable != &_IO_file_jumps)
82 IO_set_accept_foreign_vtables (&_IO_vtable_check);
84 #endif