From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 4Y9KLBiTBmtc6jsAWB0awg (envelope-from ) for ; Wed, 25 Nov 2026 02:49:44 -0500 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=AgZ19DiX; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id B0EFC1E09B; Wed, 25 Nov 2026 02:49:44 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.1 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_SBL_CSS autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id A5E511E09B for ; Wed, 25 Nov 2026 02:49:43 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D123841F212F for ; Wed, 25 Nov 2026 07:49:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D123841F212F Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=AgZ19DiX Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 879EE41BBF92 for ; Wed, 25 Nov 2026 07:49:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 879EE41BBF92 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 879EE41BBF92 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795592942; cv=none; b=L/JI/JGZLLfzZIF1I10jEZEZw6IHfCrZdrq12FnjLJZ0Yh4l5UtTVFqchHRFXf4IDQAaU1HNYaUqA7EqixslvHyxiuS1IK7Zvv+BymvGpTPI76IDNixp3FUTTr/Io70Nu0CvHHEKIPR74PZ31yOYUuV3W4FQ6Nztjx4rz+P3dnw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795592942; c=relaxed/simple; bh=fvQof2esd+rIyRYygQBeWLnYQWoFGkofSHy2ZtCSu94=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:From:To; b=EdEGsiYBmgZgl3vitAXcTRIO/mN/Ry0gRMRWRaTk7tGzd3PduLyrMFBI/tBgsKhEnKuZCTJ301CrzV72Bx49BNgTWDaaQHw5dzEcbaBfDgXfX04ExqNFNzyzNCdEzUfeXGDy0THL9JEaEVPta9vNJSC1fH4qrpQBGq1BZ8y5rTQ= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=AgZ19DiX DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 879EE41BBF92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795592942; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2c79NatZMVOIyCC8+XWBtv3O//DS0Y/kao9DL0KLNKA=; b=AgZ19DiX+w4dqI+vepWhWwJ/41jeL6Ya+8tkjEvTGCBUVovpJO9jVTPEbEzvIu8aCA01Jg KAUeTpOKGd7/gJN1GSNaEkMT8lNlzN9VE9+5HTCwEAKVqyfUUpLU1VN3H1Et5HDvhITxiS +prR0w+WO3qMEvvk9mFo1SoGDPIDMqQ= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-66-YrG6Fh5TPc6f-EkTyr2z6w-1; Wed, 25 Nov 2026 07:49:00 +0000 X-MC-Unique: YrG6Fh5TPc6f-EkTyr2z6w-1 X-Mimecast-MFC-AGG-ID: YrG6Fh5TPc6f-EkTyr2z6w_1795592939 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 292DC19540E4 for ; Wed, 25 Nov 2026 07:48:59 +0000 (UTC) Received: from [100.90.172.125] (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 67F9A589; Wed, 25 Nov 2026 07:48:58 +0000 (UTC) Message-ID: <315f6944-532e-49e4-aef5-a7d82dcb79bb@redhat.com> Date: Wed, 25 Nov 2026 00:48:56 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 4/5] gdbserver: reject X packets with an invalid declared length References: <1be18fd0e483952b6eec6de5e3d2cbbbdcfdeb41.1795186852.git.aburgess@redhat.com> From: Kevin Buettner To: Andrew Burgess , gdb-patches@sourceware.org In-Reply-To: <1be18fd0e483952b6eec6de5e3d2cbbbdcfdeb41.1795186852.git.aburgess@redhat.com> X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 6QUl05uEOFctYrVvcErDr5u3bE1iQ4Xmo2I0ushfGjc_1795592939 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org 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,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 > > 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 . > > -# 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:' 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