Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH v2 0/1] PR gdb/server/34969
@ 2026-11-20  8:14 Kevin Buettner
  2026-11-20  8:14 ` [PATCH v2 1/1] gdbserver: reject malformed agent expressions in gdb_parse_agent_expr Kevin Buettner
  0 siblings, 1 reply; 4+ messages in thread
From: Kevin Buettner @ 2026-11-20  8:14 UTC (permalink / raw)
  To: gdb-patches; +Cc: Kevin Buettner

This is a fix for https://sourceware.org/bugzilla/show_bug.cgi?id=34969 .

Changes v1 -> v2:

- Incorporated private feedback from Keith Seitz; a more precise
  technique is used to determine whether the length specified
  for hex encoded data is over-long.
- For tracepoints, v1 silently dropped malformed conditions and
  actions.  v2 does not; it causes an error reply to be sent back
  to gdb.
- gdbserver code implementing support for Z0 packets was revised to
  also send error replies.
- Test case entirely rewritten by me. (The v1 test case was LLM generated.)

Kevin Buettner (1):
  gdbserver: reject malformed agent expressions in gdb_parse_agent_expr

 .../gdb.server/agent-expr-length.exp          | 215 ++++++++++++++++++
 gdbserver/ax.cc                               |  13 +-
 gdbserver/ax.h                                |   4 +-
 gdbserver/mem-break.cc                        |  10 +-
 gdbserver/server.cc                           |  27 ++-
 gdbserver/tracepoint.cc                       |  25 +-
 6 files changed, 272 insertions(+), 22 deletions(-)
 create mode 100644 gdb/testsuite/gdb.server/agent-expr-length.exp

-- 
2.56.1


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

* [PATCH v2 1/1] gdbserver: reject malformed agent expressions in gdb_parse_agent_expr
  2026-11-20  8:14 [PATCH v2 0/1] PR gdb/server/34969 Kevin Buettner
@ 2026-11-20  8:14 ` Kevin Buettner
  2026-11-22 21:19   ` Andrew Burgess
  0 siblings, 1 reply; 4+ messages in thread
From: Kevin Buettner @ 2026-11-20  8:14 UTC (permalink / raw)
  To: gdb-patches; +Cc: Kevin Buettner

gdb_parse_agent_expr, defined in gdbserver/ax.cc, is called from four
different sites within gdbserver, two from mem-break.cc and two from
tracepoint.cc.

Within tracepoint.cc, gdb_parse_agent_expr is called to handle the 'X'
portion of a QTDP packet.  The 'X' portion describes agent expressions
used by tracepoints for both conditions and actions.

Within mem-break.cc, it's called to handle breakpoint conditions or
commands as part of a 'Z' packet.  These conditions or commands are
specified in a bytecode format identical to that used for agent
expressions for tracepoints.

The current implementation of gdb_parse_agent_expr contains no checks
about validity of the length specified in the portion of a packet that
represents an agent expression.  This takes the form "X<len>,<hex>".
(This is not an 'X' packet but, rather, a portion of the 'QTDP' packet
for tracepoints or 'Z0' packet for breakpoints.)  gdb_parse_agent_expr
allocates LEN bytes and asks hex2bin to convert that number of bytes,
but hex2bin stops at the end of the packet, leaving the end of the
buffer uninitialized.  For a tracepoint condition, that buffer is
later sent back to GDB in reply to qTfP/qTsP, which are used to
request tracepoint data.  Thus, using an overly large length can
provide access to uninitialized gdbserver heap memory, a potential
security concern.

The parse pointer is also advanced by 2 * LEN bytes regardless of how
much data was present, so callers continue parsing from a pointer that
can be far beyond the end of the packet; a large LEN crashes gdbserver.

Fix this by having gdb_parse_agent_expr return nullptr (representing
an error condition) when the specified length exceeds the amount of
hex data actually present.  This code also wasn't checking for the
mandated comma character in the packet syntax.  I added a check and
error return for that too.

The remainder of this patch to gdbserver consists of propagating the
error back to various points in the code where write_enn may be
called.  This causes the error to be sent as the reply to GDB.

For Z0 packet support, I removed two warning messages.  One concerned
a malformed condition and the other was about a malformed action
(command).  A malformed condition was discarded, making the breakpoint
unconditional; similarly, a malformed action was simply dropped.  The
warning message (which might not even be seen by the user) stated
these facts.  gdbserver now returns an error response for these cases.

The test case uses GDB's little-used "maintenance packet" command to
send bad packets to gdbserver.  This has been done not only for the
tracepoint related packets, but for the Z0 (set breakpoint) packet as
well.  Z0 used to silently drop malformed packets and set a breakpoint
without the (presumably intended, but malformed) condition or action.
It no longer does this.  Malformed conditions and actions within a
Z0 packet now cause an error to be returned.

While working on this patch, I spotted numerous other problems in
nearby code (e.g. memory leaks and lack of packet syntax checking).
I left these alone.  gdbserver desperately needs attention to fix
myriad problems.

This fixes the bug associated with CVE-2026-121996.

Regression tested on x86_64 Fedora 44.  The new test case was tested
on RISC-V Fedora 42, for which neither Z0 packets nor tracepoint
packets are implemented.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34969
---
 .../gdb.server/agent-expr-length.exp          | 215 ++++++++++++++++++
 gdbserver/ax.cc                               |  13 +-
 gdbserver/ax.h                                |   4 +-
 gdbserver/mem-break.cc                        |  10 +-
 gdbserver/server.cc                           |  27 ++-
 gdbserver/tracepoint.cc                       |  25 +-
 6 files changed, 272 insertions(+), 22 deletions(-)
 create mode 100644 gdb/testsuite/gdb.server/agent-expr-length.exp

diff --git a/gdb/testsuite/gdb.server/agent-expr-length.exp b/gdb/testsuite/gdb.server/agent-expr-length.exp
new file mode 100644
index 00000000000..d9e1db7865c
--- /dev/null
+++ b/gdb/testsuite/gdb.server/agent-expr-length.exp
@@ -0,0 +1,215 @@
+# Copyright 2026 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Send a variety of malformed Z0 and QTDP packets to gdbserver.  These
+# malformed packets could, at one time, crash gdbserver or provide access
+# to its memory.
+
+load_lib gdbserver-support.exp
+
+require allow_gdbserver_tests
+
+standard_testfile normal.c
+
+if {[build_executable "failed to prepare" $testfile $srcfile]} {
+    return
+}
+
+# Send a packet to gdbserver using "maintenance packet".  Returns the
+# response from gdbserver.
+proc maint_packet { packet testname } {
+    set answer ""
+    gdb_test_multiple "maintenance packet $packet" $testname {
+	-re -wrap {received: "([^"]*)"} {
+	    set answer $expect_out(1,string)
+	}
+    }
+    return $answer
+}
+
+# Do the routine things needed to set up gdbserver.
+proc prepare_server { } {
+    clean_restart $::testfile
+    gdb_test "disconnect" ".*"
+    gdbserver_run ""
+}
+
+# Send out Z0 / z0 (breakpoint) packets for this test.  Use "main" for the
+# address at which to place the breakpoint.
+proc test_Z0 {exp testname} {
+    with_test_prefix $testname {
+	prepare_server
+	set addr [format %x [get_hexadecimal_valueof "&main" 0]]
+
+	set result [maint_packet "Z0,$addr,1;$exp" "maint packet Z0"]
+	gdb_assert {[regexp {^E\d\d} $result]} "Z0 packet returns error"
+
+	# Check that a failed insert didn't leave a breakpoint.
+	set result [maint_packet "z0,$addr,1" "maint packet z0"]
+	gdb_assert {[regexp {^E\d\d} $result]} \
+	    "z0 packet confirms breakpoint was not inserted"
+    }
+}
+
+# Send/receive tracepoint related packets for this test.
+proc test_QTDP {cond action testname} {
+    with_test_prefix $testname {
+	prepare_server
+	set addr [format %x [get_hexadecimal_valueof "&main" 0]]
+
+	set result [maint_packet "QTinit" "maint packet QTinit"]
+	gdb_assert {$result eq "OK"} "QTinit packet returns OK"
+
+	set pcond ""
+	set pcont ""
+	if { $cond ne "" } {
+	    set pcond ":$cond"
+	}
+	if { $action ne "" } {
+	    set pcont "-"
+	}
+
+	set result \
+	    [maint_packet "QTDP:1:$addr:E:0:0$pcond$pcont" "maint packet QTDP"]
+	if {$pcond eq ""} {
+	    gdb_assert {$result eq "OK"} "QTDP packet returns OK"
+	} else {
+	    gdb_assert {[regexp {^E\d\d} $result]} "QTDP packet returns error"
+	}
+
+	if { $action ne "" } {
+	    set result [maint_packet "QTDP:-1:$addr:$action" \
+		"maint packet QTDP continuation"]
+	    gdb_assert {[regexp {^E\d\d} $result]} \
+		"QTDP continuation packet returns error"
+	}
+
+	set result [maint_packet "qTfP" "maint packet qTfP"]
+	gdb_assert {$result eq "l"} "tracepoint list is empty"
+    }
+}
+
+# Check whether gdbserver supports the Z0 packet; return 1 if so, otherwise
+# return 0 and show that tests involving Z0 packets are unsupported.
+proc check_Z0_breakpoint_support {} {
+    with_test_prefix "check Z0 support" {
+	prepare_server
+	set addr [format %x [get_hexadecimal_valueof "&main" 0]]
+
+	# The hex agent expression below pushes 1 onto the evaluation stack
+	# and then returns it.
+	set result [maint_packet "Z0,$addr,1;X03,220127" "maint packet Z0"]
+	if {$result eq ""} {
+	    unsupported "Z0 packets"
+	    return 0
+	}
+	gdb_assert {$result eq "OK"} "Z0 packet returns OK"
+
+	# Attempt to delete the breakpoint created above.  Probably
+	# overkill, but we'll note it as unsupported if it doesn't work.
+	set result [maint_packet "z0,$addr,1" "maint packet z0"]
+	if {$result eq ""} {
+	    unsupported "z0 packets"
+	    return 0
+	}
+	gdb_assert {$result eq "OK"} "z0 packet returns OK"
+
+	return 1
+    }
+}
+
+# Tracepoint support predicate; this one just checks whether QTinit works.
+# Return 0 and show an "unsupported" message if not supported; otherwise
+# return 1 when supported.
+proc check_tracepoint_support {} {
+    with_test_prefix "check tracepoint support" {
+	prepare_server
+	set addr [format %x [get_hexadecimal_valueof "&main" 0]]
+
+	set result [maint_packet "QTinit" "maint packet QTinit"]
+	if {$result eq ""} {
+	    unsupported "tracepoints"
+	    return 0
+	}
+
+	gdb_assert {$result eq "OK"} "QTinit packet returns OK"
+
+	# Check that a valid expression passes; the agent expression pushes
+	# 1 and returns it.
+	set result [maint_packet "QTDP:1:$addr:E:0:0:X03,220127" \
+	    "good maint packet QTDP"]
+	gdb_assert {$result eq "OK"} "good maint packet QTDP returns OK"
+
+	# Fetch the tracepoint list; it should contain the tracepoint
+	# defined above.
+	set result [maint_packet "qTfP" "maint packet qTfP"]
+	gdb_assert {[regexp {^T1:} $result]} "tracepoint list is not empty"
+	return 1
+    }
+}
+
+# Conditions for both Z0 and QTDP packets.  Note that "cc" and "ee" aren't
+# real opcodes.  This does not matter because each of these expressions is
+# malformed in some way.
+set large_cond "X20,cc"
+set huge_cond "X800000,cc"
+set no_comma_cond "X04"
+
+# Z0 commands:
+set large_exp "cmds:0,X20,ee"
+set huge_exp "cmds:0,X800000,ee"
+set no_comma_exp "cmds:0,X04"
+
+# QTDP actions:
+set large_action "X20,ee"
+set huge_action "X800000,ee"
+set no_comma_action "X04"
+
+if {[check_Z0_breakpoint_support]} {
+    with_test_prefix "breakpoint tests" {
+	test_Z0 $large_cond "over-long condition"
+	test_Z0 $huge_cond "massively over-long condition"
+	test_Z0 $no_comma_cond "missing comma before condition"
+	test_Z0 $large_exp "over-long expression"
+	test_Z0 $huge_exp "massively over-long expression"
+	test_Z0 $no_comma_exp "missing comma before expression"
+    }
+}
+
+if {[check_tracepoint_support]} {
+    with_test_prefix "tracepoint tests" {
+	test_QTDP $large_cond "" "over-long condition"
+	test_QTDP $huge_cond "" "massively over-long condition"
+	test_QTDP $no_comma_cond "" "missing comma before condition"
+
+	test_QTDP "" $large_action "over-long action"
+	test_QTDP "" $huge_action "massively over-long action"
+	test_QTDP "" $no_comma_action "missing comma before action"
+
+	test_QTDP $large_cond $large_action \
+	    "over-long condition and over-long action"
+	test_QTDP $large_cond $huge_action \
+	    "over-long condition and massively over-long action"
+	test_QTDP $large_cond $no_comma_action \
+	    "over-long condition and missing comma before action"
+
+	test_QTDP $huge_cond $large_action \
+	    "massively over-long condition and over-long action"
+	test_QTDP $huge_cond $huge_action \
+	    "massively over-long condition and massively over-long action"
+	test_QTDP $huge_cond $no_comma_action \
+	    "massively over-long condition and missing comma before action"
+    }
+}
diff --git a/gdbserver/ax.cc b/gdbserver/ax.cc
index 4993bed2532..f73a407f70a 100644
--- a/gdbserver/ax.cc
+++ b/gdbserver/ax.cc
@@ -91,8 +91,7 @@ gdb_agent_op_name (int op)
 
 #ifndef IN_PROCESS_AGENT
 
-/* The packet form of an agent expression consists of an 'X', number
-   of bytes in expression, a comma, and then the bytes.  */
+/* See ax.h.  */
 
 struct agent_expr *
 gdb_parse_agent_expr (const char **actparm)
@@ -103,7 +102,15 @@ gdb_parse_agent_expr (const char **actparm)
 
   ++act;  /* skip the X */
   act = unpack_varlen_hex (act, &xlen);
-  ++act;  /* skip a comma */
+
+  if (*act != ',')
+    return nullptr;
+  ++act;  /* skip the comma */
+
+  /* Return error if provided length exceeds size of actual hex string.  */
+  if (xlen > ((ULONGEST) strspn (act, "0123456789abcdefABCDEF") / 2))
+    return nullptr;
+
   aexpr = XNEW (struct agent_expr);
   aexpr->length = xlen;
   aexpr->bytes = (unsigned char *) xmalloc (xlen);
diff --git a/gdbserver/ax.h b/gdbserver/ax.h
index 03882f4888a..7ad077f8195 100644
--- a/gdbserver/ax.h
+++ b/gdbserver/ax.h
@@ -48,7 +48,9 @@ struct agent_expr
 #ifndef IN_PROCESS_AGENT
 
 /* The packet form of an agent expression consists of an 'X', number
-   of bytes in expression, a comma, and then the bytes.  */
+   of bytes in expression, a comma, and then the bytes.  Parse one at
+   *ACTPARM, advancing *ACTPARM past it.  Return nullptr, leaving
+   *ACTPARM unchanged, if the expression is malformed.  */
 struct agent_expr *gdb_parse_agent_expr (const char **actparm);
 
 /* Release an agent expression.  */
diff --git a/gdbserver/mem-break.cc b/gdbserver/mem-break.cc
index 4e944ff1df2..071f8330c50 100644
--- a/gdbserver/mem-break.cc
+++ b/gdbserver/mem-break.cc
@@ -1205,10 +1205,7 @@ add_breakpoint_condition (struct gdb_breakpoint *bp, const char **condition)
   cond = gdb_parse_agent_expr (&actparm);
 
   if (cond == NULL)
-    {
-      warning (_("Condition evaluation failed. Assuming unconditional."));
-      return 0;
-    }
+    return 0;
 
   add_condition_to_breakpoint (bp, cond);
 
@@ -1304,10 +1301,7 @@ add_breakpoint_commands (struct gdb_breakpoint *bp, const char **command,
   cmd = gdb_parse_agent_expr (&actparm);
 
   if (cmd == NULL)
-    {
-      warning (_("Command evaluation failed. Disabling."));
-      return 0;
-    }
+    return 0;
 
   add_commands_to_breakpoint (bp, cmd, persist);
 
diff --git a/gdbserver/server.cc b/gdbserver/server.cc
index 7bd725f81ed..ac300eee084 100644
--- a/gdbserver/server.cc
+++ b/gdbserver/server.cc
@@ -4744,17 +4744,20 @@ main (int argc, char *argv[])
 
 /* Process options coming from Z packets for a breakpoint.  PACKET is
    the packet buffer.  *PACKET is updated to point to the first char
-   after the last processed option.  */
+   after the last processed option.  Return true on success and false
+   if caller should report an error.  */
 
-static void
+static bool
 process_point_options (struct gdb_breakpoint *bp, const char **packet)
 {
   const char *dataptr = *packet;
   int persist;
 
-  /* Check if data has the correct format.  */
+  /* Return success if the next character is not semicolon; the semicolon
+     introduces optional conditional and/or bytecode expressions to
+     evaluate/execute.  */
   if (*dataptr != ';')
-    return;
+    return true;
 
   dataptr++;
 
@@ -4768,7 +4771,7 @@ process_point_options (struct gdb_breakpoint *bp, const char **packet)
 	  /* Conditional expression.  */
 	  threads_debug_printf ("Found breakpoint condition.");
 	  if (!add_breakpoint_condition (bp, &dataptr))
-	    dataptr = strchrnul (dataptr, ';');
+	    return false;
 	}
       else if (startswith (dataptr, "cmds:"))
 	{
@@ -4776,8 +4779,9 @@ process_point_options (struct gdb_breakpoint *bp, const char **packet)
 	  threads_debug_printf ("Found breakpoint commands %s.", dataptr);
 	  persist = (*dataptr == '1');
 	  dataptr += 2;
-	  if (add_breakpoint_commands (bp, &dataptr, persist))
-	    dataptr = strchrnul (dataptr, ';');
+	  if (!add_breakpoint_commands (bp, &dataptr, persist))
+	    return false;
+	  dataptr = strchrnul (dataptr, ';');
 	}
       else
 	{
@@ -4788,6 +4792,7 @@ process_point_options (struct gdb_breakpoint *bp, const char **packet)
 	}
     }
   *packet = dataptr;
+  return true;
 }
 
 /* Event loop callback that handles a serial event.  The first byte in
@@ -5058,7 +5063,13 @@ process_serial_event (void)
 		   instead.  */
 		clear_breakpoint_conditions_and_commands (bp);
 		const char *options = dataptr;
-		process_point_options (bp, &options);
+		if (!process_point_options (bp, &options))
+		  {
+		    /* Error: remove breakpoint and write error.  */
+		    delete_gdb_breakpoint (type, addr, kind);
+		    write_enn (cs.own_buf);
+		    break;
+		  }
 	      }
 	  }
 	else
diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc
index 6f657257ee8..4cd1722869b 100644
--- a/gdbserver/tracepoint.cc
+++ b/gdbserver/tracepoint.cc
@@ -1802,7 +1802,7 @@ find_next_tracepoint_by_number (struct tracepoint *prev_tp, int num)
 
 /* Append another action to perform when the tracepoint triggers.  */
 
-static void
+static bool
 add_tracepoint_action (struct tracepoint *tpoint, const char *packet)
 {
   const char *act;
@@ -1877,6 +1877,11 @@ add_tracepoint_action (struct tracepoint *tpoint, const char *packet)
 
 	    trace_debug ("Want to evaluate expression");
 	    xaction->expr = gdb_parse_agent_expr (&act);
+	    if (xaction->expr == nullptr)
+	      {
+		free (xaction);
+		return false;
+	      }
 	    break;
 	  }
 	default:
@@ -1916,6 +1921,7 @@ add_tracepoint_action (struct tracepoint *tpoint, const char *packet)
 	    = savestring (act_start, act - act_start);
 	}
     }
+  return true;
 }
 
 #endif
@@ -2404,6 +2410,13 @@ cmd_qtdp (char *own_buf)
 	  else if (*packet == 'X')
 	    {
 	      tpoint->cond = gdb_parse_agent_expr (&packet);
+	      if (tpoint->cond == nullptr)
+		{
+		  trace_debug ("Malformed agent expression for conditional");
+		  write_enn (own_buf);
+		  remove_tracepoint (tpoint);
+		  return;
+		}
 	    }
 	  else if (*packet == '-')
 	    break;
@@ -2425,7 +2438,15 @@ cmd_qtdp (char *own_buf)
 		   tpoint->step_count, tpoint->pass_count);
     }
   else if (tpoint)
-    add_tracepoint_action (tpoint, packet);
+    {
+      if (!add_tracepoint_action (tpoint, packet))
+	{
+	  trace_debug ("Malformed tracepoint action");
+	  write_enn (own_buf);
+	  remove_tracepoint (tpoint);
+	  return;
+	}
+    }
   else
     {
       trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
-- 
2.56.1


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

* Re: [PATCH v2 1/1] gdbserver: reject malformed agent expressions in gdb_parse_agent_expr
  2026-11-20  8:14 ` [PATCH v2 1/1] gdbserver: reject malformed agent expressions in gdb_parse_agent_expr Kevin Buettner
@ 2026-11-22 21:19   ` Andrew Burgess
  2026-11-24 18:56     ` Kevin Buettner
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Burgess @ 2026-11-22 21:19 UTC (permalink / raw)
  To: Kevin Buettner, gdb-patches; +Cc: Kevin Buettner

Kevin Buettner <kevinb@redhat.com> writes:

> gdb_parse_agent_expr, defined in gdbserver/ax.cc, is called from four
> different sites within gdbserver, two from mem-break.cc and two from
> tracepoint.cc.
>
> Within tracepoint.cc, gdb_parse_agent_expr is called to handle the 'X'
> portion of a QTDP packet.  The 'X' portion describes agent expressions
> used by tracepoints for both conditions and actions.
>
> Within mem-break.cc, it's called to handle breakpoint conditions or
> commands as part of a 'Z' packet.  These conditions or commands are
> specified in a bytecode format identical to that used for agent
> expressions for tracepoints.
>
> The current implementation of gdb_parse_agent_expr contains no checks
> about validity of the length specified in the portion of a packet that
> represents an agent expression.  This takes the form "X<len>,<hex>".
> (This is not an 'X' packet but, rather, a portion of the 'QTDP' packet
> for tracepoints or 'Z0' packet for breakpoints.)  gdb_parse_agent_expr
> allocates LEN bytes and asks hex2bin to convert that number of bytes,
> but hex2bin stops at the end of the packet, leaving the end of the
> buffer uninitialized.  For a tracepoint condition, that buffer is
> later sent back to GDB in reply to qTfP/qTsP, which are used to
> request tracepoint data.  Thus, using an overly large length can
> provide access to uninitialized gdbserver heap memory, a potential
> security concern.
>
> The parse pointer is also advanced by 2 * LEN bytes regardless of how
> much data was present, so callers continue parsing from a pointer that
> can be far beyond the end of the packet; a large LEN crashes gdbserver.
>
> Fix this by having gdb_parse_agent_expr return nullptr (representing
> an error condition) when the specified length exceeds the amount of
> hex data actually present.  This code also wasn't checking for the
> mandated comma character in the packet syntax.  I added a check and
> error return for that too.
>
> The remainder of this patch to gdbserver consists of propagating the
> error back to various points in the code where write_enn may be
> called.  This causes the error to be sent as the reply to GDB.
>
> For Z0 packet support, I removed two warning messages.  One concerned
> a malformed condition and the other was about a malformed action
> (command).  A malformed condition was discarded, making the breakpoint
> unconditional; similarly, a malformed action was simply dropped.  The
> warning message (which might not even be seen by the user) stated
> these facts.  gdbserver now returns an error response for these cases.
>
> The test case uses GDB's little-used "maintenance packet" command to
> send bad packets to gdbserver.  This has been done not only for the
> tracepoint related packets, but for the Z0 (set breakpoint) packet as
> well.  Z0 used to silently drop malformed packets and set a breakpoint
> without the (presumably intended, but malformed) condition or action.
> It no longer does this.  Malformed conditions and actions within a
> Z0 packet now cause an error to be returned.
>
> While working on this patch, I spotted numerous other problems in
> nearby code (e.g. memory leaks and lack of packet syntax checking).
> I left these alone.  gdbserver desperately needs attention to fix
> myriad problems.
>
> This fixes the bug associated with CVE-2026-121996.
>
> Regression tested on x86_64 Fedora 44.  The new test case was tested
> on RISC-V Fedora 42, for which neither Z0 packets nor tracepoint
> packets are implemented.
>
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34969

Hi Kevin,

Thanks for working on this.  I agree that the gdbserver code is in need
of some serious updates, but you cannot be expected to fix everything in
one go.  I think this fix is basically sound.  I do have a couple of
minor nits (see inline below), but I think once these are resolved:

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


> +# Send out Z0 / z0 (breakpoint) packets for this test.  Use "main" for the
> +# address at which to place the breakpoint.
> +proc test_Z0 {exp testname} {
> +    with_test_prefix $testname {
> +	prepare_server
> +	set addr [format %x [get_hexadecimal_valueof "&main" 0]]

Is the 'format' really needed here?  Doesn't get_hexadecimal_valueof
return a value in hex format?

> +
> +# Check whether gdbserver supports the Z0 packet; return 1 if so, otherwise
> +# return 0 and show that tests involving Z0 packets are unsupported.

Throughout this file: TCL supports true/false, so you don't need to use 1/0.


> diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc
> index 6f657257ee8..4cd1722869b 100644
> --- a/gdbserver/tracepoint.cc
> +++ b/gdbserver/tracepoint.cc
> @@ -1802,7 +1802,7 @@ find_next_tracepoint_by_number (struct tracepoint *prev_tp, int num)
>  
>  /* Append another action to perform when the tracepoint triggers.  */
>  
> -static void
> +static bool
>  add_tracepoint_action (struct tracepoint *tpoint, const char *packet)

The header comment for this function should be updated to reflect the
change in return type.

Thanks,
Andrew


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

* Re: [PATCH v2 1/1] gdbserver: reject malformed agent expressions in gdb_parse_agent_expr
  2026-11-22 21:19   ` Andrew Burgess
@ 2026-11-24 18:56     ` Kevin Buettner
  0 siblings, 0 replies; 4+ messages in thread
From: Kevin Buettner @ 2026-11-24 18:56 UTC (permalink / raw)
  To: Andrew Burgess, gdb-patches



On 11/22/26 2:19 PM, Andrew Burgess wrote:
> Kevin Buettner <kevinb@redhat.com> writes:
> 
>> gdb_parse_agent_expr, defined in gdbserver/ax.cc, is called from four
>> different sites within gdbserver, two from mem-break.cc and two from
>> tracepoint.cc.
>>
>> Within tracepoint.cc, gdb_parse_agent_expr is called to handle the 'X'
>> portion of a QTDP packet.  The 'X' portion describes agent expressions
>> used by tracepoints for both conditions and actions.
>>
>> Within mem-break.cc, it's called to handle breakpoint conditions or
>> commands as part of a 'Z' packet.  These conditions or commands are
>> specified in a bytecode format identical to that used for agent
>> expressions for tracepoints.
>>
>> The current implementation of gdb_parse_agent_expr contains no checks
>> about validity of the length specified in the portion of a packet that
>> represents an agent expression.  This takes the form "X<len>,<hex>".
>> (This is not an 'X' packet but, rather, a portion of the 'QTDP' packet
>> for tracepoints or 'Z0' packet for breakpoints.)  gdb_parse_agent_expr
>> allocates LEN bytes and asks hex2bin to convert that number of bytes,
>> but hex2bin stops at the end of the packet, leaving the end of the
>> buffer uninitialized.  For a tracepoint condition, that buffer is
>> later sent back to GDB in reply to qTfP/qTsP, which are used to
>> request tracepoint data.  Thus, using an overly large length can
>> provide access to uninitialized gdbserver heap memory, a potential
>> security concern.
>>
>> The parse pointer is also advanced by 2 * LEN bytes regardless of how
>> much data was present, so callers continue parsing from a pointer that
>> can be far beyond the end of the packet; a large LEN crashes gdbserver.
>>
>> Fix this by having gdb_parse_agent_expr return nullptr (representing
>> an error condition) when the specified length exceeds the amount of
>> hex data actually present.  This code also wasn't checking for the
>> mandated comma character in the packet syntax.  I added a check and
>> error return for that too.
>>
>> The remainder of this patch to gdbserver consists of propagating the
>> error back to various points in the code where write_enn may be
>> called.  This causes the error to be sent as the reply to GDB.
>>
>> For Z0 packet support, I removed two warning messages.  One concerned
>> a malformed condition and the other was about a malformed action
>> (command).  A malformed condition was discarded, making the breakpoint
>> unconditional; similarly, a malformed action was simply dropped.  The
>> warning message (which might not even be seen by the user) stated
>> these facts.  gdbserver now returns an error response for these cases.
>>
>> The test case uses GDB's little-used "maintenance packet" command to
>> send bad packets to gdbserver.  This has been done not only for the
>> tracepoint related packets, but for the Z0 (set breakpoint) packet as
>> well.  Z0 used to silently drop malformed packets and set a breakpoint
>> without the (presumably intended, but malformed) condition or action.
>> It no longer does this.  Malformed conditions and actions within a
>> Z0 packet now cause an error to be returned.
>>
>> While working on this patch, I spotted numerous other problems in
>> nearby code (e.g. memory leaks and lack of packet syntax checking).
>> I left these alone.  gdbserver desperately needs attention to fix
>> myriad problems.
>>
>> This fixes the bug associated with CVE-2026-121996.
>>
>> Regression tested on x86_64 Fedora 44.  The new test case was tested
>> on RISC-V Fedora 42, for which neither Z0 packets nor tracepoint
>> packets are implemented.
>>
>> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34969
> 
> Hi Kevin,
> 
> Thanks for working on this.  I agree that the gdbserver code is in need
> of some serious updates, but you cannot be expected to fix everything in
> one go.  I think this fix is basically sound.  I do have a couple of
> minor nits (see inline below), but I think once these are resolved:
> 
> Approved-By: Andrew Burgess <aburgess@redhat.com>

Thanks for the approval.  I've pushed this commit with most of the
changes you've suggested.  I'll address the one that I had to tweak
below...

>> +# Send out Z0 / z0 (breakpoint) packets for this test.  Use "main" for the
>> +# address at which to place the breakpoint.
>> +proc test_Z0 {exp testname} {
>> +    with_test_prefix $testname {
>> +	prepare_server
>> +	set addr [format %x [get_hexadecimal_valueof "&main" 0]]
> 
> Is the 'format' really needed here?  Doesn't get_hexadecimal_valueof
> return a value in hex format?

For placing an address in a packet, we need a bare hex value, i.e. one
without the leading '0x'.  But `get_hexadecimal_valueof` uses GDB's
`print /x` in its implementation - that causes a leading `0x` to be
placed in front of the hex digits that we want to place in the packet.

It turns out that many of the packets which gdbserver consumes don't do
proper checking of expected delimiters.  So, for the packets that are
addressed by this commit, gdbserver might silently accept a hex value
with a leading 0x, but it'd get things wrong, e.g. reading an address of
0, followed by ignoring the x as some other delimiter (which should
actually be checked), followed by interpreting the actual address as
some other field that's not the address.  gdbserver *should* (IMO) check
each delimiter and send back an error reply when the format is wrong.

But `format %x` wasn't quite right either.  While it worked for this
test case, and on the architectures that I tested, the `%x` resulted in
a 32-bit truncation.  I decided to take the approach used by hex_in_list
in lib/gdb.exp.  It strips the leading `0x` via use of the TCL `regsub`
command.  `hex_in_list` also strips leading zeros; I didn't do that for
this test case.

I also put what I needed for this testcase into a proc named
`fetch_main_addr` so that if we decide that some other approach is
better, we can change just that one spot:

  # Return the address of main as bare hex digits.  Addresses in remote
  # protocol packets have no "0x" prefix; gdbserver would stop parsing
  # the address at the "x".
  proc fetch_main_addr {} {
      return [regsub ^0x [get_hexadecimal_valueof "&main" 0] ""]
  }

Note that this works even when `get_hexadecimal_valueof` returns a 0
in the unlikely case where `main` is not found.  In that case, the
`regsub` command won't find anything to substitute and will just leave
the string alone.

Regression tested again just prior to pushing; no regressions.

Kevin


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

end of thread, other threads:[~2026-11-24 18:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20  8:14 [PATCH v2 0/1] PR gdb/server/34969 Kevin Buettner
2026-11-20  8:14 ` [PATCH v2 1/1] gdbserver: reject malformed agent expressions in gdb_parse_agent_expr Kevin Buettner
2026-11-22 21:19   ` Andrew Burgess
2026-11-24 18:56     ` Kevin Buettner

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