Skip to contentSkip to conversation

Looking at "int vforked" in signal handler is racy

DISCUSSION Denys Vlasenko 3 participants 9 messages Aug 9, 2025 – Aug 24, 2025

lore
Share & tools
lore
#0 lore raw

struct job *vforkexec(union node *n, char **argv, const char *path, int idx)

{
        struct job *jp;
        int pid;

        jp = makejob(1);

        sigblockall(NULL);
        vforked++;

<<<< Parent can get a signal here.

        pid = vfork();

        if (!pid) {
                forkchild(jp, n, FORK_FG);
                sigclearmask();
                shellexec(argv, path, idx);
                /* NOTREACHED */
        }

<<<< Parent can get a signal here. The window is in fact not that small:
<<<< in the child, execve() syscall is rather complex (needs to tear down memory <<<< mappings, which causes TLB invalidation) and takes time.
<<<< all this time parent is blocked and does not execute,
<<<< so it don't yet reach the next line:

        vforked = 0;
	sigclearmask();

Signal handler:

void
onsig(int signo)

{
        if (vforked)
                return;

^^^^^^^^^^ this assumes we dot signal in the vforked child, but it may be false! We may be in the parent!

How to solve this.... at any given time,
there are only two processes which share address space via vfork, all other possibly existing copies of shell processes (such as the grandparent shell if the parent is a subshell) have a separate address space.

So, in signal handler, we are in three possible situations:

* we don't have a "vfork sibling" at all (we aren't after vfork)
* we are after vfork and we are parent: need to handle the signal
* we are after vfork and we are child: must not mess up the parent's address space, thus do NOT handle the signal
(or else it can e.g. set intpending = 1 *in the parent too*!)

I see this solution:

vfork_parent_pid = getpid();
have_vfork_sibling = 1;
pid = vfork();
if (!pid) { child ops; NORETURN; }
have_vfork_sibling = 0;

void
onsig(int signo)

{
        if (after_vfork && getpid() != vfork_parent_pid)
                /* We are vfork child, DO NOT MODIFY ANY VARIABLES! */
                return;
#1 lore raw
Quoted 9 lines

I see this solution:

vfork_parent_pid = getpid();
have_vfork_sibling = 1;
pid = vfork();
if (!pid) { child ops; NORETURN; }
have_vfork_sibling = 0;

Quoted 4 lines

void
onsig(int signo)
{
        if (after_vfork && getpid() != vfork_parent_pid)

               ^^^^^^^^^^^s/after_vfork/have_vfork_sibling/
Quoted 2 lines

                /* We are vfork child, DO NOT MODIFY ANY VARIABLES! */ return;

#2 lore raw
In reply to Denys Vlasenko

Hi,

Quoted 11 lines · Denys Vlasenko

struct job *vforkexec(union node *n, char **argv, const char *path, int idx) {
        struct job *jp;
        int pid;

        jp = makejob(1);

        sigblockall(NULL);
        vforked++;

<<<< Parent can get a signal here.

The sigblockall(NULL) is meant to prevent that from happening. Likewise for the next one you point out. Is sigblockall(NULL) insufficient for some reason?

Cheers,
Harald van Dijk

#3 lore raw
Quoted 8 lines · Harald van Dijk

Hi,

Quoted 5 lines · Denys Vlasenko

struct job *vforkexec(union node *n, char **argv, const char *path, int idx) {
         struct job *jp;
         int pid;

Quoted 7 lines
Quoted 6 lines

         jp = makejob(1);

         sigblockall(NULL);
         vforked++;

<<<< Parent can get a signal here.

The sigblockall(NULL) is meant to prevent that from happening.

Yep, missed that. Should be ok. I'm mistaken.

However, this method requires three syscalls:
one to mask all signals before vfork,
then two syscalls (one in the parent and one in the child) to unmask them back.

Whereas the method of recording PID usually needs
just one getpid() syscall.
Should we consider switching to that method?

#4 lore raw
Quoted 19 lines · Denys Vlasenko
Quoted 8 lines · Harald van Dijk

Hi,

Quoted 5 lines · Denys Vlasenko

struct job *vforkexec(union node *n, char **argv, const char *path, int idx) {
         struct job *jp;
         int pid;

Quoted 7 lines
Quoted 6 lines

         jp = makejob(1);

         sigblockall(NULL);
         vforked++;

<<<< Parent can get a signal here.

The sigblockall(NULL) is meant to prevent that from happening.

Yep, missed that. Should be ok. I'm mistaken.

Quoted 8 lines

However, this method requires three syscalls:
one to mask all signals before vfork,
then two syscalls (one in the parent and one in the child) to unmask them back.

Whereas the method of recording PID usually needs
just one getpid() syscall.
Should we consider switching to that method?

That approach seems solid to me at a very quick glance. I think you don't even need separate have_vfork_sibling and vfork_parent_pid variables, just make the existing vforked variable a pid, or 0. A mostly untested patch using that:

src/jobs.c +1 -4
@@ -991,20 +991,17 @@ struct job *vforkexec(union node *n, char **argv, const char *path, int idx)
 
        jp = makejob(1);
 
-       sigblockall(NULL);
-       vforked++;
+       vforked = getpid();
 
        pid = vfork();
 
        if (!pid) {
                forkchild(jp, n, FORK_FG);
-               sigclearmask();
                shellexec(argv, path, idx);
                /* NOTREACHED */
        }
 
        vforked = 0;
-       sigclearmask();
        forkparent(jp, n, FORK_FG, pid);
 
        return jp;
src/trap.c +1 -1
@@ -312,7 +312,7 @@ ignoresig(int signo)
 void
 onsig(int signo)
 {
-       if (vforked)
+       if (vforked && getpid() != vforked)
                return;
 
        if (signo == SIGCHLD) {

I do wonder if the use of vforked in a signal handler, even in current dash, would require the use of volatile to ensure no compiler optimisations mess with it, but I think that is not affected by this patch. If it is necessary, it is already necessary now, and if it is not necessary now, it will not become necessary with this patch.

Cheers,
Harald van Dijk

#5 lore raw
Quoted 5 lines · Harald van Dijk

That approach seems solid to me at a very quick glance. I think you don't even need separate have_vfork_sibling and vfork_parent_pid variables, just make the existing vforked variable a pid, or 0. A mostly untested patch using that:

Looks good to me. Could you turn this into a patch please? If you could run some quick speed tests on this versus the existing code that would be even better :)

Quoted 5 lines

I do wonder if the use of vforked in a signal handler, even in current dash, would require the use of volatile to ensure no compiler optimisations mess with it, but I think that is not affected by this patch. If it is necessary, it is already necessary now, and if it is not necessary now, it will not become necessary with this patch.

Yes vfork should be marked as volatile. Although being a global variable means that there shouldn't be any practical difference.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#6 lore raw
Quoted 10 lines · Herbert Xu
Quoted 5 lines · Harald van Dijk

That approach seems solid to me at a very quick glance. I think you don't even need separate have_vfork_sibling and vfork_parent_pid variables, just make the existing vforked variable a pid, or 0. A mostly untested patch using that:

Looks good to me. Could you turn this into a patch please? If you could run some quick speed tests on this versus the existing code that would be even better :)

It will be a little while before I can do meaningful measurements, but I will do so when I can.

Cheers,
Harald van Dijk

#7 lore raw
Quoted 8 lines · Harald van Dijk
Quoted 7 lines · Herbert Xu
Quoted 5 lines · Harald van Dijk

That approach seems solid to me at a very quick glance. I think you don't even need separate have_vfork_sibling and vfork_parent_pid variables, just make the existing vforked variable a pid, or 0. A mostly untested patch using that:

Quoted 5 lines
Quoted 3 lines

Looks good to me.  Could you turn this into a patch please? If you could run some quick speed tests on this versus the existing code that would be even better :)

It will be a little while before I can do meaningful measurements, but I will do so when I can.

The impact on config.status --recheck, measured by running it 100 times with the patch, 100 times without, and averaging the run times, reveals that the impact is so low it is lost in the noise, and not even clear it is beneficial. One attempt showed an average of 2.225s without the patch, 2.221s with the patch. Doing the exact same measurements again showed an average of 2.221s without the patch, 2.223s with the patch.

The build was using clang 19.1.7 on x86_64, no special compiler flags, just -O2.

The patch that I tested is attached.

I welcome anyone doing their own measurements using better benchmarks to see if they can determine whether this patch is useful.

Cheers,
Harald van Dijk

From a112e6581540bf7d34bb2f47042e8927bcd95d0d Mon Sep 17 00:00:00 2001
From: Harald van Dijk <[email protected]>
Date: Fri, 22 Aug 2025 02:05:14 +0100
Subject: [PATCH] jobs: avoid blocking signals on vfork().

As pointed out by Denys Vlasenko, we can avoid blocking signals on vfork() by making the signal handler of a vfork child immediately return. This saves a syscall.

---
 src/jobs.c | 5 +----
 src/trap.c | 2 +-
 2 files changed, 2 insertions(+), 5 deletions(-)
src/jobs.c +1 -4
index 51e6fa1..83d9694 100644
@@ -991,20 +991,17 @@ struct job *vforkexec(union node *n, char **argv, const char *path, int idx)
 
 	jp = makejob(1);
 
-	sigblockall(NULL);
-	vforked++;
+	vforked = getpid();
 
 	pid = vfork();
 
 	if (!pid) {
 		forkchild(jp, n, FORK_FG);
-		sigclearmask();
 		shellexec(argv, path, idx);
 		/* NOTREACHED */
 	}
 
 	vforked = 0;
-	sigclearmask();
 	forkparent(jp, n, FORK_FG, pid);
 
 	return jp;
src/trap.c +1 -1
index aebffa0..23829a5 100644
@@ -312,7 +312,7 @@ ignoresig(int signo)
 void
 onsig(int signo)
 {
-	if (vforked)
+	if (vforked && getpid() != vforked)
 		return;
 
 	if (signo == SIGCHLD) {
Signature · 1 line
2.47.2
#8 lore raw
Quoted 6 lines · Harald van Dijk

From a112e6581540bf7d34bb2f47042e8927bcd95d0d Mon Sep 17 00:00:00 2001
From: Harald van Dijk <[email protected]>
Date: Fri, 22 Aug 2025 02:05:14 +0100
Subject: [PATCH] jobs: avoid blocking signals on vfork().

Quoted 7 lines

As pointed out by Denys Vlasenko, we can avoid blocking signals on vfork() by making the signal handler of a vfork child immediately return. This saves a syscall.

---
 src/jobs.c | 5 +----
 src/trap.c | 2 +-
 2 files changed, 2 insertions(+), 5 deletions(-)

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt