Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Keith Seitz <keiths@redhat.com>
To: Andrew Burgess <aburgess@redhat.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH 2/5] gdbserver: prevent buffer overrun for QTDPsrc packet
Date: Thu, 19 Nov 2026 11:51:57 -0800	[thread overview]
Message-ID: <ed3a8549-dd89-403a-902f-580c7e0cb2c9@redhat.com> (raw)
In-Reply-To: <a821514b3e7196d3006866b0204329a49cc30942.1794827178.git.aburgess@redhat.com>

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 <http://www.gnu.org/licenses/>.
> +
> +# 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! :-)]

> 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<int> (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?

Keith


  reply	other threads:[~2026-11-19 19:52 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 11:10 [PATCH 0/5] gdbserver: fix buffer overflow issue in " Andrew Burgess
2026-11-16 11:10 ` [PATCH 1/5] gdbserver: clean up some older code in tracepoint.cc Andrew Burgess
2026-11-16 11:10 ` [PATCH 2/5] gdbserver: prevent buffer overrun for QTDPsrc packet Andrew Burgess
2026-11-19 19:51   ` Keith Seitz [this message]
2026-11-23 17:15     ` Andrew Burgess
2026-11-23 17:57       ` Keith Seitz
2026-11-16 11:10 ` [PATCH 3/5] gdbserver: improve packet parsing in tracepoint.cc Andrew Burgess
2026-11-16 11:10 ` [PATCH 4/5] gdbserver: don't leak trace variable names if reusing variable numbers Andrew Burgess
2026-11-16 11:10 ` [PATCH 5/5] gdbserver: use unique_xmalloc_ptr to avoid memory leaks Andrew Burgess
2026-11-19 19:52 ` [PATCH 0/5] gdbserver: fix buffer overflow issue in QTDPsrc packet Keith Seitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ed3a8549-dd89-403a-902f-580c7e0cb2c9@redhat.com \
    --to=keiths@redhat.com \
    --cc=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox