Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH 3/5] gdbserver: fix buffer overflow in more m/x packet handling
Date: Fri, 20 Nov 2026 15:12:48 +0000	[thread overview]
Message-ID: <7ec90ad7624f32c6edc9298eb067092c6b9631ee.1795186852.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1795186852.git.aburgess@redhat.com>

Building on the work from two patches back, this commit addresses more
buffer overflow issues with 'm' and 'x' packet handling.  The function
handle_limited_packets_while_waiting (in gdbserver/remote-utils.cc)
contains a loop which handles a small sub-set of packets, including 'm'
and 'x'.

Both the 'm' and 'x' packets include a length field which declares how
much data should be read from the inferior.  This length is used,
unchecked, to allocate a buffer in gdbserver.  A malicious, or buggy
debug client could ask for a very large read which would cause gdbserver
to go out of memory trying to allocate the buffer.

If the buffer allocation succeeds then the result is copied into the
client_state's own_buf buffer, which is 'PBUFSIZ + 1' bytes in size.
For the 'm' packet the amount written into own_buf is determined by the
size the client requested, which means we could overflow own_buf with
content read from the inferior's memory.

This commit fixes these issues.  For the 'm' packet we only support
lengths up to PBUFSIZ / 2.  Each byte read from the inferior is encoded
as two hex characters, so trying to read any more from the inferior will
overflow own_buf.  This limit is imposed before we try to allocate the
temporary buffer, so we know the requested buffer size will be
reasonable.

For the 'x' packet we only allow reads of up to PBUFSIZ bytes.  This
limit means that the allocated buffer size will be reasonable.  The 'x'
packet code already protected writes into own_buf so overflow was never
a problem in this case.

I've used gdb::unique_xmalloc_ptr to handle the buffer allocations for
'm' and 'x' packets.  This means that for the 'X' and 'M' packets, which
still manage their buffers with xmalloc and free, the MEM_BUF variable
is pushed down into the specific case block for each packet type.  I
also used SCOPE_EXIT to ensure the buffers are always freed for these
two packets.

There's an extension to the gdb.server/mx-packet-overflow.exp test which
manually sends a qSymbol packet to gdbserver to enter
handle_limited_packets_while_waiting, then sends various m and x packets
to test their handling.

It is worth noting that the 'M' and 'X' packet handling performed by
decode_M_packet and decode_X_packet also have issues with trusting
client supplied lengths which could lead to excessive memory allocation
and buffer overflow.  These will be addressed in a follow on commit.
---
 .../gdb.server/mx-packet-overflow.exp         | 40 ++++++++++++++
 gdbserver/remote-utils.cc                     | 54 ++++++++++++++++---
 2 files changed, 86 insertions(+), 8 deletions(-)

diff --git a/gdb/testsuite/gdb.server/mx-packet-overflow.exp b/gdb/testsuite/gdb.server/mx-packet-overflow.exp
index 09cb4a4cb83..3e5f97872d0 100644
--- a/gdb/testsuite/gdb.server/mx-packet-overflow.exp
+++ b/gdb/testsuite/gdb.server/mx-packet-overflow.exp
@@ -177,3 +177,43 @@ proc run_mx_packet_tests {} {
 
 # Run the m/x packet tests.
 run_mx_packet_tests
+
+# Test within a qSymbol loop.  Send a 'qSymbol::' packet to gdbserver.
+# It should either reply with 'qSymbol:<name>' or 'OK'.  If we get OK
+# then gdbserver doesn't want to know any symbols from GDB, so we
+# cannot get access to its inner packet processing loop.  If we get
+# back a symbol request though, then we can send some m/x packets to
+# test the inner packet processing loop.
+set first_symbol_requested ""
+gdb_test_multiple "maint packet qSymbol::" "start qSymbol loop" {
+    -re -wrap "\r\nreceived: \"OK\"" {
+	unsupported "$gdb_test_name (no symbols requested)"
+    }
+
+    -re -wrap "\r\nreceived: \"qSymbol:(\[^\"\]+)\"" {
+	set first_symbol_requested $expect_out(1,string)
+	pass $gdb_test_name
+    }
+}
+
+# We got a symbol request from gdbserver; run the m/x packet tests
+# then, complete the qSymbol loop.
+if { $first_symbol_requested ne "" } {
+    with_test_prefix "in qSymbol loop" {
+	run_mx_packet_tests
+    }
+
+    set symbol_name $first_symbol_requested
+    gdb_test_multiple "maint packet qSymbol::$symbol_name" \
+	"qSymbol loop completed" {
+	    -re -wrap "\r\nreceived: \"OK\"" {
+		pass $gdb_test_name
+	    }
+
+	    -re -wrap "\r\nreceived: \"qSymbol:(\[^\"\]+)\"" {
+		set symbol_name $expect_out(1,string)
+		send_gdb "maint packet qSymbol::$symbol_name\n"
+		exp_continue
+	    }
+	}
+}
diff --git a/gdbserver/remote-utils.cc b/gdbserver/remote-utils.cc
index d1b17a33e2b..0d600e8844d 100644
--- a/gdbserver/remote-utils.cc
+++ b/gdbserver/remote-utils.cc
@@ -1454,7 +1454,6 @@ handle_limited_packets_while_waiting ()
 	return false;
 
       CORE_ADDR mem_addr;
-      unsigned char *mem_buf = nullptr;
       unsigned int mem_len;
       int new_len = -1;
 
@@ -1463,9 +1462,24 @@ handle_limited_packets_while_waiting ()
 	case 'm':
 	  {
 	    decode_m_packet (&cs.own_buf[1], &mem_addr, &mem_len);
-	    mem_buf = (unsigned char *) xmalloc (mem_len);
-	    if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
-	      bin2hex (mem_buf, cs.own_buf, mem_len);
+	    /* MEM_LEN will be used to allocate a buffer, MEM_BUF, which is
+	       filled from inferior memory.  The bin2hex call converts each
+	       byte in MEM_BUF into two hex characters, doubling the number
+	       of bytes required.  This result is written to the
+	       client_state's OWN_BUF, which is PBUFSIZ in length (+1 for a
+	       trailing null).  We can therefore only read half a packet's
+	       worth of bytes from the inferior memory; otherwise we will
+	       overflow OWN_BUF.  */
+	    if (mem_len > PBUFSIZ / 2)
+	      {
+		write_enn (cs.own_buf);
+		break;
+	      }
+
+	    gdb::unique_xmalloc_ptr<unsigned char> mem_buf
+	      ((unsigned char *) xmalloc (mem_len));
+	    if (read_inferior_memory (mem_addr, mem_buf.get (), mem_len) == 0)
+	      bin2hex (mem_buf.get (), cs.own_buf, mem_len);
 	    else
 	      write_enn (cs.own_buf);
 	  }
@@ -1473,14 +1487,35 @@ handle_limited_packets_while_waiting ()
 	case 'x':
 	  {
 	    decode_x_packet (&cs.own_buf[1], &mem_addr, &mem_len);
-	    mem_buf = (unsigned char *) xmalloc (mem_len);
-	    if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
+
+	    /* MEM_LEN will be used to allocate a buffer, MEM_BUF, which is
+	       filled from inferior memory.  This buffer is then escaped
+	       (see remote_escape_output) into the client_state's OWN_BUF,
+	       which is PBUFSIZ in length (+1 for the leading 'b').  The
+	       escaping never reduces the number of bytes needed, so a
+	       request for more than PBUFSIZ bytes could never produce a
+	       complete reply.
+
+	       GDB usually only requests small reads, a few bytes at a time.
+	       So, limiting MEM_LEN to PBUFSIZ will easily handle everything
+	       GDB sends us, and allows other debug clients space to ask for
+	       larger reads if they need it, but still places a sane limit
+	       on the size of buffer we are about to allocate.  */
+	    if (mem_len > PBUFSIZ)
+	      {
+		write_enn (cs.own_buf);
+		break;
+	      }
+
+	    gdb::unique_xmalloc_ptr<unsigned char> mem_buf
+	      ((unsigned char *) xmalloc (mem_len));
+	    if (read_inferior_memory (mem_addr, mem_buf.get (), mem_len) == 0)
 	      {
 		gdb_byte *buffer = (gdb_byte *) cs.own_buf;
 		*buffer++ = 'b';
 
 		int out_len_units;
-		new_len = remote_escape_output (mem_buf, mem_len, 1,
+		new_len = remote_escape_output (mem_buf.get (), mem_len, 1,
 						buffer,
 						&out_len_units,
 						PBUFSIZ);
@@ -1500,6 +1535,8 @@ handle_limited_packets_while_waiting ()
 	  break;
 	case 'X':
 	  {
+	    unsigned char *mem_buf = nullptr;
+	    SCOPE_EXIT { free (mem_buf); };
 	    if (decode_X_packet (&cs.own_buf[1], len - 1, &mem_addr,
 				 &mem_len, &mem_buf) < 0
 		|| target_write_memory (mem_addr, mem_buf, mem_len) != 0)
@@ -1510,6 +1547,8 @@ handle_limited_packets_while_waiting ()
 	  break;
 	case 'M':
 	  {
+	    unsigned char *mem_buf = nullptr;
+	    SCOPE_EXIT { free (mem_buf); };
 	    decode_M_packet (&cs.own_buf[1], &mem_addr, &mem_len, &mem_buf);
 	    if (target_write_memory (mem_addr, mem_buf, mem_len) == 0)
 	      write_ok (cs.own_buf);
@@ -1535,7 +1574,6 @@ handle_limited_packets_while_waiting ()
 	  leave_packet_handling_loop = true;
 	  break;
 	}
-      free (mem_buf);
 
       if (!leave_packet_handling_loop)
 	{
-- 
2.56.0


  parent reply	other threads:[~2026-11-20 15:15 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 15:12 [PATCH 0/5] gdbserver: fix memory bugs in x, X, m, and M " Andrew Burgess
2026-11-20 15:12 ` [PATCH 1/5] gdbserver: avoid buffer overflow from 'm' and 'x' packets Andrew Burgess
2026-11-20 15:37   ` Eli Zaretskii
2026-11-23  5:26   ` Kevin Buettner
2026-11-20 15:12 ` [PATCH 2/5] gdbserver: merge look_up_one_symbol and relocate_instruction packet handling Andrew Burgess
2026-11-25  5:55   ` Kevin Buettner
2026-11-26 14:58     ` Andrew Burgess
2026-11-20 15:12 ` Andrew Burgess [this message]
2026-11-25  7:24   ` [PATCH 3/5] gdbserver: fix buffer overflow in more m/x " Kevin Buettner
2026-11-20 15:12 ` [PATCH 4/5] gdbserver: reject X packets with an invalid declared length Andrew Burgess
2026-11-25  7:48   ` Kevin Buettner
2026-11-25  7:48   ` Kevin Buettner
2026-11-20 15:12 ` [PATCH 5/5] gdbserver: reject M " Andrew Burgess
2026-11-25  8:03   ` Kevin Buettner

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=7ec90ad7624f32c6edc9298eb067092c6b9631ee.1795186852.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