public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: exec: don't access cygheap before it's initialized
Date: Thu,  1 Dec 2022 21:53:20 +0000 (GMT)	[thread overview]
Message-ID: <20221201215320.CCEE2385840F@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=30add3e6b3e3211b3e2d4a093f45bee5c6e24b8b

commit 30add3e6b3e3211b3e2d4a093f45bee5c6e24b8b
Author:     Corinna Vinschen <corinna@vinschen.de>
AuthorDate: Thu Dec 1 21:33:14 2022 +0100
Commit:     Corinna Vinschen <corinna@vinschen.de>
CommitDate: Thu Dec 1 22:34:53 2022 +0100

    Cygwin: exec: don't access cygheap before it's initialized
    
    This is a long-standing thinko.
    
    When you exec a process, dll_crt0_0 in the child process calls
    child_info_spawn::handle_spawn().  handle_spawn() initialises the
    cygheap.
    
    Now consider calling strace.  Strace is a non-Cygwin process dynamically
    loading cygwin1.dll via LoadLibrary.  This in turn initializes the DLL:
    
    - dll_crt0_0 finds that the process it attaches to has been exec'd, so
      child_info_spawn::handle_spawn() is called.
    
    - 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.
    
    - However, at this time, the cygheap has not yet been initialized.  This
      only occurs in the cygheap_fixup_in_child() call after get_parent_handle()
      returns.
    
    --> SEGV
    
    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().
    
    Fix this problem by moving the assignment after the call to
    cygheap_fixup_in_child().
    
    Fixes: 3de7be4c1deb ("* DevNotes: Add entry cgf-000007. [...]")
    Fixes: 2f9b8ff00cce ("Cygwin: decouple cygheap from Cygwin DLL")
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

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 = OpenProcess (PROCESS_VM_READ, FALSE, parent_winpid);
-  moreinfo->myself_pinfo = NULL;
   return !!parent;
 }
 
@@ -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 = NULL;
 	memory_init ();
       }

                 reply	other threads:[~2022-12-01 21:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20221201215320.CCEE2385840F@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@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).