From: Tom de Vries <tdevries@suse.de>
To: Pedro Alves <pedro@palves.net>, gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@polymtl.ca>
Subject: Re: [PATCH v2] [gdb] Fix heap-use-after-free in select_event_lwp
Date: Thu, 22 Feb 2024 12:43:55 +0100 [thread overview]
Message-ID: <ab380bc0-46aa-44d4-b6d9-44f87f52a9d5@suse.de> (raw)
In-Reply-To: <17059615-cba2-4ff2-a8dc-c6799930406f@palves.net>
On 2/21/24 18:42, Pedro Alves wrote:
> On 2024-02-19 15:04, Tom de Vries wrote:
>> On 2/9/24 16:46, Pedro Alves wrote:
>>> On 2024-01-23 11:48, Tom de Vries wrote:
>>> It sounds like we were about to report a stop for a thread that isn't marked as stopped?
>>> Now it looks to me that _that_ would be the bug to fix.
>>
>> This patch adds an assert to catch the bug you mention, and a fix in wait_lwp:
>> ...
>> diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
>> index e91c57ba239..5022da9abd2 100644
>> --- a/gdb/linux-nat.c
>> +++ b/gdb/linux-nat.c
>> @@ -2210,6 +2210,7 @@ wait_lwp (struct lwp_info *lp)
>> core. Store it in lp->waitstatus, because lp->status
>> would be ambiguous (W_EXITCODE(0,0) == 0). */
>> lp->waitstatus = host_status_to_waitstatus (status);
>> + lp->stopped = 1;
>> return 0;
>> }
>>
>> @@ -3368,6 +3369,7 @@ linux_nat_wait_1 (ptid_t ptid, struct target_waitstatus *ourstatus,
>> }
>>
>> gdb_assert (lp);
>> + gdb_assert (lp->stopped);
>>
>> status = lp->status;
>> lp->status = 0;
>> ...
>>
>> This fixes the problem observed in the PR, and passes testing on x86_64-linux and aarch64-linux.
>>
>> WDYT?
>
> Ah, thanks, so it was an exiting lwp that we're going to report the exit for.
>
> Yes, this is the right thing to do. We do the same in linux_nat_filter_event:
>
> /* This LWP is stopped now. (And if dead, this prevents it from
> ever being continued.) */
> lp->stopped = 1;
>
>
> 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. Like below. I ran the testsuite and saw no regressions.
>
I agree that it's a good idea, and also ran the test-suite and saw no
regressions.
> I tried to reproduce the gdb.base/vfork-follow-parent.exp issue described in the bug, but when I
> run that with a gdb built with -O0 -fsanitize=thread, the testcase just seemingly hangs starting
> up until I ctrl-c:
>
> Running /home/pedro/rocm/gdb/build-master-sanitize-thread/gdb/testsuite/../../../src/gdb/testsuite/gdb.base/vfork-follow-parent.exp ...
> WARNING: Couldn't set the height to 0
> WARNING: Couldn't set the width to 0.
> ^Cgot a INT signal, interrupted by user
>
> and when I look at gdb.log, I see it is full of the same report over and over:
>
> ...
> WARNING: ThreadSanitizer: signal-unsafe call inside of a signal (pid=3086741)
> #0 free ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:711 (libtsan.so.0+0x37ab8)
> #1 __gconv_close_transform iconv/gconv_db.c:800 (libc.so.6+0x2be3a)
> #2 ext_lang_initialization() ../../src/gdb/extension.c:339 (gdb+0x5c6a3f)
> #3 captured_main_1 ../../src/gdb/main.c:1058 (gdb+0x7c94da)
> #4 captured_main ../../src/gdb/main.c:1329 (gdb+0x7ca7be)
> #5 gdb_main(captured_main_args*) ../../src/gdb/main.c:1358 (gdb+0x7ca886)
> #6 main ../../src/gdb/gdb.c:39 (gdb+0xf98b3)
>
> SUMMARY: ThreadSanitizer: signal-unsafe call inside of a signal iconv/gconv_db.c:800 in __gconv_close_transform
> ...
>
>
Hmm, I can't reproduce that, not on aarch64-linux f39, nor on Leap 15.4
x86_64-linux.
At first glance this could be a tsan problem, given that the stack trace
doesn't show entry into a signal handler.
> Does this patch work for you?
>
It does.
As mentioned in the PR, on aarch64-linux this reproduces for me 5/10
times, and with your patch 0/10 times.
Given that pretty much the entire patch is yours, do you want to proceed
with this, or do you want me to integrate this into my patch with a
Co-Authored-By tag?
Thanks,
- Tom
> From 8cbc62be0dab8bb4b09fe05a52a348ddbfb2e33c Mon Sep 17 00:00:00 2001
> From: Pedro Alves <pedro@palves.net>
> Date: Wed, 21 Feb 2024 16:23:55 +0000
> Subject: [PATCH] mark_lwp_dead
>
> Change-Id: I4a6169976f89bf714c478cbb2b7d4c32365e62a9
> ---
> gdb/linux-nat.c | 34 +++++++++++++++++++++++++---------
> 1 file changed, 25 insertions(+), 9 deletions(-)
>
> diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
> index e91c57ba239..b3d319e0a82 100644
> --- a/gdb/linux-nat.c
> +++ b/gdb/linux-nat.c
> @@ -2123,6 +2123,27 @@ wait_for_signal ()
> }
> }
>
> +/* Mark LWP dead, with STATUS as exit status pending to report
> + later. */
> +
> +static void
> +mark_lwp_dead (lwp_info *lp, int status)
> +{
> + /* Store the exit status lp->waitstatus, because lp->status would be
> + ambiguous (W_EXITCODE(0,0) == 0). */
> + lp->waitstatus = host_status_to_waitstatus (status);
> +
> + /* If we're processing LP's status, there should be no other event
> + already recorded as pending. */
> + gdb_assert (lp->status == 0);
> +
> + /* Dead LWP's aren't expected to reported a pending sigstop. */
> + lp->signalled = 0;
> +
> + /* Prevent trying to stop it. */
> + lp->stopped = 1;
> +}
> +
> /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
> exited. */
>
> @@ -2207,9 +2228,8 @@ wait_lwp (struct lwp_info *lp)
>
> /* If this is the leader exiting, it means the whole
> process is gone. Store the status to report to the
> - core. Store it in lp->waitstatus, because lp->status
> - would be ambiguous (W_EXITCODE(0,0) == 0). */
> - lp->waitstatus = host_status_to_waitstatus (status);
> + core. */
> + mark_lwp_dead (lp, status);
> return 0;
> }
>
> @@ -3013,12 +3033,7 @@ linux_nat_filter_event (int lwpid, int status)
> linux_nat_debug_printf ("LWP %ld exited (resumed=%d)",
> lp->ptid.lwp (), lp->resumed);
>
> - /* Dead LWP's aren't expected to reported a pending sigstop. */
> - lp->signalled = 0;
> -
> - /* Store the pending event in the waitstatus, because
> - W_EXITCODE(0,0) == 0. */
> - lp->waitstatus = host_status_to_waitstatus (status);
> + mark_lwp_dead (lp, status);
> return;
> }
>
> @@ -3368,6 +3383,7 @@ linux_nat_wait_1 (ptid_t ptid, struct target_waitstatus *ourstatus,
> }
>
> gdb_assert (lp);
> + gdb_assert (lp->stopped);
>
> status = lp->status;
> lp->status = 0;
>
> base-commit: 99eeecc8d276e5af745e48825d66efff693a7678
next prev parent reply other threads:[~2024-02-22 11:43 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-23 11:48 Tom de Vries
2024-01-23 16:08 ` Simon Marchi
2024-01-23 17:52 ` Tom de Vries
2024-02-09 15:46 ` Pedro Alves
2024-02-19 15:04 ` Tom de Vries
2024-02-21 17:42 ` Pedro Alves
2024-02-22 11:43 ` Tom de Vries [this message]
2024-02-23 14:33 ` Pedro Alves
2024-02-26 14:23 ` Tom de Vries
2024-02-26 15:28 ` Pedro Alves
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=ab380bc0-46aa-44d4-b6d9-44f87f52a9d5@suse.de \
--to=tdevries@suse.de \
--cc=gdb-patches@sourceware.org \
--cc=pedro@palves.net \
--cc=simon.marchi@polymtl.ca \
/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).