Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Kevin Buettner <kevinb@redhat.com>
To: Andrew Burgess <aburgess@redhat.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH 5/5] gdbserver: reject M packets with an invalid declared length
Date: Wed, 25 Nov 2026 01:03:41 -0700	[thread overview]
Message-ID: <cfb22f3b-e835-4878-a874-c22321ef77cd@redhat.com> (raw)
In-Reply-To: <99d2250b206ab3fc3a5d404a08464ef1315e4fd5.1795186852.git.aburgess@redhat.com>

Just one nit involving a comment in this one.

On 11/20/26 8:12 AM, Andrew Burgess wrote:
> The decode_M_packet function in gdbserver/remote-utils.cc decodes
> incoming 'M' packets.  Each of these packets includes a declared length
> that says how much data the packet is carrying.  This declared length
> was used, unchecked, in two places: to allocate a buffer to hold the
> packet's data, when the caller passes a null pointer for the buffer
> argument, TO_P, and as the number of bytes to write into the inferior in
> handle_limited_packets_while_waiting and process_serial_event.
> 
> In handle_limited_packets_while_waiting we pass a null TO_P to
> decode_M_packet. The handle_limited_packets_while_waiting function is
> the loop that handles packets while gdbserver waits for the reply to a
> qSymbol or qRelocInsn query.  A malicious, or buggy, debug client could
> reply to a qSymbol query with an M packet that declares a huge length,
> for example 'M<addr>,ffffffff:0a0b0c0d', while only actually sending
> four bytes of data.  This would cause gdbserver to try to allocate a ~4
> GB buffer, at which point gdbserver would either die with an out of
> memory error, or slow the machine down through memory pressure.
> 
> If the declared length in the packet was longer than the provided data,
> but not enough to crash gdbserver then in decode_M_packet the return
> value from hex2bin, which says how many bytes were actually decoded, was
> ignored.  The remaining bytes within the allocated buffer would be
> uninitialised (containing whatever old content happened to be there),
> and this would then be written into the inferior.
> 
> In this commit, decode_M_packet now rejects any packet for which the
> declared length is not exactly half the number of bytes remaining in the
> packet after the header, or for which the remaining number of bytes is
> odd.  This is a stricter check than the equivalent for the 'X' packet,
> which only requires that the declared length be no greater than the
> remaining bytes; the difference is that the data in an M packet is
> always encoded as exactly two hex characters per byte, with no escaping,
> so the payload length of a well formed packet clearly indicates the
> actual data length.
> 
> Performing this check before the buffer allocation means the size of the
> allocation is now bounded by the size of the incoming packet, and, as
> the decoded length is now known to match the declared length, the
> uninitialized memory problem is closed too.  A payload that contains
> non-hex characters still causes hex2bin to throw an error, which causes
> gdbserver to return an error and reset the connection.  This solution is
> not ideal, and should probably be adjusted, but I don't plan to work on
> that in this series.
> 
> To go with the new checks, decode_M_packet now returns a bool to say if
> the packet was decoded successfully or not, and both callers have been
> updated so that the inferior memory is only written when the packet was
> accepted.  The function's comment has moved into remote-utils.h to sit
> beside the declaration.
> 
> The gdb.server/mx-packet-overflow.exp test is extended: the
> run_X_packet_tests proc has been renamed to run_MX_packet_tests and now
> also sends M packets.  As with the previous commit, the tests are run
> twice, once against the main packet loop, and once inside a qSymbol
> exchange, in order to reach handle_limited_packets_while_waiting, where
> the vulnerable code path lives.
> 
> The tests send valid M packets, then read the memory back to check that
> the correct bytes were written, as well as sending packets that declare
> too much or too little data, packets with an odd number of payload
> characters, and packets with a huge declared length, all of which should
> be rejected.
> ---
>  .../gdb.server/mx-packet-overflow.exp         | 69 ++++++++++++++-----
>  gdbserver/remote-utils.cc                     | 25 +++++--
>  gdbserver/remote-utils.h                      | 10 ++-
>  gdbserver/server.cc                           |  4 +-
>  4 files changed, 85 insertions(+), 23 deletions(-)
> 
> diff --git a/gdb/testsuite/gdb.server/mx-packet-overflow.exp b/gdb/testsuite/gdb.server/mx-packet-overflow.exp
> index e41495b8f86..8080cdbb377 100644
> --- a/gdb/testsuite/gdb.server/mx-packet-overflow.exp
> +++ b/gdb/testsuite/gdb.server/mx-packet-overflow.exp
> @@ -16,8 +16,8 @@
>  # along with this program.  If not, see <http://www.gnu.org/licenses/>.
>  
>  # Test that the 'm' and 'x' packets protect against buffer overflow,
> -# and that the 'X' packet rejects packets where the declared length
> -# doesn't match the amount of data actually sent.
> +# and that the 'M' and 'X' packets reject packets where the declared
> +# length doesn't match the amount of data actually sent.
>  
>  load_lib gdbserver-support.exp
>  
> @@ -177,13 +177,13 @@ proc run_mx_packet_tests {} {
>      }
>  }
>  
> -# Send 'X' packets to gdbserver with various different declared
> -# lengths and payloads, check that gdbserver handles the packets as we
> -# expect; rejecting those where the declared length doesn't match the
> -# amount of data actually sent.
> -proc run_X_packet_tests {} {
> -    # The 'X' packet tests write into the inferior.  Use BUFFER2 as the
> -    # write target.
> +# Send 'M' and 'X' packets to gdbserver with various different
> +# declared lengths and payloads, check that gdbserver handles the
> +# packets as we expect; rejecting those where the declared length
> +# doesn't match the amount of data actually sent.
> +proc run_MX_packet_tests {} {
> +    # The 'M' and 'X' packet tests write into the inferior.  Use
> +    # BUFFER2 as the write target.
>      set x_addr [get_hexadecimal_valueof buffer2 0x0 "get buffer2 address"]
>      set x_addr [string range $x_addr 2 end]
>  
> @@ -221,18 +221,55 @@ proc run_X_packet_tests {} {
>      # declared data, and run out of memory.
>      gdb_test "maint packet X${x_addr},ffffffff:a" "\r\nreceived: \"E01\"" \
>  	"test 'X' packet, huge declared length"
> +
> +    # Send a couple of valid 'M' packets, and, for each one, read the
> +    # memory back to check that the correct bytes were written.
> +    gdb_test "maint packet M${x_addr},4:0a0b0c0d" "\r\nreceived: \"OK\"" \
> +	"test 'M' packet, 4 declared, 4 sent"
> +    gdb_test "maint packet m${x_addr},4" "\r\nreceived: \"0a0b0c0d\"" \
> +	"check 'M' packet, 4 declared, wrote correct bytes"
> +
> +    gdb_test "maint packet M${x_addr},2:0e0f" "\r\nreceived: \"OK\"" \
> +	"test 'M' packet, 2 declared, 2 sent"
> +    gdb_test "maint packet m${x_addr},2" "\r\nreceived: \"0e0f\"" \
> +	"check 'M' packet, 2 declared, wrote correct bytes"
> +
> +    # Claim to be sending more data than is actually sent.
> +    gdb_test "maint packet M${x_addr},8:0a0b0c0d" "\r\nreceived: \"E01\"" \
> +	"test 'M' packet, 8 declared, 4 sent"
> +
> +    # Send fewer bytes than are declared.
> +    gdb_test "maint packet M${x_addr},2:0a" "\r\nreceived: \"E01\"" \
> +	"test 'M' packet, 2 declared, 1 sent"
> +
> +    # Send more bytes than are declared.
> +    gdb_test "maint packet M${x_addr},2:0a0b0c0d" \
> +	"\r\nreceived: \"E01\"" "test 'M' packet, 2 declared, 4 sent"
> +
> +    # Send partial bytes: half a byte too many, and half a byte too
> +    # few.
> +    gdb_test "maint packet M${x_addr},2:0a0b0" "\r\nreceived: \"E01\"" \
> +	"test 'M' packet, 2 declared, 2.5 sent"
> +    gdb_test "maint packet M${x_addr},2:0a0" "\r\nreceived: \"E01\"" \
> +	"test 'M' packet, 2 declared, 1.5 sent"
> +
> +    # Claim a huge payload is being sent.  Without some sanity checks
> +    # gdbserver would try to allocate a buffer to hold all the
> +    # declared data, and run out of memory.
> +    gdb_test "maint packet M${x_addr},ffffffff:0a0b0c0d" \
> +	"\r\nreceived: \"E01\"" "test 'M' packet, huge declared length"
>  }
>  
> -# Run the m/x packet tests, then the 'X' packet tests.
> +# Run the m/x packet tests, then the 'M' and 'X' packet tests.
>  run_mx_packet_tests
> -run_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/X packets to
> -# test the inner packet processing loop.
> +# back a symbol request though, then we can send some m/x/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\"" {
> @@ -245,12 +282,12 @@ gdb_test_multiple "maint packet qSymbol::" "start qSymbol loop" {
>      }
>  }
>  
> -# We got a symbol request from gdbserver; run the m/x and 'X' packet
> -# tests, then complete the qSymbol loop.
> +# We got a symbol request from gdbserver; run the m/x, 'M' and 'X'
> +# packet tests, then complete the qSymbol loop.
>  if { $first_symbol_requested ne "" } {
>      with_test_prefix "in qSymbol loop" {
>  	run_mx_packet_tests
> -	run_X_packet_tests
> +	run_MX_packet_tests
>      }
>  
>      set symbol_name $first_symbol_requested
> diff --git a/gdbserver/remote-utils.cc b/gdbserver/remote-utils.cc
> index 24f7c9ccdb6..faf2ed7e3f6 100644
> --- a/gdbserver/remote-utils.cc
> +++ b/gdbserver/remote-utils.cc
> @@ -1301,16 +1301,33 @@ decode_m_packet (const char *from, CORE_ADDR *mem_addr_ptr,
>    decode_m_packet_params (from, mem_addr_ptr, len_ptr, '\0');
>  }
>  
> -void
> +/* See remote-utils.h.  */
> +
> +bool
>  decode_M_packet (const char *from, CORE_ADDR *mem_addr_ptr,
>  		 unsigned int *len_ptr, unsigned char **to_p)
>  {
>    from = decode_m_packet_params (from, mem_addr_ptr, len_ptr, ':');
>  
> +  /* Each byte of data is encoded as 2 characters in the remaining packet
> +     length.  If there are an odd number of characters remaining, or if the
> +     declared length isn't half the remaining packet length, then this is
> +     not a valid M packet.  */
> +  size_t remaining_packet_len = strlen (from);
> +  if (remaining_packet_len % 2 != 0 || *len_ptr != remaining_packet_len / 2)
> +    return false;
> +
>    if (*to_p == NULL)
>      *to_p = (unsigned char *) xmalloc (*len_ptr);
>  
> -  hex2bin (from, *to_p, *len_ptr);
> +  int converted = hex2bin (from, *to_p, *len_ptr);
> +
> +  /* We already sanity checked *LEN_PTR above so we should always convert
> +     the expected number of bytes, or hex2bin will throw an exception if it
> +     encounters a non-hex character.  */
> +  gdb_assert (converted == *len_ptr);
> +
> +  return true;
>  }
>  
>  void
> @@ -1558,8 +1575,8 @@ handle_limited_packets_while_waiting ()
>  	  {
>  	    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)
> +	    if (decode_M_packet (&cs.own_buf[1], &mem_addr, &mem_len, &mem_buf)
> +		&& target_write_memory (mem_addr, mem_buf, mem_len) == 0)
>  	      write_ok (cs.own_buf);
>  	    else
>  	      write_enn (cs.own_buf);
> diff --git a/gdbserver/remote-utils.h b/gdbserver/remote-utils.h
> index b710a8917f1..9b9e9437829 100644
> --- a/gdbserver/remote-utils.h
> +++ b/gdbserver/remote-utils.h
> @@ -55,8 +55,16 @@ const char *decode_m_packet_params (const char *from, CORE_ADDR *mem_addr_ptr,
>  				    const char end_marker);
>  void decode_m_packet (const char *from, CORE_ADDR * mem_addr_ptr,
>  		      unsigned int *len_ptr);
> -void decode_M_packet (const char *from, CORE_ADDR * mem_addr_ptr,
> +
> +/* Decode an 'M' packet from the buffer FROM.  Place the address into
> +   *MEM_ADDR_PTR and the length into *LEN_PTR.  If *TO_P is NULL then
> +   allocate a suitably sized buffer and *TO_P is updated.  Decode the
> +   data payload from the packet FROM into *TO_P.  Return true if
> +   everything succeeded, the M packet data will be in *TO_P.
> +   Otherwise return false and *TO_P will not have been touched.  */

Claude says:

  remote-utils.h:59-64 says that on failure "TO_P will not have been
  touched." But a non-hex digit makes hex2bin throw after the buffer has
  been allocated and assigned to *to_p (remote-utils.cc:1321). Both
  callers are safe: the qSymbol loop frees it with SCOPE_EXIT, and the
  main loop passes its own buffer. So only the promise is wrong. Fix:
  allocate into a local unique_xmalloc_ptr and hand it over only on
  success, or reword the comment. "Return true if everything succeeded,
  the M packet data will be in TO_P" also needs rephrasing.

> +bool decode_M_packet (const char *from, CORE_ADDR * mem_addr_ptr,
>  		      unsigned int *len_ptr, unsigned char **to_p);
> +
>  void decode_x_packet (const char *from, CORE_ADDR *mem_addr_ptr,
>  		      unsigned int *len_ptr);
>  int decode_X_packet (char *from, int packet_len, CORE_ADDR * mem_addr_ptr,
> diff --git a/gdbserver/server.cc b/gdbserver/server.cc
> index 7ad40e8e2b2..b62d80b08d4 100644
> --- a/gdbserver/server.cc
> +++ b/gdbserver/server.cc
> @@ -4963,8 +4963,8 @@ process_serial_event (void)
>        break;
>      case 'M':
>        require_running_or_break (cs.own_buf);
> -      decode_M_packet (&cs.own_buf[1], &mem_addr, &len, &mem_buf);
> -      if (gdb_write_memory (mem_addr, mem_buf, len) == 0)
> +      if (decode_M_packet (&cs.own_buf[1], &mem_addr, &len, &mem_buf)
> +	  && gdb_write_memory (mem_addr, mem_buf, len) == 0)
>  	write_ok (cs.own_buf);
>        else
>  	write_enn (cs.own_buf);

Approved-By: Kevin Buettner <kevinb@redhat.com>


      reply	other threads:[~2026-11-25  8:04 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 packet handling 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 ` [PATCH 3/5] gdbserver: fix buffer overflow in more m/x " Andrew Burgess
2026-11-25  7:24   ` 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 [this message]

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=cfb22f3b-e835-4878-a874-c22321ef77cd@redhat.com \
    --to=kevinb@redhat.com \
    --cc=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