[PATCH v2] [gdb] Fix heap-use-after-free in select_event_lwp
Pedro Alves
pedro@palves.net
Wed Feb 21 17:42:26 GMT 2024
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 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
...
Does this patch work for you?
>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
--
2.43.2
More information about the Gdb-patches
mailing list