Harden output destination open against symlink clobbering (CWE-59)#763
Closed
thesmartshadow wants to merge 1 commit into
Closed
Harden output destination open against symlink clobbering (CWE-59)#763thesmartshadow wants to merge 1 commit into
thesmartshadow wants to merge 1 commit into
Conversation
Author
|
Hi I ran into a symlink-following output open on Unix (CWE-59) where the destination could be truncated at open time. This PR hardens the destination open using O_NOFOLLOW (kernel-enforced) while preserving common stdout targets, and adds a small regression test to prevent symlink destinations from truncating the target. |
Collaborator
Author
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.
What changed
This PR hardens output file creation on Unix by preventing symlink-following writes at open time.
O_NOFOLLOWwhen opening the destination path (kernel-enforced, avoids TOCTOU-style races)./dev/stdout,/proc/self/fd/1,/dev/fd/1) by opening them withoutO_CREAT/O_TRUNC.Why
The current implementation opens the destination with
create(true)+truncate(true). On Unix, that follows symlinks by default. If the destination path is a symlink, the target can be truncated/overwritten.The regression test specifically covers the important reliability aspect: the destination open happens early, so rejecting symlinks at open time ensures the target cannot be truncated even when later processing fails.
Testing
cargo test --release(includes the newtests/symlink_output.rstest)