From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id UQh0HyNkAGs0eyQAWB0awg (envelope-from ) for ; Fri, 20 Nov 2026 10:15:47 -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=h7iHgMkD; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 7D8441E0CA; Fri, 20 Nov 2026 10:15:47 -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.4 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_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED 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 A7FFC1E023 for ; Fri, 20 Nov 2026 10:15:46 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6E5BE4422B1D for ; Fri, 20 Nov 2026 15:15:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6E5BE4422B1D 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=h7iHgMkD 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 06D454315708 for ; Fri, 20 Nov 2026 15:13:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 06D454315708 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 06D454315708 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=1795187593; cv=none; b=FGOUjEJ8onF441yDeUdh8urH9ckvE2k1EReXVPXeenlVhaIjqDOoTXMNaeJMYSmrSkiUzHjdSzAxNDZAxRKtplHfxEG2aHN4PRhffOxy3kfdZGzpa0MwDhfAgPQIhX3x6Qn3cRG7BLxmGTHjKc0Ik+Y/MnyRTS+UXZOQLGITvHw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795187593; c=relaxed/simple; bh=Sli2gaxJp3YXaZpfa44zht4cxDC8arDYF0banUJx71s=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ErZG8c2iyu1qmCXpWpq3DLohEu0n8KFrMb9YQ8oJvkT5hjOCv53JBPNOHI29XfH2yFOFURo4nvoaSuqno7cDd9Gpuq0tH+jIWtTG/FUSsNQqC9Sko6chCTxGbl2fOV2wzM+PjDN0yF3MhjVg8/vR+IWaEb7vtWj9Zr3DnIgi0Fk= 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=h7iHgMkD DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 06D454315708 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795187592; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/Aa8rAyIgfutinzwYuYwSWF787L0TqSf6JcAFQIVEfU=; b=h7iHgMkDnhZDO3qfL1JxN3Rt6Gn1AJobFxDZLbCdNA8WteQW+ftzBejiNkyUlDJkl2t+za yzeHG1rDQhdmbGFJc3EfcSEoR+8jgqMRN/gaoNFilXvyVUqvbNp9DJLIKK0/XNwQodAy0p OD/ets/yJWb+UtXkF5rqbS5G3fhnDcw= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-550-xYsY-alVPuSrMy4gS70oLw-1; Fri, 20 Nov 2026 15:13:11 +0000 X-MC-Unique: xYsY-alVPuSrMy4gS70oLw-1 X-Mimecast-MFC-AGG-ID: xYsY-alVPuSrMy4gS70oLw_1795187590 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-48e0461e040so815201f8f.0 for ; Fri, 20 Nov 2026 07:13:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795187590; x=1795792390; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=/Aa8rAyIgfutinzwYuYwSWF787L0TqSf6JcAFQIVEfU=; b=MrWl/tpk+unOJGBhRtS72TvnB4SmgZpJw8zIxttzkdxhfIYcclcuNAkwkanDYShk4F oTd1Ueg3z0NCmQtDotdDVT//5wiMbMkfh9SPq/dFVWrnYUlE7yq43unm/b8abRvJN1FQ cQenms3fiDSkKsICMFsh0vFvu45g7h8cGzHT7vhK2jjwlp00VDxoveP4Itv5AGC6o0ku 3BkSVGNHxqx9jN+UVclQhWKtLToDru8mmF+yf+TZjHweHfmOln8NGIhKYDTnX1y9smKG 1c2WKXBY4k/B4eyUkR9hz1ZwOpwl+yGNJWsC8xBINOnOxTUtq6g4B7Y9zzcF+zBvTJi0 I15A== X-Gm-Message-State: APuU4Z8eoASYmcDuazKy2GSoQ/vdrlhc9zjH5uow4EKdbRGgJUg6PZ1/ uBjXgstfWQ3jm8kP4eH7d8g2Hl3+3tgsbhud2nUJAwjTA793EpFrbwfbZoTrEmbSpN77/lEADXy nNLSbKDlvOgHwNM0spjqVshtpaaHf5j5BlAxiES68ZNUhWDsYumdb4QUvAXi4z7R9UXTJN2QIcc Q9FR9OpjN7hIWixFM/0Yw/NFW9Wb7C0fEYBN/TRSRgPb3XBhU= X-Gm-Gg: AZFdTZfgTpU5LuyIyQqR5qTBhNl80tV3GueTg7JtHv/ZNm/E3UbxnUOJKtjAP16oC97 Sde8g57H77lLYP3HcpAXnOJAmeUq6jopTntBcSxzHvtw3eaBvNvvk1yV2UmLtAtXAwoFt8pcBUN Dha+TjCotBsrIfoO+xlFwlABJ3FvJy+rN1l8CKkw7Wr6+oKZe2HUuKjvKt0wtwDxuIcrgR5Q/fl QhMBQ+jg6OR0rt4HYco/jFHUUN65L+e1KAqGoCsVZASP/RQqN/KJSgnAxmIA52YH3+Fe7gMcLFS +o1ze+ZndLqXkzyepAuS7CEHq1m/YG9saORP7LRdGTaDPy0Hw6xjUviS3LX+q1N9xVwFYea1Jhb u0c/m5PFeVJ8753FT3cBRgEJOQuaezv/KYNBNjVkpOeYfFUg= X-Received: by 2002:a05:6000:4b1c:b0:48e:5550:c64c with SMTP id ffacd0b85a97d-4abb0c5d67bmr7440544f8f.14.1795187589722; Fri, 20 Nov 2026 07:13:09 -0800 (PST) X-Received: by 2002:a05:6000:4b1c:b0:48e:5550:c64c with SMTP id ffacd0b85a97d-4abb0c5d67bmr7440457f8f.14.1795187589138; Fri, 20 Nov 2026 07:13:09 -0800 (PST) Received: from localhost ([83.216.138.189]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab69bf630esm6308973f8f.16.2026.11.20.07.13.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 07:13:08 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 4/5] gdbserver: reject X packets with an invalid declared length Date: Fri, 20 Nov 2026 15:12:49 +0000 Message-ID: <1be18fd0e483952b6eec6de5e3d2cbbbdcfdeb41.1795186852.git.aburgess@redhat.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: yZs3yNSKFjf7yIg6hELi7cf_Zhk2Rvsdf-1Gkh5va1U_1795187590 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true 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 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 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 ); + 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 + 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; -- 2.56.0