Repository navigation
fix(bwrap): close pdeathsig parent race - #730
danielchristiancazares wants to merge 1 commit into
Conversation
Capture parent pid before PR_SET_PDEATHSIG and exit if parent changed after prctl.
|
This needs a lot more explanation, including what the race actually is, why |
|
Updated the description with a cleaner how and why. |
|
Was this tested? Does it work? |
|
What if the parent dies before the first |
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. |
I'll grab the testing scenarios I ran to show as part of the PR. |
This PR can be closed. It is absolute nonsense.
|
|
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. |
|
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.
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:
What you need to do instead is record the parent pid in the parent, and then compare that to Shouldn't be too hard to implement. |
Note that To further complicate the situation, |
|
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?). |
|
As long as we close it before the |
That's a fair and valid assessment. I see the issue. I'll look into this and propose a second PR. |
Honestly, that might be a better approach to the high-level use-case for this functionality. Using 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 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 I think |
When implementing (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.) |
|
@smcv I'm working on this now and I hoped you could give it a once over. I'll open a draft PR. |
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.