Documentation
¶
Overview ¶
Package pathjail confines file operations to a directory tree.
It exists because the obvious guard does not work. Checking a path with Lstat and then handing the same string to MkdirAll, CreateTemp, Rename or Remove is two independent resolutions with a gap between them, and every operation after the check re-resolves the ANCESTORS as well. A store that verified only its own directory and file was therefore writable outside its tree whenever any component above them was a link: the check passed, and the syscall that followed went somewhere else.
On Windows the same guard failed for a second, independent reason. A junction is a reparse point but NOT a symlink, so os.Lstat reports it as ModeIrregular with ModeSymlink clear, and a guard testing only ModeSymlink returns "not a link" when pointed straight at one. Junctions also need no privilege to create, unlike Windows symlinks, so this is reachable by exactly the unprivileged user a workspace guard is meant to contain.
The fix for both is to stop naming things by path. Open a handle on the root once and address everything below it relative to that handle, so the kernel resolves each component against an object that cannot be swapped underneath. os.Root is that handle: it refuses any component that would escape, including a Windows reparse point, and it is the same idea the sandbox uses for ACL materialization.
Index ¶
- Variables
- func CreateTemp(handle *os.Root, dir, prefix, suffix string) (*os.File, string, error)
- func CreateTempMode(handle *os.Root, dir, prefix, suffix string, perm os.FileMode) (*os.File, string, error)
- func Open(root, dir string) (*os.Root, string, error)
- func RefuseReparse(handle *os.Root, relative string) error
- func RenameNoReplace(root *os.Root, oldname, newname string) error
Constants ¶
This section is empty.
Variables ¶
var ErrEscapes = errors.New("path escapes its root")
ErrEscapes reports a target that is not inside the root it was confined to. Returned before anything is opened, so a caller cannot act on the path first.
var ErrReparse = errors.New("refusing to write through a link")
ErrReparse reports a link or reparse point where a real file or directory was required.
Functions ¶
func CreateTemp ¶
CreateTemp makes an O_EXCL temp file named prefix.<random>suffix inside dir, relative to handle, and returns it with its relative path. The file is created 0o600 so bytes staged in it are not readable before publication.
O_EXCL rather than a fixed name: a link planted at a predictable temp path between the check and the create is the same race this package exists to close, and O_EXCL makes the kernel refuse it rather than us.
func CreateTempMode ¶ added in v0.14.0
func CreateTempMode(handle *os.Root, dir, prefix, suffix string, perm os.FileMode) (*os.File, string, error)
CreateTempMode is CreateTemp with an explicit create mode. The mode is passed to the kernel's exclusive create, so the process umask still applies; callers that must publish the permissions a normal create would have been granted should read the returned file's mode rather than assigning perm later, because Chmod does not consult the umask.
func Open ¶
Open confines dir to root and returns a handle on root plus dir expressed relative to it. The caller closes the handle.
root is resolved normally, because it is the boundary the caller chose and trusts (a workspace the operator opened, say, which may legitimately sit under a link). Everything BELOW it is resolved against the returned handle and cannot escape.
func RefuseReparse ¶
RefuseReparse fails when relative names a link or reparse point.
Absent is fine: a name that does not exist yet is what a create is for.
This is belt to os.Root's braces. The handle already refuses to traverse a reparse point, so this exists to give the FINAL component a clear error instead of an "escapes from parent" one, and to keep the refusal explicit at the point a reader looks for it.
ModeIrregular is tested alongside ModeSymlink deliberately: that is what a Windows junction reports, and a guard checking only ModeSymlink lets one straight through.
func RenameNoReplace ¶ added in v0.14.0
RenameNoReplace renames oldname to newname within the directory bound by root, and refuses to replace an existing newname with the same atomic guarantee the kernel gives a create's exclusive open. A caller that loses the race receives fs.ErrExist and nothing at newname changes.
Both names must be single path components: they are resolved relative to root's directory handle, so a separator (or an ancestor link) must not be able to redirect the syscall outside the checked root. Callers with a path in a subdirectory open that directory with Root.OpenRoot first.
Platforms whose kernel cannot publish without replacing fall back to ErrUnsupported, which lets the caller use a no-replace hard link instead.
Types ¶
This section is empty.