From: Kevin Buettner <kevinb@redhat.com>
To: gdb-patches@sourceware.org
Cc: Kevin Buettner <kevinb@redhat.com>
Subject: [PATCH 1/2] gdbserver: reject malformed agent expressions in gdb_parse_agent_expr
Date: Tue, 10 Nov 2026 00:07:53 -0700 [thread overview]
Message-ID: <20261110074218.1600179-2-kevinb@redhat.com> (raw)
In-Reply-To: <20261110074218.1600179-1-kevinb@redhat.com>
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 an agent
expression 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' or 'Z' packet. These conditions or commands
are specified in a bytecode format identical to that used for agent
expressions for tracepoints. Despite the fact that they use the same
format, this functionality is separate from the tracepoint facility.
(Though within gdbserver, some of it is implemented using the same code.)
The current implementation of gdb_parse_agent_expr has 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 'Z' 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.
The parse pointer is also advanced by 2 * LEN regardless of how much
data was present, so the callers continue parsing from a pointer that
can be far beyond the end of the packet; a large LEN crashes
gdbserver. Similarly, a missing comma after LEN causes the parser to
step past the packet's terminating NUL.
Fix this by having gdb_parse_agent_expr return nullptr when the comma
is missing or when LEN exceeds the amount of hex data remaining in the
packet. The Z packet condition and command paths (in mem-break.cc)
already handle a nullptr return. Make the tracepoint action parser
discard an action whose expression could not be parsed, and make the
QTDP parser skip a malformed condition, so that any remaining fields,
including the trailing hyphen, are still processed. As is already the
case for breakpoint conditions, this change causes the tracepoint
to be defined without the condition.
This fixes the bug associated with CVE-2026-121996.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34969
---
gdbserver/ax.cc | 15 ++++++++++++---
gdbserver/ax.h | 4 +++-
gdbserver/tracepoint.cc | 12 ++++++++++++
3 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/gdbserver/ax.cc b/gdbserver/ax.cc
index 4993bed2532..beec324f9b0 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,17 @@ gdb_parse_agent_expr (const char **actparm)
++act; /* skip the X */
act = unpack_varlen_hex (act, &xlen);
- ++act; /* skip a comma */
+
+ /* The length must be followed by a comma, and must not exceed the
+ amount of hex data present in the packet. Otherwise HEX2BIN would
+ leave the tail of the buffer uninitialized, and the parse pointer
+ would be advanced past the end of the packet. */
+ if (*act != ',')
+ return nullptr;
+ ++act; /* skip the comma */
+ if (xlen > strlen (act) / 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/tracepoint.cc b/gdbserver/tracepoint.cc
index 6f657257ee8..d0f9228cad1 100644
--- a/gdbserver/tracepoint.cc
+++ b/gdbserver/tracepoint.cc
@@ -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);
+ action = nullptr;
+ }
break;
}
default:
@@ -2404,6 +2409,13 @@ cmd_qtdp (char *own_buf)
else if (*packet == 'X')
{
tpoint->cond = gdb_parse_agent_expr (&packet);
+ if (tpoint->cond == nullptr)
+ {
+ /* Skip the malformed expression, so that any remaining
+ fields, and the trailing hyphen, are still seen. */
+ trace_debug ("Malformed tracepoint condition, ignoring");
+ packet += strcspn (packet, ":-");
+ }
}
else if (*packet == '-')
break;
--
2.56.0
next prev parent reply other threads:[~2026-11-10 7:43 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-10 7:07 [PATCH 0/2] PR gdb/server/34969 Kevin Buettner
2026-11-10 7:07 ` Kevin Buettner [this message]
2026-11-10 7:07 ` [PATCH 2/2] gdb/testsuite: test malformed agent expressions sent to gdbserver Kevin Buettner
2026-11-11 12:36 ` Andrew Burgess
2026-11-10 12:58 ` [PATCH 0/2] PR gdb/server/34969 Eli Zaretskii
2026-11-10 15:15 ` Simon Marchi
2026-11-11 6:13 ` Kevin Buettner
2026-11-11 12:37 ` Eli Zaretskii
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=20261110074218.1600179-2-kevinb@redhat.com \
--to=kevinb@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