Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH 0/5] tracepoint.c hardening against missing separators
@ 2026-11-12 17:41 Keith Seitz
  2026-11-12 17:41 ` [PATCH 1/5] [gdb/remote] Fix stack overflow in parse_tsv_definition Keith Seitz
                   ` (5 more replies)
  0 siblings, 6 replies; 12+ messages in thread
From: Keith Seitz @ 2026-11-12 17:41 UTC (permalink / raw)
  To: gdb-patches

Hi,

This small series introduces some hardening in tracepoint parsers
which have given rise to CVE-2026-121995 and CVE-2026-123151, filed
as gdb/34967 and gdb/34971.

The common thread here is that the various tracepoint parsers do
not validate their inputs, so malformed packets can actually smash
the stack in some cases.  That's Real Bad (TM).

Patch 1 adds the helpers and fixes parse_tsv_definition.
Patch 2 applies them throughout parse_tracepoint_definition (and
  tightens the enable flag to require 'E' or 'D').
Patches 3-5 cover the marker definition parser, and other hardening
  suggested/spotted by AI.

I intentionally stopped short of further hardening such as bounding
the ':X' payload length or rewriting parse_trace_status; those are potential
later follow-ups.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34967
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34971

Keith Seitz (5):
  [gdb/remote] Fix stack overflow in parse_tsv_definition
  [gdb/remote] Fix stack overflow in parse_tracepoint_definition
  [gdb/remote] Harden parse_static_tracepoint_marker_definition
  [gdb/remote] Harden comma separator in parse_tracepoint_definition
  [gdb/remote] Harden parse_tracepoint_status

 gdb/tracepoint.c                     | 107 ++++++----
 gdb/unittests/tracepoint-selftests.c | 289 +++++++++++++++++++++++++++
 2 files changed, 359 insertions(+), 37 deletions(-)


base-commit: 96248aa4173886a47a7aa1798861267edde28801
-- 
2.56.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 1/5] [gdb/remote] Fix stack overflow in parse_tsv_definition
  2026-11-12 17:41 [PATCH 0/5] tracepoint.c hardening against missing separators Keith Seitz
@ 2026-11-12 17:41 ` 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
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: Keith Seitz @ 2026-11-12 17:41 UTC (permalink / raw)
  To: gdb-patches

gdb/34967 demonstrates how a malicious remote stub may cause
a stack overflow with a malformed tsv definition.

parse_tsv_definition skips field separators without checking whether
the skipped character is really the field separator.  Worse, a short
definition such as "1:0:0000" (which is missing a colon) would point
a buffer past the terminating NUL into leftover bytes of a reused RSP packet
buffer. hex2bin then copied more bytes than allocated by alloca.

Fix this by introducing shared helpers that require a field separator
and report a malformed definition.  Only parse_tsv_definition is
converted here; follow-up patches will use the same helpers for the
remaining trace definition parsers.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34967
---
 gdb/tracepoint.c                     |  40 +++++++--
 gdb/unittests/tracepoint-selftests.c | 119 +++++++++++++++++++++++++++
 2 files changed, 150 insertions(+), 9 deletions(-)

diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index d903887e3ac..7a5af33b42e 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -3377,6 +3377,29 @@ Status line: '%s'\n"), p, line);
     }
 }
 
+/* Report a malformed uploaded trace-related definition.  KIND is a short
+   label such as "tsv" or "tracepoint"; LINE is the full text being
+   parsed.  */
+
+[[noreturn]] static void
+bad_trace_definition (const char *kind, const char *line)
+{
+  error (_("bad %s definition: %s"), kind, line);
+}
+
+/* If P points at SEP, return the character after it.  Otherwise throw
+   via bad_trace_definition.  P may be NULL (e.g. a failed strchr),
+   which is treated as a missing separator.  */
+
+static const char *
+trace_definition_require_char (const char *p, char sep, const char *kind,
+			       const char *line)
+{
+  if (p == nullptr || *p != sep)
+    bad_trace_definition (kind, line);
+  return p + 1;
+}
+
 void
 parse_tracepoint_status (const char *p, tracepoint *tp,
 			 struct uploaded_tp *utp)
@@ -3518,27 +3541,26 @@ void
 parse_tsv_definition (const char *line, struct uploaded_tsv **utsvp)
 {
   const char *p;
-  char *buf;
   ULONGEST num, initval, builtin;
-  int end;
   struct uploaded_tsv *utsv = NULL;
 
-  buf = (char *) alloca (strlen (line));
+  auto require = [&] (const char *q, char sep = ':')
+    {
+      return trace_definition_require_char (q, sep, "tsv", line);
+    };
 
   p = line;
   p = unpack_varlen_hex (p, &num);
-  p++; /* skip a colon */
+  p = require (p);
   p = unpack_varlen_hex (p, &initval);
-  p++; /* skip a colon */
+  p = require (p);
   p = unpack_varlen_hex (p, &builtin);
-  p++; /* skip a colon */
-  end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
-  buf[end] = '\0';
+  p = require (p);
 
   utsv = get_uploaded_tsv (num, utsvp);
   utsv->initial_value = initval;
   utsv->builtin = builtin;
-  utsv->name = xstrdup (buf);
+  utsv->name = xstrdup (hex2str (p).c_str ());
 }
 
 /* Given a line of text defining a static tracepoint marker, parse it
diff --git a/gdb/unittests/tracepoint-selftests.c b/gdb/unittests/tracepoint-selftests.c
index 6349f5075f4..314390403b6 100644
--- a/gdb/unittests/tracepoint-selftests.c
+++ b/gdb/unittests/tracepoint-selftests.c
@@ -20,9 +20,44 @@
 #include "gdbsupport/selftest.h"
 #include "tracepoint.h"
 
+#include <string>
+#include <vector>
+
 namespace selftests {
 namespace tracepoint_tests {
 
+/* Run PARSE and assert that it throws a gdb_exception_error.  CLEANUP
+   always runs afterward.  */
+
+template<typename Parse, typename Cleanup>
+static void
+expect_bad_definition (Parse parse, Cleanup cleanup)
+{
+  bool threw = false;
+
+  try
+    {
+      parse ();
+    }
+  catch (const gdb_exception_error &ex)
+    {
+      SELF_CHECK (ex.reason == RETURN_ERROR);
+      threw = true;
+    }
+
+  SELF_CHECK (threw);
+  cleanup ();
+}
+
+/* Like the two-argument overload, with a no-op cleanup.  */
+
+template<typename Parse>
+static void
+expect_bad_definition (Parse parse)
+{
+  expect_bad_definition (parse, [] () {});
+}
+
 static void
 test_parse_static_tracepoint_marker_definition ()
 {
@@ -57,6 +92,87 @@ test_parse_static_tracepoint_marker_definition ()
   SELF_CHECK (end == def + strlen (def));
 }
 
+/* Free UTSVP, including each node's name.  */
+
+static void
+cleanup_uploaded_tsvs (uploaded_tsv **utsvp)
+{
+  for (uploaded_tsv *utsv = *utsvp; utsv != nullptr; utsv = utsv->next)
+    xfree (const_cast<char *> (utsv->name));
+  free_uploaded_tsvs (utsvp);
+}
+
+/* Build a buffer that mimics a reused RSP packet buffer (rs->buf):
+   PREFIX is a NUL-terminated definition, and STALE_HEX is appended
+   immediately after that NUL -- as leftover bytes from a previous, longer
+   reply (for example qTStatus) that a subsequent short reply overwrote.
+
+   Returns a vector with the following layout:
+
+     [ prefix bytes | NUL | stale_hex bytes | NUL ]
+
+   The final NUL keeps strlen on the stale region well-defined.  */
+
+static std::vector<char>
+make_stale_tail_buffer (const char *prefix, const std::string &stale_hex)
+{
+  std::vector<char> buf (prefix, prefix + strlen (prefix) + 1);
+  buf.insert (buf.end (), stale_hex.begin (), stale_hex.end ());
+  buf.push_back ('\0');
+  return buf;
+}
+
+/* Assert that LINE is rejected as a malformed TSV definition.  */
+
+static void
+expect_bad_tsv_definition (const char *line)
+{
+  uploaded_tsv *utsvs = nullptr;
+
+  expect_bad_definition ([&] () { parse_tsv_definition (line, &utsvs); },
+			 [&] () { cleanup_uploaded_tsvs (&utsvs); });
+}
+
+/* Test parse_tsv_definition, including buffers with stale bytes after a
+   short definition's terminating NUL, as when reusing an RSP packet
+   buffer.  */
+
+static void
+test_parse_tsv_definition ()
+{
+  uploaded_tsv *utsvs = nullptr;
+
+  /* Well-formed definition: name hex-encodes "foo".  */
+  {
+    const char *good = "1:0:0:666f6f";
+
+    parse_tsv_definition (good, &utsvs);
+    SELF_CHECK (utsvs != nullptr);
+    SELF_CHECK (utsvs->number == 1);
+    SELF_CHECK (utsvs->initial_value == 0);
+    SELF_CHECK (utsvs->builtin == 0);
+    SELF_CHECK (utsvs->name != nullptr && streq (utsvs->name, "foo"));
+    cleanup_uploaded_tsvs (&utsvs);
+  }
+
+  /* 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[] = "1:0:0000";
+    std::vector<char> buf = make_stale_tail_buffer (prefix, "4444");
+    expect_bad_tsv_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[] = "1:0:0000";
+    std::vector<char> buf
+      = make_stale_tail_buffer (prefix, std::string (64, '4'));
+    expect_bad_tsv_definition (buf.data ());
+  }
+}
+
 } /* namespace tracepoint_tests */
 } /* namespace selftests */
 
@@ -65,4 +181,7 @@ INIT_GDB_FILE (tracepoint_selftests)
   selftests::register_test
     ("parse_static_tracepoint_marker_definition",
      selftests::tracepoint_tests::test_parse_static_tracepoint_marker_definition);
+  selftests::register_test
+    ("parse_tsv_definition",
+     selftests::tracepoint_tests::test_parse_tsv_definition);
 }
-- 
2.56.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 2/5] [gdb/remote] Fix stack overflow in parse_tracepoint_definition
  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-12 17:41 ` Keith Seitz
  2026-11-24 14:45   ` Andrew Burgess
  2026-11-12 17:41 ` [PATCH 3/5] [gdb/remote] Harden parse_static_tracepoint_marker_definition Keith Seitz
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: Keith Seitz @ 2026-11-12 17:41 UTC (permalink / raw)
  To: gdb-patches

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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 3/5] [gdb/remote] Harden parse_static_tracepoint_marker_definition
  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-12 17:41 ` [PATCH 2/5] [gdb/remote] Fix stack overflow in parse_tracepoint_definition Keith Seitz
@ 2026-11-12 17:41 ` Keith Seitz
  2026-11-12 17:41 ` [PATCH 4/5] [gdb/remote] Harden comma separator in parse_tracepoint_definition Keith Seitz
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 12+ messages in thread
From: Keith Seitz @ 2026-11-12 17:41 UTC (permalink / raw)
  To: gdb-patches

Similar to parse_tsv_definition and parse_tracepoint_definition,
parse_static_tracepoint_marker_definition skipped field separators
without checking whether the skipped character is really a colon.

Convert it to the shared separator helpers and add unit tests for
missing-colon inputs.
---
 gdb/tracepoint.c                     | 17 +++++++++--------
 gdb/unittests/tracepoint-selftests.c | 17 +++++++++++++++++
 2 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index 7a3339617ef..65e17517a77 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -3577,21 +3577,22 @@ parse_static_tracepoint_marker_definition (const char *line, const char **pp,
   const char *p, *endp;
   ULONGEST addr;
 
+  auto require = [&] (const char *q, char sep = ':')
+    {
+      return trace_definition_require_char (q, sep, "marker", line);
+    };
+
   p = line;
   p = unpack_varlen_hex (p, &addr);
-  p++;  /* skip a colon */
+  p = require (p);
 
   marker->gdbarch = current_inferior ()->arch ();
   marker->address = (CORE_ADDR) addr;
 
+  const char *id_start = p;
   endp = strchr (p, ':');
-  if (endp == NULL)
-    error (_("bad marker definition: %s"), line);
-
-  marker->str_id = hex2str (p, (endp - p) / 2);
-
-  p = endp;
-  p++; /* skip a colon */
+  p = require (endp);
+  marker->str_id = hex2str (id_start, (endp - id_start) / 2);
 
   /* This definition may be followed by another one, separated by a comma.  */
   int hex_len;
diff --git a/gdb/unittests/tracepoint-selftests.c b/gdb/unittests/tracepoint-selftests.c
index ded37f390e6..768e1a9e83b 100644
--- a/gdb/unittests/tracepoint-selftests.c
+++ b/gdb/unittests/tracepoint-selftests.c
@@ -58,6 +58,20 @@ expect_bad_definition (Parse parse)
   expect_bad_definition (parse, [] () {});
 }
 
+/* Assert that LINE is rejected as a malformed static tracepoint marker
+   definition.  */
+
+static void
+expect_bad_static_tracepoint_marker_definition (const char *line)
+{
+  static_tracepoint_marker marker;
+
+  expect_bad_definition ([&] ()
+    {
+      parse_static_tracepoint_marker_definition (line, nullptr, &marker);
+    });
+}
+
 static void
 test_parse_static_tracepoint_marker_definition ()
 {
@@ -90,6 +104,9 @@ test_parse_static_tracepoint_marker_definition ()
   SELF_CHECK (marker.str_id == "marker3");
   SELF_CHECK (marker.extra == "morestuff");
   SELF_CHECK (end == def + strlen (def));
+
+  expect_bad_static_tracepoint_marker_definition ("1234");
+  expect_bad_static_tracepoint_marker_definition ("1234:6d61726b657231");
 }
 
 /* Free UTSVP, including each node's name.  */
-- 
2.56.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 4/5] [gdb/remote] Harden comma separator in parse_tracepoint_definition
  2026-11-12 17:41 [PATCH 0/5] tracepoint.c hardening against missing separators Keith Seitz
                   ` (2 preceding siblings ...)
  2026-11-12 17:41 ` [PATCH 3/5] [gdb/remote] Harden parse_static_tracepoint_marker_definition Keith Seitz
@ 2026-11-12 17:41 ` 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
  5 siblings, 0 replies; 12+ messages in thread
From: Keith Seitz @ 2026-11-12 17:41 UTC (permalink / raw)
  To: gdb-patches

The optional :X<len>,<hex> condition field in a tracepoint definition
skipped past a comma without checking that the separator was present.
Convert that site to the shared separator helper and add unit tests
for missing-comma inputs.
---
 gdb/tracepoint.c                     |  2 +-
 gdb/unittests/tracepoint-selftests.c | 21 +++++++++++++++++++--
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index 65e17517a77..285fbd18c8c 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -3477,7 +3477,7 @@ parse_tracepoint_definition (const char *line, struct uploaded_tp **utpp)
 	    {
 	      p++;
 	      p = unpack_varlen_hex (p, &xlen);
-	      p++;  /* skip a comma */
+	      p = require (p, ',');
 	      cond = make_unique_xstrndup (p, 2 * xlen);
 	      p += 2 * xlen;
 	    }
diff --git a/gdb/unittests/tracepoint-selftests.c b/gdb/unittests/tracepoint-selftests.c
index 768e1a9e83b..f6d5317e9d2 100644
--- a/gdb/unittests/tracepoint-selftests.c
+++ b/gdb/unittests/tracepoint-selftests.c
@@ -201,8 +201,9 @@ expect_bad_tracepoint_definition (const char *line)
 			 [&] () { free_uploaded_tps (&utps); });
 }
 
-/* Test parse_tracepoint_definition, including missing-colon rejects and
-   enable-flag rejects.  */
+/* Test parse_tracepoint_definition, including missing-colon rejects,
+   enable-flag rejects, missing-comma rejects for the 'X' condition
+   field, and the 'Z' branch stale-tail cases.  */
 
 static void
 test_parse_tracepoint_definition ()
@@ -240,6 +241,22 @@ test_parse_tracepoint_definition ()
   expect_bad_tracepoint_definition ("T1:0:E");
   expect_bad_tracepoint_definition ("T1:0:E:0");
 
+  /* Well-formed T definition with an agent-expression condition.
+     Encoded form is ...:X<len>,<hex>.  */
+  {
+    const char *good = "T1:0:E:0:0:X1,00";
+
+    parse_tracepoint_definition (good, &utps);
+    SELF_CHECK (utps != nullptr);
+    SELF_CHECK (utps->cond != nullptr
+		&& streq (utps->cond.get (), "00"));
+    free_uploaded_tps (&utps);
+  }
+
+  /* Missing comma after the X length.  */
+  expect_bad_tracepoint_definition ("T1:0:E:0:0:X1");
+  expect_bad_tracepoint_definition ("T1:0:E:0:0:X1:00");
+
   /* Well-formed Z definition: at-string hex-encodes "foo".
      Encoded form is Z<num>:<addr>:<srctype>:<start>:<len>:<hex>.  */
   {
-- 
2.56.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 5/5] [gdb/remote] Harden parse_tracepoint_status
  2026-11-12 17:41 [PATCH 0/5] tracepoint.c hardening against missing separators Keith Seitz
                   ` (3 preceding siblings ...)
  2026-11-12 17:41 ` [PATCH 4/5] [gdb/remote] Harden comma separator in parse_tracepoint_definition Keith Seitz
@ 2026-11-12 17:41 ` Keith Seitz
  2026-11-24 17:14 ` [PATCH 0/5] tracepoint.c hardening against missing separators Andrew Burgess
  5 siblings, 0 replies; 12+ messages in thread
From: Keith Seitz @ 2026-11-12 17:41 UTC (permalink / raw)
  To: gdb-patches

parse_tracepoint_status skipped the colon between hit count and
traceframe usage without checking that the separator was present.
Convert it to the shared separator helper and add unit tests.
---
 gdb/tracepoint.c                     | 10 +++++-
 gdb/unittests/tracepoint-selftests.c | 48 ++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index 285fbd18c8c..d1053174eb4 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -3405,13 +3405,21 @@ parse_tracepoint_status (const char *p, tracepoint *tp,
 			 struct uploaded_tp *utp)
 {
   ULONGEST uval;
+  const char *line = p;
+
+  auto require = [&] (const char *q, char sep = ':')
+    {
+      return trace_definition_require_char (q, sep, "tracepoint status",
+					    line);
+    };
 
   p = unpack_varlen_hex (p, &uval);
   if (tp)
     tp->hit_count += uval;
   else
     utp->hit_count += uval;
-  p = unpack_varlen_hex (p + 1, &uval);
+  p = require (p);
+  p = unpack_varlen_hex (p, &uval);
   if (tp)
     tp->traceframe_usage += uval;
   else
diff --git a/gdb/unittests/tracepoint-selftests.c b/gdb/unittests/tracepoint-selftests.c
index f6d5317e9d2..479288b0bef 100644
--- a/gdb/unittests/tracepoint-selftests.c
+++ b/gdb/unittests/tracepoint-selftests.c
@@ -292,6 +292,51 @@ test_parse_tracepoint_definition ()
   }
 }
 
+/* Assert that LINE is rejected as a malformed tracepoint status.  */
+
+static void
+expect_bad_tracepoint_status (const char *line)
+{
+  uploaded_tp utp;
+
+  expect_bad_definition ([&] ()
+    {
+      parse_tracepoint_status (line, nullptr, &utp);
+    });
+}
+
+/* Test parse_tracepoint_status, including missing-colon rejects.
+   Status form is <hits>:<usage>.  */
+
+static void
+test_parse_tracepoint_status ()
+{
+  /* Well-formed status.  */
+  {
+    uploaded_tp utp;
+
+    parse_tracepoint_status ("5:a", nullptr, &utp);
+    SELF_CHECK (utp.hit_count == 5);
+    SELF_CHECK (utp.traceframe_usage == 0xa);
+  }
+
+  /* Also reachable via a V tracepoint definition piece.  */
+  {
+    uploaded_tp *utps = nullptr;
+
+    parse_tracepoint_definition ("V1:0:5:a", &utps);
+    SELF_CHECK (utps != nullptr);
+    SELF_CHECK (utps->hit_count == 5);
+    SELF_CHECK (utps->traceframe_usage == 0xa);
+    free_uploaded_tps (&utps);
+  }
+
+  expect_bad_tracepoint_status ("5");
+  expect_bad_tracepoint_status ("5a");
+  expect_bad_tracepoint_definition ("V1:0:5");
+  expect_bad_tracepoint_definition ("V1:0:5a");
+}
+
 } /* namespace tracepoint_tests */
 } /* namespace selftests */
 
@@ -306,4 +351,7 @@ INIT_GDB_FILE (tracepoint_selftests)
   selftests::register_test
     ("parse_tracepoint_definition",
      selftests::tracepoint_tests::test_parse_tracepoint_definition);
+  selftests::register_test
+    ("parse_tracepoint_status",
+     selftests::tracepoint_tests::test_parse_tracepoint_status);
 }
-- 
2.56.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/5] [gdb/remote] Fix stack overflow in parse_tsv_definition
  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
  0 siblings, 0 replies; 12+ messages in thread
From: Andrew Burgess @ 2026-11-24 12:07 UTC (permalink / raw)
  To: Keith Seitz, gdb-patches

Keith Seitz <keiths@redhat.com> writes:

> gdb/34967 demonstrates how a malicious remote stub may cause
> a stack overflow with a malformed tsv definition.
>
> parse_tsv_definition skips field separators without checking whether
> the skipped character is really the field separator.  Worse, a short
> definition such as "1:0:0000" (which is missing a colon) would point
> a buffer past the terminating NUL into leftover bytes of a reused RSP packet
> buffer. hex2bin then copied more bytes than allocated by alloca.
>
> Fix this by introducing shared helpers that require a field separator
> and report a malformed definition.  Only parse_tsv_definition is
> converted here; follow-up patches will use the same helpers for the
> remaining trace definition parsers.
>
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34967

Thanks for fixing this.  I have one small suggestion, see inline below.

>  
> +/* Free UTSVP, including each node's name.  */
> +
> +static void
> +cleanup_uploaded_tsvs (uploaded_tsv **utsvp)
> +{
> +  for (uploaded_tsv *utsv = *utsvp; utsv != nullptr; utsv = utsv->next)
> +    xfree (const_cast<char *> (utsv->name));
> +  free_uploaded_tsvs (utsvp);
> +}

I think it would be better to move the free of uploaded_tsv::name into
free_uploaded_tsvs, which would close a memory leak.  I took a look
through the GDB code and I think name is only ever assigned in one place
by strdup, and the name is owned entirely by the uploaded_tsv, so I
cannot see why freeing the name would cause any problems.

This could be done in this commit as it's a small enough fix.

Thanks,
Andrew


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 2/5] [gdb/remote] Fix stack overflow in parse_tracepoint_definition
  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
  2026-11-24 17:12     ` Andrew Burgess
  2026-11-24 19:28     ` Keith Seitz
  0 siblings, 2 replies; 12+ messages in thread
From: Andrew Burgess @ 2026-11-24 14:45 UTC (permalink / raw)
  To: Keith Seitz, gdb-patches


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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 2/5] [gdb/remote] Fix stack overflow in parse_tracepoint_definition
  2026-11-24 14:45   ` Andrew Burgess
@ 2026-11-24 17:12     ` Andrew Burgess
  2026-11-24 19:28     ` Keith Seitz
  1 sibling, 0 replies; 12+ messages in thread
From: Andrew Burgess @ 2026-11-24 17:12 UTC (permalink / raw)
  To: Keith Seitz, gdb-patches

Andrew Burgess <aburgess@redhat.com> writes:

> Thanks for working on this.  I have a couple of really minor thoughts,
> see inline below.
>
> Keith Seitz <keiths@redhat.com> writes:
>
>>  	  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?

Ahh, just found this in a later patch in the series :)

Ignore this part then.

Thanks,
Andrew


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 0/5] tracepoint.c hardening against missing separators
  2026-11-12 17:41 [PATCH 0/5] tracepoint.c hardening against missing separators Keith Seitz
                   ` (4 preceding siblings ...)
  2026-11-12 17:41 ` [PATCH 5/5] [gdb/remote] Harden parse_tracepoint_status Keith Seitz
@ 2026-11-24 17:14 ` Andrew Burgess
  2026-11-25 17:38   ` Keith Seitz
  5 siblings, 1 reply; 12+ messages in thread
From: Andrew Burgess @ 2026-11-24 17:14 UTC (permalink / raw)
  To: Keith Seitz, gdb-patches

Keith Seitz <keiths@redhat.com> writes:

> Hi,
>
> This small series introduces some hardening in tracepoint parsers
> which have given rise to CVE-2026-121995 and CVE-2026-123151, filed
> as gdb/34967 and gdb/34971.
>
> The common thread here is that the various tracepoint parsers do
> not validate their inputs, so malformed packets can actually smash
> the stack in some cases.  That's Real Bad (TM).
>
> Patch 1 adds the helpers and fixes parse_tsv_definition.
> Patch 2 applies them throughout parse_tracepoint_definition (and
>   tightens the enable flag to require 'E' or 'D').
> Patches 3-5 cover the marker definition parser, and other hardening
>   suggested/spotted by AI.
>
> I intentionally stopped short of further hardening such as bounding
> the ':X' payload length or rewriting parse_trace_status; those are potential
> later follow-ups.
>
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34967
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34971

Hey Keith,

Thanks for fixing these.  I left a few notes on the first two patches,
nothing huge.  All the other patches look great.

I think the nits I pointed out are minor enough that you can merge once
they are addressed:

Approved-By: Andrew Burgess <aburgess@redhat.com>

Thanks,
Andrew


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 2/5] [gdb/remote] Fix stack overflow in parse_tracepoint_definition
  2026-11-24 14:45   ` Andrew Burgess
  2026-11-24 17:12     ` Andrew Burgess
@ 2026-11-24 19:28     ` Keith Seitz
  1 sibling, 0 replies; 12+ messages in thread
From: Keith Seitz @ 2026-11-24 19:28 UTC (permalink / raw)
  To: Andrew Burgess, gdb-patches

Hi,

Thank you for the review. I'd like to give you my opinion
on your follow-up question below.

On 11/24/26 6:45 AM, Andrew Burgess wrote:
>> @@ -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.

I believe that the meaning of the fields of 'Z' follow 'QTDPsrc':

Z<n>:<addr>:<type>:<start>:<slen>:<bytes>

 From the manual (QTDPsrc):

start — offset of this packet’s bytes within the overall source string
slen (xlen in the parser) — total length of the source string
bytes — hex encoding of a chunk (possibly not the whole string)

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

That's how I interpret it.

> 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?
Honestly, I think I'd rather not touch this because the issue isn't
quite so simple.

XLEN is the Z/QTDPsrc 'slen', the total length of the source
string in bytes.  'start' is the offset of this packet's bytes within
that string, for strings that need more than one packet.

I think the main issue here is that the code is attempting to partially
implement that multi-packet protocol possibility. It parses start and
slen, but then ignores both and hex2str's "whatever remains as if it
were the whole string" <cough>feature</cough>.

GDB's encoder always sends start=0 with the full string today,
so in practice decoded.size() == xlen is correct, but a strict
equality check would reject a valid chunked reply if that ever
did occur.

This is definitely another cleanup that could be done, but I'd prefer
to not cloud the CVE-related malformed-packet issues that this
series focuses on.

Is that acceptable?

Keith


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 0/5] tracepoint.c hardening against missing separators
  2026-11-24 17:14 ` [PATCH 0/5] tracepoint.c hardening against missing separators Andrew Burgess
@ 2026-11-25 17:38   ` Keith Seitz
  0 siblings, 0 replies; 12+ messages in thread
From: Keith Seitz @ 2026-11-25 17:38 UTC (permalink / raw)
  To: Andrew Burgess, gdb-patches

On 11/24/26 9:14 AM, Andrew Burgess wrote:
> 
> Hey Keith,
> 
> Thanks for fixing these.  I left a few notes on the first two patches,
> nothing huge.  All the other patches look great.
> 
> I think the nits I pointed out are minor enough that you can merge once
> they are addressed:
> 
> Approved-By: Andrew Burgess <aburgess@redhat.com>
I have corrected all your findings and updated all patches in the
series as needed. Thank you for your prompt review.

I've now pushed the entire series.

Keith


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-11-25 17:38 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox