From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id MZWSDul0BGubvi8AWB0awg (envelope-from ) for ; Mon, 23 Nov 2026 12:16:25 -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=VwfFMDG6; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 28C201E023; Mon, 23 Nov 2026 12:16:25 -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 E24FF1E023 for ; Mon, 23 Nov 2026 12:16:23 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 45CA5465DE65 for ; Mon, 23 Nov 2026 17:16:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 45CA5465DE65 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=VwfFMDG6 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 1E1C646BBFA8 for ; Mon, 23 Nov 2026 17:15:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1E1C646BBFA8 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 1E1C646BBFA8 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=1795454145; cv=none; b=p7BOLnL2NqF8IT+2kgqGmOld/GmR4DA6Ex5qqPNH7gp4pfWZabMdnkounzgqfEQJ5EtNu+J76YCaiT/ZiBw7zhlRBVHy6CeCsf6sHtYdmfOmCeC01/sBOSZKXVZWV0ROhN8DucCzgFbzeS3NZ9BHBr4oMmhRGQvrV0hVIDHVPoI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795454145; c=relaxed/simple; bh=xL8C3n7Wh7uQKpGCT3FfijTuvHmrjbk443huvKWd6/g=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=bcFVGV8RH3HhSJEE7IYE+XKJK4rtmj8iigmNZFjONuqL9Utg4D7pIicmWkdzgDF7AsN7r/+xFxqWyGwzQ2PKpNilskpY9dNTi5B0HOu7GF+Zi3Jnch9EFmtmMdtePzq9xTmmiVJa6UW4eboopHXfHQ33+FbBNzBXPCVjuzjgjLw= 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=VwfFMDG6 DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1E1C646BBFA8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795454144; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=G9KNIvgcxaWfOaHD22rAgiImjeIZtI6gkqjAUoFjqN8=; b=VwfFMDG6QY/0zPqsv78aKb2qGygh113CFkH0gh+UegvkfBU2vSiJnxIVzLc+6rvonmfVs5 FY7r+1Gmt12cRdyaLOpoDGHYYedpJd6twnOLjPd148l5kMvwM9o0tTbglYfdNa+71tHorq HJwGqsn/cq9T3DlTw6b9qUoLTdLzACM= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-577-HYPFybFAM9muQyUxKtiSVQ-1; Mon, 23 Nov 2026 17:15:40 +0000 X-MC-Unique: HYPFybFAM9muQyUxKtiSVQ-1 X-Mimecast-MFC-AGG-ID: HYPFybFAM9muQyUxKtiSVQ_1795454139 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4a359c7f47bso29809125e9.2 for ; Mon, 23 Nov 2026 09:15:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795454139; x=1796058939; h=content-type:mime-version:message-id:date:references:in-reply-to :subject:to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=G9KNIvgcxaWfOaHD22rAgiImjeIZtI6gkqjAUoFjqN8=; b=mkP4JrrJ2DPkm7zbE9mLKq+eAc/G0fzP1jmPR0PM1tLiJNRW4jvs1VIpxU9TPl3i/r mkvIlyfdS6wRlNdWXsEcYMztaH4yLwfkrK0O2hcRZkXeGwGZVk5+UCzXbW9SEq5veUtU cM9qiOGPcehFikuk4yJ+aq/AZ/C9aqZ5gvzBh2M5BOm2rjIsdWUUQnz+Hrf8INVbPcA8 7g3SIjyCKysq8tmQIh9LfvSOHfXilQBVmvJ6AbOET0/qcafFL2z9fFccq1jgO0VBtjLU 6QnEMByueg6FBbpMkF93F9tX3yWb1ilvRKm39cCExL/h72ZyQV5syOpTsks0bq50hE7u MGxA== X-Forwarded-Encrypted: i=1; AL+hErGoW2vGN+2pozpzTTjZzph+EFxYPzIX821LIiPpgXmBFx61/HN+PEpcCm4F4PpravwxpMAE6Aw/5vgbdw==@sourceware.org X-Gm-Message-State: APuU4Z/RZ32KdrEmUYrKoIP0kn1GsvgsW/XKrfd9FUQCgKDQGUBJtzkT 5HW62bnDne9pXVUX87umqokpDfuyZu6T0JHSzUtK2AaDrMAUvnM+HDwYlEbj+mM7wwYc6w1KqpP wPEV7dUH7ngBy1gETE1jzX2jdxsS9kqBxCMV0OejiflNxccBttMtViE88069VlLs= X-Gm-Gg: AZFdTZf8XnQRJGNkP/cHoOYEnTXM6TBH8MwzOZ0X2+o0MLwSifFbzPXMMsixLmTepCN peJDf2juzCscxoAQtJPeuga2XNCGcbN2DrfKO5v9TI5Y1D7Y+H7EQcjkLPrnse37fPaCTCm0gsV IRh0OYQvV2jb+/H36M0kM5jIZRiU/hSHdUBLyN0zW764WE4NKRdKzNumNRiMOWLoHz964BO/S3Q EY0xO1zNjkH37+7z3JnbRcT+EzWHTXeAN+u71D3AlqMKIv87OqDEWAOj0qr+g5IKIGCoCxmNGWd nyjTPURkzs31nM9V+1NaegOjkRNm2qDkGCvRiuZMvhWgvWugzBzNNOMypaji+CDVkDygdqr4Kom ZmFMV8sEMRbYtQd9ciosvA8V4T9TJ5gMbmW3MNvSj9UOU7MM= X-Received: by 2002:a05:600d:1c:b0:4bb:685e:cc41 with SMTP id 5b1f17b1804b1-4bb8c526ac4mr207645905e9.8.1795454139078; Mon, 23 Nov 2026 09:15:39 -0800 (PST) X-Received: by 2002:a05:600d:1c:b0:4bb:685e:cc41 with SMTP id 5b1f17b1804b1-4bb8c526ac4mr207645045e9.8.1795454138388; Mon, 23 Nov 2026 09:15:38 -0800 (PST) Received: from localhost ([83.216.138.189]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb8533579csm326804775e9.2.2026.11.23.09.15.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Nov 2026 09:15:37 -0800 (PST) From: Andrew Burgess To: Keith Seitz , gdb-patches@sourceware.org Subject: Re: [PATCH 2/5] gdbserver: prevent buffer overrun for QTDPsrc packet In-Reply-To: References: Date: Mon, 23 Nov 2026 17:15:36 +0000 Message-ID: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 0VCg2wg2gR0tR_NKTzphEqJjtWGvW5Pr7yRAwihvYbA_1795454139 X-Mimecast-Originator: redhat.com Content-Type: text/plain 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 Keith Seitz writes: > Hi, > > I just have a small style nit that I'd like to point > out. Nothing major here, and you are, of course, free > to ignore my Tcl pedantry. > > On 11/16/26 3:10 AM, Andrew Burgess wrote: >> 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] > > List assignments to variables like this are more commonly done > with `lassign': > > lassign $res gdbserver_protocol gdbserver_gdbport > > [I did warn you! :-)] I'll change this as you suggest, see the updated patch below. > >> 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'; > > Just for the record, hex2bin can throw, so this can leak > `src', but you fix this in patch 5/5. I don't know if this is > worth mentioning in the commit message for someone reviewing > this patch in the future? I fix this in patch #5. I have added some additional text to the commit message to acknowledge that this bug exists, and explain that it is fixed in a later patch. See the updated patch below. Let me know what you think. Thanks, Andrew --- commit d5f102b72ca7644cfad07691ff6ed8203ddb41eb Author: Andrew Burgess Date: Thu Nov 12 10:59:54 2026 +0000 gdbserver: prevent buffer overrun for QTDPsrc packet 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 focused. Additionally there are existing memory leaks in the code, buffers allocated with xmalloc can be leaked if hex2bin throws an exception (which it does if it encounters a non-hex character). These leaks are resolved by a later patch in this series. 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 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..9b585d4de46 --- /dev/null +++ b/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp @@ -0,0 +1,140 @@ +# 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 ""] + lassign $res gdbserver_protocol gdbserver_gdbport + 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;