Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Keith Seitz <keiths@redhat.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/5] [gdb/remote] Fix stack overflow in parse_tracepoint_definition
Date: Thu, 12 Nov 2026 09:41:13 -0800	[thread overview]
Message-ID: <b5cc686d77710cfac6b7a42cb734fd043039db0e.1794430701.git.keiths@redhat.com> (raw)
In-Reply-To: <cover.1794430701.git.keiths@redhat.com>

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 = ':')
+    {
+      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);
 	  if (*p == 'F')
 	    {
 	      type = bp_fast_tracepoint;
@@ -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);
 
       if (startswith (srctype, "at:"))
-	utp->at_string = make_unique_xstrdup (buf);
+	utp->at_string = make_unique_xstrdup (decoded.c_str ());
       else if (startswith (srctype, "cond:"))
-	utp->cond_string = make_unique_xstrdup (buf);
+	utp->cond_string = make_unique_xstrdup (decoded.c_str ());
       else if (startswith (srctype, "cmd:"))
-	utp->cmd_strings.emplace_back (xstrdup (buf));
+	utp->cmd_strings.emplace_back (xstrdup (decoded.c_str ()));
     }
   else if (piece == 'V')
     {
diff --git a/gdb/unittests/tracepoint-selftests.c b/gdb/unittests/tracepoint-selftests.c
index 314390403b6..ded37f390e6 100644
--- a/gdb/unittests/tracepoint-selftests.c
+++ b/gdb/unittests/tracepoint-selftests.c
@@ -173,6 +173,91 @@ test_parse_tsv_definition ()
   }
 }
 
+/* Assert that LINE is rejected as a malformed tracepoint definition.  */
+
+static void
+expect_bad_tracepoint_definition (const char *line)
+{
+  uploaded_tp *utps = nullptr;
+
+  expect_bad_definition ([&] () { parse_tracepoint_definition (line, &utps); },
+			 [&] () { free_uploaded_tps (&utps); });
+}
+
+/* Test parse_tracepoint_definition, including missing-colon rejects and
+   enable-flag rejects.  */
+
+static void
+test_parse_tracepoint_definition ()
+{
+  uploaded_tp *utps = nullptr;
+
+  /* Well-formed T definition: T<num>:<addr>:<E|D>:<step>:<pass>.  */
+  {
+    const char *good = "T1:0:E:0:0";
+
+    parse_tracepoint_definition (good, &utps);
+    SELF_CHECK (utps != nullptr);
+    SELF_CHECK (utps->number == 1);
+    SELF_CHECK (utps->addr == 0);
+    SELF_CHECK (utps->enabled);
+    SELF_CHECK (utps->step == 0);
+    SELF_CHECK (utps->pass == 0);
+    free_uploaded_tps (&utps);
+  }
+
+  /* Well-formed disabled T definition.  */
+  {
+    const char *good = "T1:0:D:0:0";
+
+    parse_tracepoint_definition (good, &utps);
+    SELF_CHECK (utps != nullptr);
+    SELF_CHECK (!utps->enabled);
+    free_uploaded_tps (&utps);
+  }
+
+  expect_bad_tracepoint_definition ("T1");
+  expect_bad_tracepoint_definition ("T1:0");
+  expect_bad_tracepoint_definition ("T1:0::0:0");
+  expect_bad_tracepoint_definition ("T1:0:X:0:0");
+  expect_bad_tracepoint_definition ("T1:0:E");
+  expect_bad_tracepoint_definition ("T1:0:E:0");
+
+  /* Well-formed Z definition: at-string hex-encodes "foo".
+     Encoded form is Z<num>:<addr>:<srctype>:<start>:<len>:<hex>.  */
+  {
+    const char *good = "Z1:0:at:0:3:666f6f";
+
+    parse_tracepoint_definition (good, &utps);
+    SELF_CHECK (utps != nullptr);
+    SELF_CHECK (utps->number == 1);
+    SELF_CHECK (utps->addr == 0);
+    SELF_CHECK (utps->at_string != nullptr
+		&& streq (utps->at_string.get (), "foo"));
+    free_uploaded_tps (&utps);
+  }
+
+  expect_bad_tracepoint_definition ("Z1");
+  expect_bad_tracepoint_definition ("Z1:0");
+
+  /* Short stale tail.  Too small to overflow the old alloca buffer, but
+     still missing a trailing colon, so the parser must throw.  */
+  {
+    static const char prefix[] = "Z1:0:at:0:0000";
+    std::vector<char> buf = make_stale_tail_buffer (prefix, "4444");
+    expect_bad_tracepoint_definition (buf.data ());
+  }
+
+  /* Long stale tail.  Large enough to overflow the old alloca buffer; with
+     the fix, the missing trailing colon must cause a throw instead.  */
+  {
+    static const char prefix[] = "Z1:0:at:0:0000";
+    std::vector<char> buf
+      = make_stale_tail_buffer (prefix, std::string (64, '4'));
+    expect_bad_tracepoint_definition (buf.data ());
+  }
+}
+
 } /* namespace tracepoint_tests */
 } /* namespace selftests */
 
@@ -184,4 +269,7 @@ INIT_GDB_FILE (tracepoint_selftests)
   selftests::register_test
     ("parse_tsv_definition",
      selftests::tracepoint_tests::test_parse_tsv_definition);
+  selftests::register_test
+    ("parse_tracepoint_definition",
+     selftests::tracepoint_tests::test_parse_tracepoint_definition);
 }
-- 
2.56.0


  parent reply	other threads:[~2026-11-12 17:42 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 ` Keith Seitz [this message]
2026-11-24 14:45   ` [PATCH 2/5] [gdb/remote] Fix stack overflow in parse_tracepoint_definition Andrew Burgess
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=b5cc686d77710cfac6b7a42cb734fd043039db0e.1794430701.git.keiths@redhat.com \
    --to=keiths@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