fix(bwrap): close pdeathsig parent race#730
Open
danielchristiancazares wants to merge 1 commit intocontainers:mainfrom
Open
fix(bwrap): close pdeathsig parent race#730danielchristiancazares wants to merge 1 commit intocontainers:mainfrom
danielchristiancazares wants to merge 1 commit intocontainers:mainfrom
Conversation
Capture parent pid before PR_SET_PDEATHSIG and exit if parent changed after prctl.
Contributor
|
This needs a lot more explanation, including what the race actually is, why |
Author
|
Updated the description with a cleaner how and why. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.