Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: YuPeng <yupeng@kylinos.cn>
To: gdb-patches@sourceware.org
Cc: tromey@sourceware.org,debianyu@163.com
Subject: Re: [PATCH v2] Fix use-after-free when Python Breakpoint.stop method deletes the breakpoint
Date: Tue, 24 Nov 2026 10:54:36 +0800	[thread overview]
Message-ID: <1795488876766726028.9924@kylinos.cn> (raw)
In-Reply-To: <xhuyf8q2oiyep.fsf@aburgess-thinkpadp1gen8.rmtgb.csb>

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

  reply	other threads:[~2026-11-24  2:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 10:34 [PATCH] " YuPeng
2026-11-18 14:26 ` Eli Zaretskii
2026-11-19 16:22 ` Andrew Burgess
2026-11-24  2:54   ` YuPeng [this message]
2026-11-24 11:26     ` [PATCH v2] " Andrew Burgess

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1795488876766726028.9924@kylinos.cn \
    --to=yupeng@kylinos.cn \
    --cc=debianyu@163.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tromey@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox