From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id ifeXH1vCBGvEUjAAWB0awg (envelope-from ) for ; Mon, 23 Nov 2026 17:46:51 -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=IR86JL4F; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 7DC751E167; Mon, 23 Nov 2026 17:46:51 -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 6E9511E023 for ; Mon, 23 Nov 2026 17:46:50 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4C5114580B03 for ; Mon, 23 Nov 2026 22:46:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4C5114580B03 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=IR86JL4F 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 7528A46569F5 for ; Mon, 23 Nov 2026 22:46:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7528A46569F5 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 7528A46569F5 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=1795473982; cv=none; b=BbH7MICgpBMBfPl1w9+KVDSsrS240Jn2bKoUIYPur9V3Bpgm8Ss594BPwnbi0txx5tTFlRyzrp/NOliCjT/2xVaTsUvd1rv4/i+gv1xFPwJJXJYYB/OfH6ONOBrRqZtqLraJKUiFg+zG/Fo3q/L0qKxGOLbTr2EJdVl3oSdBzuQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795473982; c=relaxed/simple; bh=9JMp4+InlQ8G2rfsAT/w0fpi0WiaZjezJung9t0HbPY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=etIsB5fF3Hb9QiWuWJIlYD09MjRfbpfrnwAA1uwZDDEsovJPzLrjtLhmdBMhCQQ1bAUAzWSmBwmLvkT1N7ufPSQVhRusgyDbnJUil837yulxkSgr2+gV1ki6mq9vsoewWhEalwL93C/rd1Zq9wfzMqhAj9VfH5BNGxwtKM6uUq0= 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=IR86JL4F DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7528A46569F5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795473982; 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=j5rzO92QmoDZAct+XFpE+GUFfwO3an/YGnalR9mT2VQ=; b=IR86JL4Fm/D4J8A5QIiJzGBeVVGnyFR5VIwktDwu38iRGKCOncemJX7k563G+N/Mq+Gydb KJw1kyKZjNP1zY8cuuG8KzOXrW0keTYEkFM8BbXkTmnkgEleENpvf5kHWeVGLN33kkqK11 TZQWl8ei5HkolZnmyCA4KDTu7AkCxI0= 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-115-2TiscqZmNXyDr8yrY4fm0Q-1; Mon, 23 Nov 2026 22:46:20 +0000 X-MC-Unique: 2TiscqZmNXyDr8yrY4fm0Q-1 X-Mimecast-MFC-AGG-ID: 2TiscqZmNXyDr8yrY4fm0Q_1795473979 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-48e0af48c18so2298431f8f.2 for ; Mon, 23 Nov 2026 14:46:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795473979; x=1796078779; 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=j5rzO92QmoDZAct+XFpE+GUFfwO3an/YGnalR9mT2VQ=; b=g17jPlDN1R550dH4rJnsEalVpNPBno/NaqWXximbGD4VqCi0W0zxExP7h58NsWCsRy 3Vvdj+KXr2uUu8kKw4Tv53AfHwJQ+grPJEKsSMzpS7hyBagzTtE+0pUt6jtRAEE+1Mkd A7Ewi66AkTTTLX/dCM4ScffV0YNxv9jjhoy7oG3Cx9Am19hzf9Pu8SNPnOa3CZ2Zu/k/ kzwtZQC5P1LylrEW6CErXxH2ibZ9QAB2+dXsDsFGtcLvR147ZTsF08JmgEvFljKvQT1g u6jbdPduP+EqyFZmizJbPaMieOlvx2rjaz9GtX4+XQq/1pwZpzoOW1aU88xbJGXjTUth +roQ== X-Gm-Message-State: APuU4Z8oH8PNRC/OtfbPji8EH3Y03P0ruf5CNdsk3+XWDSHHflWRQiLZ nfYIVLfhYGSi3zPsL713FdAYftoR55fWTzuDfvhU7B15SI2xqThsA7C9lVs1nhqFUsDXwGQSphD sRRYuVmuIQ+JRqxvWOlPR4KD0GzcqG81Z7fzWY5wkfH8SeRf3kK319kpW76tY/vZjqSIrHoiJTN d53nwWDZMeE5riamEame6bsayipXOOAvrmfaFpaCwKf1JX6+w= X-Gm-Gg: AZFdTZdSXa7qPgUWeeD97SXdsz0BjvAUVt8BRYZm7ZB919JIXJVHhld9eGutWJjEdqC e4EoFVXPDlzGpsOlDgF4o0++6XFxM64a2TIsQJHKwEiji4XBpNUf3PjbceKL3srNXhXQBJ9DQoU VcUGoMzCU0b86K+cT+I79f2DiQT3JQffExJk3HeZ7p1g5Cy0pZ6any2TCz//M86xOQeRN6JQm5k wFY9KieIM0xojmjquAoFphaAy/PlRoeOASRrRwF3F0pKMMSVJ3uBgUG1WsbRr9YooNbQKMKE6l8 5U2GijhQqqeUiI4MuvtuaECgCFkJHlniENJ7SKc1FYGfnGz2zBFgyBGrv7RlM+/3yKxWDFYHtUn DwkUphRRRFIfc1j7RRFD9eU1/M41Z/GLa6SXxqMN28RvwLA== X-Received: by 2002:a5d:5d0e:0:b0:48d:f831:2681 with SMTP id ffacd0b85a97d-4abb7d465a9mr26143774f8f.38.1795473978483; Mon, 23 Nov 2026 14:46:18 -0800 (PST) X-Received: by 2002:a5d:5d0e:0:b0:48d:f831:2681 with SMTP id ffacd0b85a97d-4abb7d465a9mr26143733f8f.38.1795473977766; Mon, 23 Nov 2026 14:46:17 -0800 (PST) Received: from localhost ([83.216.138.189]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab658e2d55sm24772861f8f.5.2026.11.23.14.46.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Nov 2026 14:46:16 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 2/2] gdbserver: fix buffer overflow in cmd_qtstatus Date: Mon, 23 Nov 2026 22:46:09 +0000 Message-ID: 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: fnGwzS4ehwdjR5CYAWA3K5C1VUOPjjde9JYrb8_fAGQ_1795473979 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 In cmd_qtstatus (gdbserver/tracepoint.cc), the response packet was built using sprintf into a buffer of PBUFSIZ + 1 (131105) bytes. See gdbserver/server.h for both the definition of PBUFSIZ and for the client_state constructor, where the buffer is allocated. The response packet contains several different fields, but two that are of interest for this commit are tracing_user_name and tracing_notes, which are placed into the response packet as hex-encoded string. Due to the use of sprintf, if the combined response packet exceeds PBUFSIZ + 1 bytes then sprintf overflows the packet buffer resulting in undefined behaviour. The tracing_user_name and tracing_notes fields which are mentioned above can be set to arbitrary length string by the user using QTNotes packets. Actually the length of the hex-encoded data for each field is limited to just under PBUFSIZ bytes. A QTNotes packet contains the packet name, the field being set ("user" or "notes") then the hex-encoded payload. An incoming packet is limited to PBUFSIZ + 1 bytes. What this means is that if both the tracing_user_name and tracing_notes fields are set to large strings, then the combined qTStatus response packet can overflow the output buffer, triggering undefined behaviour. Fix this by changing cmd_qtstatus to take a gdb::span rather than a bare char *, so the buffer size is carried along with it. Replace sprintf with snprintf using packet.size () as the bound. If the formatted output would exceed the buffer (the snprintf return value is negative or >= packet.size ()), call write_enn to return an error response to GDB rather than sending a truncated or corrupt packet that GDB would likely misparse. Currently I am creating the gdb::span in handle_tracepoint_query, this might not seem much better than just hard-coding PBUFSIZ into cmd_qtstatus. However, I think it is. Long term, I think most (maybe all of the functions that take a packet buffer pointer should be updated to take gdb::span. But if I try to add the gdb::span throughout the whole call stack in this commit then the commit will get very large. The change as presented here fixes the existing bug, and is just a tiny part of an eventual piece of work to start using gdb::span more in gdbserver. A new test, gdb/testsuite/gdb.server/overfull-qtstatus-packet.exp, checks that small payloads produce valid qTStatus replies, that a payload large enough to overflow the buffer causes an E01 response, and that reducing the payload size again restores normal behaviour. --- .../gdb.server/overfull-qtstatus-packet.exp | 177 ++++++++++++++++++ gdbserver/tracepoint.cc | 52 +++-- 2 files changed, 209 insertions(+), 20 deletions(-) create mode 100644 gdb/testsuite/gdb.server/overfull-qtstatus-packet.exp diff --git a/gdb/testsuite/gdb.server/overfull-qtstatus-packet.exp b/gdb/testsuite/gdb.server/overfull-qtstatus-packet.exp new file mode 100644 index 00000000000..0dc436f8392 --- /dev/null +++ b/gdb/testsuite/gdb.server/overfull-qtstatus-packet.exp @@ -0,0 +1,177 @@ +# 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 what happens when we cause the qTStatus packet to grow so large +# it can no longer fit in the gdbserver allocated buffer. We expect +# that gdbserver should start sending back errors. + +load_lib gdbserver-support.exp + +require allow_gdbserver_tests + +# Due to environment setting. +require {!is_remote host} + +standard_testfile normal.c + +if {[build_executable "failed to build" $testfile $srcfile]} { + return +} + +# Start GDB and gdbserver ready for the test. Return false if +# anything goes wrong, otherwise, return true. +proc start_gdbserver_for_test {} { + save_vars { env(ASAN_OPTIONS) env(TSAN_OPTIONS) } { + # The large QTNotes packets sent in this test trigger + # ASAN/TSAN errors which are disabled by these settings. + append_environment ASAN_OPTIONS allocator_may_return_null 1 + append_environment TSAN_OPTIONS allocator_may_return_null 1 + + 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 + } +} + +# Disassemble the main function and return a list containing the addresses +# of each instruction. The returned addresses have the leading 0x and any +# leading 0s stripped off. +proc disassemble_main_for_addresses {} { + # 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" + } + } + + return $addresses +} + +# Test for overfull qTStatus packet. +proc_with_prefix test_qtstatus {} { + if {![start_gdbserver_for_test]} { + return + } + + # Disassemble main and find an address at which we could place a + # tracepoint. + set addresses [disassemble_main_for_addresses] + + # We need at least 1 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 + } + } + + # Set 'user' and 'notes' text using separate QTNotes packets. The + # payload in each case is a hex-encoded data block of + # PAYLOAD_SIZE. As the payload is hex-encoded PAYLOAD_SIZE must + # be even for the payload to be valid. + # + # Once set, use qTStatus to read the fields back. The reply will + # include both 'user' and 'notes' text. + # + # However, the maximum buffer size within gdbserver is ~131104 + # bytes, so when the payload size is 66000 bytes, the two fields + # combined (plus the other qTStatus fields) will exceed the + # maximum packet size. In this case qTStatus should return an + # error. + # + # We finish with a small size to show that the error state isn't + # "locked in", reduce the user/notes fields sizes and the qTStatus + # will start working again. + foreach_with_prefix payload_size { 0 2 100 1000 10000 50000 66000 4 } { + set user_payload [string repeat a $payload_size] + set notes_payload [string repeat b $payload_size] + + gdb_test "maint packet QTNotes:user:$user_payload;" "\r\nreceived: \"OK\"" \ + "set 'user' text via QTNotes" + gdb_test "maint packet QTNotes:notes:$notes_payload;" "\r\nreceived: \"OK\"" \ + "set 'notes' text via QTNotes" + + if { $payload_size > 65000 } { + # Send qTStatus. In this case the payload is such that the + # reply exceeds the maximum packet size, we should get back an + # error. + gdb_test "maint packet qTStatus" "\r\nreceived: \"E01\"" \ + "over sized qTStatus reply becomes an error" + } else { + # Send qTStatus. In this case the payload is small enough that + # we should get back a valid reply. + gdb_test_multiple "maint packet qTStatus" \ + "got expected qTStatus reply" { + -re -wrap "\r\nreceived: \"T0;.*username:(a*);notes:(b*):\"" { + set user_string $expect_out(1,string) + set notes_string $expect_out(2,string) + + gdb_assert { $user_string eq $user_payload \ + && $notes_string eq $notes_payload } \ + $gdb_test_name + } + } + } + } + + gdbserver_exit false +} + +# This test receives some large packets from the remote, which are then +# displayed by GDB and processed by expect. Doing this character at a time +# make this test quite slow. Increase the timeout to compensate. +with_read1_timeout_factor 25 { + test_qtstatus +} diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc index bfe1ecc7db6..2f5b52ea3eb 100644 --- a/gdbserver/tracepoint.cc +++ b/gdbserver/tracepoint.cc @@ -3372,8 +3372,10 @@ cmd_qtframe (char *own_buf) sprintf (own_buf, "F-1"); } +/* Respond to a qTStatus packet, writing the result into PACKET. */ + static void -cmd_qtstatus (char *packet) +cmd_qtstatus (gdb::span packet) { /* Translate the plain text of the notes back into hex for transmission. */ @@ -3429,24 +3431,31 @@ cmd_qtstatus (char *packet) strcat (stop_reason_rsp, buf3); } - sprintf (packet, - "T%d;" - "%s:%x;" - "tframes:%x;tcreated:%x;" - "tfree:%x;tsize:%s;" - "circular:%d;" - "disconn:%d;" - "starttime:%s;stoptime:%s;" - "username:%s;notes:%s:", - tracing ? 1 : 0, - stop_reason_rsp, tracing_stop_tpnum, - traceframe_count, traceframes_created, - free_space (), phex_nz (trace_buffer_hi - trace_buffer_lo, 0), - circular_trace_buffer, - disconnected_tracing, - phex_nz (tracing_start_time), - phex_nz (tracing_stop_time), - buf1, buf2); + int res = snprintf (packet.data (), packet.size (), + "T%d;" + "%s:%x;" + "tframes:%x;tcreated:%x;" + "tfree:%x;tsize:%s;" + "circular:%d;" + "disconn:%d;" + "starttime:%s;stoptime:%s;" + "username:%s;notes:%s:", + tracing ? 1 : 0, + stop_reason_rsp, tracing_stop_tpnum, + traceframe_count, traceframes_created, + free_space (), phex_nz (trace_buffer_hi + - trace_buffer_lo, 0), + circular_trace_buffer, + disconnected_tracing, + phex_nz (tracing_start_time), + phex_nz (tracing_stop_time), + buf1, buf2); + + /* Don't send back a partial packet. We cannot know which field in the + reply was truncated, so a partial reply cannot be safely interpreted by + the debug client. */ + if (res < 0 || res >= packet.size ()) + write_enn (packet.data ()); } static void @@ -3943,7 +3952,10 @@ handle_tracepoint_query (char *packet) { if (streq ("qTStatus", packet)) { - cmd_qtstatus (packet); + /* This assumes the size of PACKET, e.g. PACKET is client_state::own_buf. + This was true at the time this comment was added. A better solution + would be to pass a gdb::span into this function. */ + cmd_qtstatus ({packet, PBUFSIZ + 1}); return 1; } else if (startswith (packet, "qTP:")) -- 2.56.1