-
Notifications
You must be signed in to change notification settings - Fork 36
security: refuse to write credentials through pre-existing symlinks #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
garagon
wants to merge
3
commits into
stripe:main
Choose a base branch
from
garagon:security/credential-output-symlink-toctou
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry one more q - in what context would this be triggered and how has its behavior changed? Why would we want to unlink a symlinked file? Before
fs.accesswas just testing access permissions - why do we want to now potentially unlink? Wouldn't that invalidate the checks below about not following symlinked files?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the close read. Fair point that the prior --force flow didn't fully match the no-follow contract.
The unlink runs only when --force is set, and it clears the path so the subsequent open with O_CREAT | O_EXCL can succeed. For regular files that preserves the prior --force semantics (replace the existing output file), with the credential landing in a fresh 0o600 inode rather than racing a chmod on an already-open file descriptor.
For symlinks specifically, the no-follow guarantee doesn't cover the existing entry. The unlink runs before O_NOFOLLOW can fire, so the protection only covers the race window after the unlink, not the symlink that was already there. The credential never reaches the link target, but --force ends up silently destroying a symlink the operator may not have intended to remove.
Pushed a fix: lstat first, refuse if the final path is a symlink (with or without --force), unlink and recreate only for non-symlink existing files. O_EXCL | O_NOFOLLOW stays as the race defense between the precheck and the atomic create. With this, --force replaces an existing regular output file and rejects everything else.
Thanks again for the careful pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first part of the change makes sense. Why don't we simply add that check up top and leave the rest of the code unchanged? I'm lost on why we need all the changes below. I'd propose we keep things simply
That would make the diff much more reasonable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I agree the current diff is larger than ideal.
The part I'm hesitant to drop is the final
openpath. Thelstatcheck up front is useful for clearer errors, but it is still only a precheck. If we go back tofs.writeFile, the actual write resolves the path again and follows symlinks.The other subtle bit is
mode: 0o600: it only applies when a new file is created. In the--forcepath, if the destination already exists,writeFilewrites first and the explicitchmodhappens after the credential bytes are already on disk.So I think we need two things:
0o600file atomically.Your suggested
lstatblock covers the first one. Thefs.open(O_CREAT | O_EXCL | O_WRONLY | O_NOFOLLOW, 0o600)part is what covers the second one.A concrete
--forcerace I'm trying to avoid:T0: we
lstatand see no file atresolvedT1: another local user creates
resolvedas a symlink to a file they can already readT2: because
force=true, thefs.accesscheck is skippedT3:
fs.writeFilefollows the symlink and writes the credential into that targetT4:
fs.chmodfollows the symlink and locks the target down to0o600, but the credential was already written through the symlinkI can definitely trim the implementation and comments so the diff is smaller. My proposal would be:
lstatprecheck for clear symlink errors--forcelimited to replacing existing regular filesfs.open(... O_EXCL | O_NOFOLLOW, 0o600)plushandle.writeThat should keep the PR focused while preserving the part that closes the TOCTOU.