Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Kevin Buettner <kevinb@redhat.com>
To: gdb-patches@sourceware.org
Cc: Kevin Buettner <kevinb@redhat.com>
Subject: [PATCH 2/2] gdb/testsuite: test malformed agent expressions sent to gdbserver
Date: Tue, 10 Nov 2026 00:07:54 -0700	[thread overview]
Message-ID: <20261110074218.1600179-3-kevinb@redhat.com> (raw)
In-Reply-To: <20261110074218.1600179-1-kevinb@redhat.com>

Add gdb.server/agent-expr-length.exp, which uses "maintenance packet"
to send gdbserver agent expressions whose declared length exceeds the
hex data present in the packet, or which lack the comma after the
length.  They are sent as breakpoint conditions and commands (Z0) and
as tracepoint conditions and actions (QTDP).  The test checks that
gdbserver survives each one and, by reading the tracepoint definitions
back with qTfP/qTsP, that the malformed parts are dropped instead of
being stored with an uninitialized tail, while the well-formed
conditions and actions alongside them are kept.

Against a gdbserver without the previous commit's fix, the test shows
the heap disclosure (a tracepoint condition read back with 63 bytes of
heap contents beyond the single byte sent), and gdbserver crashes on
the expressions with a huge declared length.

This is a test for CVE-2026-121996.

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

Assisted-by: LLM
---
 .../gdb.server/agent-expr-length.exp          | 188 ++++++++++++++++++
 1 file changed, 188 insertions(+)
 create mode 100644 gdb/testsuite/gdb.server/agent-expr-length.exp

diff --git a/gdb/testsuite/gdb.server/agent-expr-length.exp b/gdb/testsuite/gdb.server/agent-expr-length.exp
new file mode 100644
index 00000000000..f9c85fa5190
--- /dev/null
+++ b/gdb/testsuite/gdb.server/agent-expr-length.exp
@@ -0,0 +1,188 @@
+# Copyright 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/>.
+
+# Test that gdbserver rejects agent expressions ("X<len>,<hex>") whose
+# declared length exceeds the hex data present in the packet, or which
+# lack the comma after the length (CVE-2026-121996).  Such expressions
+# used to leave the tail of the expression's buffer uninitialized,
+# which was then sent back to GDB as part of a tracepoint condition,
+# and to advance gdbserver's packet parser past the end of the packet,
+# crashing gdbserver.
+#
+# GDB never sends such an expression itself, so the packets are sent
+# with "maintenance packet".  Each case uses a fresh gdbserver, so
+# that a crash in one does not hide the results of the others.
+
+load_lib gdbserver-support.exp
+
+require allow_gdbserver_tests
+
+standard_testfile normal.c
+
+if {[build_executable "failed to prepare" $testfile $srcfile]} {
+    return
+}
+
+# Malformed expressions to try: one declaring 0x40 bytes but carrying
+# only one, one whose declared length would move the parse pointer far
+# beyond the end of the packet buffer, and one without the comma.
+set bad_exprs {"X40,ab" "X10000000,ab" "X40"}
+
+# Start GDB and a new gdbserver, and connect them.  Return true on
+# success.
+
+proc start_gdbserver {} {
+    clean_restart $::testfile
+
+    # Make sure we're disconnected, in case we're testing with an
+    # extended-remote board, therefore already connected.
+    gdb_test "disconnect" ".*"
+
+    if {[gdbserver_run ""] != 0} {
+	fail "connect to gdbserver"
+	return false
+    }
+    return true
+}
+
+# Send PACKET to gdbserver and return its reply, or the empty string
+# if no reply was seen.  TEST is the test name.
+
+proc send_packet { packet test } {
+    set reply ""
+    gdb_test_multiple "maintenance packet $packet" $test {
+	-re -wrap "received: \"(\[^\r\n\]*)\"" {
+	    set reply $expect_out(1,string)
+	    pass $gdb_test_name
+	}
+    }
+    return $reply
+}
+
+# Send PACKET to gdbserver, expecting an "OK" reply.
+
+proc send_packet_ok { packet test } {
+    gdb_test "maintenance packet $packet" "received: \"OK\"" $test
+}
+
+# Return true if gdbserver supports tracepoints.
+
+proc supports_tracepoints {} {
+    if {![start_gdbserver]} {
+	return false
+    }
+    return [expr {[send_packet "qTStatus" "query trace status"] != ""}]
+}
+
+# Insert a breakpoint at main whose condition or commands (according to
+# KIND) are the malformed expression EXPR.  The expression must be
+# rejected without disturbing gdbserver; the breakpoint itself is still
+# inserted.
+
+proc test_breakpoint { kind expr } {
+    if {![start_gdbserver]} {
+	return
+    }
+
+    set addr [format %x [get_hexadecimal_valueof "&main" 0]]
+    if {$kind == "condition"} {
+	set option $expr
+    } else {
+	set option "cmds:0,$expr"
+    }
+
+    send_packet_ok "Z0,$addr,1;$option" "insert breakpoint"
+    send_packet_ok "z0,$addr,1" "remove breakpoint"
+}
+
+# Send the tracepoint definition PACKETS to gdbserver, then read the
+# definitions back, and check that they match EXPECTED.  The
+# malformed parts must not appear, in particular not with the heap
+# contents an uninitialized buffer would contain.
+
+proc test_tracepoints { packets expected } {
+    if {![start_gdbserver]} {
+	return
+    }
+
+    send_packet_ok "QTinit" "clear tracepoints"
+    set i 0
+    foreach packet $packets {
+	incr i
+	send_packet_ok $packet "define tracepoint piece $i"
+    }
+
+    set pieces {}
+    set reply [send_packet "qTfP" "read first tracepoint piece"]
+    set i 0
+    while {$reply != "" && $reply != "l" && $i < 20} {
+	lappend pieces $reply
+	incr i
+	set reply [send_packet "qTsP" "read subsequent tracepoint piece $i"]
+    }
+
+    gdb_assert {$pieces eq $expected} "tracepoint definitions"
+}
+
+foreach_with_prefix kind {"condition" "commands"} {
+    foreach_with_prefix expr $bad_exprs {
+	test_breakpoint $kind $expr
+    }
+}
+
+if {![supports_tracepoints]} {
+    unsupported "gdbserver does not support tracepoints"
+    return
+}
+
+# Malformed tracepoint conditions are dropped.
+
+foreach_with_prefix expr $bad_exprs {
+    with_test_prefix "condition" {
+	test_tracepoints \
+	    [list \
+		 "QTDP:1:1000:E:1:1:X1,ab" \
+		 "QTDP:2:2000:E:1:1:$expr"] \
+	    [list \
+		 "T1:1000:E:1:1:X1,ab" \
+		 "T2:2000:E:1:1"]
+    }
+}
+
+# The parser must find its way past a malformed condition to any
+# following field, here a good condition.
+
+foreach_with_prefix expr $bad_exprs {
+    with_test_prefix "condition, then good condition" {
+	test_tracepoints \
+	    [list "QTDP:1:1000:E:1:1:$expr:X1,cd"] \
+	    [list "T1:1000:E:1:1:X1,cd"]
+    }
+}
+
+# Malformed tracepoint actions are dropped, leaving the good ones.
+
+foreach_with_prefix expr $bad_exprs {
+    with_test_prefix "action" {
+	test_tracepoints \
+	    [list \
+		 "QTDP:1:1000:E:1:1-" \
+		 "QTDP:-1:1000:X1,ab-" \
+		 "QTDP:-1:1000:$expr"] \
+	    [list \
+		 "T1:1000:E:1:1" \
+		 "A1:1000:X1,ab"]
+    }
+}
-- 
2.56.0


  parent reply	other threads:[~2026-11-10  7:44 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-10  7:07 [PATCH 0/2] PR gdb/server/34969 Kevin Buettner
2026-11-10  7:07 ` [PATCH 1/2] gdbserver: reject malformed agent expressions in gdb_parse_agent_expr Kevin Buettner
2026-11-10  7:07 ` Kevin Buettner [this message]
2026-11-11 12:36   ` [PATCH 2/2] gdb/testsuite: test malformed agent expressions sent to gdbserver Andrew Burgess
2026-11-10 12:58 ` [PATCH 0/2] PR gdb/server/34969 Eli Zaretskii
2026-11-10 15:15   ` Simon Marchi
2026-11-11  6:13   ` Kevin Buettner
2026-11-11 12:37     ` Eli Zaretskii

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=20261110074218.1600179-3-kevinb@redhat.com \
    --to=kevinb@redhat.com \
    --cc=gdb-patches@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