From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id EXOMLMvk+mp4/xIAWB0awg (envelope-from ) for ; Mon, 16 Nov 2026 06:11:07 -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=aT+24dPP; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id ACC261E087; Mon, 16 Nov 2026 06:11:07 -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 B47681E087 for ; Mon, 16 Nov 2026 06:11:05 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 64D054B920D5 for ; Mon, 16 Nov 2026 11:11:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 64D054B920D5 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=aT+24dPP 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 0B7254BA798C for ; Mon, 16 Nov 2026 11:10:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0B7254BA798C 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 0B7254BA798C 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=1794827426; cv=none; b=tvPAam0cRJ7hgsUqiGDkWFNGzDqnnBee239cEaOGkmv1MDYGrhKGtQP2YfSqFBhe6po8v9FwDc65P+G3WS1w/SDH7H3EME9tUTkU2FUtxBeLREcleZdr+1NpDL/Dg6bh6totvFDQ2WcTfmZGXuSbCWtcKMMafbtWJFh+gGKnEgY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794827426; c=relaxed/simple; bh=u/S7hJawWN7PMJAoWSipPIleE8quy8MwqCax5jJJpDc=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=RIXx5yt0fY1e4HTcSxz3oElp3BfZr0FHLhpQeoyn526mIQfsnROvJXlQtJmB/PcYuwc/bqGEKoULVWP1or2ROs9P+GgM/RIyNYSr1qMpgKpfQbrNzbBd0AeMvW0s0l2m/7VMDaK3EDgJCSEjWHtlbYQxKxvDzwrHzRPmW3oQ9D0= 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=aT+24dPP DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0B7254BA798C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1794827425; 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=7yIXdi1RuqMyawfKf+PtbSrTou4av+GrVjyA9Ajvh/g=; b=aT+24dPPSE9u3SQhiCU6c4tU4M5SjHqVcx37KQrbZC+sdgxnq/jrbQjH229WLLy/8zl9ls wCTvnsAxz69HCCMg1oF5HNS72kmPlC6SyMfhmq5ZuDDGrn9cT6u8Bjpg+fQtrxxPtm8xpc 69AHlHdfpLM0BUqCzDMCk6Jvl31mkjs= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-92-ObfJaImNO8OpA96IHSjlAw-1; Mon, 16 Nov 2026 06:10:19 -0500 X-MC-Unique: ObfJaImNO8OpA96IHSjlAw-1 X-Mimecast-MFC-AGG-ID: ObfJaImNO8OpA96IHSjlAw_1794827418 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-49166c8a60dso2337729f8f.0 for ; Mon, 16 Nov 2026 03:10:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794827418; x=1795432218; 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=7yIXdi1RuqMyawfKf+PtbSrTou4av+GrVjyA9Ajvh/g=; b=hqsfGJUOieKH9b+ztOsuNAfS/MpOFd+I0I/7DlUq6c3cHqHkEdDKf9Q8MqPKFosIb/ NGmXC3k1qbZFLMSibNCxCsZB30upsNn3c6ixtGtFE4/wQYzPIgDP8qs6TbmBWYiO+vhq /sIxeOuNA6iiPKkvmUlKrzvF5BiErlxmtsIQw3UStB7s3CCw06tssSyLg6xF8ja/CAc2 Mlu5Nct5A6mA50LCCMeEWQZW3w2s4Dnwf20bedCfocTfVasPOP9aoL5tfbyLBtka6Fq9 P0flJbklCRQSbsHMSFn8vnMN/MOnpnUeMNg4SB+D8MOjctWxp41D+mEGXZ1EmgbgpkwM Qmzg== X-Gm-Message-State: APuU4Z839Ut8z3v/BcrHoS95M60xxDUvacGCfVmXhmGWd4bmLcl4vsdQ 6rQAVMxLDb4o32INOIJ1jozW183KqfqijjX9ZBF96Q+NzqdZF0LkrAMJJNy6nifL3w8FbNkOLG5 wVN3pe/Bg48qGdsUxKHDbZrR2SIEamXrhG/tVk5jPAkHYERNynhVWaz+bLEWV8owCwqHpwyhEZ5 EHYeKgbq3vph/hlDQge2zyCrOZIdtTHrvyZI4aFLC3EvpdcWM= X-Gm-Gg: AZFdTZcPW+dfdMIalg3y2Qxfw3c781Z+WW3CNLc434DsKFvDuy6hA5dfkc/PAPbb/cC MRw6BeJL1NIRjEWXLW+AblQJVCN7wybnvW3dwdvWBUvvQTJypt3SBDmtkENp1ZAITILCCCB8V7j dbH2Hxl9Ccn3yCmzcUp5y2Q8Vp/e+chDKn/x6O2mzSotc49JBPRcluzM4iXpmuCSVlVvlA2WakC 16Axh/KMSPskbzphHFAMviJUoDB3US3k+zvygsPa/F851GoHHnj/Whzsi/485r+7iH4gvZnvSsv 86cfdo3BdQJgvdVq7jO2t0Ix2DIyZquufR2SVBVwdGK3PmpTKShorHYYbQNWzg4taVLK5QNcK1h axd/JngX7W9w47QG6m5ef2rP9KJVgGOR1Vn9a5tk2U9DC X-Received: by 2002:a5d:5f41:0:b0:49e:1601:2fa1 with SMTP id ffacd0b85a97d-49e1601356bmr15989764f8f.33.1794827417856; Mon, 16 Nov 2026 03:10:17 -0800 (PST) X-Received: by 2002:a5d:5f41:0:b0:49e:1601:2fa1 with SMTP id ffacd0b85a97d-49e1601356bmr15989684f8f.33.1794827417252; Mon, 16 Nov 2026 03:10:17 -0800 (PST) Received: from localhost ([83.216.138.189]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-49c60f8d66esm19814044f8f.57.2026.11.16.03.10.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Nov 2026 03:10:16 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 2/5] gdbserver: prevent buffer overrun for QTDPsrc packet Date: Mon, 16 Nov 2026 11:10:08 +0000 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 6-1qw9bXK9305ABdF2yr4nsnPBZSZdbBa6aFNl2uDUw_1794827418 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 QTDPsrc packet format is: QTDPsrc:N:ADDR:TYPE:START:SLEN:BYTES The BYTES is a hex encoded string. The START and SLEN allow for strings that are longer than a single packet, START is the position of BYTES within the full string, and SLEN is the length of the full string. Critically, the hex decoding of BYTES might not equal SLEN. The current handling of QTDPsrc makes two errors: it allocates a buffer based on SLEN. A malicious incoming packet could include an arbitrarily large SLEN value causing gdbserver to allocate a huge amount of memory. This would either cause the gdbserver process to terminate with an out of memory error, or might bring down the machine due to memory pressure. The second problem is that gdbserver converts bytes from the packet into the allocated buffer using a call to hex2bin, and uses the packet length as the limit on the number of bytes translated. This means that if SLEN is less than the length of the hex decoded string, gdbserver will overflow the allocated buffer and write to arbitrary memory. This commit makes the following changes: 1. Ensure that SLEN matches the length of the hex decoded BYTES string. I did consider just ignoring SLEN, but that means gdbserver would accept invalid packets, e.g. SLEN less than the length of the hex decoded BYTES string. And that seemed like a bad idea. 2. Allocate the buffer, and cap the hex2bin call based on the length of the hex decoded BYTES string, not SLEN. This means that if the rules for SLEN change in the future the buffer will be the right size to decode BYTES. One point of interest is that I added a call `xfree (srctype)` on the error path. This avoids leaking a previously allocated buffer. This is temporary, a later commit in this series switches the buffer to use gdb::unique_xmalloc_ptr instead and the xfree will be removed. Adding the xfree now just means this commit can be more focussed. The header comment in the new test script, and the name of the script itself, both indicate that this script is for testing more than the QTDPsrc packet. Right now this is not the case, but later commits in this series will add more tests. I've left the header comment as generic text that talks about testing multiple tracepoint packets to avoid churn in the next commit. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34968 --- .../gdb.server/invalid-tracepoint-packets.exp | 141 ++++++++++++++++++ gdbserver/tracepoint.cc | 29 +++- 2 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp diff --git a/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp b/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp new file mode 100644 index 00000000000..d82647cbe63 --- /dev/null +++ b/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp @@ -0,0 +1,141 @@ +# This testcase is part of GDB, the GNU debugger. +# +# Copyright 2026 Free Software Foundation, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test some tracepoint related packets, focusing mostly on error +# handling within gdbserver. This does not test every tracepoint +# related packet. This test was written while fixing some bugs in +# various packets and the tests were only added for packets being +# fixed. +# +# Start gdbserver and connect. Use 'maint packet ...' to send +# tracepoint related packets to gdbserver. Some of these packets are +# valid, others are invalid. +# +# This manual packet sending is used in order to be able to send the +# invalid packets. + +load_lib gdbserver-support.exp + +require allow_gdbserver_tests + +standard_testfile normal.c + +if {[build_executable "failed to build" $testfile $srcfile]} { + return +} + +# Some global regexp used in the tests below. +set error_re "\r\nreceived: \"E01\"" +set ok_re "\r\nreceived: \"OK\"" + +# Start GDB and gdbserver ready for the test. Return false if +# anything goes wrong, otherwise, return true. +proc start_gdbserver_for_test {} { + clean_restart $::testfile + + # Make sure we're disconnected, in case we're testing with an + # extended-remote board, therefore already connected. + gdb_test "disconnect" ".*" + + # Start gdbserver and connect. + set res [gdbserver_spawn ""] + set gdbserver_protocol [lindex $res 0] + set gdbserver_gdbport [lindex $res 1] + set res [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] + if {![gdb_assert {$res == 0} "connect"]} { + return false + } + + return true +} + +# Test the QTDPsrc packet. +proc_with_prefix test_qtdpsrc {} { + if {![start_gdbserver_for_test]} { + return + } + + # Disassemble main and find an address at which we could place a + # tracepoint. + set addresses {} + gdb_test_multiple "disassemble main" "" -lbl { + -re "\r\n\\s+0x0*(\[0-9a-f\]+)\\s+\[^\r\n\]+(?=\r\n)" { + set addr $expect_out(1,string) + lappend addresses $addr + exp_continue + } + + -re "^\r\n$::gdb_prompt $" { + gdb_assert {[llength $addresses] > 0} \ + "found instruction addresses" + } + } + + # We need at least one address in main for this test. + if {[llength $addresses] < 1} { + unsupported "not enough instruction addresses in main" + gdbserver_exit false + return + } + + # Record the address we'll use in this test. + set addr [lindex $addresses 0] + + # Create a tracepoint at ADDR. Do this manually as the rest of this + # test also does manual packet manipulation. + gdb_test_multiple "maint packet QTDP:1:$addr:D:1:1" "create tracepoint" { + -re -wrap "\r\nreceived: \"OK\"" { + pass $gdb_test_name + } + + -re -wrap "" { + unsupported $gdb_test_name + gdbserver_exit false + return + } + } + + # Now send invalid QTDPsrc packets at gdbserver and check they are all + # rejected. + set prefix "QTDPsrc:1:$addr:cond:0" + + # Undersized strings. The packet declares that it has a string + # containing 2 bytes, but COUNT is the actual number of nibbles + # supplied. + foreach count {0 1 2 3} { + gdb_test "maint packet ${prefix}:2:[string repeat 1 $count]" \ + $::error_re "string of $count nibbles and declared length 2 bytes" + } + + # Packets with oversized strings of various sizes. The packet + # declares that it has a string containing 1 byte, but COUNT is the + # actual number of nibbles supplied. + foreach count {3 100 500 1000 2000 4000} { + gdb_test "maint packet ${prefix}:1:[string repeat 1 $count]" \ + $::error_re "string of $count nibbles and declared length 1 byte" + } + + # Test some valid packets. + gdb_test "maint packet ${prefix}:1:41" $::ok_re "valid string accepted" + gdb_test "maint packet ${prefix}:3:414243" $::ok_re \ + "valid multi-byte string accepted" + gdb_test "maint packet ${prefix}:0:" $::ok_re "empty string accepted" + + gdbserver_exit false +} + +test_qtdpsrc diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc index e7c1f0c9e7f..00cbec8c8da 100644 --- a/gdbserver/tracepoint.cc +++ b/gdbserver/tracepoint.cc @@ -2532,15 +2532,40 @@ cmd_qtdpsrc (char *own_buf) srctype[packet - start_of_type] = '\0'; ++packet; + /* The QTDPsrc spec says that SLEN can be longer than the delivered string + in order to support strings longer than can fit in a single packet. + The START indicates where the string from this packet fits into the + overall string. + + However, gdbserver has never (yet) supported strings larger than a + single packet, which is why the START field here is ignored, and why we + check SLEN against the remaining packet length. */ ULONGEST start, slen; packet = unpack_varlen_hex (packet, &start); ++packet; /* skip a colon */ packet = unpack_varlen_hex (packet, &slen); ++packet; /* skip a colon */ - char *src = (char *) xmalloc (slen + 1); - size_t nbytes = hex2bin (packet, (gdb_byte *) src, strlen (packet) / 2); + + size_t len = strlen (packet); + if (slen != len / 2 || len % 2 != 0) + { + trace_debug ("Tracepoint error: invalid string length %s in: %s", + pulongest (slen), own_buf); + write_enn (own_buf); + xfree (srctype); + return; + } + + char *src = (char *) xmalloc (len / 2 + 1); + size_t nbytes = hex2bin (packet, (gdb_byte *) src, + static_cast (len / 2)); src[nbytes] = '\0'; + /* Because we check SLEN against the packet length above, either the packet + is fully parsed, or hex2bin will have thrown an error on seeing a + non-hex character. */ + gdb_assert (packet[nbytes * 2] == '\0'); + struct source_string *newlast = XNEW (struct source_string); newlast->type = srctype; newlast->str = src; -- 2.25.4