Skip to content

Commit 8a5d395

Browse files
committed
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 <cygwin@jdrake.com>
1 parent 3a03874 commit 8a5d395

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

winsup/cygwin/fork.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ frok::parent (volatile char * volatile stack_here)
329329
/* NEVER, EVER, call a function which in turn calls malloc&friends while this
330330
malloc lock is active! */
331331
__malloc_lock ();
332+
cygheap->lock ();
332333
bool locked = true;
333334

334335
/* Remove impersonation */
@@ -483,6 +484,7 @@ frok::parent (volatile char * volatile stack_here)
483484
impure, impure_beg, impure_end,
484485
NULL);
485486

487+
cygheap->unlock ();
486488
__malloc_unlock ();
487489
locked = false;
488490
if (!rc)
@@ -568,7 +570,10 @@ frok::parent (volatile char * volatile stack_here)
568570
if (fix_impersonation)
569571
cygheap->user.reimpersonate ();
570572
if (locked)
571-
__malloc_unlock ();
573+
{
574+
cygheap->unlock ();
575+
__malloc_unlock ();
576+
}
572577

573578
/* Remember to de-allocate the fd table. */
574579
if (hchild)

winsup/cygwin/release/3.6.5

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ Fixes:
2525

2626
- Fix Ctrl-O (FLUSHO) handling.
2727
Addresses: https://cygwin.com/pipermail/cygwin/2025-August/258717.html
28+
29+
- Fix multi-thread safety of fork()/exec() by adding the same locking as was
30+
done for spawn.
31+
Addresses: https://cygwin.com/pipermail/cygwin/2025-September/258801.html

0 commit comments

Comments
 (0)