Skip to content

fix(bwrap): close pdeathsig parent race - #730

Closed
danielchristiancazares wants to merge 1 commit into
containers:mainfrom
danielchristiancazares:fix/pdeathsig-race-check
Closed

danielchristiancazares wants to merge 1 commit into
containers:mainfrom
danielchristiancazares:fix/pdeathsig-race-check

Conversation

@danielchristiancazares

@danielchristiancazares danielchristiancazares commented Feb 23, 2026 •

Copy link
Copy Markdown
Contributor

At startup, the child process needs to die if the parent dies, so the child process sets PR_SET_PDEATHSIG so the kernel will SIGKILL it if the parent dies. Which is the whole setting up a parent-death signal with prctl. The problem is that if the parent dies after fork but before or during the prctl call, the child won't receive it because the parent is gone already so nothing will trigger it. So we end up with a VERY narrow window for failure after the fork/exec that can cause potential issues if the parent dies unexpectedly. The kill and _exit calls are there to handle the process safely if one of those windows is missed. I used _exit instead of exit so cleanup handlers don't run in post-forked code. It's a well known race and systemd and other projects use similar patterns to close it out.

As for the how it goes:
parent spawns child
child executes but hasn't called prctl(PR_SET_PDEATHSIG)
parent dies or exits
child then calls prctl(PR_SET_PDEATHSIG) but by now its too late

We grab ppid, set PDEATHSIG, then check later if getppid() isn't what what ppid() was before which means the parent is gone or swapped (like to PID 1 or something else) and this part is why it works at all. It's the reference point to compare against with later. It hardly ever happens, but it's documented and has a known a fix.

Capture parent pid before PR_SET_PDEATHSIG and exit if parent changed after prctl.
@swick

swick commented Feb 23, 2026

Copy link
Copy Markdown
Contributor

This needs a lot more explanation, including what the race actually is, why _exit is used, why it calls kill on itself instead of relying on exit. The PR description also suspiciously looks vibe-coded.

@danielchristiancazares

Copy link
Copy Markdown
Contributor Author

Updated the description with a cleaner how and why.

@rusty-snake

rusty-snake commented Feb 23, 2026 •

Copy link
Copy Markdown
Contributor

Was this tested? Does it work?
If so how was it tested?

@charmander

charmander commented Feb 24, 2026 •

Copy link
Copy Markdown

What if the parent dies before the first getppid call?

@danielchristiancazares

Copy link
Copy Markdown
Contributor Author

What if the parent dies before the first getppid call?

Then that's just pre-existing behavior that my change doesn't claim to fix. If the parent is already gone, you were never running under it in the meaningful sense. If the parent dies before getppid(), the child was already orphaned before it even entered this function. The patch doesn't make that case worse and it's not what PR_SET_PDEATHSIG is designed to cover either.

@danielchristiancazares

Copy link
Copy Markdown
Contributor Author

Was this tested? Does it work? If so how was it tested?

I'll grab the testing scenarios I ran to show as part of the PR.

@rusty-snake

Copy link
Copy Markdown
Contributor

What if the parent dies before the first getppid call?

Then that's just pre-existing behavior that my change doesn't claim to fix. If the parent is already gone, you were never running under it in the meaningful sense. If the parent dies before getppid(), the child was already orphaned before it even entered this function. The patch doesn't make that case worse and it's not what PR_SET_PDEATHSIG is designed to cover either.

This PR can be closed. It is absolute nonsense.

  • We still race in both cases.
  • We only mitigate the time frame that prctl (PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) is slower[citation needed] than getppid ().

@danielchristiancazares

Copy link
Copy Markdown
Contributor Author

That's not even what the patch is about. The race isn't a performance thing, it's a sequencing thing. The signal can't retroactively fire for a parent that already exited. The getppid check isn't faster, it's a detection mechanism. he whole point is you can't eliminate the race, you can only detect and handle it after the fact. That's what the systemd pattern does too. That's what every implementation of this does. There is no race-free version of PR_SET_PDEATHSIG​. The API itself has the inherent TOCTOU gap. The kernel would need a different syscall to close it atomically.

Closing.

@swick

swick commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

So I looked into this a bit, and I agree with @rusty-snake. The approach taken to detect the race doesn't work reliably. I still think it's valuable to detect the race reliably.

you can only detect and handle it after the fact

Yes, but the question here is if you can always detect the race or not. The code here does not always detect the race. Consider this sequence:

  1. process A forks
  2. process A exits
  3. process B calls getppid()
  4. process B sets up PR_SET_PDEATHSIG
  5. process B checks previous ppid against the new getppid(); they match
  6. process B didn't notice process A exiting

What you need to do instead is record the parent pid in the parent, and then compare that to getppid() in the child.

Shouldn't be too hard to implement.

@rusty-snake

Copy link
Copy Markdown
Contributor

What you need to do instead is record the parent pid in the parent, and then compare that to getppid() in the child.

Shouldn't be too hard to implement.

Note that handle_die_with_parent is called twice, once in the child to bind the lifetime to parent and once by the parent to bind its lifetime to the process calling bwrap. Only the first can be solved by recording in the parent. The second requires an pid argument for --die-with-parent that is checked again.

To further complicate the situation, getppid will always return 0 if --unshare-pid is used. Here you would need a different approach such as an anonymous pipe to check if the parent hasn't exited.

@swick

swick commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

Might be possible to always pass a pidfd instead. Not entirely sure if this doesn't also imply some capabilities on the process (i.e. can one kill it even from a new pidns?).

@rusty-snake

Copy link
Copy Markdown
Contributor

As long as we close it before the execveing other code it is fine.

@danielchristiancazares

Copy link
Copy Markdown
Contributor Author

So I looked into this a bit, and I agree with @rusty-snake. The approach taken to detect the race doesn't work reliably. I still think it's valuable to detect the race reliably.

you can only detect and handle it after the fact

Yes, but the question here is if you can always detect the race or not. The code here does not always detect the race. Consider this sequence:

  1. process A forks
  2. process A exits
  3. process B calls getppid()
  4. process B sets up PR_SET_PDEATHSIG
  5. process B checks previous ppid against the new getppid(); they match
  6. process B didn't notice process A exiting

What you need to do instead is record the parent pid in the parent, and then compare that to getppid() in the child.

Shouldn't be too hard to implement.

That's a fair and valid assessment. I see the issue. I'll look into this and propose a second PR.

@smcv

smcv commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator

To further complicate the situation, getppid will always return 0 if --unshare-pid is used. Here you would need a different approach such as an anonymous pipe to check if the parent hasn't exited.

Honestly, that might be a better approach to the high-level use-case for this functionality. Using PDEATHSIG is an implementation detail: if a caller of bwrap uses --die-with-parent, it does so because it wants some sort of high-level functionality.

I believe the high-level functionality that is wanted is something like this:

Application xyz runs a sandboxed thumbnailer/helper/something in a bubblewrap sandbox. If xyz exits, crashes or is killed for any reason, we want the bwrap supervisor (pid 1 in the sandbox if using --unshare-pid) to exit, which will result in the thumbnailer/helper also being killed.

Prior art: https://gitlab.steamos.cloud/steamrt/steam-runtime-tools/-/blob/main/bin/launcher-service.md?ref_type=heads, https://gitlab.steamos.cloud/steamrt/steam-runtime-tools/-/blob/main/bin/launcher-service.c?ref_type=heads (a service used by Steam, the details aren't particularly important) has --exit-on-readable=FD. Steam doesn't actually seem to use this option right now, but it could. test_exit_on_readable in https://gitlab.steamos.cloud/steamrt/steam-runtime-tools/-/blob/main/tests/test-launcher.py?ref_type=heads illustrates how a caller can use it to arrange for the service to be terminated.

I think --exit-on-readable FD would be a good name for this, if someone wants to add it to bwrap.

@smcv

smcv commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator

Note that handle_die_with_parent is called twice, once in the child to bind the lifetime to parent and once by the parent to bind its lifetime to the process calling bwrap.

When implementing --exit-on-readable FD, the child would probably still have to use PDEATHSIG, but the parent could use the pipe.

(The number of processes involved in bwrap is a bit of a maze, and it would be great if someone could add a diagram to the source similar to #672 (comment) which would give us clear, unambiguous terms for the various processes involved: the parent ("bwrap monitor" in that diagram), possibly an "intermediate child" inside the sandbox, possibly a pid 1 for the new namespace, and finally the COMMAND specified on bwrap's command-line.)

@danielchristiancazares

danielchristiancazares commented Mar 6, 2026 •

Copy link
Copy Markdown
Contributor Author

@smcv I'm working on this now and I hoped you could give it a once over. I'll open a draft PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants