public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
From: "cvs-commit at gcc dot gnu.org" <sourceware-bugzilla@sourceware.org>
To: gdb-prs@sourceware.org
Subject: [Bug gdb/31259] [gdb] ThreadSanitizer: heap-use-after-free linux-nat.c:2809 in select_event_lwp
Date: Mon, 26 Feb 2024 15:28:53 +0000	[thread overview]
Message-ID: <bug-31259-4717-pAurWJpnyN@http.sourceware.org/bugzilla/> (raw)
In-Reply-To: <bug-31259-4717@http.sourceware.org/bugzilla/>

https://sourceware.org/bugzilla/show_bug.cgi?id=31259

--- Comment #2 from Sourceware Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Pedro Alves <palves@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=3d2d21728b6db4430ff168ee27e12fc6e2627fad

commit 3d2d21728b6db4430ff168ee27e12fc6e2627fad
Author: Pedro Alves <pedro@palves.net>
Date:   Wed Feb 21 16:23:55 2024 +0000

    [gdb] Fix heap-use-after-free in select_event_lwp

    PR gdb/31259 reveals one scenario where we run into a
    heap-use-after-free reported by thread sanitizer, while running
    gdb.base/vfork-follow-parent.exp.

    The heap-use-after-free happens during the following scenario:

     - linux_nat_wait_1 is about to return an event for T2.  It stops all
       other threads, and while doing so, stop_wait_callback -> wait_lwp
       sees T1 exit, and decides to leave the exit event pending.  It
       should have set the lp->stopped flag too, but does not -- this is
       the bug.

     - The event for T2 is reported, is processed by infrun, and we're
       back at linux_nat_wait_1.

     - linux_nat_wait_1 selects LWP T1 with the pending exit status to
       report.

     - it sets variable lp to point to the corresponding lwp_info.

     - it calls stop_callback and stop_wait_callback for all threads
       (because !target_is_non_stop_p ()).

     - it calls select_event_lwp to maybe pick another thread than T1, to
       prevent starvation.

    The problem is the following:

     - while calling stop_wait_callback for all threads, it also does this
       for T1.  While doing so, the corresponding lwp_info is deleted
       (callstack stop_wait_callback -> wait_lwp -> exit_lwp ->
       delete_lwp), leaving variable lp as a dangling pointer.

     - variable lp is passed to select_event_lwp, which derefences it,
       which causes the heap-use-after-free.

    Note that the comment here mentions "all other LWP's":
    ...
          /* Now stop all other LWP's ...  */
          iterate_over_lwps (minus_one_ptid, stop_callback);
          /* ... and wait until all of them have reported back that
            they're no longer running.  */
          iterate_over_lwps (minus_one_ptid, stop_wait_callback);
    ...

    The reason the comments say "all other LWP's", and doesn't bother
    filtering out LP is that lp->stopped should be true at this point, and
    the callbacks (both stop_callback and stop_wait_callback) check that
    flag, and do nothing if set.  I.e., they skip already-stopped threads,
    so they should skip LP.

    In this particular scenario, though, we missed setting the stopped
    flag right in the first step described above, so LP was iterated over
    incorrectly.

    The fix is to make wait_lwp set the lp->stopped flag when it decides
    to leave the exit event pending.  However, going a bit further,
    gdbserver has a mark_lwp_dead function to centralize setting up
    various lwp flags such that the rest of the code doesn't mishandle
    them, and it seems like a good idea to do a similar thing in gdb as
    well.  That is what this patch does.

    PR gdb/31259
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31259
    Co-Authored-By: Tom de Vries <tdevries@suse.de>
    Change-Id: I4a6169976f89bf714c478cbb2b7d4c32365e62a9

-- 
You are receiving this mail because:
You are on the CC list for the bug.

  parent reply	other threads:[~2024-02-26 15:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-18  9:12 [Bug gdb/31259] New: " vries at gcc dot gnu.org
2024-01-22 13:34 ` [Bug gdb/31259] " vries at gcc dot gnu.org
2024-02-26 15:28 ` cvs-commit at gcc dot gnu.org [this message]
2024-02-27  0:00 ` tromey at sourceware dot org
2024-02-27  8:20 ` vries at gcc dot gnu.org

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=bug-31259-4717-pAurWJpnyN@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=gdb-prs@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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).