From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id SkLrHIiWBmvG8jsAWB0awg (envelope-from ) for ; Wed, 25 Nov 2026 03:04:24 -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=WeYLsKnY; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 5F3101E166; Wed, 25 Nov 2026 03:04:24 -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 [IPv6:2620:52:6:3111::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 325481E09B for ; Wed, 25 Nov 2026 03:04:23 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2D159438FEAE for ; Wed, 25 Nov 2026 08:04:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2D159438FEAE 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=WeYLsKnY Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 3ABC04B920B3 for ; Wed, 25 Nov 2026 08:03:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3ABC04B920B3 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 3ABC04B920B3 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795593829; cv=none; b=iU/uTh7dNncYOV4MurAwAo7stcx6pdy8z/iuCzuEPg5X7+do/NVgs7iNESMgbe0LDiRt24fTRCx/9UpJmU3Em7z3m00f8S+RR/DAP/QkViaNjN0LAemBuy/J2g6cBoUvrVFP9KcDMsteKJn8OOR+aTqWb6WZ8HJXrAm4cK8M7xk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795593829; c=relaxed/simple; bh=k2TCCfNJRxRYgTy21P89gg28nwYn7IwOtT0RjKn3oUo=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=HCaS2iucctJ3Ee5P7frdB1UK8MMZ8NFoyMosxN/UZgSNxJyPNkyP3DlX5FimkZSWavHJKSrHNsjxq2bZ146oXe6uoe9OCYONzqB/zpxT1w7Ttdtw6IG459wYTlpOJq3v2YVk+OMEmbPnjfoVRLuaH025no2AWcHKmb2ccuKv0nI= 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=WeYLsKnY DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3ABC04B920B3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795593828; 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=kt5UegFEnv4Y/GHY+UfmW4E3A0/JrKXXvloTcTLc0d4=; b=WeYLsKnYggOWuQoQLkpbjFf3YYuJWovB12YJ2AM4KBKLqHOBX15ehXTHpbYhMfT9NE+Wxk kJ63y/QG8cdrwbY3H762I46N4vEcLIbFF9gI0E/XP9VEeO89CbBffrdJ8E3xft25PPrdPE IXyxRL4sii+DY0kBqvynB+ju2Xp+Kjg= Received: from mx-prod-mc-05.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-249-z4jpeEchPhuOEbTgbo6ERA-1; Wed, 25 Nov 2026 08:03:46 +0000 X-MC-Unique: z4jpeEchPhuOEbTgbo6ERA-1 X-Mimecast-MFC-AGG-ID: z4jpeEchPhuOEbTgbo6ERA_1795593825 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 47A12195607D for ; Wed, 25 Nov 2026 08:03:45 +0000 (UTC) Received: from [100.90.172.125] (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E8E341800548; Wed, 25 Nov 2026 08:03:43 +0000 (UTC) Message-ID: Date: Wed, 25 Nov 2026 01:03:41 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 5/5] gdbserver: reject M packets with an invalid declared length To: Andrew Burgess , gdb-patches@sourceware.org References: <99d2250b206ab3fc3a5d404a08464ef1315e4fd5.1795186852.git.aburgess@redhat.com> From: Kevin Buettner In-Reply-To: <99d2250b206ab3fc3a5d404a08464ef1315e4fd5.1795186852.git.aburgess@redhat.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: lmREQYJRg2jBOIskkPbySnWlbTdxYkTqGqtYk73BV6k_1795593825 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 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,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 . > > # 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:' 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