Reject symlinks and hardlinks on Windows when opening .part files - #39
Merged
Conversation
The POSIX path refused a pre-planted symlink (O_NOFOLLOW) and a hardlink (st_nlink == 1) at the .part path, but the Windows path opened it with CREATE_ALWAYS and no reparse-point handling. A pre-planted reparse point or hardlink therefore redirected the receiver's writes to a victim file, and CREATE_ALWAYS truncated it first. Open with OPEN_ALWAYS (never truncates) and FILE_FLAG_OPEN_REPARSE_POINT (the link itself, not its target), then reject reparse points, directories and any file with more than one link via GetFileInformationByHandle - the Win32 counterpart to the POSIX guard, covering both fresh and --resume opens.
Mirror the existing hardlink test: plant a symlink at the predictable <name>.part and assert the receiver refuses (non-zero exit) and leaves the victim byte-for-byte intact. O_NOFOLLOW (POSIX) and FILE_FLAG_OPEN_REPARSE_POINT + GetFileInformationByHandle (Windows) both refuse it; this pins that against regressions. The Windows e2e job is skipped in CI (Winsock SO_REUSEADDR), so the test covers the POSIX half while the compile job covers the Windows guard. Also expand the isLoneRegularFile(HANDLE) comment to explain that the DIRECTORY bit is defence in depth - CreateFileA without FILE_FLAG_BACKUP_SEMANTICS won't return a directory handle, but rejecting it keeps the guard honest if that ever changes.
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.
Problem
The POSIX receiver already refuses a pre-planted symlink (
O_NOFOLLOW) and hardlink (st_nlink == 1) at the predictable<name>.partpath (#32). The Windows path had no equivalent guard: it opened.partwithCREATE_ALWAYSand no reparse-point handling. A local attacker who can write the receiver's working directory could therefore plant a reparse point or hardlink at the predictable<name>.partname and:CREATE_ALWAYStruncate the victim first.Fix
openPartFileon Windows now mirrors the POSIX guard:CREATE_ALWAYS→OPEN_ALWAYS— never truncates an existing file before it is vetted.FILE_FLAG_OPEN_REPARSE_POINT— opens the link itself, not its target.isLoneRegularFile(HANDLE)viaGetFileInformationByHandlerejects a reparse point, a directory, or any file withnNumberOfLinks != 1, before any write — covering both fresh and--resumeopens.The check runs on the same handle used for writes, so there is no TOCTOU window between vetting and use.
Tests
run_symlink_part_teste2e case, mirroring the existing hardlink test: plants a symlink at<name>.part, then asserts the receiver exits non-zero and leaves the victim byte-for-byte intact.Notes
FILE_ATTRIBUTE_DIRECTORYbit inisLoneRegularFileis defence in depth (CreateFileAwithoutFILE_FLAG_BACKUP_SEMANTICSwon't return a directory handle); a comment now explains why it stays.