[binutils-gdb] gdbserver: prevent buffer overrun for QTDPsrc packet
Andrew Burgess
aburgess@sourceware.org
Tue Dec 1 10:39:52 GMT 2026
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=bc5823da3ee0aca8b2bee26125d3a349b58bab0c
commit bc5823da3ee0aca8b2bee26125d3a349b58bab0c
Author: Andrew Burgess <aburgess@redhat.com>
Date: Thu Nov 12 10:59:54 2026 +0000
gdbserver: prevent buffer overrun for QTDPsrc packet
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 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 focused.
Additionally there are existing memory leaks in the code, buffers
allocated with xmalloc can be leaked if hex2bin throws an
exception (which it does if it encounters a non-hex character). These
leaks are resolved by a later patch in this series.
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
Reviewed-By: Keith Seitz <keiths@redhat.com>
Diff:
---
.../gdb.server/invalid-tracepoint-packets.exp | 140 +++++++++++++++++++++
gdbserver/tracepoint.cc | 29 ++++-
2 files changed, 167 insertions(+), 2 deletions(-)
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..9b585d4de46
--- /dev/null
+++ b/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp
@@ -0,0 +1,140 @@
+# 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 ""]
+ lassign $res gdbserver_protocol gdbserver_gdbport
+ 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 87b0c04d78e..e8ec8ad68f1 100644
--- a/gdbserver/tracepoint.cc
+++ b/gdbserver/tracepoint.cc
@@ -2556,15 +2556,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;
More information about the Gdb-cvs
mailing list