diff options
author | Tom Tromey <tom@tromey.com> | 2023-02-08 13:59:36 -0700 |
---|---|---|
committer | Tom Tromey <tom@tromey.com> | 2023-02-27 15:46:31 -0700 |
commit | f3d3bbbcdd8af6295458eee3b023447c13edabd3 (patch) | |
tree | 7f505c42452ba1c2890b2c56e278eb53dd2a1fed /gdb/python/py-frame.c | |
parent | 9b955acd7f5029345e7bd8e31a4af0e6b356f0ae (diff) | |
download | gdb-f3d3bbbcdd8af6295458eee3b023447c13edabd3.zip gdb-f3d3bbbcdd8af6295458eee3b023447c13edabd3.tar.gz gdb-f3d3bbbcdd8af6295458eee3b023447c13edabd3.tar.bz2 |
Fix value chain use-after-free
Hannes filed a bug showing a crash, where a pretty-printer written in
Python could cause a use-after-free. He sent a patch, but I thought a
different approach was needed.
In a much earlier patch (see bug #12533), we changed the Python code
to release new values from the value chain when constructing a
gdb.Value. The rationale for this is that if you write a command that
does a lot of computations in a loop, all the values will be kept live
by the value chain, resulting in gdb using a large amount of memory.
However, suppose a value is passed to Python from some code in gdb
that needs to use the value after the call into Python. In this
scenario, value_to_value_object will still release the value -- and
because gdb code doesn't generally keep strong references to values (a
consequence of the ancient decision to use the value chain to avoid
memory management), this will result in a use-after-free.
This scenario can happen, as it turns out, when a value is passed to
Python for pretty-printing. Now, normally this route boxes the value
via value_to_value_object_no_release, avoiding the problematic release
from the value chain. However, if you then call Value.cast, the
underlying value API might return the same value, when is then
released from the chain.
This patch fixes the problem by changing how value boxing is done.
value_to_value_object no longer removes a value from the chain.
Instead, every spot in gdb that might construct new values uses a
scoped_value_mark to ensure that the requirements of bug #12533 are
met. And, because incoming values aren't ever released from the chain
(the Value.cast one comes earlier on the chain than the
scoped_value_mark), the bug can no longer occur. (Note that many
spots in the Python layer already take this approach, so not many
places needed to be touched.)
In the future I think we should replace the use of raw "value *" with
value_ref_ptr pretty much everywhere. This will ensure lifetime
safety throughout gdb.
The test case in this patch comes from Hannes' original patch. I only
made a trivial ("require") change to it. However, while this fails
for him, I can't make it fail on this machine; nevertheless, he tried
my patch and reported the bug as being fixed.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30044
Diffstat (limited to 'gdb/python/py-frame.c')
-rw-r--r-- | gdb/python/py-frame.c | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c index f66d22b..ecd55d2 100644 --- a/gdb/python/py-frame.c +++ b/gdb/python/py-frame.c @@ -241,12 +241,13 @@ static PyObject * frapy_read_register (PyObject *self, PyObject *args) { PyObject *pyo_reg_id; - struct value *val = NULL; + PyObject *result = nullptr; if (!PyArg_UnpackTuple (args, "read_register", 1, 1, &pyo_reg_id)) return NULL; try { + scoped_value_mark free_values; frame_info_ptr frame; int regnum; @@ -257,17 +258,19 @@ frapy_read_register (PyObject *self, PyObject *args) return nullptr; gdb_assert (regnum >= 0); - val = value_of_register (regnum, frame); + struct value *val = value_of_register (regnum, frame); if (val == NULL) PyErr_SetString (PyExc_ValueError, _("Can't read register.")); + else + result = value_to_value_object (val); } catch (const gdb_exception &except) { GDB_PY_HANDLE_EXCEPTION (except); } - return val == NULL ? NULL : value_to_value_object (val); + return result; } /* Implementation of gdb.Frame.block (self) -> gdb.Block. @@ -482,7 +485,6 @@ frapy_read_var (PyObject *self, PyObject *args) PyObject *sym_obj, *block_obj = NULL; struct symbol *var = NULL; /* gcc-4.3.2 false warning. */ const struct block *block = NULL; - struct value *val = NULL; if (!PyArg_ParseTuple (args, "O|O", &sym_obj, &block_obj)) return NULL; @@ -540,18 +542,21 @@ frapy_read_var (PyObject *self, PyObject *args) return NULL; } + PyObject *result = nullptr; try { FRAPY_REQUIRE_VALID (self, frame); - val = read_var_value (var, block, frame); + scoped_value_mark free_values; + struct value *val = read_var_value (var, block, frame); + result = value_to_value_object (val); } catch (const gdb_exception &except) { GDB_PY_HANDLE_EXCEPTION (except); } - return value_to_value_object (val); + return result; } /* Select this frame. */ |