From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 8QIgOPpjAGuPeSQAWB0awg (envelope-from ) for ; Fri, 20 Nov 2026 10:15:06 -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=CTE4nUSN; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id DF7CB1E0CA; Fri, 20 Nov 2026 10:15:06 -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 075B31E023 for ; Fri, 20 Nov 2026 10:15:06 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5065E445759C for ; Fri, 20 Nov 2026 15:15:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5065E445759C 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=CTE4nUSN 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 E6E504898CB1 for ; Fri, 20 Nov 2026 15:13:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E6E504898CB1 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 E6E504898CB1 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=1795187592; cv=none; b=wOXZD0AE0dHDaM/19U4+MvkS5JxGM8heO0MFx4TR3UQvMOdQOlwG++eL/SYVxC9sn6YWXdWfqpdpwMh5irP7Ietr5v1pGxKI0tjpBMVZh6uynT/Hr8Wua67i67ep25TVe9UJbGFcdXiaGsjNciF6seIoui9BZLjFnGPAtATJrvQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795187592; c=relaxed/simple; bh=8M0lxurBzTLiK30kf1KplF1JGRzIa1GqASp/haKRhzU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=wHZVjymo+8ZaK5lxdS3mk5oPRPVA/94lJ+BMPMS4Ljyn3kfpgApwKUeFadFgYynbSPQVjJGjXjfi0jU8grkGNQOJIupuWSOm8MPPbB1dvSNhnljnU4iTJCzmJbWskjf2samh/0OSryfPI+cVHzrGIT5TUPFYouY9HE9fr/CE7wo= 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=CTE4nUSN DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E6E504898CB1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795187591; 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=RM2E4aPcR0J4rMppJ8Vr8K61+Ma0BE/ow1H6wxr8iYc=; b=CTE4nUSNQ8EsQTbm5OzEsneDgw2ETChe2ATj+Sa8GpsaaHzNPeLQLbus5LeXf4mvVP3uZk 7C9R7oCKfB3iSAtFCtr5O1SnxY+LPVx1GMVZRFNvnr9Hh/2ef0P+2ytbYT8xfN0K+Y5n8J Z/l0e+na3DEATMAV4HIl0JC9rolgNdQ= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-351-5CitBQkkMaiyVjh6y_NVtA-1; Fri, 20 Nov 2026 15:13:09 +0000 X-MC-Unique: 5CitBQkkMaiyVjh6y_NVtA-1 X-Mimecast-MFC-AGG-ID: 5CitBQkkMaiyVjh6y_NVtA_1795187588 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4a2065a2ef9so7801465e9.3 for ; Fri, 20 Nov 2026 07:13:09 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795187588; x=1795792388; 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=RM2E4aPcR0J4rMppJ8Vr8K61+Ma0BE/ow1H6wxr8iYc=; b=UbjgVV9HA+HxoUh5om14UoFBeEPk/bZqRh6sdAAbFk1aPG1SV9NlLY84cZ5MjPdr4v bGHOwklSeeYr0inx57VDe75nCM0dxN6X5Sz9X5+Eo95D07CmiDEkBB1kYj2W0qCgPpY/ QMGE85KQDCYYbB1sKvPOb303/Ikyye8A4HywlqEZ/yZL6BAm4wZBu3lvCmmoGEQysLtw 36GG6Kel8xSf1GYCR/4KUcYmS6Y/kwSj+EDX7GFqn1WCVvSaGL96zWA/cCJ7Odx+gZOs rcg6lBmI4ngWMLJ65rFWZDlnpvpVes/QEk6XXqQo/y/32ogWwMWupbxXDUuxHvXGwF/t JxMA== X-Gm-Message-State: APuU4Z94C6WWthmE2foLBf6AHl0QCaSQSkbk44KWZORypjPsFHz0sX41 Fkg69XE2r/McyLpxsmzux/yqMUjebYSFzyq60wHdYjk3zmu2i7ywZsxdLaXbcTegVsbcImBye8B uMim5ZFHrephCOk6qp4+7rpXf+x81rUIro/7F6CzmZTYuHFxW2hrn+WNZp415bkZtKss98tQgAP zWLhggWF96YI+ICRFT7ks85iJDlx6QraX5j8hpKZmaHVlUjN8= X-Gm-Gg: AYBFou0LXbm963zalE1sM38g6lgUmu6VSuUDCN0sJnbQsPz7M5KmP/35UHMrm4RQvFu 2mTKxAYbXreU510J96i7l9ql4MFvNGaH4TYcm3Y4XzTUh3edaAet2e6YF/TvcxlaT1aoSWooCDh 55adb1vTwsbGUN/9YX2N/O56rV/otgJZpK9MKf5BXU4I44GJ84YJojrFXTCwq8hXXNh6445Q6cO UHBLVRpWAU1RbFj5vjLc9kr5I/aJ8IxmNYU1PQgT6NON4bj5+o/wsoRrI42W2j9f5PPPntDeYy/ Rt/vpi9STHMrZYXK6hd/xV8kSH0kUnqowDXFE84SqjZcL9VYY7TmOAoP2N8rtFOiGEcDglB2c5L ZCKvoZK7gcBSB6slmH2ozqNBFUQQ+MHaNFl0H4YTQsDqT5g== X-Received: by 2002:a05:600c:4584:b0:4bb:5de1:e90f with SMTP id 5b1f17b1804b1-4bb88819bcdmr69820335e9.31.1795187588195; Fri, 20 Nov 2026 07:13:08 -0800 (PST) X-Received: by 2002:a05:600c:4584:b0:4bb:5de1:e90f with SMTP id 5b1f17b1804b1-4bb88819bcdmr69819535e9.31.1795187587635; Fri, 20 Nov 2026 07:13:07 -0800 (PST) Received: from localhost ([83.216.138.189]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb88f14f1esm49569315e9.2.2026.11.20.07.13.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 07:13:07 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 3/5] gdbserver: fix buffer overflow in more m/x packet handling Date: Fri, 20 Nov 2026 15:12:48 +0000 Message-ID: <7ec90ad7624f32c6edc9298eb067092c6b9631ee.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: jBAvaeK9IU8p21WInYtaOgXDDJw7IMNLn5VWuHUhuSw_1795187588 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 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. --- .../gdb.server/mx-packet-overflow.exp | 40 ++++++++++++++ gdbserver/remote-utils.cc | 54 ++++++++++++++++--- 2 files changed, 86 insertions(+), 8 deletions(-) diff --git a/gdb/testsuite/gdb.server/mx-packet-overflow.exp b/gdb/testsuite/gdb.server/mx-packet-overflow.exp index 09cb4a4cb83..3e5f97872d0 100644 --- a/gdb/testsuite/gdb.server/mx-packet-overflow.exp +++ b/gdb/testsuite/gdb.server/mx-packet-overflow.exp @@ -177,3 +177,43 @@ proc run_mx_packet_tests {} { # Run the m/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 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\"" { + unsupported "$gdb_test_name (no symbols requested)" + } + + -re -wrap "\r\nreceived: \"qSymbol:(\[^\"\]+)\"" { + set first_symbol_requested $expect_out(1,string) + pass $gdb_test_name + } +} + +# We got a symbol request from gdbserver; run the m/x packet tests +# then, complete the qSymbol loop. +if { $first_symbol_requested ne "" } { + with_test_prefix "in qSymbol loop" { + run_mx_packet_tests + } + + set symbol_name $first_symbol_requested + gdb_test_multiple "maint packet qSymbol::$symbol_name" \ + "qSymbol loop completed" { + -re -wrap "\r\nreceived: \"OK\"" { + pass $gdb_test_name + } + + -re -wrap "\r\nreceived: \"qSymbol:(\[^\"\]+)\"" { + set symbol_name $expect_out(1,string) + send_gdb "maint packet qSymbol::$symbol_name\n" + exp_continue + } + } +} diff --git a/gdbserver/remote-utils.cc b/gdbserver/remote-utils.cc index d1b17a33e2b..0d600e8844d 100644 --- a/gdbserver/remote-utils.cc +++ b/gdbserver/remote-utils.cc @@ -1454,7 +1454,6 @@ handle_limited_packets_while_waiting () return false; CORE_ADDR mem_addr; - unsigned char *mem_buf = nullptr; unsigned int mem_len; int new_len = -1; @@ -1463,9 +1462,24 @@ handle_limited_packets_while_waiting () case 'm': { decode_m_packet (&cs.own_buf[1], &mem_addr, &mem_len); - mem_buf = (unsigned char *) xmalloc (mem_len); - if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0) - bin2hex (mem_buf, cs.own_buf, mem_len); + /* MEM_LEN will be used to allocate a buffer, MEM_BUF, which is + filled from inferior memory. The bin2hex call converts each + byte in MEM_BUF into two hex characters, doubling the number + of bytes required. This result is written to the + client_state's OWN_BUF, which is PBUFSIZ in length (+1 for a + trailing null). We can therefore only read half a packet's + worth of bytes from the inferior memory; otherwise we will + overflow OWN_BUF. */ + if (mem_len > PBUFSIZ / 2) + { + write_enn (cs.own_buf); + break; + } + + gdb::unique_xmalloc_ptr mem_buf + ((unsigned char *) xmalloc (mem_len)); + if (read_inferior_memory (mem_addr, mem_buf.get (), mem_len) == 0) + bin2hex (mem_buf.get (), cs.own_buf, mem_len); else write_enn (cs.own_buf); } @@ -1473,14 +1487,35 @@ handle_limited_packets_while_waiting () case 'x': { decode_x_packet (&cs.own_buf[1], &mem_addr, &mem_len); - mem_buf = (unsigned char *) xmalloc (mem_len); - if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0) + + /* MEM_LEN will be used to allocate a buffer, MEM_BUF, which is + filled from inferior memory. This buffer is then escaped + (see remote_escape_output) into the client_state's OWN_BUF, + which is PBUFSIZ in length (+1 for the leading 'b'). The + escaping never reduces the number of bytes needed, so a + request for more than PBUFSIZ bytes could never produce a + complete reply. + + GDB usually only requests small reads, a few bytes at a time. + So, limiting MEM_LEN to PBUFSIZ will easily handle everything + GDB sends us, and allows other debug clients space to ask for + larger reads if they need it, but still places a sane limit + on the size of buffer we are about to allocate. */ + if (mem_len > PBUFSIZ) + { + write_enn (cs.own_buf); + break; + } + + gdb::unique_xmalloc_ptr mem_buf + ((unsigned char *) xmalloc (mem_len)); + if (read_inferior_memory (mem_addr, mem_buf.get (), mem_len) == 0) { gdb_byte *buffer = (gdb_byte *) cs.own_buf; *buffer++ = 'b'; int out_len_units; - new_len = remote_escape_output (mem_buf, mem_len, 1, + new_len = remote_escape_output (mem_buf.get (), mem_len, 1, buffer, &out_len_units, PBUFSIZ); @@ -1500,6 +1535,8 @@ handle_limited_packets_while_waiting () break; case 'X': { + unsigned char *mem_buf = nullptr; + SCOPE_EXIT { free (mem_buf); }; if (decode_X_packet (&cs.own_buf[1], len - 1, &mem_addr, &mem_len, &mem_buf) < 0 || target_write_memory (mem_addr, mem_buf, mem_len) != 0) @@ -1510,6 +1547,8 @@ handle_limited_packets_while_waiting () break; case 'M': { + 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) write_ok (cs.own_buf); @@ -1535,7 +1574,6 @@ handle_limited_packets_while_waiting () leave_packet_handling_loop = true; break; } - free (mem_buf); if (!leave_packet_handling_loop) { -- 2.56.0