From: Kevin Buettner <kevinb@redhat.com>
To: Andrew Burgess <aburgess@redhat.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH 3/5] gdbserver: fix buffer overflow in more m/x packet handling
Date: Wed, 25 Nov 2026 00:24:05 -0700 [thread overview]
Message-ID: <ac9f9fb8-5c19-4115-9ef9-f0c7de304b9b@redhat.com> (raw)
In-Reply-To: <7ec90ad7624f32c6edc9298eb067092c6b9631ee.1795186852.git.aburgess@redhat.com>
On 11/20/26 8:12 AM, Andrew Burgess wrote:
> 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.
Claude's comments on this one:
Patch 3/5 looks good to me. I have one optional suggestion, for the
test.
The new qSymbol part of mx-packet-overflow.exp calls
run_mx_packet_tests (and, after patches 4 and 5,
run_MX_packet_tests) while gdbserver is waiting for the qSymbol
reply. Both procs start by calling get_hexadecimal_valueof to find
the addresses of buffer and buffer2 and the value of buffer_length.
That means GDB evaluates expressions, and sends its own packets, in
the middle of the exchange.
This works today, because GDB reads those variables with 'm' or 'x'
packets, which handle_limited_packets_while_waiting services. But
the test then depends on GDB never sending anything else while
evaluating an expression. If that ever changed, gdbserver would
leave the loop, treat GDB's packet as a malformed qSymbol reply, and
every later test would fail without pointing at the real cause.
A cheap way to avoid that is to look the values up once, before
sending "maint packet qSymbol::", and pass them in, something like:
set addr [get_hexadecimal_valueof buffer 0x0 \
"get buffer address"]
set buffer_len [get_hexadecimal_valueof buffer_length 0x0 \
"get buffer length"]
...
run_mx_packet_tests $addr $buffer_len
Then the only packets sent during the exchange are the ones the test
sends on purpose. It would also avoid running the same "get ...
address" tests twice under two prefixes. This is only a robustness
point, though, so I'm happy for it to go in as is.
For my part, I checked how get_hexadecimal_valueof was being used. I
discovered that you're correctly stripping the leading `0x` when it
matters.
Approved-By: Kevin Buettner <kevinb@redhat.com>
next prev parent reply other threads:[~2026-11-25 7:24 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 ` [PATCH 3/5] gdbserver: fix buffer overflow in more m/x " Andrew Burgess
2026-11-25 7:24 ` Kevin Buettner [this message]
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=ac9f9fb8-5c19-4115-9ef9-f0c7de304b9b@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