binutils-gdb/gdb/python
Tom Tromey f3d3bbbcdd 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
2023-02-27 15:46:31 -07:00
..
lib/gdb gdb: reformat Python files with black 23.1.0 2023-02-27 13:28:32 -05:00
py-all-events.def Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-arch.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-auto-load.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-block.c Remove ALL_BLOCK_SYMBOLS_WITH_NAME 2023-02-19 12:51:06 -07:00
py-bpevent.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-breakpoint.c gdb: use -1 for breakpoint::task default value 2023-02-12 05:46:44 +00:00
py-cmd.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-connection.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-continueevent.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-dap.c Simplify interp::exec / interp_exec - let exceptions propagate 2023-02-08 17:28:42 +00:00
py-disasm.c gdb: remove language.h include from frame.h 2023-01-20 14:48:56 -05:00
py-event-types.def Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-event.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-event.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-events.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-evtregistry.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-evts.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-exitedevent.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-finishbreakpoint.c Fix value chain use-after-free 2023-02-27 15:46:31 -07:00
py-frame.c Fix value chain use-after-free 2023-02-27 15:46:31 -07:00
py-framefilter.c Turn value_type into method 2023-02-13 15:21:06 -07:00
py-function.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-gdb-readline.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-inferior.c Turn remaining value_contents functions into methods 2023-02-13 15:22:16 -07:00
py-infevents.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-infthread.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-instruction.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-instruction.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-lazy-string.c Fix value chain use-after-free 2023-02-27 15:46:31 -07:00
py-linetable.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-membuf.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-micmd.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-newobjfileevent.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-objfile.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-param.c GDB: Allow arbitrary keywords in integer set commands 2023-01-19 21:15:56 +00:00
py-prettyprint.c Fix value chain use-after-free 2023-02-27 15:46:31 -07:00
py-progspace.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-record-btrace.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-record-btrace.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-record-full.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-record-full.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-record.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-record.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-ref.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-registers.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-signalevent.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-stopevent.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-stopevent.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-symbol.c Fix value chain use-after-free 2023-02-27 15:46:31 -07:00
py-symtab.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-threadevent.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-tui.c gdb/python: deallocate tui window factories at Python shut down 2023-02-13 14:50:46 +00:00
py-type.c Fix value chain use-after-free 2023-02-27 15:46:31 -07:00
py-unwind.c Fix value chain use-after-free 2023-02-27 15:46:31 -07:00
py-utils.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-value.c Fix value chain use-after-free 2023-02-27 15:46:31 -07:00
py-varobj.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
py-xmethods.c Fix value chain use-after-free 2023-02-27 15:46:31 -07:00
python-config.py gdb/python-config: replace deprecated distutils.sysconfig 2022-09-01 04:51:33 -04:00
python-internal.h Fix value chain use-after-free 2023-02-27 15:46:31 -07:00
python.c Fix value chain use-after-free 2023-02-27 15:46:31 -07:00
python.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00