From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id nXBeIqD8BGvC0jAAWB0awg (envelope-from ) for ; Mon, 23 Nov 2026 21:55:28 -0500 Received: by simark.ca (Postfix, from userid 112) id 7CCFE1E023; Mon, 23 Nov 2026 21:55:28 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 98DCB1E023 for ; Mon, 23 Nov 2026 21:55:25 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AAF9C475ACB7 for ; Tue, 24 Nov 2026 02:55:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AAF9C475ACB7 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) by sourceware.org (Postfix) with ESMTPS id 9325544532F2; Tue, 24 Nov 2026 02:54:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9325544532F2 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=kylinos.cn ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9325544532F2 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795488896; cv=none; b=diJulnYPvK5UZQNf2TBg9McgnhO+dmjI9HdDBjdP75DzKmvXTJCtIbG8YL+6SKcZfInAQ4kqt10RJ/QoEu7iFtIxnMskALKo3KycL8a2BMMIfTo1iYMtTj7Tuun6NGkRwfj0hOoqnU1iYfzJMG9HbVSFYglJlVIs4AYbSYV7uPo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795488896; c=relaxed/simple; bh=ensKTf3KwmVOrKPy3ruWbeDZ68L1PN6HJZC5wsz93Ks=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=oziFibLe2OTvSSAU77nBRFwEK0e/Vd1dA2ax+d8vN/DdJrTTPyVZo4Vff8GAFgFGqoNp1p0ZM8numHmYAtX81nyK5KZ6q1945kPc+qJ1oINgio/H+9FcUmnQJo1S/GZkiSnxnivqCf3p775JhnPOdKd+ctMk9iLN7GX7AGQz7E0= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9325544532F2 X-UUID: 651335e2e7b211f19a56ed5b684f684d-20261124 X-CID-UNFAMILIAR: 1 X-CID-O-RULE: Release_Ham X-CID-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.19, REQID:badb2965-3c2b-48ce-9598-b16becd3e88c, IP:10, URL:0,TC:0,Content:0,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:10 X-CID-INFO: VERSION:1.3.19, REQID:badb2965-3c2b-48ce-9598-b16becd3e88c, IP:10, UR L:0,TC:0,Content:0,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION:r elease,TS:10 X-CID-META: VersionHash:7db8b62, CLOUDID:e6cee8dd32f413c614c2c40418beda97, BulkI D:2611241054456VGDEOCY,BulkQuantity:0,SF:16|19|66|78|81|82|83|102|127|153| 865|898,TC:nil,Content:0|15|50|99,EDM:-3,IP:-2,URL:99|97|1,File:nil,RT:nil ,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,B RR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR,TF_CID_SPAM_USA,TF_CID_SPAM_FSD,TF_CID_SPAM_ULS X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 651335e2e7b211f19a56ed5b684f684d-20261124 X-User: yupeng@kylinos.cn Received: from send-mail.fpxfiu [(223.104.41.125)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA with TLSv1.3 TLS_AES_256_GCM_SHA384 256/256) with ESMTP id 1523629068; Tue, 24 Nov 2026 10:54:41 +0800 From: YuPeng 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 Message-ID: <1795488876766726028.9924@kylinos.cn> In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org 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 (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 bp_obj_ref + = gdbpy_ref::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 . */ + +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 . + +# 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 . + +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