feat: apply sealed installation plans without interactive setup
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package safeio
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// PublishCanonicalNewFile durably publishes complete bytes without replacing an existing
|
||||
// target. A crash before publication can leave a private temporary file, never a partial target.
|
||||
func PublishCanonicalNewFile(path string, contents []byte, mode os.FileMode) error {
|
||||
if ValidateCanonicalPath(path) != nil || ValidatePrivateDirectory(filepath.Dir(path)) != nil || (mode != 0o600 && mode != 0o644) {
|
||||
return ErrUnsafeFile
|
||||
}
|
||||
return publishCanonicalNewFile(path, contents, mode)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package safeio
|
||||
|
||||
import "golang.org/x/sys/unix"
|
||||
|
||||
func publishNoReplace(directory int, source, target string) error {
|
||||
return unix.RenameatxNp(directory, source, directory, target, unix.RENAME_EXCL)
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
//go:build linux || darwin
|
||||
|
||||
package safeio
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func TestPublicationWriteFailureLeavesNoTarget(t *testing.T) {
|
||||
if path := os.Getenv("THT_SAFEIO_WRITE_FAILURE_PATH"); path != "" {
|
||||
signal.Ignore(syscall.SIGXFSZ)
|
||||
if err := unix.Setrlimit(unix.RLIMIT_FSIZE, &unix.Rlimit{Cur: 1, Max: 1}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if PublishCanonicalNewFile(path, bytes.Repeat([]byte("x"), 4096), 0o600) == nil {
|
||||
t.Fatal("short write published")
|
||||
}
|
||||
if _, err := os.Lstat(path); !os.IsNotExist(err) {
|
||||
t.Fatal("failed write exposed target")
|
||||
}
|
||||
return
|
||||
}
|
||||
root, _ := filepath.EvalSymlinks(t.TempDir())
|
||||
if err := os.Chmod(root, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cmd := exec.Command(os.Args[0], "-test.run=^TestPublicationWriteFailureLeavesNoTarget$")
|
||||
cmd.Env = append(os.Environ(), "THT_SAFEIO_WRITE_FAILURE_PATH="+filepath.Join(root, "asset"))
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("write fault proof: %v %s", err, output)
|
||||
}
|
||||
entries, _ := os.ReadDir(root)
|
||||
if len(entries) != 0 {
|
||||
t.Fatal("failed write leaked temporary file")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package safeio
|
||||
|
||||
import "golang.org/x/sys/unix"
|
||||
|
||||
func publishNoReplace(directory int, source, target string) error {
|
||||
return unix.Renameat2(directory, source, directory, target, unix.RENAME_NOREPLACE)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
//go:build !linux && !darwin && !windows
|
||||
|
||||
package safeio
|
||||
|
||||
import "os"
|
||||
|
||||
func publishCanonicalNewFile(string, []byte, os.FileMode) error { return ErrUnsafeFile }
|
||||
@@ -0,0 +1,66 @@
|
||||
package safeio
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPublishNewFileExposesOnlyCompleteWinner(t *testing.T) {
|
||||
root, err := filepath.EvalSymlinks(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Chmod(root, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := filepath.Join(root, "asset")
|
||||
a, b := bytes.Repeat([]byte("a"), 2<<20), bytes.Repeat([]byte("b"), 2<<20)
|
||||
start := make(chan struct{})
|
||||
results := make(chan error, 2)
|
||||
var writers sync.WaitGroup
|
||||
for _, value := range [][]byte{a, b} {
|
||||
writers.Add(1)
|
||||
go func(value []byte) {
|
||||
defer writers.Done()
|
||||
<-start
|
||||
results <- PublishCanonicalNewFile(path, value, 0o600)
|
||||
}(value)
|
||||
}
|
||||
done := make(chan struct{})
|
||||
go func() { writers.Wait(); close(done) }()
|
||||
close(start)
|
||||
for {
|
||||
contents, err := os.ReadFile(path)
|
||||
if err == nil && !bytes.Equal(contents, a) && !bytes.Equal(contents, b) {
|
||||
t.Fatal("partial publication observed")
|
||||
}
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case <-done:
|
||||
goto finished
|
||||
default:
|
||||
}
|
||||
}
|
||||
finished:
|
||||
first, second := <-results, <-results
|
||||
if (first == nil) == (second == nil) {
|
||||
t.Fatal("exactly one writer must publish")
|
||||
}
|
||||
before, _ := os.ReadFile(path)
|
||||
if PublishCanonicalNewFile(path, []byte("replacement"), 0o600) == nil {
|
||||
t.Fatal("existing publication overwritten")
|
||||
}
|
||||
after, _ := os.ReadFile(path)
|
||||
if !bytes.Equal(before, after) {
|
||||
t.Fatal("failed publication changed target")
|
||||
}
|
||||
entries, _ := os.ReadDir(root)
|
||||
if len(entries) != 1 {
|
||||
t.Fatal("temporary files leaked")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
//go:build linux || darwin
|
||||
|
||||
package safeio
|
||||
|
||||
import (
|
||||
"golang.org/x/sys/unix"
|
||||
"os"
|
||||
)
|
||||
|
||||
func publishCanonicalNewFile(path string, contents []byte, mode os.FileMode) error {
|
||||
directory, target, err := openCanonicalParentDirectory(path)
|
||||
if err != nil {
|
||||
return ErrUnsafeFile
|
||||
}
|
||||
defer unix.Close(directory)
|
||||
temporary, err := writePrivateTemporaryAt(directory, contents)
|
||||
if err != nil {
|
||||
return ErrUnsafeFile
|
||||
}
|
||||
defer func() {
|
||||
if temporary != "" {
|
||||
_ = unix.Unlinkat(directory, temporary, 0)
|
||||
}
|
||||
}()
|
||||
fd, err := unix.Openat(directory, temporary, unix.O_RDONLY|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0)
|
||||
if err != nil {
|
||||
return ErrUnsafeFile
|
||||
}
|
||||
err = unix.Fchmod(fd, uint32(mode))
|
||||
if err == nil {
|
||||
err = unix.Fsync(fd)
|
||||
}
|
||||
_ = unix.Close(fd)
|
||||
if err != nil {
|
||||
return ErrUnsafeFile
|
||||
}
|
||||
if err := publishNoReplace(directory, temporary, target); err != nil {
|
||||
return ErrUnsafeFile
|
||||
}
|
||||
temporary = ""
|
||||
if unix.Fsync(directory) != nil {
|
||||
return ErrUnsafeFile
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package safeio
|
||||
|
||||
import (
|
||||
"golang.org/x/sys/windows"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func publishCanonicalNewFile(path string, contents []byte, mode os.FileMode) error {
|
||||
parents, target, err := openCanonicalWindowsParent(path)
|
||||
if err != nil {
|
||||
return ErrUnsafeFile
|
||||
}
|
||||
defer parents.Close()
|
||||
if len(parents.handles) == 0 || validateOwnerOnlyDACL(parents.handles[len(parents.handles)-1]) != nil {
|
||||
return ErrUnsafeFile
|
||||
}
|
||||
temporary, err := writePrivateTemporary(parents.directory, contents)
|
||||
if err != nil {
|
||||
return ErrUnsafeFile
|
||||
}
|
||||
defer os.Remove(temporary)
|
||||
from, err := windows.UTF16PtrFromString(temporary)
|
||||
if err != nil {
|
||||
return ErrUnsafeFile
|
||||
}
|
||||
to, err := windows.UTF16PtrFromString(filepath.Join(parents.directory, target))
|
||||
if err != nil {
|
||||
return ErrUnsafeFile
|
||||
}
|
||||
if windows.MoveFileEx(from, to, windows.MOVEFILE_WRITE_THROUGH) != nil {
|
||||
return ErrUnsafeFile
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -116,7 +116,8 @@ func writePrivateTemporaryAt(directory int, contents []byte) (string, error) {
|
||||
unix.Close(descriptor)
|
||||
return "", ErrUnsafeFile
|
||||
}
|
||||
if err := file.Chmod(0o600); err == nil {
|
||||
err = file.Chmod(0o600)
|
||||
if err == nil {
|
||||
var written int
|
||||
written, err = file.Write(contents)
|
||||
if err == nil && written != len(contents) {
|
||||
|
||||
Reference in New Issue
Block a user