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

commit 275c91f518b9182c94bc33c2788d661f9b6e0a54
Author: Jeremy Drake <cyg...@jdrake.com>
Date:   Fri Sep 19 13:36:30 2025 -0700

    Cygwin: lock cygheap during fork
    
    another thread may simultaneously be doing a cmalloc/cfree while the
    cygheap is being copied to the child.
    
    Addresses: https://cygwin.com/pipermail/cygwin/2025-September/258801.html
    Signed-off-by: Jeremy Drake <cyg...@jdrake.com>
    (cherry picked from commit 8a5d39527f9a56d1a623e86d30af6b590fd1472d)

Diff:
---
 winsup/cygwin/fork.cc       | 7 ++++++-
 winsup/cygwin/release/3.6.5 | 4 ++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/fork.cc b/winsup/cygwin/fork.cc
index f88acdbbf..2aa52737c 100644
--- a/winsup/cygwin/fork.cc
+++ b/winsup/cygwin/fork.cc
@@ -329,6 +329,7 @@ frok::parent (volatile char * volatile stack_here)
   /* NEVER, EVER, call a function which in turn calls malloc&friends while this
      malloc lock is active! */
   __malloc_lock ();
+  cygheap->lock ();
   bool locked = true;
 
   /* Remove impersonation */
@@ -483,6 +484,7 @@ frok::parent (volatile char * volatile stack_here)
                   impure, impure_beg, impure_end,
                   NULL);
 
+  cygheap->unlock ();
   __malloc_unlock ();
   locked = false;
   if (!rc)
@@ -568,7 +570,10 @@ cleanup:
   if (fix_impersonation)
     cygheap->user.reimpersonate ();
   if (locked)
-    __malloc_unlock ();
+    {
+      cygheap->unlock ();
+      __malloc_unlock ();
+    }
 
   /* Remember to de-allocate the fd table. */
   if (hchild)
diff --git a/winsup/cygwin/release/3.6.5 b/winsup/cygwin/release/3.6.5
index 6a37b6a43..d7cf8381f 100644
--- a/winsup/cygwin/release/3.6.5
+++ b/winsup/cygwin/release/3.6.5
@@ -25,3 +25,7 @@ Fixes:
 
 - Fix Ctrl-O (FLUSHO) handling.
   Addresses: https://cygwin.com/pipermail/cygwin/2025-August/258717.html
+
+- Fix multi-thread safety of fork()/exec() by adding the same locking as was
+  done for spawn.
+  Addresses: https://cygwin.com/pipermail/cygwin/2025-September/258801.html

Reply via email to