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

* Re: [PATCH] Fix use-after-free when Python Breakpoint.stop method deletes the breakpoint
  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
  1 sibling, 0 replies; 5+ messages in thread
From: Eli Zaretskii @ 2026-11-18 14:26 UTC (permalink / raw)
  To: YuPeng; +Cc: gdb-patches, tromey, debianyu

> From: YuPeng <yupeng@kylinos.cn>
> Cc: tromey@sourceware.org,debianyu@163.com
> Date: Wed, 18 Nov 2026 18:34:15 +0800
> 
> 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.

Thanks, but could you tell if the patch and the description were
generated by LLM, by any chance?

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

* Re: [PATCH] Fix use-after-free when Python Breakpoint.stop method deletes the breakpoint
  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
  1 sibling, 1 reply; 5+ messages in thread
From: Andrew Burgess @ 2026-11-19 16:22 UTC (permalink / raw)
  To: YuPeng, gdb-patches; +Cc: tromey, debianyu

YuPeng <yupeng@kylinos.cn> writes:

> 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(+)

This will for sure need a test adding to the testsuite.  You could look
at gdb.python/py-breakpoint*.exp for inspiration.

>
> 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)

GDB style uses nullptr not NULL.

Thanks,
Andrew


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

* Re: [PATCH v2] Fix use-after-free when Python Breakpoint.stop method deletes the breakpoint
  2026-11-19 16:22 ` Andrew Burgess
@ 2026-11-24  2:54   ` YuPeng
  2026-11-24 11:26     ` Andrew Burgess
  0 siblings, 1 reply; 5+ messages in thread
From: YuPeng @ 2026-11-24  2:54 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 nullptr 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 nullptr.
- In gdbpy_breakpoint_cond_says_stop, keep a reference to the Python
  breakpoint object while calling its stop method.  Deleting the
  breakpoint drops the reference owned by the breakpoint object
  registry, which can free the Python object before the caller has
  finished using it.

Add a testsuite test in which a Python breakpoint deletes itself from its
stop method, and check that gdb does not crash and that the breakpoint
is gone afterwards.

Tested on x86_64-linux with an ASan build: without the fix gdb aborts
with a heap-use-after-free; with it the new test passes 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.

---

Andrew,

> This will for sure need a test adding to the testsuite.  You could look
> at gdb.python/py-breakpoint*.exp for inspiration.

You're right.  v2 adds gdb.python/py-breakpoint-delete.{c,exp,py}.  The
test creates a gdb.Breakpoint subclass whose stop method calls
self.delete() and returns False, then continues to the end of the
program and checks that the breakpoint is gone.  With an ASan build the
new test aborts with a heap-use-after-free before the fix and passes
with it applied.

Writing the test also turned up a second dangling reference: when the
stop method deletes the breakpoint, the breakpoint object registry
drops the last reference to the Python object while
gdbpy_breakpoint_cond_says_stop is still using it.  v2 therefore also
keeps a reference to the Python breakpoint object across the stop
call.  The new test hits this case as well.

> GDB style uses nullptr not NULL.

Fixed in v2, thanks.

Eli,

> Thanks, but could you tell if the patch and the description were
> generated by LLM, by any chance?

I prepared the patch and the description with the help of an AI
assistant, and I reviewed and tested the result myself.

Thanks,
YuPeng

 gdb/breakpoint.c                              | 14 +++++
 gdb/python/py-breakpoint.c                    |  6 +++
 .../gdb.python/py-breakpoint-delete.c         | 32 +++++++++++
 .../gdb.python/py-breakpoint-delete.exp       | 53 +++++++++++++++++++
 .../gdb.python/py-breakpoint-delete.py        | 28 ++++++++++
 5 files changed, 133 insertions(+)
 create mode 100644 gdb/testsuite/gdb.python/py-breakpoint-delete.c
 create mode 100644 gdb/testsuite/gdb.python/py-breakpoint-delete.exp
 create mode 100644 gdb/testsuite/gdb.python/py-breakpoint-delete.py

diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 0f173fa9fe6..9d9098b0881 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 == nullptr)
+    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 == nullptr)
+	    continue;
+	  b = bs->breakpoint_at;
+
 	  if (bs->stop)
 	    {
 	      ++(b->hit_count);
diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index 9621154224e..6d3f820119f 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -1235,6 +1235,12 @@ gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
 
   gdbpy_enter enter_py (b->gdbarch);
 
+  /* The stop method may delete the breakpoint, which drops the
+     reference held by the breakpoint object registry.  Keep the
+     object alive until we are done with it.  */
+  gdbpy_ref<gdbpy_breakpoint_object> bp_obj_ref
+    = gdbpy_ref<gdbpy_breakpoint_object>::new_reference (bp_obj);
+
   if (bp_obj->is_finish_bp)
     bpfinishpy_pre_stop_hook (bp_obj);
 
diff --git a/gdb/testsuite/gdb.python/py-breakpoint-delete.c b/gdb/testsuite/gdb.python/py-breakpoint-delete.c
new file mode 100644
index 00000000000..1dee55ee375
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-breakpoint-delete.c
@@ -0,0 +1,32 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright (C) 2026 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 <http://www.gnu.org/licenses/>.  */
+
+static int
+subroutine (int a)
+{
+  return a;
+}
+
+int
+main (void)
+{
+  int i;
+
+  for (i = 0; i < 5; i++)
+    subroutine (i);
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.python/py-breakpoint-delete.exp b/gdb/testsuite/gdb.python/py-breakpoint-delete.exp
new file mode 100644
index 00000000000..86eaf81e111
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-breakpoint-delete.exp
@@ -0,0 +1,53 @@
+# Copyright (C) 2026 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 <http://www.gnu.org/licenses/>.
+
+# Check that a Python Breakpoint.stop method can delete its own breakpoint
+# without leaving a dangling pointer behind (PR 34379).
+
+load_lib gdb-python.exp
+
+require allow_python_tests
+
+standard_testfile
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile]} {
+    return
+}
+
+if {![runto_main]} {
+    return
+}
+
+# Drop the breakpoint that runto_main left behind, so the counts below
+# only track the breakpoint created by the Python script.
+delete_breakpoints
+
+# For remote host testing.
+set pyfile [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py]
+
+gdb_test "python print(len(gdb.breakpoints()))" "0" \
+    "no breakpoints before sourcing the script"
+
+gdb_test "source $pyfile" "Python script imported" \
+    "source python script"
+
+gdb_test "python print(len(gdb.breakpoints()))" "1" \
+    "one breakpoint was created"
+
+# The stop method prints a message, hence allow extra output.
+gdb_continue_to_end "continue to the end of the program" "continue" 1
+
+gdb_test "python print(len(gdb.breakpoints()))" "0" \
+    "the breakpoint was deleted"
diff --git a/gdb/testsuite/gdb.python/py-breakpoint-delete.py b/gdb/testsuite/gdb.python/py-breakpoint-delete.py
new file mode 100644
index 00000000000..c0e59ced3d0
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-breakpoint-delete.py
@@ -0,0 +1,28 @@
+# Copyright (C) 2026 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 <http://www.gnu.org/licenses/>.
+
+import gdb
+
+
+class SelfDeletingBreakpoint(gdb.Breakpoint):
+    def stop(self):
+        print("Deleting breakpoint from its stop method.")
+        self.delete()
+        return False
+
+
+SelfDeletingBreakpoint("subroutine", internal=True)
+
+print("Python script imported")

base-commit: 633ae02c4ea8f87c670f7192804349860cfa291d
-- 
2.39.5

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

* Re: [PATCH v2] Fix use-after-free when Python Breakpoint.stop method deletes the breakpoint
  2026-11-24  2:54   ` [PATCH v2] " YuPeng
@ 2026-11-24 11:26     ` Andrew Burgess
  0 siblings, 0 replies; 5+ messages in thread
From: Andrew Burgess @ 2026-11-24 11:26 UTC (permalink / raw)
  To: YuPeng, gdb-patches; +Cc: tromey, debianyu

YuPeng <yupeng@kylinos.cn> writes:

> 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 nullptr 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 nullptr.
> - In gdbpy_breakpoint_cond_says_stop, keep a reference to the Python
>   breakpoint object while calling its stop method.  Deleting the
>   breakpoint drops the reference owned by the breakpoint object
>   registry, which can free the Python object before the caller has
>   finished using it.
>
> Add a testsuite test in which a Python breakpoint deletes itself from its
> stop method, and check that gdb does not crash and that the breakpoint
> is gone afterwards.
>
> Tested on x86_64-linux with an ASan build: without the fix gdb aborts
> with a heap-use-after-free; with it the new test passes 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.
>
> ---
>
> Andrew,
>
>> This will for sure need a test adding to the testsuite.  You could look
>> at gdb.python/py-breakpoint*.exp for inspiration.
>
> You're right.  v2 adds gdb.python/py-breakpoint-delete.{c,exp,py}.  The
> test creates a gdb.Breakpoint subclass whose stop method calls
> self.delete() and returns False, then continues to the end of the
> program and checks that the breakpoint is gone.  With an ASan build the
> new test aborts with a heap-use-after-free before the fix and passes
> with it applied.
>
> Writing the test also turned up a second dangling reference: when the
> stop method deletes the breakpoint, the breakpoint object registry
> drops the last reference to the Python object while
> gdbpy_breakpoint_cond_says_stop is still using it.  v2 therefore also
> keeps a reference to the Python breakpoint object across the stop
> call.  The new test hits this case as well.
>
>> GDB style uses nullptr not NULL.
>
> Fixed in v2, thanks.
>
> Eli,
>
>> Thanks, but could you tell if the patch and the description were
>> generated by LLM, by any chance?
>
> I prepared the patch and the description with the help of an AI
> assistant, and I reviewed and tested the result myself.

Thanks you for preparing this patch, and the updates with a test.

Unless I've missed a policy change, then unfortunately, if an AI has
written any significant part of this patch, it cannot currently be
accepted into GDB.

I really hope this position will change soon.

If I'm wrong about the current GDB position then hopefully someone will
jump in and correct me.

I know this is not the answer you would have hoped for.

Thanks,
Andrew


^ 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