Skip to content

Windows: admit paths on a mapped network drive (#1107) - #1110

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/1107
Oct 18, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/1107

Conversation

@scttfrdmn

@scttfrdmn scttfrdmn commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1107

The bug

ValidatePathForReading resolves an existing path with GetFinalPathNameByHandle (#1036) so that a junction or symlink can't lead into a system directory. On a mapped network drive, Z:\data\x resolves to \\?\UNC\server\share\data\x, and normaliseFinalPath turns that into \\server\share\data\x. The resolved-path shape check refuses every UNC form, so every existing path on a mapped drive was refused. It failed closed: nothing unsafe got through, it just refused legitimate paths.

(The issue calls this an "allowed root" mismatch. In this code the roots are a denylist, forbiddenReadRoots. The refusal actually comes from windowsPathShapeError(resolved) rejecting UNC. The fix follows the issue's direction anyway.)

The change

When the resolved path is refused for its shape, mappedDriveError (path.go, plain string handling, called with resolveExisting and onSystemVolume) decides whether it is a path on a mapped drive. It resolves the drive's root (Z:\) through the same resolveExisting, so both sides are in \\server\share form. It admits the path only if all of these hold:

  1. abs is on a drive letter, and that drive's root resolves to a plain \\server\share[\...]. It must not be \\?\ or \\.\, and it must not be a drive form: a subst drive resolves to the local directory it names, and that keeps the existing comparison.
  2. The resolved path is at or under that share, so no link on the drive led to another share or out of the mapped folder. The part after the share also passes windowsPathShapeError (no streams, device names, or trailing dot/space).
  3. onSystemVolume(resolved) is false. This reads the volume serial of the longest existing prefix (GetFileInformationByHandle) and compares it with the serials of the existing forbidden roots. It is true, or an error, so the path is refused, when they match or when no system directory's serial can be read.

Nothing is translated back to the drive letter. The resolved path is checked as the place it actually reaches. The existing underAnyRoot(resolved, roots) check still runs afterwards.

Security reasoning

  • Junction or symlink under a root that points outside it: unchanged on every OS. A non-UNC resolved path takes the same code path as before. A local link to a share fails rule 1 (the drive root C:\ is not UNC). A link on a mapped drive to a different share or folder fails rule 2.
  • A root that is itself a link can't widen what is admitted. subst W: C:\Windows resolves to C:\Windows\... and is refused by the existing root comparison, because nothing maps it back to W:. A drive mapped to a loopback share is the case the prefix comparison can't see: \\localhost\C$, a share of C:\Windows, or a share containing a junction into it (the server follows the junction, so the client's final path still names the share). All of these were refused before because they are UNC. Rule 3 keeps them refused: the server reports the serial of the volume the file is really on, so they match the local system volume however they are spelled.
  • What it costs: a mapped share is refused if its volume serial matches this machine's system volume. That covers a loopback share of any directory on C:, and a share on another machine cloned from the same image (the same AMI gives the same serial). Both were refused before this change, so nothing regresses. The CHANGELOG says so.
  • It is still advisory against a racing local attacker, as before.

Tests

  • TestMappedDriveError and TestIsPlainUNC (path_test.go, every OS): table tests with a fake resolver and a fake volume check. Admitted: a file on the share, the share root, a case-different spelling, a drive mapped to a sub-folder, a lower-case drive letter. Refused: links to another share, to a sibling share, or out of the mapped folder; a local link to a share; subst, volume-GUID, device and malformed roots; an unresolvable drive; a non-drive path; ADS, device name or trailing dot after the share; on a system volume; volume check error. One more check asserts that the volume check is asked about the resolved path, not the drive letter.
  • TestValidatePathForReading_WindowsMappedNetworkDrive (Windows, real fixture). It attaches a fresh 64 MB NTFS VHD with diskpart, so the share is not on the system volume. It shares a folder with New-SmbShare and maps a drive with net use to \\localhost\<share>. It first asserts that finalPathName really returns a \\server\share path, so the fixture has the Windows: path validators refuse paths on a mapped network drive #1107 shape. Admitted: a file, the root, a new file, and a glob on the drive. Refused: a junction inside the share into %SystemRoot%, both as the local path and through the drive. A second drive mapped to a loopback share of %SystemRoot% is refused too. Under GITHUB_ACTIONS an unbuildable fixture fails the test. Elsewhere (a non-elevated dev shell) it skips and names the step that needed elevation.
  • TestValidatePathForReading_WindowsSubstDrive (Windows, no elevation needed): a subst drive for a temp dir is admitted, and a subst drive for %SystemRoot% is refused. This is a guard, not a reproduction. subst does not reproduce Windows: path validators refuse paths on a mapped network drive #1107, because GetFinalPathNameByHandle names the local directory and not a UNC path. The test passes on the old code too. It is there to stop a future fix from "mapping the drive back", which would admit subst W: C:\Windows.

Verified

Locally (macOS):

  • go test -exec $(pwd)/bin/hermetic-exec ./pkg/security/: pass.
  • go vet ./pkg/security/ for linux, darwin and windows: clean. GOOS=windows go test -c -o /dev/null ./pkg/security/: compiles.
  • golangci-lint run ./pkg/security/ (native and GOOS=windows): 0 issues.
  • go test -run 'APISurface|PublicSurface|Changelog|Windows' .: pass. pkg/security is not in scripts/windows-test-skips.txt.
  • make check: everything passes except the known Docker-only TestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker (no Docker daemon here). Because that failure stopped make, the nested-module step did not run. This PR touches no nested module.
  • Each mappedDriveError rule was watched failing against a compiling mutation: always refuse; no plain-UNC check; no containment check; no tail shape check; if false && sys; volume asked about abs; volume error ignored; isPlainUNC accepting \\?\.

On Windows CI only (I could not run this locally):

  • The first push of this branch had the fix's call site reverted, in a form that compiles (the shape error returned unconditionally). Test (Windows) failed on exactly the four admitted rows of TestValidatePathForReading_WindowsMappedNetworkDrive, with "UNC and device-namespace paths ... are not allowed". The fixture built (diskpart, New-SmbShare and net use all succeeded), the refused rows passed, and the subst test passed. With the fix restored, Test (Windows) passes.
  • That run also showed that diskpart didn't detach a VHD it had attached through the runner's 8.3 temp path (RUNNER~1). The test now uses the long path, and a failed detach is reported as a test error.
  • The history was then squashed into one commit.

A path on a drive mapped to a network share resolves (GetFinalPathNameByHandle)
to \\server\share form, which the #1000 check refused as UNC. Resolve the
drive's root through the same resolution and admit the path only when it stays
on that share, its tail passes the Windows shape rules, and it is not on a
volume holding this machine's system directories (by volume serial), so a
loopback mapping such as \\localhost\C$ stays refused.

Fixes #1107
@scttfrdmn
scttfrdmn marked this pull request as ready for review October 18, 2026 05:56
@codecov

codecov Bot commented Oct 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.66667% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/security/path.go 92.85% 2 Missing ⚠️
pkg/security/path_resolve_other.go 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit f0f1f65 into main Oct 18, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/1107 branch October 18, 2026 06:11
@scttfrdmn scttfrdmn mentioned this pull request Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows: path validators refuse paths on a mapped network drive

1 participant