libio: Implement vtable verification [BZ #20191]
This commit puts all libio vtables in a dedicated, read-only ELF
section, so that they are consecutive in memory. Before any indirect
jump, the vtable pointer is checked against the section boundaries,
and the process is terminated if the vtable pointer does not fall into
the special ELF section.
To enable backwards compatibility, a special flag variable
(_IO_accept_foreign_vtables), protected by the pointer guard, avoids
process termination if libio stream object constructor functions have
been called earlier. Such constructor functions are called by the GCC
2.95 libstdc++ library, and this mechanism ensures compatibility with
old binaries. Existing callers inside glibc of these functions are
adjusted to call the original functions, not the wrappers which enable
vtable compatiblity.
The compatibility mechanism is used to enable passing FILE * objects
across a static dlopen boundary, too.
2016-06-23 20:01:40 +02:00
|
|
|
/* libio vtable validation.
|
2018-01-01 01:32:25 +01:00
|
|
|
Copyright (C) 2016-2018 Free Software Foundation, Inc.
|
libio: Implement vtable verification [BZ #20191]
This commit puts all libio vtables in a dedicated, read-only ELF
section, so that they are consecutive in memory. Before any indirect
jump, the vtable pointer is checked against the section boundaries,
and the process is terminated if the vtable pointer does not fall into
the special ELF section.
To enable backwards compatibility, a special flag variable
(_IO_accept_foreign_vtables), protected by the pointer guard, avoids
process termination if libio stream object constructor functions have
been called earlier. Such constructor functions are called by the GCC
2.95 libstdc++ library, and this mechanism ensures compatibility with
old binaries. Existing callers inside glibc of these functions are
adjusted to call the original functions, not the wrappers which enable
vtable compatiblity.
The compatibility mechanism is used to enable passing FILE * objects
across a static dlopen boundary, too.
2016-06-23 20:01:40 +02:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <libioP.h>
|
|
|
|
#include <stdio.h>
|
2017-12-18 20:04:13 +01:00
|
|
|
#include <ldsodefs.h>
|
libio: Implement vtable verification [BZ #20191]
This commit puts all libio vtables in a dedicated, read-only ELF
section, so that they are consecutive in memory. Before any indirect
jump, the vtable pointer is checked against the section boundaries,
and the process is terminated if the vtable pointer does not fall into
the special ELF section.
To enable backwards compatibility, a special flag variable
(_IO_accept_foreign_vtables), protected by the pointer guard, avoids
process termination if libio stream object constructor functions have
been called earlier. Such constructor functions are called by the GCC
2.95 libstdc++ library, and this mechanism ensures compatibility with
old binaries. Existing callers inside glibc of these functions are
adjusted to call the original functions, not the wrappers which enable
vtable compatiblity.
The compatibility mechanism is used to enable passing FILE * objects
across a static dlopen boundary, too.
2016-06-23 20:01:40 +02:00
|
|
|
|
|
|
|
#ifdef SHARED
|
|
|
|
|
|
|
|
void (*IO_accept_foreign_vtables) (void) attribute_hidden;
|
|
|
|
|
|
|
|
/* Used to detected multiple libcs. */
|
|
|
|
extern struct dl_open_hook *_dl_open_hook;
|
|
|
|
libc_hidden_proto (_dl_open_hook);
|
|
|
|
|
|
|
|
#else /* !SHARED */
|
|
|
|
|
|
|
|
/* Used to check whether static dlopen support is needed. */
|
|
|
|
# pragma weak __dlopen
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void attribute_hidden
|
|
|
|
_IO_vtable_check (void)
|
|
|
|
{
|
|
|
|
#ifdef SHARED
|
|
|
|
/* Honor the compatibility flag. */
|
|
|
|
void (*flag) (void) = atomic_load_relaxed (&IO_accept_foreign_vtables);
|
2016-07-06 11:43:54 +02:00
|
|
|
#ifdef PTR_DEMANGLE
|
libio: Implement vtable verification [BZ #20191]
This commit puts all libio vtables in a dedicated, read-only ELF
section, so that they are consecutive in memory. Before any indirect
jump, the vtable pointer is checked against the section boundaries,
and the process is terminated if the vtable pointer does not fall into
the special ELF section.
To enable backwards compatibility, a special flag variable
(_IO_accept_foreign_vtables), protected by the pointer guard, avoids
process termination if libio stream object constructor functions have
been called earlier. Such constructor functions are called by the GCC
2.95 libstdc++ library, and this mechanism ensures compatibility with
old binaries. Existing callers inside glibc of these functions are
adjusted to call the original functions, not the wrappers which enable
vtable compatiblity.
The compatibility mechanism is used to enable passing FILE * objects
across a static dlopen boundary, too.
2016-06-23 20:01:40 +02:00
|
|
|
PTR_DEMANGLE (flag);
|
2016-07-06 11:43:54 +02:00
|
|
|
#endif
|
libio: Implement vtable verification [BZ #20191]
This commit puts all libio vtables in a dedicated, read-only ELF
section, so that they are consecutive in memory. Before any indirect
jump, the vtable pointer is checked against the section boundaries,
and the process is terminated if the vtable pointer does not fall into
the special ELF section.
To enable backwards compatibility, a special flag variable
(_IO_accept_foreign_vtables), protected by the pointer guard, avoids
process termination if libio stream object constructor functions have
been called earlier. Such constructor functions are called by the GCC
2.95 libstdc++ library, and this mechanism ensures compatibility with
old binaries. Existing callers inside glibc of these functions are
adjusted to call the original functions, not the wrappers which enable
vtable compatiblity.
The compatibility mechanism is used to enable passing FILE * objects
across a static dlopen boundary, too.
2016-06-23 20:01:40 +02:00
|
|
|
if (flag == &_IO_vtable_check)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* In case this libc copy is in a non-default namespace, we always
|
|
|
|
need to accept foreign vtables because there is always a
|
|
|
|
possibility that FILE * objects are passed across the linking
|
|
|
|
boundary. */
|
|
|
|
{
|
|
|
|
Dl_info di;
|
|
|
|
struct link_map *l;
|
2017-12-18 20:04:13 +01:00
|
|
|
if (!rtld_active ()
|
libio: Implement vtable verification [BZ #20191]
This commit puts all libio vtables in a dedicated, read-only ELF
section, so that they are consecutive in memory. Before any indirect
jump, the vtable pointer is checked against the section boundaries,
and the process is terminated if the vtable pointer does not fall into
the special ELF section.
To enable backwards compatibility, a special flag variable
(_IO_accept_foreign_vtables), protected by the pointer guard, avoids
process termination if libio stream object constructor functions have
been called earlier. Such constructor functions are called by the GCC
2.95 libstdc++ library, and this mechanism ensures compatibility with
old binaries. Existing callers inside glibc of these functions are
adjusted to call the original functions, not the wrappers which enable
vtable compatiblity.
The compatibility mechanism is used to enable passing FILE * objects
across a static dlopen boundary, too.
2016-06-23 20:01:40 +02:00
|
|
|
|| (_dl_addr (_IO_vtable_check, &di, &l, NULL) != 0
|
|
|
|
&& l->l_ns != LM_ID_BASE))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !SHARED */
|
|
|
|
/* We cannot perform vtable validation in the static dlopen case
|
|
|
|
because FILE * handles might be passed back and forth across the
|
|
|
|
boundary. Therefore, we disable checking in this case. */
|
|
|
|
if (__dlopen != NULL)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
__libc_fatal ("Fatal error: glibc detected an invalid stdio handle\n");
|
|
|
|
}
|
2018-06-26 10:24:52 +02:00
|
|
|
|
|
|
|
/* Some variants of libstdc++ interpose _IO_2_1_stdin_ etc. and
|
|
|
|
install their own vtables directly, without calling _IO_init or
|
|
|
|
other functions. Detect this by looking at the vtables values
|
|
|
|
during startup, and disable vtable validation in this case. */
|
|
|
|
#ifdef SHARED
|
|
|
|
__attribute__ ((constructor))
|
|
|
|
static void
|
|
|
|
check_stdfiles_vtables (void)
|
|
|
|
{
|
|
|
|
if (_IO_2_1_stdin_.vtable != &_IO_file_jumps
|
|
|
|
|| _IO_2_1_stdout_.vtable != &_IO_file_jumps
|
|
|
|
|| _IO_2_1_stderr_.vtable != &_IO_file_jumps)
|
|
|
|
IO_set_accept_foreign_vtables (&_IO_vtable_check);
|
|
|
|
}
|
|
|
|
#endif
|