Fix fd races in dup and fcntl duplication paths#2732
Open
SongXiaoXi wants to merge 1 commit into
Open
Conversation
Take fd references under the fdtable lock when duplicating descriptors so dup(), dup3(), and fcntl(F_DUPFD*) cannot race with a concurrent close() and retain a freed fd. This also makes fd refcount updates use the fd lock and adds retained-fd helpers for fcntl paths that operate on an existing descriptor after the table lookup. Add a guest e2e regression test that stresses dup, dup2, and fcntl(F_DUPFD_CLOEXEC) concurrently. The same test fails on an unpatched tree and passes with this change. While tightening refcount handling, fix stdio initialization to transfer the original fd reference directly instead of reviving a zero-refcount fd.
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.
This PR fixes a file descriptor race in the duplication paths.
Before this change,
dup(),dup3(), andfcntl(F_DUPFD/F_DUPFD_CLOEXEC)could look up an fd, drop the table lock, and only then increment the fd
refcount. A concurrent
close()could free the underlyingstruct fdduring that window, leaving the duplication path with a stale pointer.
This patch fixes that by:
dup/dup3/fcntl(F_DUPFD*)lookup and install sequence inthe same locked region
fcntlpaths that need to operate on anfd after lookup
While doing that, this also fixes standalone stdio initialization to transfer
the initial fd reference directly instead of setting the refcount to zero and
reviving it with
fd_retain().Test coverage
This PR adds a guest-side e2e regression test that compiles and runs inside
iSH and stresses:
dup()dup2()fcntl(F_DUPFD_CLOEXEC)The test reproduces the problem on an unpatched tree and passes with this fix.