From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id AQ1CBsxjAGuPeSQAWB0awg (envelope-from ) for ; Fri, 20 Nov 2026 10:14:20 -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=POshP8Q+; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 145EF1E0CA; Fri, 20 Nov 2026 10:14:20 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-6.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 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 D22331E023 for ; Fri, 20 Nov 2026 10:14:18 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7CB9243D018A for ; Fri, 20 Nov 2026 15:14:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7CB9243D018A 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=POshP8Q+ 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 935AA42F9C29 for ; Fri, 20 Nov 2026 15:13:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 935AA42F9C29 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 935AA42F9C29 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=1795187597; cv=none; b=inRIz5IM3h44CbEKS7tKI3krLN5HOPtH1wPJUQ+bMf/mB0d+3h8RHaiG/SlNCXJcOLM4Mvfabvk0mtriChLkAHV7ROEVSeddsFdMbjGBEvElrbtJBzTz+NN7DGkn/cgMGDU3XR468zdIB1xFXltxeovAOXYtghUi/z/1WThMjC4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795187597; c=relaxed/simple; bh=9SRMepdb2j8MulWwa0nLzAYWzxYZoQiGsAnM+b2WmzM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=tdMx8eIUrh3zfpomE6u1mwJD4E5boRns0TfXdvo3hQ3RKnJLwJ9yWbLRJKWnDgGHCmf7NBvTz3jhuuL5Wf5VmYieq8PnL49vR0lI4Uj27bGV3qNipHZTFVTtCg5QroSYT89Zx61aH3uTq3XLchEiqQnbhk+GcRjL/2PXJzWEPwc= 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=POshP8Q+ DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 935AA42F9C29 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795187597; 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=cAepuJT7gWivbALqKzmqQm/5FA20Qp7kBiQf7IWC2BU=; b=POshP8Q+HCxxiInvI15Xl+9ZULvLi7WJk445QKMkfjQyOdvE0aOVSzj89oh/rT9J4421/8 fEt3GQIYBxXVJ6KG/9BkajmTTAcDr96VVKfQzp242hsbm1IRrZr8f14FiNpoofQWwE7oj6 Aiw7dicEdmMOl/FAPoc8HFZ9t6nPook= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-564-ctz0HuqHMwuuIvP8-JEh2Q-1; Fri, 20 Nov 2026 15:13:13 +0000 X-MC-Unique: ctz0HuqHMwuuIvP8-JEh2Q-1 X-Mimecast-MFC-AGG-ID: ctz0HuqHMwuuIvP8-JEh2Q_1795187592 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-492b5408dedso583390f8f.3 for ; Fri, 20 Nov 2026 07:13:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795187592; x=1795792392; 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=cAepuJT7gWivbALqKzmqQm/5FA20Qp7kBiQf7IWC2BU=; b=HNjr53l1SVIUaKIhxaoNFMdWHVt/T3TukSKejiM17/h6GtLfKteS4HbesTADtuP87b DdzmhkJ4BO+BOEvTcgoCHkvR5GH8nFdjf3kB0joShI8ToPRPg2kYB/mpjnxcGaGi27Yg Keaaq0IIjTi3vMH5z+queKbQ96jCJYdjNEEceerbUMaYEiC7m3MaM5t4RpQtHHVD3Wvy khpkcD6R+7uwblIPUJaf+3V5RCT11ZMlykTJORyXWpQpHgd1Pc505iWvvvftei4t/w5J Ah9lLKFeigsVbZ1woscbiR8iwMzV9eVVkpdaRGGMcTTpOdcbyBa6WQYYM0X++0VMqVYH rStQ== X-Gm-Message-State: APuU4Z8AyngV5mi0bOqvCKy6rxSzcXPiiH/FlhAx1roNvK7J9kzMmVWy EU3U9sbLYBNpWH5h1zN+Ls2srBC4/r21XFMc1kLWQYoWc/4qQw3I2D2wKE1/of2z684HFSzSV4h WamM8cs7huhqZXLx0JNnvdAKv7NnyHcZMLqEEn5qYktE1GCmSRmoKE24Wn27NskwyWKeLZyBzAJ SWHXXp+arYLTWUlkuWHwBZpGrv3goJH1XpcWGg7UD0UFZtv0k= X-Gm-Gg: AZFdTZe5SaNgcvKPl/I8kpuagktI8SrPOa+rUT7zVN8EyOcr2F+7RgtjFS4iyCi8ISR apioarpVm29r+K+Kpu7LIlL5RcEhnhrwXpU6rZT1nlh4hc3nbkrb1WeKn9fX+jpnrlHHd9Hr/Hb iR1gktzoODGk+TSjtYfLAlzs4FPjqB4L2SVzfK/Q+2m72DaRuxQMFajiH6vqXkwFYWh0LeKN8Co peoA2uOQsuNjkPbi0mMQARSndta97sDu/Kdz0AyPMeGj/xhDMhKad6sDyrV2K46ebjFxuZGYkwL EW1kXgEXat5fLuO0+p8ALp/YvJfKCOl15YXYK8MiXFhrXmS+gutoe/Xu6Y/cACOhw44ABPxPUR2 dVBzaoyfOiF+1KtYY55NZ1FgmR7xXgmjgxY+Yg9KknVXqsA== X-Received: by 2002:a5d:4b51:0:b0:49a:fbfd:92c4 with SMTP id ffacd0b85a97d-4abb3a4d650mr5340715f8f.29.1795187591747; Fri, 20 Nov 2026 07:13:11 -0800 (PST) X-Received: by 2002:a5d:4b51:0:b0:49a:fbfd:92c4 with SMTP id ffacd0b85a97d-4abb3a4d650mr5340615f8f.29.1795187590928; Fri, 20 Nov 2026 07:13:10 -0800 (PST) Received: from localhost ([83.216.138.189]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6c43563esm6426084f8f.57.2026.11.20.07.13.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 07:13:10 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 5/5] gdbserver: reject M packets with an invalid declared length Date: Fri, 20 Nov 2026 15:12:50 +0000 Message-ID: <99d2250b206ab3fc3a5d404a08464ef1315e4fd5.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: aMPur0MIsi_3w_eL2eLpSHnlZg6uutsKUM39BOt_1Mk_1795187592 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_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. */ +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); -- 2.56.0