1 /* libio vtable validation.
2 Copyright (C) 2016-2022 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/>. */
23 #include <pointer_guard.h>
27 void (*IO_accept_foreign_vtables
) (void) attribute_hidden
;
31 /* Used to check whether static dlopen support is needed. */
32 # pragma weak __dlopen
37 _IO_vtable_check (void)
40 /* Honor the compatibility flag. */
41 void (*flag
) (void) = atomic_load_relaxed (&IO_accept_foreign_vtables
);
43 if (flag
== &_IO_vtable_check
)
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
54 || (_dl_addr (_IO_vtable_check
, &di
, &l
, NULL
) != 0
55 && l
->l_ns
!= LM_ID_BASE
))
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. */
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. */
75 __attribute__ ((constructor
))
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
);