Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: Keith Seitz <keiths@redhat.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH 2/5] [gdb/remote] Fix stack overflow in parse_tracepoint_definition
Date: Tue, 24 Nov 2026 14:45:21 +0000	[thread overview]
Message-ID: <xhuyf8q2ithj2.fsf@aburgess-thinkpadp1gen8.rmtgb.csb> (raw)
In-Reply-To: <b5cc686d77710cfac6b7a42cb734fd043039db0e.1794430701.git.keiths@redhat.com>


Thanks for working on this.  I have a couple of really minor thoughts,
see inline below.

Keith Seitz <keiths@redhat.com> writes:

> gdb/34971 demonstrates how a malicious remote stub may cause
> a stack overflow with a malformed tracepoint definition.
>
> Similar to parse_tsv_definition issues with missing colons in
> the packet reply and how that can cause hex2bin to copy more
> bytes than allocated, fix this in the same way: more stringent
> checking for proper packet layout.
>
> This fixes the missing-colon occurrences in
> parse_tracepoint_definition.  While touching that parser, also
> require the enable flag to be 'E' or 'D' rather than treating any
> other character as disabled, and add unit tests for both.
>
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34971
> ---
>  gdb/tracepoint.c                     | 38 ++++++------
>  gdb/unittests/tracepoint-selftests.c | 88 ++++++++++++++++++++++++++++
>  2 files changed, 108 insertions(+), 18 deletions(-)
>
> diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
> index 7a5af33b42e..7a3339617ef 100644
> --- a/gdb/tracepoint.c
> +++ b/gdb/tracepoint.c
> @@ -3428,34 +3428,40 @@ parse_tracepoint_definition (const char *line, struct uploaded_tp **utpp)
>    const char *p;
>    char piece;
>    ULONGEST num, addr, step, pass, orig_size, xlen, start;
> -  int enabled, end;
> +  int enabled;
>    enum bptype type;
>    const char *srctype;
> -  char *buf;
>    struct uploaded_tp *utp = NULL;
>  
> +  auto require = [&] (const char *q, char sep = ':')

Can you add the " -> const char *" at the end of this line to make the
return type explicit please.

> +    {
> +      return trace_definition_require_char (q, sep, "tracepoint", line);
> +    };
> +
>    p = line;
>    /* Both tracepoint and action definitions start with the same number
>       and address sequence.  */
>    piece = *p++;
>    p = unpack_varlen_hex (p, &num);
> -  p++;  /* skip a colon */
> +  p = require (p);
>    p = unpack_varlen_hex (p, &addr);
> -  p++;  /* skip a colon */
> +  p = require (p);
>    if (piece == 'T')
>      {
>        gdb::unique_xmalloc_ptr<char> cond;
>  
> +      if (*p != 'E' && *p != 'D')
> +	bad_trace_definition ("tracepoint", line);
>        enabled = (*p++ == 'E');
> -      p++;  /* skip a colon */
> +      p = require (p);
>        p = unpack_varlen_hex (p, &step);
> -      p++;  /* skip a colon */
> +      p = require (p);
>        p = unpack_varlen_hex (p, &pass);
>        type = bp_tracepoint;
>        /* Thumb through optional fields.  */
>        while (*p == ':')
>  	{
> -	  p++;  /* skip a colon */
> +	  p = require (p);

Could avoid the 'require' here:

  /* Skip the colon the while loop checks for.  */
  p++;

there are places below where you left 'p++' when someone else already
checks the character for us.  But also, a redundant check isn't the end
of the world, so if you'd rather keep this, then that's fine with me.


>  	  if (*p == 'F')
>  	    {
>  	      type = bp_fast_tracepoint;

Inside the F, S, and X handling there are some p++; lines.  These are
fine as these are skipping over the F, S, and X characters which have
just been checked.

However, in the X case there is also this line:

  p++;  /* skip a comma */

which I think can become:

  p = require (p, ',');

maybe a test for this case could be added?

> @@ -3501,24 +3507,20 @@ parse_tracepoint_definition (const char *line, struct uploaded_tp **utpp)
>        /* Parse a chunk of source form definition.  */
>        utp = get_uploaded_tp (num, addr, utpp);
>        srctype = p;
> -      p = strchr (p, ':');
> -      p++;  /* skip a colon */
> +      p = require (strchr (p, ':'));
>        p = unpack_varlen_hex (p, &start);
> -      p++;  /* skip a colon */
> +      p = require (p);
>        p = unpack_varlen_hex (p, &xlen);
> -      p++;  /* skip a colon */
> -
> -      buf = (char *) alloca (strlen (line));
> +      p = require (p);
>  
> -      end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
> -      buf[end] = '\0';
> +      std::string decoded = hex2str (p);

Maybe I'm looking in the wrong place, but I couldn't find the
specification for this 'Z' case anywhere, so I'm guessing a little bit
about what the fields mean.

Still, we parse XLEN from the packet, then process the remaining bytes
as a string.  XLEN isn't used anywhere else.  I'm assuming that XLEN is
supposed to be the number of bytes in the string?

The old code didn't check that these things matched, but maybe, as
you're improving the code anyway, we could/should add something like:

  if (decoded.size () != xlen)
    bad_trace_definition ("tracepoint", line);

What do you think?

Thanks,
Andrew


  reply	other threads:[~2026-11-24 14:46 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 17:41 [PATCH 0/5] tracepoint.c hardening against missing separators Keith Seitz
2026-11-12 17:41 ` [PATCH 1/5] [gdb/remote] Fix stack overflow in parse_tsv_definition Keith Seitz
2026-11-24 12:07   ` Andrew Burgess
2026-11-12 17:41 ` [PATCH 2/5] [gdb/remote] Fix stack overflow in parse_tracepoint_definition Keith Seitz
2026-11-24 14:45   ` Andrew Burgess [this message]
2026-11-24 17:12     ` Andrew Burgess
2026-11-24 19:28     ` Keith Seitz
2026-11-12 17:41 ` [PATCH 3/5] [gdb/remote] Harden parse_static_tracepoint_marker_definition Keith Seitz
2026-11-12 17:41 ` [PATCH 4/5] [gdb/remote] Harden comma separator in parse_tracepoint_definition Keith Seitz
2026-11-12 17:41 ` [PATCH 5/5] [gdb/remote] Harden parse_tracepoint_status Keith Seitz
2026-11-24 17:14 ` [PATCH 0/5] tracepoint.c hardening against missing separators Andrew Burgess
2026-11-25 17:38   ` 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=xhuyf8q2ithj2.fsf@aburgess-thinkpadp1gen8.rmtgb.csb \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=keiths@redhat.com \
    /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