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 1/5] [gdb/remote] Fix stack overflow in parse_tsv_definition
Date: Thu, 12 Nov 2026 09:41:12 -0800	[thread overview]
Message-ID: <0075bc3fc2908558e86385531ee62479683149a7.1794430701.git.keiths@redhat.com> (raw)
In-Reply-To: <cover.1794430701.git.keiths@redhat.com>

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


  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 ` Keith Seitz [this message]
2026-11-24 12:07   ` [PATCH 1/5] [gdb/remote] Fix stack overflow in parse_tsv_definition 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

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=0075bc3fc2908558e86385531ee62479683149a7.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