From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7833) id 9A3C03858CDA; Tue, 26 Jul 2022 07:59:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9A3C03858CDA Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Lancelot SIX To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb/varobj: Fix use after free in varobj X-Act-Checkin: binutils-gdb X-Git-Author: Lancelot SIX X-Git-Refname: refs/heads/master X-Git-Oldrev: 60cd08d40309a2b4ce1daae84074893e6303ad17 X-Git-Newrev: bc20e562ec0436b6117b989c0e3d8f66c9d4d979 Message-Id: <20220726075925.9A3C03858CDA@sourceware.org> Date: Tue, 26 Jul 2022 07:59:25 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 26 Jul 2022 07:59:25 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dbc20e562ec04= 36b6117b989c0e3d8f66c9d4d979 commit bc20e562ec0436b6117b989c0e3d8f66c9d4d979 Author: Lancelot SIX Date: Fri Jul 8 11:37:18 2022 +0100 gdb/varobj: Fix use after free in varobj =20 Varobj object contains references to types, variables (i.e. struct variable) and expression. All of those can reference data on an objfile's obstack. It is possible for this objfile to be deleted (and the obstack to be feed), while the varobj remains valid. Later, if the user uses the varobj, this will result in a use-after-free error. With address sanitizer build, this leads to a plain error. For non address sanitizer build we might see undefined behaviour, which manifest themself as assertion failures when accessing data backed by feed memory. =20 This can be observed if we create a varobj that refers to ta symbol in a shared library, after either the objfile gets reloaded (using the `file` command) or after the shared library is unloaded (with a call to dlclose for example). =20 This patch fixes those issues by: =20 - Adding cleanup procedure to the free_objfile observable. When activated this observer clears expressions referencing the objfile being freed, and removes references to blocks belonging to this objfile. - Adding varobj support in the `preserve_values` (gdb.value.c). This ensures that before the objfile is unloaded, any type owned by the objfile referenced by the varobj is replaced by an equivalent type not owned by the objfile. This process is done here instead of in the free_objfile observer in order to reuse the type hash table already used for similar purpose when replacing types of values kept in the value history. =20 This patch also makes sure to keep a reference to the expression's gdbarch and language_defn members when the varobj->root->exp is initialized. Those structures outlive the objfile, so this is safe. This is done because those references might be used initialize a python context even after exp is invalidated. Another approach could have been to initialize the python context with default gdbarch and language_defn (i.e. nullptr) if expr is NULL, but since we might still try to display the value which was obtained by evaluating exp when it was still valid, keeping track of the context which was used at this time seems reasonable. =20 Tested on x86_64-Linux. =20 Co-Authored-By: Pedro Alves Diff: --- gdb/testsuite/gdb.mi/mi-var-invalidate-shlib-lib.c | 30 +++++++ gdb/testsuite/gdb.mi/mi-var-invalidate-shlib.c | 41 ++++++++++ gdb/testsuite/gdb.mi/mi-var-invalidate-shlib.exp | 93 ++++++++++++++++++= ++++ gdb/value.c | 27 +++++++ gdb/varobj.c | 66 ++++++++++++++- 5 files changed, 256 insertions(+), 1 deletion(-) diff --git a/gdb/testsuite/gdb.mi/mi-var-invalidate-shlib-lib.c b/gdb/tests= uite/gdb.mi/mi-var-invalidate-shlib-lib.c new file mode 100644 index 00000000000..3d4c7aa11b6 --- /dev/null +++ b/gdb/testsuite/gdb.mi/mi-var-invalidate-shlib-lib.c @@ -0,0 +1,30 @@ +/* Copyright 2022 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . = */ + +struct bar +{ + int a; + int b; +}; + +struct bar global_shlib_var =3D { 2, 2 }; + +void +foo () +{ + struct bar local_var =3D { 1, 1 }; +} diff --git a/gdb/testsuite/gdb.mi/mi-var-invalidate-shlib.c b/gdb/testsuite= /gdb.mi/mi-var-invalidate-shlib.c new file mode 100644 index 00000000000..f9237c3b853 --- /dev/null +++ b/gdb/testsuite/gdb.mi/mi-var-invalidate-shlib.c @@ -0,0 +1,41 @@ +/* Copyright 2022 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . = */ + +#include + +static void +no_varobj_in_scope (void) +{ +} + +static void +floating_varobj_in_scope (void) +{ + int local_var =3D 0; +} + +int +main (int argc, char *argv []) +{ + void *shlib =3D dlopen (SHLIB_PATH, RTLD_LAZY); + void (*foo)() =3D dlsym (shlib, "foo"); + foo (); + dlclose (shlib); + + no_varobj_in_scope (); + floating_varobj_in_scope (); +} diff --git a/gdb/testsuite/gdb.mi/mi-var-invalidate-shlib.exp b/gdb/testsui= te/gdb.mi/mi-var-invalidate-shlib.exp new file mode 100644 index 00000000000..b405c832f29 --- /dev/null +++ b/gdb/testsuite/gdb.mi/mi-var-invalidate-shlib.exp @@ -0,0 +1,93 @@ +# Copyright 2007-2022 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# Test that varobj are invalidated after the shlib they point to goes +# away. + + +load_lib mi-support.exp +set MIFLAGS "-i=3Dmi" + +if { [skip_shlib_tests] } { + return 0 +} + +standard_testfile .c -lib.c +set shlib_path [standard_output_file ${testfile}-lib.so] + +if { [gdb_compile_shlib $srcdir/$subdir/$srcfile2 $shlib_path {debug}] != =3D "" } { + untested "failed to compile" + return -1 +} + + +set opts [list shlib_load debug additional_flags=3D-DSHLIB_PATH=3D"${shlib= _path}"] +if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executabl= e $opts] !=3D "" } { + untested "failed to compile" + return -1 +} + +proc do_test { separate_debuginfo } { + if { $separate_debuginfo } { + gdb_gnu_strip_debug $::shlib_path + } + + if { [mi_clean_restart] } { + unsupported "failed to start GDB" + return + } + + # Start the process once and create varobjs referencing the loaded obj= files. + with_test_prefix "setup" { + mi_load_shlibs $::shlib_path + if { $separate_debuginfo } { + mi_load_shlibs ${::shlib_path}.debug + } + mi_delete_breakpoints + mi_gdb_reinitialize_dir $::srcdir/$::subdir + mi_gdb_load $::binfile + + mi_runto foo -pending + + mi_create_varobj global_shlib_var global_shlib_var "create global gloal_s= hlib_var" + mi_create_floating_varobj floating_local local_var "create floating local= _var" + + # Advance to a point where the shlib's objfile have been deleted. + mi_continue_to "no_varobj_in_scope" + } + + with_test_prefix "after objfile deleted" { + # The global shlib var was invalidated when the objfile got unloaded. + mi_gdb_test "-var-update global_shlib_var" \ + "\\^done,changelist=3D\\\[\{name=3D\"global_shlib_var\",in_scope=3D\"= invalid\",has_more=3D\"0\"\}\]" \ + "global_shlib_var invalidated" + + # The floating var is still valid but not in scope. + mi_gdb_test "-var-update floating_local" \ + "\\^done,changelist=3D\\\[{name=3D\"floating_local\",in_scope=3D\"fal= se\",type_changed=3D\"false\",has_more=3D\"0\"}\\\]" \ + "floating_local still valid but not in scope" + + # The varobj can be re-evaluated if the expression is valid in the current + # frame. + mi_continue_to "floating_varobj_in_scope" + mi_gdb_test "-var-update floating_local" \ + "\\^done,changelist=3D\\\[{name=3D\"floating_local\",in_scope=3D\"tru= e\",type_changed=3D\"true\",new_type=3D\"int\",new_num_children=3D\"0\",has= _more=3D\"0\"}\\\]" \ + "floating_local in scope with new type and value" + } +} + +foreach_with_prefix separate_debuginfo {0 1} { + do_test $separate_debuginfo +} diff --git a/gdb/value.c b/gdb/value.c index c9bec678d95..d027eef07e5 100644 --- a/gdb/value.c +++ b/gdb/value.c @@ -49,6 +49,7 @@ #include "cli/cli-style.h" #include "expop.h" #include "inferior.h" +#include "varobj.h" =20 /* Definition of a user function. */ struct internal_function @@ -2599,6 +2600,25 @@ preserve_one_internalvar (struct internalvar *var, s= truct objfile *objfile, } } =20 +/* Make sure that all types and values referenced by VAROBJ are updated be= fore + OBJFILE is discarded. COPIED_TYPES is used to prevent cycles and + duplicates. */ + +static void +preserve_one_varobj (struct varobj *varobj, struct objfile *objfile, + htab_t copied_types) +{ + if (varobj->type->is_objfile_owned () + && varobj->type->objfile_owner () =3D=3D objfile) + { + varobj->type + =3D copy_type_recursive (objfile, varobj->type, copied_types); + } + + if (varobj->value !=3D nullptr) + preserve_one_value (varobj->value.get (), objfile, copied_types); +} + /* Update the internal variables and value history when OBJFILE is discarded; we must copy the types out of the objfile. New global types will be created for every convenience variable which currently points to @@ -2620,6 +2640,13 @@ preserve_values (struct objfile *objfile) for (var =3D internalvars; var; var =3D var->next) preserve_one_internalvar (var, objfile, copied_types.get ()); =20 + /* For the remaining varobj, check that none has type owned by OBJFILE. = */ + all_root_varobjs ([&copied_types, objfile] (struct varobj *varobj) + { + preserve_one_varobj (varobj, objfile, + copied_types.get ()); + }); + preserve_ext_lang_values (objfile, copied_types.get ()); } =20 diff --git a/gdb/varobj.c b/gdb/varobj.c index 9a6d579ddf0..2b6dfff6888 100644 --- a/gdb/varobj.c +++ b/gdb/varobj.c @@ -32,6 +32,7 @@ #include "parser-defs.h" #include "gdbarch.h" #include +#include "observable.h" =20 #if HAVE_PYTHON #include "python/python.h" @@ -72,6 +73,12 @@ struct varobj_root /* The expression for this parent. */ expression_up exp; =20 + /* Cached arch from exp, for use in case exp gets invalidated. */ + struct gdbarch *gdbarch =3D nullptr; + + /* Cached language from exp, for use in case exp gets invalidated. */ + const struct language_defn *language_defn =3D nullptr; + /* Block for which this expression is valid. */ const struct block *valid_block =3D NULL; =20 @@ -206,7 +213,7 @@ is_root_p (const struct varobj *var) =20 /* See python-internal.h. */ gdbpy_enter_varobj::gdbpy_enter_varobj (const struct varobj *var) -: gdbpy_enter (var->root->exp->gdbarch, var->root->exp->language_defn) +: gdbpy_enter (var->root->gdbarch, var->root->language_defn) { } =20 @@ -303,6 +310,11 @@ varobj_create (const char *objname, try { var->root->exp =3D parse_exp_1 (&p, pc, block, 0, &tracker); + + /* Cache gdbarch and language_defn as they might be used even + after var is invalidated and var->root->exp cleared. */ + var->root->gdbarch =3D var->root->exp->gdbarch; + var->root->language_defn =3D var->root->exp->language_defn; } =20 catch (const gdb_exception_error &except) @@ -2378,6 +2390,55 @@ varobj_invalidate (void) all_root_varobjs (varobj_invalidate_iter); } =20 +/* Ensure that no varobj keep references to OBJFILE. */ + +static void +varobj_invalidate_if_uses_objfile (struct objfile *objfile) +{ + if (objfile->separate_debug_objfile_backlink !=3D nullptr) + objfile =3D objfile->separate_debug_objfile_backlink; + + all_root_varobjs ([objfile] (struct varobj *var) + { + if (var->root->valid_block !=3D nullptr) + { + struct objfile *bl_objfile =3D block_objfile (var->root->valid_block); + if (bl_objfile->separate_debug_objfile_backlink !=3D nullptr) + bl_objfile =3D bl_objfile->separate_debug_objfile_backlink; + + if (bl_objfile =3D=3D objfile) + { + /* The varobj is tied to a block which is going away. There is + no way to reconstruct something later, so invalidate the + varobj completly and drop the reference to the block which is + being freed. */ + var->root->is_valid =3D false; + var->root->valid_block =3D nullptr; + } + } + + if (var->root->exp !=3D nullptr + && exp_uses_objfile (var->root->exp.get (), objfile)) + { + /* The varobj's current expression references the objfile. For + globals and floating, it is possible that when we try to + re-evaluate the expression later it is still valid with + whatever is in scope at that moment. Just invalidate the + expression for now. */ + var->root->exp.reset (); + + /* It only makes sense to keep a floating varobj around. */ + if (!var->root->floating) + var->root->is_valid =3D false; + } + + /* var->value->type and var->type might also reference the objfile. + This is taken care of in value.c:preserve_values which deals with + making sure that objfile-owend types are replaced with + gdbarch-owned equivalents. */ + }); +} + /* A hash function for a varobj. */ =20 static hashval_t @@ -2412,4 +2473,7 @@ _initialize_varobj () _("When non-zero, varobj debugging is enabled."), NULL, show_varobjdebug, &setdebuglist, &showdebuglist); + + gdb::observers::free_objfile.attach (varobj_invalidate_if_uses_objfile, + "varobj"); }