From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id SZudOI3o8WrRZToAWB0awg (envelope-from ) for ; Mon, 09 Nov 2026 10:36:45 -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=YoIJHl9s; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id D6806378002; Mon, 09 Nov 2026 10:36:45 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.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,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.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 57C581E023 for ; Mon, 09 Nov 2026 10:36:44 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 79F084BA7981 for ; Mon, 9 Nov 2026 15:36:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 79F084BA7981 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=YoIJHl9s 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 4F3904BA7992 for ; Mon, 9 Nov 2026 15:36:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4F3904BA7992 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 4F3904BA7992 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=1794238566; cv=none; b=mbXlWmLh2GQdHq30H5xCpd3w6YinUK5Rc3ltvfUZCoYaavLzMvZaqZqiTytTf2Ow2tdu09nfg2mX5vFhgl1McfmCr/SHlOcHDgcOhu5aKWSSRPcaIWM+aYu4vHhFQBZt9hNjwhjja1aZDo5ndcLs+MCFnpxgWDSpnndaPRsn9jI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794238566; c=relaxed/simple; bh=vxWa9nsA66aW92raL/zZ0I/+fep1IFb08dff4DycaPI=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=fAqKEtWQ1xj5Hqt0kYe6wUdI7PuTPSvNiKirfY+KtFaMuuE5zOCgVqUl5FxLIb0VfjVsdksQFp2WJ/3W9A+FRwertvqDPCXoE2wbFcBX6KY+D2OLsbkf5Bcgu3Iml77nau117n9uXM9i3HzoUME5iksjDP2CtH47EZSJv3+qs1U= 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=YoIJHl9s DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4F3904BA7992 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1794238565; 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=Gua9Ca11HJOazFpBq8VphvteRXTbRkjNKczjpALj+VI=; b=YoIJHl9ssefsdzgD9sYnewrA33wzzAiX1EOrsT7JaF7Z4hy7WdgmwC7VoxliEL4/51Lsrj Qn6jjD0bGry+v1fuO22/Qv812EDd9rD0zhC1rk2ad2WVDB1tFPvSluuM13y3yvydS0hA9/ lxRGYMP1SmLym2kgcZ9kFW1AzCXBbWA= 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-356-W-j7BSiVPd6lSjCozGPlHg-1; Mon, 09 Nov 2026 10:36:03 -0500 X-MC-Unique: W-j7BSiVPd6lSjCozGPlHg-1 X-Mimecast-MFC-AGG-ID: W-j7BSiVPd6lSjCozGPlHg_1794238563 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4a77c1cbd15so8169345e9.0 for ; Mon, 09 Nov 2026 07:36:03 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794238562; x=1794843362; 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=Gua9Ca11HJOazFpBq8VphvteRXTbRkjNKczjpALj+VI=; b=Hge1yJAklcv9MKb38Gk0J+jXgBwVoVZiXh8DdgasVKq08HRilDpHep0tI5zrol18jB 92nfBhAaOURSEvfAcYrHA/r5VlT/0e08e6xoYwGa0TQjbUEZIze4L8zwjbRbfgp99k72 5IbPVS5nD6bBuGOcaTT4gziT/G5IGb31tR8sbv8Uqb++2iH81lOhnXPTxFG0FAg883Ot ZvobPz2PZHU8dPlNBrlAzZZCmOK1fiQ/c7XfDJXIvk0+fLG0GgwI3U3ft5fckLSWLiKi QcgKdaDGF7Usyv8Kod7AByIUfb+I3Mi7wUNuv/bfF9SQZkEw7JGMAGcHKdO+nfv7AWrV /6YA== X-Gm-Message-State: APuU4Z9yz30Sbg1dBI00Hqfv1xs2cVp/jcLcSj9PfTPUYEZvxFAShmoS 4TAxnUknVubnbw3Ss91mDqBt+5vFHihzjBn5kVmwQbx3IW1EG7K2HHWN+nTDCmbQWMMmrI/ZZ0p QkynSzCYVgwcR+vsmtL4fYQX+Mc433cxfnnkHQm0wajbrZbcHesSLvuraUZddcp62uTqfZTynnu rapABDhV1X6b2f4rXTY7UbFKEPv/g9fgAwYsE7ftEluAWWY1o= X-Gm-Gg: AYBFou22TRBOSycZaNUJ4h4Nwz7gnFq2eOOPagHkpYA6Todg/6/S+yow123tRysgdtY zUJKoy515SCrKNYEiZZkDppasYpW64h3cKCE1sEpZcjxJW9xSw42yNJSgWqLmVLTKQG7fWOT1iD rGvBUtQLlylzSXiUfhaQGNlFRHxgM9DGSwQrK3w+2L7AJ9sNn7A1T7EMxnxjQuHl62Y3wYTkZ8t AdZReuhzmBOp2+rBUmC/54EEcDiv7eu5jq9AMKS9cfPdr9JpXRzC6TwrI1ZUveISOp4Y+jAtHSn 1dfJZSf1lsxtq2+e51sbDbreeaLoicHCj7wxE2OYAxk5JvWsyaskYr+kLK7Nn+dHHpubWBxBq1Q wuh84/Vr/cUGfsEwuof1vAw1/JZ44bCb+nh49pRS8NrqV1g== X-Received: by 2002:a05:600c:310e:b0:4a2:d7e:5ccb with SMTP id 5b1f17b1804b1-4b963c82e2cmr82556705e9.15.1794238562297; Mon, 09 Nov 2026 07:36:02 -0800 (PST) X-Received: by 2002:a05:600c:310e:b0:4a2:d7e:5ccb with SMTP id 5b1f17b1804b1-4b963c82e2cmr82555985e9.15.1794238561670; Mon, 09 Nov 2026 07:36:01 -0800 (PST) Received: from localhost ([83.216.138.189]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-495966f3163sm10708984f8f.11.2026.11.09.07.36.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Nov 2026 07:36:00 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 3/3] gdb/remote: improve error checking for 'p' packet reply Date: Mon, 9 Nov 2026 15:35:52 +0000 Message-Id: <74dfd6c36ec51bc513bc84ec5480d8c3421ca3a4.1794238395.git.aburgess@redhat.com> 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: lrqxngdWCR7xvDsZdL9AvMVf9hH1NTVGtwdLGP2CPkY_1794238563 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 Improve the remote_target::fetch_register_using_p function (remote.c) in a couple of ways. When building the outgoing packet use xsnprintf; this allows for bounds checking as the packet is being written. The chances of the outgoing packet overflowing are pretty slim, but using a bounded write seems better than not, so let's do that. Next, when parsing the reply packet the result is written into a buffer which is the size of the register being fetched. Unfortunately the loop that reads the packet does not have any bounds checking, which allows for buffer overflow. Convert the manual loop which calls fromhex on each character to instead use hex2bin which allows a limit to be applied to the number of bytes parsed; this prevents buffer overflow. hex2bin returns the number of bytes parsed, which also allows me to check for cases where the remote fails to supply all of the expected bytes. For testing I made use of the gdbserver option --disable-packet=gG which I added in the previous commit. I ran the testsuite like this: make check-gdb RUNTESTFLAGS="--target_board=native-gdbserver \ GDBSERVER='/path/to/patched/gdbserver --disable-packet=gG'" I've also added a new test that makes use of the gdbreplay mechanism to adjust the size of the returned register. This allows testing of the two error cases: register value too short, and register value too long. As in the previous commit the trace related tests fail as the trace functionality depends on the 'g' packet being supported, but otherwise I saw no regressions after this commit. --- gdb/remote.c | 43 +++--- gdb/testsuite/gdb.replay/p-packet.c | 22 +++ gdb/testsuite/gdb.replay/p-packet.exp | 213 ++++++++++++++++++++++++++ 3 files changed, 256 insertions(+), 22 deletions(-) create mode 100644 gdb/testsuite/gdb.replay/p-packet.c create mode 100644 gdb/testsuite/gdb.replay/p-packet.exp diff --git a/gdb/remote.c b/gdb/remote.c index e33e9f0181d..86024bb7311 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -9242,27 +9242,22 @@ int remote_target::fetch_register_using_p (struct regcache *regcache, packet_reg *reg) { - struct gdbarch *gdbarch = regcache->arch (); - struct remote_state *rs = get_remote_state (); - char *buf, *p; - gdb_byte *regp = (gdb_byte *) alloca (register_size (gdbarch, reg->regnum)); - int i; - if (m_features.packet_support (PACKET_p) == PACKET_DISABLE) return 0; if (reg->pnum == -1) return 0; - p = rs->buf.data (); - *p++ = 'p'; - p += hexnumstr (p, reg->pnum); - *p++ = '\0'; + /* Send the packet asking for the register. */ + struct remote_state *rs = get_remote_state (); + xsnprintf (rs->buf.data (), get_remote_packet_size (), "p%s", + phex_nz (reg->pnum, 0)); putpkt (rs->buf); + + /* Read the reply. */ getpkt (&rs->buf); - buf = rs->buf.data (); - + /* Check the packet status. */ packet_result result = m_features.packet_ok (rs->buf, PACKET_p); switch (result.status ()) { @@ -9277,23 +9272,27 @@ remote_target::fetch_register_using_p (struct regcache *regcache, } /* If this register is unfetchable, tell the regcache. */ + char *buf = rs->buf.data (); if (buf[0] == 'x') { regcache->raw_supply (reg->regnum, NULL); return 1; } - /* Otherwise, parse and supply the value. */ - p = buf; - i = 0; - while (p[0] != 0) - { - if (p[1] == 0) - error (_("fetch_register_using_p: early buf termination")); + /* Otherwise, parse the register value from the hex string. */ + struct gdbarch *gdbarch = regcache->arch (); + int reg_size = register_size (gdbarch, reg->regnum); + gdb_byte *regp = (gdb_byte *) alloca (reg_size); + int bytes_converted = hex2bin (buf, regp, reg_size); - regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]); - p += 2; - } + /* Check the whole packet was parsed, and that the packet length matches + the register size. */ + if (buf[bytes_converted * 2] != '\0' || bytes_converted != reg_size) + error (_("Remote 'p' packet reply for register %s is of unexpected " + "length (expected %d bytes): %s"), + gdbarch_register_name (gdbarch, reg->regnum), reg_size, buf); + + /* Supply the value to the register cache. */ regcache->raw_supply (reg->regnum, regp); return 1; } diff --git a/gdb/testsuite/gdb.replay/p-packet.c b/gdb/testsuite/gdb.replay/p-packet.c new file mode 100644 index 00000000000..6a0e311ef41 --- /dev/null +++ b/gdb/testsuite/gdb.replay/p-packet.c @@ -0,0 +1,22 @@ +/* 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 . */ + +int +main (void) +{ + return 0; +} diff --git a/gdb/testsuite/gdb.replay/p-packet.exp b/gdb/testsuite/gdb.replay/p-packet.exp new file mode 100644 index 00000000000..f59fc13faa2 --- /dev/null +++ b/gdb/testsuite/gdb.replay/p-packet.exp @@ -0,0 +1,213 @@ +# 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 error handling for the 'p' packet. Start gdbserver with the g/G +# packets disabled. Start GDB, connect to the server, and then run 'info +# registers', this will make use of the 'p' packet. +# +# Update the recorded logfile to replace the reply to the first 'p' packet, +# the updated reply will be an invalid size. +# +# Replay the log and check that the 'info registers' gives a suitable error +# message. + +load_lib gdbserver-support.exp +load_lib gdbreplay-support.exp + +require allow_gdbserver_tests +require has_gdbreplay + +standard_testfile + +if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } { + return +} + +# Check that remote register 0 is a suitable size. The test could be +# updated to handle other register sizes, but for now handle 4 or 8 byte +# registers. +set reg_size 0 +gdb_test_multiple "maint print remote-registers" "" -lbl { + -re "\r\n\\S+\\s+$::decimal\\s+$::decimal\\s+$::decimal\\s+($::decimal)\\s+\[^\r\n\]+\\s+0\\s+0\\s*(?=\r\n)" { + set reg_size $expect_out(1,string) + exp_continue + } + + -re "\r\n$gdb_prompt $" { + pass $gdb_test_name + } +} + +if { $reg_size != 4 && $reg_size != 8 } { + unsupported "remote register 0 is an unsupported size" + return +} + +# Connect to gdbserver and run to a breakpoint in main, then use the 'info +# registers' command. Record the remotelogfile into the REMOTELOG log +# file. +proc_with_prefix record_initial_logfile { remotelog } { + # Make sure we're disconnected, in case we're testing with an + # extended-remote board, therefore already connected. + gdb_test "disconnect" ".*" + + gdb_test_no_output "set sysroot" \ + "setting sysroot before starting gdbserver" + + # Start gdbserver like: + # gdbserver :PORT .... + set res [gdbserver_start "--disable-packet=gG" $::binfile] + set gdbserver_protocol [lindex $res 0] + set gdbserver_gdbport [lindex $res 1] + + gdb_test_no_output "set remotelogfile $remotelog" \ + "setup remotelogfile" + + # Connect to gdbserver. + if {![gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] == 0} { + unsupported "couldn't start gdbserver" + return + } + + # If we're connecting as 'remote' then we can't use 'runto'. + gdb_breakpoint main + gdb_continue_to_breakpoint "continuing to main" + + gdb_test "info registers" ".*" +} + +# Connect to gdbreplay using the REMOTELOG log file. Runs to a breakpoint in +# main and then uses the 'info registers' command. +proc_with_prefix replay_without_error { remotelog } { + clean_restart $::testfile + + # Make sure we're disconnected, in case we're testing with an + # extended-remote board, therefore already connected. + gdb_test "disconnect" ".*" + + gdb_test_no_output "set sysroot" "setting sysroot" + + set res [gdbreplay_start $remotelog] + set gdbserver_protocol [lindex $res 0] + set gdbserver_gdbport [lindex $res 1] + + # Connect to gdbreplay. + if {![gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] == 0} { + unsupported "couldn't start gdbreplay" + return + } + + gdb_breakpoint main + gdb_continue_to_breakpoint "continue to main" + + gdb_test "info registers" ".*" +} + +# Connect to gdbreplay using the REMOTELOG log file. Runs to a breakpoint in +# main and then uses the 'info registers' command. Expect an error from the +# 'info registers' command, the error will include the VALUE string. +proc_with_prefix replay_with_error { remotelog value } { + clean_restart $::testfile + + # Make sure we're disconnected, in case we're testing with an + # extended-remote board, therefore already connected. + gdb_test "disconnect" ".*" + + gdb_test_no_output "set sysroot" "setting sysroot" + + set res [gdbreplay_start $remotelog] + set gdbserver_protocol [lindex $res 0] + set gdbserver_gdbport [lindex $res 1] + + # Connect to gdbreplay. + if {![gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] == 0} { + unsupported "couldn't start gdbreplay" + return + } + + gdb_breakpoint main + gdb_continue_to_breakpoint "continue to main" + + gdb_test "info registers" \ + "Remote 'p' packet reply for register \[^\r\n\]+ is of unexpected length \\(expected $::decimal bytes\\): $value" \ + "info registers gives an error" +} + +# Create a copy of file SRC into DST. While making the copy look for the +# 'info registers' command. After the 'info registers' GDB will send a 'p' +# packet, and the remote will send a reply. +# +# Rewrite the reply to the first 'p' packet, replacing the returned register +# value with NEW_VALUE. +proc create_modified_remote_logfile { src dst new_value } { + set dst_fd [open $dst w] + set src_fd [open $src r] + + # State machine: 'init' -> 'info regs' -> 'p pkt' -> 'done'. + # 'init' -- before we find the "info registers" command. + # 'info regs' -- after seeing the "info registers" command, before + # finding the outbound 'p' packet. + # 'p pkt' -- after seeing the outbound 'p' packet, looking for the reply + # line. + # 'done' -- after finding and modifying the reply to the first 'p' + # packet. + set state "init" + while { [gets $src_fd line] != -1 } { + if { $state eq "init" } { + if { $line eq "c info registers" } { + set state "info regs" + } + } elseif { $state eq "info regs" } { + if { [regexp "^w \\\$p\[0-9a-f\]+#" $line] } { + set state "p pkt" + } else { + set state "done" + } + } elseif { $state eq "p pkt" } { + if { [regexp "^r \\\$\[0-9a-f\]+.*#" $line] } { + set line "r \$${new_value}" + } + set state "done" + } + + puts $dst_fd $line + } + + close $src_fd + close $dst_fd +} + +# The replay log is placed in 'replay.log'. +set remotelog [standard_output_file replay.log] + +record_initial_logfile $remotelog +replay_without_error $remotelog + +# Create two modified logfiles. The first replaces a register with a 1 byte +# value. +set remotelog_short [standard_output_file replay-short.log] +set short_value_str 00 +create_modified_remote_logfile $remotelog $remotelog_short $short_value_str +with_test_prefix "with short register" { + replay_with_error $remotelog_short $short_value_str +} + +# The second replaces a register with a 10 byte value. +set remotelog_long [standard_output_file replay-long.log] +set long_value_str 00000000000000000000 +create_modified_remote_logfile $remotelog $remotelog_long $long_value_str +with_test_prefix "with long register" { + replay_with_error $remotelog_long $long_value_str +} -- 2.25.4