Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] Fix use-after-free when Python Breakpoint.stop method deletes the breakpoint
@ 2026-11-18 10:34 YuPeng
  2026-11-18 14:26 ` Eli Zaretskii
  2026-11-19 16:22 ` Andrew Burgess
  0 siblings, 2 replies; 5+ messages in thread
From: YuPeng @ 2026-11-18 10:34 UTC (permalink / raw)
  To: gdb-patches; +Cc: tromey, debianyu

If a Python Breakpoint.stop method calls self.delete(), the underlying
breakpoint is freed while bpstat is still processing it.

delete_breakpoint tries to clear references with
bpstat_remove_bp_location, but that only walks
thread->control.stop_bpstat, which still points at the bpstat chain from
the previous stop (it is cleared in proceed()), not the chain currently
being built by bpstat_stop_status.  The current chain therefore keeps a
dangling breakpoint pointer, and gdb crashes on the next access.

The reproducer from the bug report returns False from stop(), so the
crash happens right after the callback while infrun examines the bpstat
chain (the report shows bpstat_explains_signal).

Fix:
- Assign thread->control.stop_bpstat = bs_head before the check_status
  loop in bpstat_stop_status, so bpstat_remove_bp_location can find and
  nullify the reference in the chain being processed.
- In bpstat_check_breakpoint_conditions, return early if
  bs->breakpoint_at became NULL after the extension language callback.
- In bpstat_stop_status, re-read b = bs->breakpoint_at after
  bpstat_check_breakpoint_conditions and skip the entry if it is NULL.

Tested on x86_64-linux with an ASan build: without the fix gdb aborts
with a heap-use-after-free; with it the breakpoint is deleted and the
inferior exits normally.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34379

I do not have an FSF copyright assignment on file; please let me know
if you think this change needs one.
---
 gdb/breakpoint.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index a23f5998..37be75e4 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -5988,6 +5988,10 @@ bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
      implemented.  */
   bs->stop = breakpoint_ext_lang_cond_says_stop (b);
 
+  /* The callback may have deleted the breakpoint.  */
+  if (bs->breakpoint_at == NULL)
+    return;
+
   if (is_watchpoint (b))
     {
       watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
@@ -6231,6 +6235,10 @@ bpstat_stop_status (const address_space *aspace,
 
   removed_any = 0;
 
+  /* Let delete_breakpoint see the chain being built, so that it can
+     clear references to a breakpoint deleted by a callback.  */
+  thread->control.stop_bpstat = bs_head;
+
   for (bs = bs_head; bs != NULL; bs = bs->next)
     {
       if (!bs->stop)
@@ -6242,6 +6250,12 @@ bpstat_stop_status (const address_space *aspace,
 	{
 	  bpstat_check_breakpoint_conditions (bs, thread);
 
+	  /* Skip if the callback deleted the breakpoint, otherwise
+	     re-read it.  */
+	  if (bs->breakpoint_at == NULL)
+	    continue;
+	  b = bs->breakpoint_at;
+
 	  if (bs->stop)
 	    {
 	      ++(b->hit_count);

base-commit: bd92f21ae14b14aa135fb94919a92f2cdf66b2a4
-- 
2.39.5

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-24 11:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 10:34 [PATCH] Fix use-after-free when Python Breakpoint.stop method deletes the breakpoint YuPeng
2026-11-18 14:26 ` Eli Zaretskii
2026-11-19 16:22 ` Andrew Burgess
2026-11-24  2:54   ` [PATCH v2] " YuPeng
2026-11-24 11:26     ` Andrew Burgess

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox