From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id CCEE2385840F; Thu, 1 Dec 2022 21:53:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CCEE2385840F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1669931600; bh=FTaPsNJPLPr3k66ikM/ExAdypY1f4M770apV/fP5JY4=; h=From:To:Subject:Date:From; b=ylJib36JJ0z9S8bwHZvrupgpZf6xSVy1SWxhuTw8GpGCnFONZSLzhuPN9zGwHvjO2 jg/nE9EtDBDrJg3k98ZUbN/s7SOG0D7GW3uGxFrPCreeSrVl6dnrMMKUsaBLUekrnu P8OaghAcHKyVUV1j47z/gjnkHysT/BeIlrrik0JM= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: exec: don't access cygheap before it's initialized X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/master X-Git-Oldrev: e358f8d12f1537a7d0509984207a700205b2c20f X-Git-Newrev: 30add3e6b3e3211b3e2d4a093f45bee5c6e24b8b Message-Id: <20221201215320.CCEE2385840F@sourceware.org> Date: Thu, 1 Dec 2022 21:53:20 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D30add3e6b3e= 3211b3e2d4a093f45bee5c6e24b8b commit 30add3e6b3e3211b3e2d4a093f45bee5c6e24b8b Author: Corinna Vinschen AuthorDate: Thu Dec 1 21:33:14 2022 +0100 Commit: Corinna Vinschen CommitDate: Thu Dec 1 22:34:53 2022 +0100 Cygwin: exec: don't access cygheap before it's initialized =20 This is a long-standing thinko. =20 When you exec a process, dll_crt0_0 in the child process calls child_info_spawn::handle_spawn(). handle_spawn() initialises the cygheap. =20 Now consider calling strace. Strace is a non-Cygwin process dynamically loading cygwin1.dll via LoadLibrary. This in turn initializes the DLL: =20 - dll_crt0_0 finds that the process it attaches to has been exec'd, so child_info_spawn::handle_spawn() is called. =20 - If the DLL is being dynamically loaded, handle_spawn() calls child_info_spawn::get_parent_handle(). This in turn tries to set the moreinfo->myself_pinfo value inside the cygheap to NULL. =20 - However, at this time, the cygheap has not yet been initialized. This only occurs in the cygheap_fixup_in_child() call after get_parent_han= dle() returns. =20 --> SEGV =20 This thinko never had a negative side effect, because the cygheap was pre-allocated at DLL load time until commit 2f9b8ff00cce ("Cygwin: decouple cygheap from Cygwin DLL"). With 2f9b8ff00cce, the cygheap actually doesn't exist until after the call to cygheap_fixup_in_child(). =20 Fix this problem by moving the assignment after the call to cygheap_fixup_in_child(). =20 Fixes: 3de7be4c1deb ("* DevNotes: Add entry cgf-000007. [...]") Fixes: 2f9b8ff00cce ("Cygwin: decouple cygheap from Cygwin DLL") Signed-off-by: Corinna Vinschen Diff: --- winsup/cygwin/dcrt0.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc index e5f19f1f930d..5c5a280cce9d 100644 --- a/winsup/cygwin/dcrt0.cc +++ b/winsup/cygwin/dcrt0.cc @@ -620,7 +620,6 @@ bool child_info_spawn::get_parent_handle () { parent =3D OpenProcess (PROCESS_VM_READ, FALSE, parent_winpid); - moreinfo->myself_pinfo =3D NULL; return !!parent; } =20 @@ -632,6 +631,8 @@ child_info_spawn::handle_spawn () if (!dynamically_loaded || get_parent_handle ()) { cygheap_fixup_in_child (true); + if (dynamically_loaded) + moreinfo->myself_pinfo =3D NULL; memory_init (); }