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 2/5] gdbserver: merge look_up_one_symbol and relocate_instruction packet handling
Date: Tue, 24 Nov 2026 22:55:27 -0700	[thread overview]
Message-ID: <93333453-b4d8-40e2-a20f-adabf34d9690@redhat.com> (raw)
In-Reply-To: <18919cf28001b47de5aa101126ad7a17a69a469a.1795186852.git.aburgess@redhat.com>



On 11/20/26 8:12 AM, Andrew Burgess wrote:
> Both the look_up_one_symbol and relocate_instruction functions contain a
> loop which handles a limited number of packets received from GDB.  This
> commit merges these loops in order to reduce code duplication.  A
> follow-up commit fixes a buffer overflow bug in this code.  The bug was
> present at both locations, merging the code means only one site needs
> fixing.  The new merged code is added in a new function
> handle_limited_packets_while_waiting.
> 
> There is a potentially user-visible change in this commit though.
> Previously look_up_one_symbol handled m, x, and v packets only, while
> relocate_instruction handled m, M, x, and X packets.  By merging the
> code both functions now handle the superset of these packets, m, M, x,
> X, and v.  Both functions contained a comment saying something along the
> lines of "we should really handle all packets here, but for now these
> will do", I assume the current lists are based on what GDB happens to
> send.  Supporting more packets in each case should not have any negative
> consequences.  If it turns out that there are problems caused by
> accepting this wider set of packets then we can add a packet filter
> argument to handle_limited_packets_while_waiting, but I'd prefer to only
> do this once we know there's a need for it.
> 
> As part of the commit, the result of sending the reply to a 'v' packet
> is now error checked; previously, the look_up_one_symbol loop ignored a
> failure to send this reply.
> 
> Additionally, while thinking about the 'v' packet handling, I wondered
> what would happen if we received a vCont or vKill while we were within
> look_up_one_symbol or relocate_instruction, and I suspect the outcome
> would not be good.  If the inferior disappeared then these functions
> might end up trying to operate on freed process state.  The 'v' packet
> handling was added in commit:
> 
>   commit 28170b88cc8b40fdea2b065dafe6e1872a47ee4e
>   Date:   Sat Mar 12 14:03:26 2016 +0100
> 
>     gdbserver: Handle 'v' packet while processing qSymbol.
> 
> The commit message talks about needing vFile support.  So, while making
> this merge I have made the 'v' packet handling stricter.  We now drop
> out of the handle_limited_packets_while_waiting loop for vCont, vAttach,
> vRun, or vKill.  All other 'v' packets (including vFile) are handled as
> before.
> 
> To the best of my knowledge, existing GDB does not send any packets
> other than m, M, x, X, or v during these two exchanges.  If it did, the
> old code would have rejected the packet as a malformed response, and the
> resulting symbol lookup or instruction relocation failure would most
> likely have shown up as a failing test somewhere.  There should
> therefore be no visible changes after this commit.

I ran the entire series through an AI patch review.  After reading
through a very dense version of what has now been rewritten to be more
comprehensible, it appeared to me that you were aware of these concerns.
Nonetheless, you may wish to read what Claude thought of this patch:

  I have one point, and I'd class it as a nit: four 'v' packets now get
  no reply when they arrive during a qSymbol or qRelocInsn exchange.

  As I read handle_limited_packets_while_waiting, anything other than
  m, M, x, X, or a 'v' packet ends the loop and is handed back to the
  caller, which expects it to be the qSymbol or qRelocInsn answer.
  With this patch, vCont;, vAttach;, vRun;, and vKill; also end the
  loop instead of being carried out.  I agree with the reason you give:
  carrying out any of them in the middle of the exchange could resume,
  kill, or replace the inferior, and leave the caller working on stale
  or freed state.  Removing that risk fits well in this series.

  The side effect I noticed is that neither caller recognises these
  packets when they come back.  Both print "Malformed response ..."
  and give up, and nothing ever replies to the packet itself.  In the
  qSymbol case the main loop then sends "OK", so a client that sent
  vCont would get "OK" instead of a stop reply, and the two sides end
  up out of step.

  When I compared this with master, I found:

    - qSymbol path: this is new with the patch.  On master these
      packets were carried out and answered (unsafely, as you point
      out).

    - qRelocInsn path: this was already the case.  On master that loop
      ended on every 'v' packet and dropped it in the same way.

    - More generally, any unexpected packet (a 'g', say) was already
      handled like this in both loops.  The patch just puts these four
      packets into that existing group.

  I don't think this needs to hold up the series.  Your commit message
  covers it: these packets now end the loop, existing GDB sends nothing
  but m, M, x, X, or v during these exchanges, and you'd rather add
  filtering "once we know there's a need for it".  GDB can't send vCont
  in the middle of the exchange anyway, because it is busy servicing
  the exchange.  What's left is a protocol mix-up with a client that
  has already broken the protocol, not a memory-safety bug, so leaving
  it seems a reasonable choice to me.

  If you do want to tidy it up, one option would be to reply with an
  error rather than ending the loop:

            if (startswith (cs.own_buf, "vCont;")
                || startswith (cs.own_buf, "vAttach;")
                || startswith (cs.own_buf, "vRun;")
                || startswith (cs.own_buf, "vKill;"))
              write_enn (cs.own_buf);
            else
              handle_v_requests (cs.own_buf, len, &new_len);

  The putpkt at the bottom of the loop would send the E01 straight back
  as the reply to the refused packet, and the loop would go on waiting
  for the real qSymbol or qRelocInsn answer.  The client gets a clear
  refusal, and clients already have to cope with E01 from these
  packets.  The comment above the check would then say "refuse with an
  error" rather than "don't service".  I'd be equally happy to see this
  as a follow-up.

  Otherwise I like this patch.  Merging the two copied loops into one
  function means the later fixes only need one site, the reply to 'v'
  packets is now checked for send errors, and the vFile support added
  in 2016 is kept.

I'm fine with your approach of waiting until there's a demonstrated need
for a fix.  Obviously, if you want to post a follow-up that's okay too.

Approved-By: Kevin Buettner <kevinb@redhat.com>


  reply	other threads:[~2026-11-25  5:56 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 15:12 [PATCH 0/5] gdbserver: fix memory bugs in x, X, m, and M " Andrew Burgess
2026-11-20 15:12 ` [PATCH 1/5] gdbserver: avoid buffer overflow from 'm' and 'x' packets Andrew Burgess
2026-11-20 15:37   ` Eli Zaretskii
2026-11-23  5:26   ` Kevin Buettner
2026-11-20 15:12 ` [PATCH 2/5] gdbserver: merge look_up_one_symbol and relocate_instruction packet handling Andrew Burgess
2026-11-25  5:55   ` Kevin Buettner [this message]
2026-11-26 14:58     ` Andrew Burgess
2026-11-20 15:12 ` [PATCH 3/5] gdbserver: fix buffer overflow in more m/x " Andrew Burgess
2026-11-25  7:24   ` Kevin Buettner
2026-11-20 15:12 ` [PATCH 4/5] gdbserver: reject X packets with an invalid declared length Andrew Burgess
2026-11-25  7:48   ` Kevin Buettner
2026-11-25  7:48   ` Kevin Buettner
2026-11-20 15:12 ` [PATCH 5/5] gdbserver: reject M " Andrew Burgess
2026-11-25  8:03   ` Kevin Buettner

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=93333453-b4d8-40e2-a20f-adabf34d9690@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