From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH 2/5] gdbserver: prevent buffer overrun for QTDPsrc packet
Date: Mon, 16 Nov 2026 11:10:08 +0000 [thread overview]
Message-ID: <a821514b3e7196d3006866b0204329a49cc30942.1794827178.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1794827178.git.aburgess@redhat.com>
The QTDPsrc packet format is:
QTDPsrc:N:ADDR:TYPE:START:SLEN:BYTES
The BYTES is a hex encoded string. The START and SLEN allow for
strings that are longer than a single packet, START is the position of
BYTES within the full string, and SLEN is the length of the full
string.
Critically, the hex decoding of BYTES might not equal SLEN.
The current handling of QTDPsrc makes two errors: it allocates a
buffer based on SLEN. A malicious incoming packet could include an
arbitrarily large SLEN value causing gdbserver to allocate a huge
amount of memory. This would either cause the gdbserver process to
terminate with an out of memory error, or might bring down the machine
due to memory pressure.
The second problem is that gdbserver converts bytes from the packet
into the allocated buffer using a call to hex2bin, and uses the packet
length as the limit on the number of bytes translated. This means
that if SLEN is less than the length of the hex decoded string,
gdbserver will overflow the allocated buffer and write to arbitrary
memory.
This commit makes the following changes:
1. Ensure that SLEN matches the length of the hex decoded BYTES
string.
I did consider just ignoring SLEN, but that means gdbserver would
accept invalid packets, e.g. SLEN less than the length of the hex
decoded BYTES string. And that seemed like a bad idea.
2. Allocate the buffer, and cap the hex2bin call based on the length
of the hex decoded BYTES string, not SLEN. This means that if
the rules for SLEN change in the future the buffer will be the
right size to decode BYTES.
One point of interest is that I added a call `xfree (srctype)` on the
error path. This avoids leaking a previously allocated buffer. This
is temporary, a later commit in this series switches the buffer to use
gdb::unique_xmalloc_ptr<char> instead and the xfree will be removed.
Adding the xfree now just means this commit can be more focussed.
The header comment in the new test script, and the name of the script
itself, both indicate that this script is for testing more than the
QTDPsrc packet. Right now this is not the case, but later commits in
this series will add more tests. I've left the header comment as
generic text that talks about testing multiple tracepoint packets to
avoid churn in the next commit.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34968
---
.../gdb.server/invalid-tracepoint-packets.exp | 141 ++++++++++++++++++
gdbserver/tracepoint.cc | 29 +++-
2 files changed, 168 insertions(+), 2 deletions(-)
create mode 100644 gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp
diff --git a/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp b/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp
new file mode 100644
index 00000000000..d82647cbe63
--- /dev/null
+++ b/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp
@@ -0,0 +1,141 @@
+# This testcase is part of GDB, the GNU debugger.
+#
+# 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 some tracepoint related packets, focusing mostly on error
+# handling within gdbserver. This does not test every tracepoint
+# related packet. This test was written while fixing some bugs in
+# various packets and the tests were only added for packets being
+# fixed.
+#
+# Start gdbserver and connect. Use 'maint packet ...' to send
+# tracepoint related packets to gdbserver. Some of these packets are
+# valid, others are invalid.
+#
+# This manual packet sending is used in order to be able to send the
+# invalid packets.
+
+load_lib gdbserver-support.exp
+
+require allow_gdbserver_tests
+
+standard_testfile normal.c
+
+if {[build_executable "failed to build" $testfile $srcfile]} {
+ return
+}
+
+# Some global regexp used in the tests below.
+set error_re "\r\nreceived: \"E01\""
+set ok_re "\r\nreceived: \"OK\""
+
+# Start GDB and gdbserver ready for the test. Return false if
+# anything goes wrong, otherwise, return true.
+proc start_gdbserver_for_test {} {
+ clean_restart $::testfile
+
+ # Make sure we're disconnected, in case we're testing with an
+ # extended-remote board, therefore already connected.
+ gdb_test "disconnect" ".*"
+
+ # Start gdbserver and connect.
+ set res [gdbserver_spawn ""]
+ set gdbserver_protocol [lindex $res 0]
+ set gdbserver_gdbport [lindex $res 1]
+ set res [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
+ if {![gdb_assert {$res == 0} "connect"]} {
+ return false
+ }
+
+ return true
+}
+
+# Test the QTDPsrc packet.
+proc_with_prefix test_qtdpsrc {} {
+ if {![start_gdbserver_for_test]} {
+ return
+ }
+
+ # Disassemble main and find an address at which we could place a
+ # tracepoint.
+ set addresses {}
+ gdb_test_multiple "disassemble main" "" -lbl {
+ -re "\r\n\\s+0x0*(\[0-9a-f\]+)\\s+\[^\r\n\]+(?=\r\n)" {
+ set addr $expect_out(1,string)
+ lappend addresses $addr
+ exp_continue
+ }
+
+ -re "^\r\n$::gdb_prompt $" {
+ gdb_assert {[llength $addresses] > 0} \
+ "found instruction addresses"
+ }
+ }
+
+ # We need at least one address in main for this test.
+ if {[llength $addresses] < 1} {
+ unsupported "not enough instruction addresses in main"
+ gdbserver_exit false
+ return
+ }
+
+ # Record the address we'll use in this test.
+ set addr [lindex $addresses 0]
+
+ # Create a tracepoint at ADDR. Do this manually as the rest of this
+ # test also does manual packet manipulation.
+ gdb_test_multiple "maint packet QTDP:1:$addr:D:1:1" "create tracepoint" {
+ -re -wrap "\r\nreceived: \"OK\"" {
+ pass $gdb_test_name
+ }
+
+ -re -wrap "" {
+ unsupported $gdb_test_name
+ gdbserver_exit false
+ return
+ }
+ }
+
+ # Now send invalid QTDPsrc packets at gdbserver and check they are all
+ # rejected.
+ set prefix "QTDPsrc:1:$addr:cond:0"
+
+ # Undersized strings. The packet declares that it has a string
+ # containing 2 bytes, but COUNT is the actual number of nibbles
+ # supplied.
+ foreach count {0 1 2 3} {
+ gdb_test "maint packet ${prefix}:2:[string repeat 1 $count]" \
+ $::error_re "string of $count nibbles and declared length 2 bytes"
+ }
+
+ # Packets with oversized strings of various sizes. The packet
+ # declares that it has a string containing 1 byte, but COUNT is the
+ # actual number of nibbles supplied.
+ foreach count {3 100 500 1000 2000 4000} {
+ gdb_test "maint packet ${prefix}:1:[string repeat 1 $count]" \
+ $::error_re "string of $count nibbles and declared length 1 byte"
+ }
+
+ # Test some valid packets.
+ gdb_test "maint packet ${prefix}:1:41" $::ok_re "valid string accepted"
+ gdb_test "maint packet ${prefix}:3:414243" $::ok_re \
+ "valid multi-byte string accepted"
+ gdb_test "maint packet ${prefix}:0:" $::ok_re "empty string accepted"
+
+ gdbserver_exit false
+}
+
+test_qtdpsrc
diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc
index e7c1f0c9e7f..00cbec8c8da 100644
--- a/gdbserver/tracepoint.cc
+++ b/gdbserver/tracepoint.cc
@@ -2532,15 +2532,40 @@ cmd_qtdpsrc (char *own_buf)
srctype[packet - start_of_type] = '\0';
++packet;
+ /* The QTDPsrc spec says that SLEN can be longer than the delivered string
+ in order to support strings longer than can fit in a single packet.
+ The START indicates where the string from this packet fits into the
+ overall string.
+
+ However, gdbserver has never (yet) supported strings larger than a
+ single packet, which is why the START field here is ignored, and why we
+ check SLEN against the remaining packet length. */
ULONGEST start, slen;
packet = unpack_varlen_hex (packet, &start);
++packet; /* skip a colon */
packet = unpack_varlen_hex (packet, &slen);
++packet; /* skip a colon */
- char *src = (char *) xmalloc (slen + 1);
- size_t nbytes = hex2bin (packet, (gdb_byte *) src, strlen (packet) / 2);
+
+ size_t len = strlen (packet);
+ if (slen != len / 2 || len % 2 != 0)
+ {
+ trace_debug ("Tracepoint error: invalid string length %s in: %s",
+ pulongest (slen), own_buf);
+ write_enn (own_buf);
+ xfree (srctype);
+ return;
+ }
+
+ char *src = (char *) xmalloc (len / 2 + 1);
+ size_t nbytes = hex2bin (packet, (gdb_byte *) src,
+ static_cast<int> (len / 2));
src[nbytes] = '\0';
+ /* Because we check SLEN against the packet length above, either the packet
+ is fully parsed, or hex2bin will have thrown an error on seeing a
+ non-hex character. */
+ gdb_assert (packet[nbytes * 2] == '\0');
+
struct source_string *newlast = XNEW (struct source_string);
newlast->type = srctype;
newlast->str = src;
--
2.25.4
next prev parent reply other threads:[~2026-11-16 11:11 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 11:10 [PATCH 0/5] gdbserver: fix buffer overflow issue in " Andrew Burgess
2026-11-16 11:10 ` [PATCH 1/5] gdbserver: clean up some older code in tracepoint.cc Andrew Burgess
2026-11-16 11:10 ` Andrew Burgess [this message]
2026-11-19 19:51 ` [PATCH 2/5] gdbserver: prevent buffer overrun for QTDPsrc packet Keith Seitz
2026-11-23 17:15 ` Andrew Burgess
2026-11-23 17:57 ` Keith Seitz
2026-11-16 11:10 ` [PATCH 3/5] gdbserver: improve packet parsing in tracepoint.cc Andrew Burgess
2026-11-16 11:10 ` [PATCH 4/5] gdbserver: don't leak trace variable names if reusing variable numbers Andrew Burgess
2026-11-16 11:10 ` [PATCH 5/5] gdbserver: use unique_xmalloc_ptr to avoid memory leaks Andrew Burgess
2026-11-19 19:52 ` [PATCH 0/5] gdbserver: fix buffer overflow issue in QTDPsrc packet Keith Seitz
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=a821514b3e7196d3006866b0204329a49cc30942.1794827178.git.aburgess@redhat.com \
--to=aburgess@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