Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH 4/5] gdbserver: don't leak trace variable names if reusing variable numbers
Date: Mon, 16 Nov 2026 11:10:10 +0000	[thread overview]
Message-ID: <3e98b6277e442950b1980af0e0f930c1a41b4824.1794827178.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1794827178.git.aburgess@redhat.com>

In the next commit I will close a memory leak relating to strings
parsed from some trace packets.

While working on this I realised that a QTDV packet, which creates
trace variables, can leak memory if a trace variable number is reused.

For example two packets:

  QTDV:2:6:0:646566
  QTDV:2:6:0:676869

The first creates trace variable #2 called "def" and the second
creates a trace variable #2 called "ghi".  When the "ghi" variable is
created the string "def" will be leaked.

It is not clear from the documentation if a client should be able to
reuse trace variable numbers like this, but as this is the existing
behaviour, I have retained it while closing off the leak.

There is one tiny issue which adds some extra complexity.  We
currently have a single built-in trace state variable
'trace_timestamp' which is created in the function
initialize_tracepoint.  The name of this trace variable is not heap
allocated, and so should not be freed.

Initially I thought the solution to this problem was to just allocate
the 'trace_timestamp' string on the heap and so allow it to be freed,
but the situation is more complex.

The trace_timestamp variable sets a getter function by calling
set_trace_state_variable_getter, and this getter is not reset in
cmd_qtdv if an existing trace_state_variable is returned from
create_trace_state_variable.  What this means is that if a client
creates a trace state variable and assigns it to number 1 then the
name of the variable will change from trace_timestamp, but its value,
as returned by the getter function will still act like
trace_timestamp.

To me, this feels like clients should not be reusing existing trace
state variable numbers at all, but definitely not those of built-in
variables.

Initially I changed cmd_qtdv to reject any QTDV packet that used the
number of an existing builtin; however the test gdb.trace/actions.exp
started failing.  What's happening is that when GDB starts tracing
(with `tstart`), all the trace state variables, including the existing
builtins, are sent to gdbserver.  By rejecting any QTDV that touched a
builtin I ended up rejecting an expected QTDV packet.

So my check became a little more specific; if a QTDV packet is for a
builtin, then the details from GDB need to exactly match the existing
builtin.  The builtin flag must be set to non-zero, and the name must
be correct.  Anything else indicates that GDB is trying to change a
builtin trace state variable, and that is not allowed.  But sending
back the current variable details is fine.

Otherwise, if the QTDV is not overriding a known builtin, we do allow
the variable to be overridden; a client can override its own
variables.  In this case we free the old name to avoid leaking.

Finally, while looking at the name management I spotted that
cmd_qtinit was also failing to free the names.  So I have updated the
function to free the name.

I've added new tests, including tests that exercise both the
rejection of an attempt to replace built-in variable #1,
trace_timestamp, and the acceptance of GDB re-sending the builtin's
definition unchanged.
---
 .../gdb.server/invalid-tracepoint-packets.exp | 26 ++++++++++++++
 gdbserver/tracepoint.cc                       | 36 +++++++++++++++++++
 2 files changed, 62 insertions(+)

diff --git a/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp b/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp
index de37d657b57..cbaddfd7e7b 100644
--- a/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp
+++ b/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp
@@ -122,6 +122,28 @@ proc_with_prefix test_qtdv {} {
 	return
     }
 
+    # Try to assign a new trace state variable to number 1.  Number 1
+    # is already in use by the builtin 'trace_timestamp' variable, so
+    # this will be rejected.
+    gdb_test "maint packet QTDV:1:5:0:616263" $::error_re \
+	"QTDV packet rejected for number 1"
+
+    # However, if we echo back the builtin trace_timestamp definition,
+    # then gdbserver will accept this.  The hex-encoded string here is
+    # 'trace_timestamp', the name of the builtin variable.
+    gdb_test \
+	"maint packet QTDV:1:0:1:74726163655f74696d657374616d70" \
+	$::ok_re "QTDV packet accepted for built-in definition"
+
+    # There's no pre-existing variable 2.  Create a new trace state
+    # variable.
+    gdb_test "maint packet QTDV:2:6:0:646566" $::ok_re \
+	"valid QTDV packet accepted for number 2"
+
+    # Immediately replace our variable 2 with a new one.
+    gdb_test "maint packet QTDV:2:7:0:676869" $::ok_re \
+	"valid QTDV packet accepted to replace variable number 2"
+
     # Tests with missing fields of the packet.
     gdb_test "maint packet QTDV:2" \
 	$::error_re "missing colon after number"
@@ -136,6 +158,10 @@ proc_with_prefix test_qtdv {} {
     gdb_test "maint packet QTDV:2:7:0:616263" $::ok_re \
 	"valid QTDV packet is accepted"
 
+    # Send a QTinit packet which frees all the trace state variables.
+    gdb_test "maint packet QTinit" $::ok_re \
+	"valid QTinit packet accepted"
+
     gdbserver_exit false
 }
 
diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc
index 7f0b233fa43..1e68b19d9d5 100644
--- a/gdbserver/tracepoint.cc
+++ b/gdbserver/tracepoint.cc
@@ -2269,6 +2269,11 @@ cmd_qtinit (char *packet)
 	  else
 	    trace_state_variables = next;
 	  trace_debug ("Deleting var %d", tsv->number);
+
+	  /* Trace state variables without a getter originated from the
+	     debugger client; the name is heap allocated, and should be
+	     freed now.  */
+	  free (tsv->name);
 	  free (tsv);
 	  tsv = next;
 	}
@@ -2673,6 +2678,37 @@ cmd_qtdv (char *own_buf)
   varname[nbytes] = '\0';
 
   struct trace_state_variable *tsv = create_trace_state_variable (num, 1);
+
+  /* If the getter is set then this is a builtin trace state variable, and
+     should not be overridden by the debug client.  However, GDB will upload
+     the target's builtin variables at connection time, and then send them
+     back to the target when starting a trace run.  Accept the debug client
+     re-declaring a builtin unchanged, but reject any attempt to change
+     it.  */
+  if (tsv->getter != nullptr)
+    {
+      if (builtin != 0 && strcmp (varname, tsv->name) == 0)
+	{
+	  /* The client is re-sending the definition of this builtin trace
+	     state variable unchanged.  There's nothing to do.  */
+	  write_ok (own_buf);
+	  xfree (varname);
+	  return;
+	}
+
+      /* The client tried to change this builtin state variable.  As the
+	 getter function is not being updated this will not do what the
+	 client expects, so reject it.  */
+      write_enn (own_buf);
+      xfree (varname);
+      return;
+    }
+
+  /* If NUM is a pre-existing variable then tsv->name will already be set.
+     Additionally, as the getter is NULL, the name is heap allocated and
+     should be freed now to avoid leaking.  */
+  xfree (tsv->name);
+
   tsv->initial_value = (LONGEST) val;
   tsv->name = varname;
 
-- 
2.25.4


  parent reply	other threads:[~2026-11-16 11:10 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 QTDPsrc packet 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
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 ` Andrew Burgess [this message]
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=3e98b6277e442950b1980af0e0f930c1a41b4824.1794827178.git.aburgess@redhat.com \
    --to=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