255 lines
8.3 KiB
Go
255 lines
8.3 KiB
Go
//go:build linux
|
|
|
|
package safeio
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"io/fs"
|
|
"os"
|
|
"strings"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// ReadCanonicalRegular opens an absolute canonical path component by component from the root
|
|
// descriptor. O_NOFOLLOW rejects symlinks at every component, and the open directory descriptors
|
|
// prevent later parent replacement from redirecting the final open.
|
|
func ReadCanonicalRegular(path string, maximum int64) ([]byte, error) {
|
|
return readCanonicalRegularWithHook(path, maximum, nil)
|
|
}
|
|
|
|
func readCanonicalRegularWithHook(path string, maximum int64, beforeRead func()) ([]byte, error) {
|
|
if err := ValidateCanonicalPath(path); err != nil {
|
|
return nil, err
|
|
}
|
|
components := strings.Split(strings.TrimPrefix(path, string(os.PathSeparator)), string(os.PathSeparator))
|
|
if len(components) == 0 || components[0] == "" {
|
|
return nil, ErrUnsafeFile
|
|
}
|
|
|
|
directory, err := unix.Open(string(os.PathSeparator), unix.O_RDONLY|unix.O_CLOEXEC|unix.O_DIRECTORY, 0)
|
|
if err != nil {
|
|
return nil, ErrUnsafeFile
|
|
}
|
|
directories := []int{directory}
|
|
defer func() { closeUnixDescriptors(directories) }()
|
|
|
|
for _, component := range components[:len(components)-1] {
|
|
nextDirectory, err := unix.Openat(directory, component, unix.O_RDONLY|unix.O_CLOEXEC|unix.O_DIRECTORY|unix.O_NOFOLLOW, 0)
|
|
if err != nil {
|
|
return nil, ErrUnsafeFile
|
|
}
|
|
directory = nextDirectory
|
|
directories = append(directories, directory)
|
|
}
|
|
|
|
descriptor, err := unix.Openat(directory, components[len(components)-1], unix.O_RDONLY|unix.O_CLOEXEC|unix.O_NOFOLLOW|unix.O_NONBLOCK, 0)
|
|
if err != nil {
|
|
return nil, ErrUnsafeFile
|
|
}
|
|
file := os.NewFile(uintptr(descriptor), "thothctl-safeio")
|
|
if file == nil {
|
|
unix.Close(descriptor)
|
|
return nil, ErrUnsafeFile
|
|
}
|
|
defer file.Close()
|
|
var before, after unix.Stat_t
|
|
if err := unix.Fstat(int(file.Fd()), &before); err != nil || before.Nlink > 1 || before.Mode&unix.S_IFMT != unix.S_IFREG {
|
|
return nil, ErrUnsafeFile
|
|
}
|
|
contents, err := readBoundedRegularFile(file, maximum, beforeRead)
|
|
if err != nil {
|
|
return nil, ErrUnsafeFile
|
|
}
|
|
// Checking the retained descriptor alone misses a pathname replacement while the
|
|
// read is in progress. The parent descriptor and final name must still resolve to
|
|
// the exact open file after reading.
|
|
if err := unix.Fstat(int(file.Fd()), &after); err != nil || after.Nlink > 1 || after.Mode != before.Mode || after.Ino != before.Ino || after.Dev != before.Dev || after.Size != before.Size {
|
|
return nil, ErrUnsafeFile
|
|
}
|
|
var named unix.Stat_t
|
|
if err := unix.Fstatat(directory, components[len(components)-1], &named, unix.AT_SYMLINK_NOFOLLOW); err != nil || named.Nlink > 1 || named.Mode != before.Mode || named.Ino != before.Ino || named.Dev != before.Dev {
|
|
return nil, ErrUnsafeFile
|
|
}
|
|
if !recheckUnixParents(components, directories) {
|
|
return nil, ErrUnsafeFile
|
|
}
|
|
return contents, nil
|
|
}
|
|
|
|
func closeUnixDescriptors(descriptors []int) {
|
|
for _, descriptor := range descriptors {
|
|
unix.Close(descriptor)
|
|
}
|
|
}
|
|
|
|
func writeCanonicalExclusive(path string, contents []byte, mode fs.FileMode) error {
|
|
if err := ValidateCanonicalPath(path); err != nil || len(contents) > 16<<20 || mode.Perm() != 0o600 {
|
|
return ErrUnsafeFile
|
|
}
|
|
components := strings.Split(strings.TrimPrefix(path, string(os.PathSeparator)), string(os.PathSeparator))
|
|
if len(components) == 0 || components[0] == "" {
|
|
return ErrUnsafeFile
|
|
}
|
|
dir, err := unix.Open("/", unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC, 0)
|
|
if err != nil {
|
|
return ErrUnsafeFile
|
|
}
|
|
defer func() { _ = unix.Close(dir) }()
|
|
for _, component := range components[:len(components)-1] {
|
|
next, openErr := unix.Openat(dir, component, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC|unix.O_NOFOLLOW, 0)
|
|
if openErr != nil {
|
|
return ErrUnsafeFile
|
|
}
|
|
_ = unix.Close(dir)
|
|
dir = next
|
|
}
|
|
// This is a capability publication API: the retained parent is the namespace
|
|
// anchor. The lexical check is only a pre-commit diagnostic and cannot close an
|
|
// ancestor rename race atomically.
|
|
if !recheckUnixParentPath(components[:len(components)-1], dir) {
|
|
return ErrUnsafeFile
|
|
}
|
|
fd, err := unix.Openat(dir, ".", unix.O_RDWR|unix.O_CLOEXEC|unix.O_TMPFILE, uint32(mode.Perm()))
|
|
if err != nil {
|
|
return ErrUnsafeFile
|
|
}
|
|
file := os.NewFile(uintptr(fd), "thothctl-safeio-anonymous")
|
|
if file == nil {
|
|
_ = unix.Close(fd)
|
|
return ErrUnsafeFile
|
|
}
|
|
closed := false
|
|
closeFile := func() error {
|
|
if closed {
|
|
return nil
|
|
}
|
|
closed = true
|
|
return file.Close()
|
|
}
|
|
fail := func() error {
|
|
_ = closeFile()
|
|
return ErrUnsafeFile
|
|
}
|
|
var expected unix.Stat_t
|
|
if err := unix.Fstat(fd, &expected); err != nil || expected.Nlink != 0 || expected.Mode&unix.S_IFMT != unix.S_IFREG {
|
|
return fail()
|
|
}
|
|
if err := file.Chmod(mode); err != nil {
|
|
return fail()
|
|
}
|
|
if n, err := file.Write(contents); err != nil || n != len(contents) {
|
|
return fail()
|
|
}
|
|
if err := file.Sync(); err != nil {
|
|
return fail()
|
|
}
|
|
var after unix.Stat_t
|
|
if err := unix.Fstat(fd, &after); err != nil || after.Nlink != 0 || after.Mode&unix.S_IFMT != unix.S_IFREG || after.Size != int64(len(contents)) {
|
|
return fail()
|
|
}
|
|
if !recheckUnixParentPath(components[:len(components)-1], dir) {
|
|
return fail()
|
|
}
|
|
// All fallible preparation, checks, and syncs happen before the linearization
|
|
// point. A directory sync here is best effort durability for the retained
|
|
// parent; it cannot be used to report failure after Linkat.
|
|
if err := unix.Fsync(dir); err != nil {
|
|
return fail()
|
|
}
|
|
if !recheckUnixParentPath(components[:len(components)-1], dir) {
|
|
return fail()
|
|
}
|
|
if err := unix.Linkat(fd, "", dir, components[len(components)-1], unix.AT_EMPTY_PATH); err != nil {
|
|
return fail()
|
|
}
|
|
// Linkat is the final commit. Close errors and any post-commit observations
|
|
// are deliberately ignored: returning ErrUnsafeFile here would lie about a
|
|
// candidate that is already committed, and pathname cleanup would be racy.
|
|
_ = closeFile()
|
|
return nil
|
|
}
|
|
func privateStageName() (string, error) {
|
|
var random [16]byte
|
|
if _, err := rand.Read(random[:]); err != nil {
|
|
return "", err
|
|
}
|
|
return ".thothctl-candidate-" + hex.EncodeToString(random[:]), nil
|
|
}
|
|
|
|
func validateCanonicalOutputPath(path string) error {
|
|
if err := ValidateCanonicalPath(path); err != nil {
|
|
return err
|
|
}
|
|
components := strings.Split(strings.TrimPrefix(path, string(os.PathSeparator)), string(os.PathSeparator))
|
|
if len(components) < 2 || components[0] == "" {
|
|
return ErrUnsafeFile
|
|
}
|
|
dir, err := unix.Open("/", unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC, 0)
|
|
if err != nil {
|
|
return ErrUnsafeFile
|
|
}
|
|
defer func() { unix.Close(dir) }()
|
|
for _, component := range components[:len(components)-1] {
|
|
next, err := unix.Openat(dir, component, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC|unix.O_NOFOLLOW, 0)
|
|
if err != nil {
|
|
return ErrUnsafeFile
|
|
}
|
|
unix.Close(dir)
|
|
dir = next
|
|
}
|
|
var st unix.Stat_t
|
|
if err := unix.Fstatat(dir, components[len(components)-1], &st, unix.AT_SYMLINK_NOFOLLOW); err == nil {
|
|
return ErrUnsafeFile
|
|
} else if err != unix.ENOENT {
|
|
return ErrUnsafeFile
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func recheckUnixParents(components []string, retained []int) bool {
|
|
if len(retained) != len(components) {
|
|
return false
|
|
}
|
|
dir, err := unix.Open("/", unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC, 0)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
defer func() { unix.Close(dir) }()
|
|
for i, component := range components[:len(components)-1] {
|
|
next, e := unix.Openat(dir, component, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC|unix.O_NOFOLLOW, 0)
|
|
if e != nil {
|
|
return false
|
|
}
|
|
var got, want unix.Stat_t
|
|
if unix.Fstat(next, &got) != nil || unix.Fstat(retained[i+1], &want) != nil || got.Ino != want.Ino || got.Dev != want.Dev {
|
|
unix.Close(next)
|
|
return false
|
|
}
|
|
unix.Close(dir)
|
|
dir = next
|
|
}
|
|
return true
|
|
}
|
|
|
|
func recheckUnixParentPath(components []string, retained int) bool {
|
|
dir, err := unix.Open("/", unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC, 0)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
defer func() { unix.Close(dir) }()
|
|
for _, component := range components {
|
|
next, e := unix.Openat(dir, component, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC|unix.O_NOFOLLOW, 0)
|
|
if e != nil {
|
|
return false
|
|
}
|
|
unix.Close(dir)
|
|
dir = next
|
|
}
|
|
var got, want unix.Stat_t
|
|
return unix.Fstat(dir, &got) == nil && unix.Fstat(retained, &want) == nil && got.Ino == want.Ino && got.Dev == want.Dev
|
|
}
|
|
|
|
func validatePlatformPathSyntax(path string) error { return nil }
|