Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Kevin Buettner <kevinb@redhat.com>
To: Andrew Burgess <aburgess@redhat.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH v2 1/1] gdbserver: reject malformed agent expressions in gdb_parse_agent_expr
Date: Tue, 24 Nov 2026 11:56:56 -0700	[thread overview]
Message-ID: <ed58a1da-f960-4fd0-9628-0d1d77f0dfd9@redhat.com> (raw)
In-Reply-To: <xhuyf1p8clg3a.fsf@aburgess-thinkpadp1gen8.rmtgb.csb>



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


      reply	other threads:[~2026-11-24 18:57 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

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=ed58a1da-f960-4fd0-9628-0d1d77f0dfd9@redhat.com \
    --to=kevinb@redhat.com \
    --cc=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