Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: YuPeng <yupeng@kylinos.cn>, gdb-patches@sourceware.org
Cc: tromey@sourceware.org, debianyu@163.com
Subject: Re: [PATCH v2] Fix use-after-free when Python Breakpoint.stop method deletes the breakpoint
Date: Tue, 24 Nov 2026 11:26:50 +0000	[thread overview]
Message-ID: <xhuyfeccatqpx.fsf@aburgess-thinkpadp1gen8.rmtgb.csb> (raw)
In-Reply-To: <1795488876766726028.9924@kylinos.cn>

YuPeng <yupeng@kylinos.cn> writes:

> If a Python Breakpoint.stop method calls self.delete(), the underlying
> breakpoint is freed while bpstat is still processing it.
>
> delete_breakpoint tries to clear references with
> bpstat_remove_bp_location, but that only walks
> thread->control.stop_bpstat, which still points at the bpstat chain from
> the previous stop (it is cleared in proceed()), not the chain currently
> being built by bpstat_stop_status.  The current chain therefore keeps a
> dangling breakpoint pointer, and gdb crashes on the next access.
>
> The reproducer from the bug report returns False from stop(), so the
> crash happens right after the callback while infrun examines the bpstat
> chain (the report shows bpstat_explains_signal).
>
> Fix:
> - Assign thread->control.stop_bpstat = bs_head before the check_status
>   loop in bpstat_stop_status, so bpstat_remove_bp_location can find and
>   nullify the reference in the chain being processed.
> - In bpstat_check_breakpoint_conditions, return early if
>   bs->breakpoint_at became nullptr after the extension language callback.
> - In bpstat_stop_status, re-read b = bs->breakpoint_at after
>   bpstat_check_breakpoint_conditions and skip the entry if it is nullptr.
> - In gdbpy_breakpoint_cond_says_stop, keep a reference to the Python
>   breakpoint object while calling its stop method.  Deleting the
>   breakpoint drops the reference owned by the breakpoint object
>   registry, which can free the Python object before the caller has
>   finished using it.
>
> Add a testsuite test in which a Python breakpoint deletes itself from its
> stop method, and check that gdb does not crash and that the breakpoint
> is gone afterwards.
>
> Tested on x86_64-linux with an ASan build: without the fix gdb aborts
> with a heap-use-after-free; with it the new test passes and the inferior
> exits normally.
>
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34379
>
> I do not have an FSF copyright assignment on file; please let me know
> if you think this change needs one.
>
> ---
>
> Andrew,
>
>> This will for sure need a test adding to the testsuite.  You could look
>> at gdb.python/py-breakpoint*.exp for inspiration.
>
> You're right.  v2 adds gdb.python/py-breakpoint-delete.{c,exp,py}.  The
> test creates a gdb.Breakpoint subclass whose stop method calls
> self.delete() and returns False, then continues to the end of the
> program and checks that the breakpoint is gone.  With an ASan build the
> new test aborts with a heap-use-after-free before the fix and passes
> with it applied.
>
> Writing the test also turned up a second dangling reference: when the
> stop method deletes the breakpoint, the breakpoint object registry
> drops the last reference to the Python object while
> gdbpy_breakpoint_cond_says_stop is still using it.  v2 therefore also
> keeps a reference to the Python breakpoint object across the stop
> call.  The new test hits this case as well.
>
>> GDB style uses nullptr not NULL.
>
> Fixed in v2, thanks.
>
> Eli,
>
>> Thanks, but could you tell if the patch and the description were
>> generated by LLM, by any chance?
>
> I prepared the patch and the description with the help of an AI
> assistant, and I reviewed and tested the result myself.

Thanks you for preparing this patch, and the updates with a test.

Unless I've missed a policy change, then unfortunately, if an AI has
written any significant part of this patch, it cannot currently be
accepted into GDB.

I really hope this position will change soon.

If I'm wrong about the current GDB position then hopefully someone will
jump in and correct me.

I know this is not the answer you would have hoped for.

Thanks,
Andrew


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

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 10:34 [PATCH] " YuPeng
2026-11-18 14:26 ` Eli Zaretskii
2026-11-19 16:22 ` Andrew Burgess
2026-11-24  2:54   ` [PATCH v2] " YuPeng
2026-11-24 11:26     ` Andrew Burgess [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=xhuyfeccatqpx.fsf@aburgess-thinkpadp1gen8.rmtgb.csb \
    --to=aburgess@redhat.com \
    --cc=debianyu@163.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tromey@sourceware.org \
    --cc=yupeng@kylinos.cn \
    /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