gdbserver: reject malformed agent expressions in gdb_parse_agent_expr

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

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