Strip leading slashes to prevent Beneath base-directory escape on non-Linux Unix#3
Open
evilgensec wants to merge 1 commit into
Open
Conversation
…se-directory escape
On non-Linux Unix builds (unix && !linux), a leading-slash argument to the
Beneath family (OpenBeneath/CreateBeneath/ReadFileBeneath/WriteFileBeneath)
escaped the base directory. unixRelativePathDoesntTraverse did not strip a
leading "/", so filepath.Clean("/../x") == "/x" -- the "../" prefix check then
passed, and openFileBeneath walked the raw path, opening ".." via unix.Openat
(O_NOFOLLOW does not stop ".."), climbing out of the base directory. This
allowed arbitrary file read and write outside the intended base, defeating the
"do not permit path traversal" / "must be somewhere underneath the base
directory" guarantee on macOS, the BSDs, illumos, Solaris and AIX. Linux was
unaffected because canTraverseUnixRelPath already strips the leading slash (and
uses openat2 RESOLVE_BENEATH); the At family is unaffected because it rejects
any "/".
Mirror the Linux behavior: strip leading slashes before the traversal check so
an absolute-looking input is treated as relative to the base directory. Add a
regression test (TestUnixBeneathLeadingSlashTraversal) covering read and write
for "/../x", "/./../x" and "/d/../../x", and an in-base "/ok.txt" that must
still succeed. The test fails before this change and passes after it.
Author
|
Checking in on this. Anything I can do to help get it reviewed? |
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.
Summary
On non-Linux Unix builds (
unix && !linux: macOS, the BSDs, illumos, Solaris, AIX), a leading-slash argument to theBeneathfamily (OpenBeneath,CreateBeneath,OpenFileBeneath,ReadFileBeneath,WriteFileBeneath) escapes the base directory, allowing arbitrary file read and write outside it — defeating the documented guarantee that these APIs "do not permit path traversal" and that the file "must be somewhere underneath the base directory".Root cause
unixRelativePathDoesntTraverse(safeopen_nix.go) does not strip a leading/before its traversal check.filepath.Clean("/../x")returns"/x", so thestrings.HasPrefix(path, "../")check passes and the path is accepted.openFileBeneaththen walks the raw path, skipping the empty leading segment and opening..viaunix.Openat(O_NOFOLLOWdoes not stop..), climbing out of the base directory.Linux is unaffected:
canTraverseUnixRelPath(safeopen_linux.go) already doespath = strings.TrimLeft(path, "/")and additionally usesopenat2RESOLVE_BENEATH. TheAtfamily is unaffected because it rejects any/.Fix
Mirror the Linux implementation: strip leading slashes in
unixRelativePathDoesntTraversebefore the traversal check, so an absolute-looking input is treated as relative to the base directory. A leading-slash input that nets a traversal (/../x) is then rejected, while a leading-slash input that stays in-base (/subdir/x) still succeeds.Test
Adds
TestUnixBeneathLeadingSlashTraversal, covering both read and write for"/../x","/./../x", and"/d/../../x", plus an in-base"/ok.txt"that must still succeed. The test fails before this change and passes after it; the existing suite is unchanged and still passes.