From: Kevin Buettner <kevinb@redhat.com>
To: Andrew Burgess <aburgess@redhat.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH 4/5] gdbserver: reject X packets with an invalid declared length
Date: Wed, 25 Nov 2026 00:48:56 -0700 [thread overview]
Message-ID: <315f6944-532e-49e4-aef5-a7d82dcb79bb@redhat.com> (raw)
In-Reply-To: <1be18fd0e483952b6eec6de5e3d2cbbbdcfdeb41.1795186852.git.aburgess@redhat.com>
Several nits in this one; see inline, below.
On 11/20/26 8:12 AM, Andrew Burgess wrote:
> The decode_X_packet function in gdbserver/remote-utils.cc decodes
> incoming 'X' packets. Each of these packets includes a declared length
> that says how much data the packet is carrying. When the caller passes
> a null pointer for the buffer argument TO_P, decode_X_packet uses this
> declared length, unchecked, to allocate a buffer to hold the packet's
> data.
>
> One of decode_X_packet's callers, the 'X' case within
> handle_limited_packets_while_waiting, passes a null TO_P; this 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 X packet that declares a huge length,
> for example 'X<addr>,ffffffff:a', while only actually sending a single
> byte 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.
>
> The main loop's 'X' handling is not affected; it always passes the
> static, pre-allocated, mem_buf buffer, so no allocation takes place on
> this path.
>
> In this commit, decode_X_packet now rejects any packet for which the
> declared length is greater than the number of bytes remaining in the
> packet after the header. The remaining data within the packet might
> include escape characters, so we might not need the full buffer size.
> But we will never need a buffer larger than the number of bytes
> remaining in the packet buffer. A packet that declares more data than
> it has room for can never be complete, so rejecting ths packet is the
s/ths/this/
> right thing to do. Performing this check before the buffer allocation
> means the size of the allocation is now bounded by the size of the
> incoming packet.
>
> Packets that carry more data than they declare are still rejected, as
> they always were, by the count check on the return value of
> remote_unescape_input; however, for these packets, remote_unescape_input
> calls error, which throws. This leaves the pre-existing behaviour for
> this mismatch, where gdbserver drops the connection, rather than
> replying with an error. We might want to improve this in the future,
> but that is not something I plan to do in this series.
>
> The 'M' packet equivalent, decode_M_packet, still has two problems: the
> declared length is used, unchecked, to allocate a buffer, and the return
> value from hex2bin, which says how many bytes were actually decoded, is
> ignored, so a packet that declares more data than it carries will have
> uninitialized bytes from the allocated buffer written into the inferior.
> Fixing these problems is left for a follow-on commit.
>
> The gdb.server/mx-packet-overflow.exp test is extended with a new
> run_X_packet_tests proc, which is 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 buggy code path lives.
> The tests send valid X packets, then read the memory back to check that
> the correct bytes were written, as well as sending packets with invalid
> declared lengths, and packets where the escaped data expands to fewer
> bytes than declared, all of which should be rejected.
> ---
> gdb/testsuite/gdb.server/mx-packet-overflow.c | 8 +++
> .../gdb.server/mx-packet-overflow.exp | 60 +++++++++++++++++--
> gdbserver/remote-utils.cc | 11 +++-
> 3 files changed, 73 insertions(+), 6 deletions(-)
>
> diff --git a/gdb/testsuite/gdb.server/mx-packet-overflow.c b/gdb/testsuite/gdb.server/mx-packet-overflow.c
> index dc3a92d8031..0603d0c75e7 100644
> --- a/gdb/testsuite/gdb.server/mx-packet-overflow.c
> +++ b/gdb/testsuite/gdb.server/mx-packet-overflow.c
> @@ -20,6 +20,7 @@
> #include <limits.h>
>
> unsigned char *buffer = NULL;
> +unsigned char *buffer2 = NULL;
> size_t buffer_length = 1024 * 1024 * 100;
> volatile int global_var = 0;
>
> @@ -46,6 +47,9 @@ main ()
> buffer = malloc ( buffer_length );
> assert (buffer != NULL);
>
> + buffer2 = malloc ( buffer_length );
Could the size of buffer2 be reduced? (Claude tells me that only 4
bytes are written to it, but recommends a buffer size of perhaps 16
bytes. I checked and it looks that way to me as well.)
> + assert (buffer2 != NULL);
> +
> unsigned char val = 0;
> for (size_t i = 0; i < buffer_length; ++i)
> {
> @@ -55,6 +59,10 @@ main ()
> val = (val + 1) % UCHAR_MAX;
> }
> while (is_escapable_byte (val));
> +
> + /* BUFFER2 is written to by GDB. Just make sure it starts in a
> + known state. */
> + buffer2[i] = 0;
> }
>
> global_var = 42; /* Break here. */
> diff --git a/gdb/testsuite/gdb.server/mx-packet-overflow.exp b/gdb/testsuite/gdb.server/mx-packet-overflow.exp
> index 3e5f97872d0..e41495b8f86 100644
> --- a/gdb/testsuite/gdb.server/mx-packet-overflow.exp
> +++ b/gdb/testsuite/gdb.server/mx-packet-overflow.exp
> @@ -15,7 +15,9 @@
> # 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 the 'm' and 'x' packets protect against buffer overflow.
> +# 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.
>
> load_lib gdbserver-support.exp
>
> @@ -175,14 +177,61 @@ proc run_mx_packet_tests {} {
> }
> }
>
> -# Run the m/x 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.
> + set x_addr [get_hexadecimal_valueof buffer2 0x0 "get buffer2 address"]
> + set x_addr [string range $x_addr 2 end]
> +
> + # Send a couple of valid 'X' packets, and, for each one, read the
> + # memory back to check that the correct bytes were written.
> + gdb_test "maint packet X${x_addr},4:abcd" "\r\nreceived: \"OK\"" \
> + "test 'X' packet, 4 declared, 4 sent"
> + gdb_test "maint packet m${x_addr},4" "\r\nreceived: \"61626364\"" \
> + "check 'X' packet, 4 declared, wrote correct bytes"
> +
> + gdb_test "maint packet X${x_addr},2:ef" "\r\nreceived: \"OK\"" \
> + "test 'X' packet, 2 declared, 2 sent"
> + gdb_test "maint packet m${x_addr},2" "\r\nreceived: \"6566\"" \
> + "check 'X' packet, 2 declared, wrote correct bytes"
> +
> + # A packet where the data contains escape sequences; the escaped
> + # sequence resolves to two bytes of data, which matches the declared
> + # length, so this should be accepted.
> + gdb_test "maint packet X${x_addr},2:\}G\}H" "\r\nreceived: \"OK\"" \
> + "test 'X' packet, 2 declared, data expands to 2 bytes"
> + gdb_test "maint packet m${x_addr},2" "\r\nreceived: \"6768\"" \
> + "check 'X' packet, 2 declared, escaped, wrote correct bytes"
> +
> + # Claim to be sending more data than is actually sent.
> + gdb_test "maint packet X${x_addr},8:ab" "\r\nreceived: \"E01\"" \
> + "test 'X' packet, 8 declared, 2 sent"
> +
> + # A packet where the declared length is more than the amount of
> + # data sent once the escape sequences have been expanded.
> + gdb_test "maint packet X${x_addr},2:\}A" "\r\nreceived: \"E01\"" \
> + "test 'X' packet, 2 declared, data expands to 1 byte"
> +
> + # 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 X${x_addr},ffffffff:a" "\r\nreceived: \"E01\"" \
> + "test 'X' packet, huge declared length"
> +}
> +
> +# Run the m/x packet tests, then the 'X' packet tests.
> run_mx_packet_tests
> +run_X_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
> +# back a symbol request though, then we can send some m/x/X packets to
> # test the inner packet processing loop.
> set first_symbol_requested ""
> gdb_test_multiple "maint packet qSymbol::" "start qSymbol loop" {
> @@ -196,11 +245,12 @@ gdb_test_multiple "maint packet qSymbol::" "start qSymbol loop" {
> }
> }
>
> -# We got a symbol request from gdbserver; run the m/x packet tests
> -# then, complete the qSymbol loop.
> +# We got a symbol request from gdbserver; run the m/x 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
> }
>
> set symbol_name $first_symbol_requested
> diff --git a/gdbserver/remote-utils.cc b/gdbserver/remote-utils.cc
> index 0d600e8844d..24f7c9ccdb6 100644
> --- a/gdbserver/remote-utils.cc
> +++ b/gdbserver/remote-utils.cc
> @@ -1340,10 +1340,19 @@ decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr,
> *len_ptr |= fromhex (ch) & 0x0f;
> }
>
> + /* Adjust PACKET_LEN to account for the bytes already read. */
> + packet_len -= i;
> +
> + /*If the declared length is greater than the number of bytes remaining in
Add a space between the asterisk and the 'I' (in 'If'). Adjust
following lines to match.
> + the packet, then the packet is malformed and cannot possibly carry
> + enough data; reject it. */
> + if (*len_ptr > packet_len)
> + return -1;
> +
> if (*to_p == NULL)
> *to_p = (unsigned char *) xmalloc (*len_ptr);
>
> - if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i,
> + if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len,
> *to_p, *len_ptr) != *len_ptr)
> return -1;
>
Consider changing the return type of decode_X_packet to bool like you
did for decode_M_packet in part 5. decode_X_packet could do with a
comment as well.
With those nits fixed:
Approved-By: Kevin Buettner <kevinb@redhat.com>
next prev parent reply other threads:[~2026-11-25 7:49 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 [this message]
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=315f6944-532e-49e4-aef5-a7d82dcb79bb@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