mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-06 14:43:58 +08:00
467 lines
14 KiB
Go
467 lines
14 KiB
Go
//go:build linux
|
|
|
|
package server
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func permissionFixture(t *testing.T) (*execRootFixture, string) {
|
|
t.Helper()
|
|
f := newExecRootFixture(t)
|
|
file := filepath.Join(f.work, "nested", "private.txt")
|
|
if err := os.MkdirAll(filepath.Dir(file), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(file, []byte("fixture"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := shareRunDirs(f.data, DefaultExecUID, DefaultExecGID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertSharedPermissionFile(t, file, DefaultExecGID)
|
|
return f, file
|
|
}
|
|
|
|
func assertSharedPermissionFile(t *testing.T, file string, gid uint32) {
|
|
t.Helper()
|
|
fi, err := os.Stat(file)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if fi.Mode().Perm() != 0o660 || fi.Sys().(*syscall.Stat_t).Gid != gid {
|
|
t.Fatalf("%s: mode %v gid %d, want 0660 gid %d", file, fi.Mode(), fi.Sys().(*syscall.Stat_t).Gid, gid)
|
|
}
|
|
}
|
|
|
|
func TestPermissionMigrationWarmStartDoesNotOpenDescendantsAsRoot(t *testing.T) {
|
|
f, file := permissionFixture(t)
|
|
// Watch below the run directory: a repeated recursive repair would open/read
|
|
// this directory and chmod its contents, even if the modes already matched.
|
|
fd, err := syscall.InotifyInit1(syscall.IN_NONBLOCK | syscall.IN_CLOEXEC)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer syscall.Close(fd)
|
|
if _, err := syscall.InotifyAddWatch(fd, filepath.Dir(file), syscall.IN_OPEN|syscall.IN_ACCESS|syscall.IN_ATTRIB); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := shareRunDirs(f.data, DefaultExecUID, DefaultExecGID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var events [4096]byte
|
|
if n, err := syscall.Read(fd, events[:]); !errors.Is(err, syscall.EAGAIN) {
|
|
t.Fatalf("warm startup accessed descendants: %d event bytes, err=%v", n, err)
|
|
}
|
|
checkDir(t, filepath.Join(f.data, "tasks"), 0, parentDirMode)
|
|
checkDir(t, filepath.Dir(f.work), 0, parentDirMode)
|
|
checkDir(t, f.work, DefaultExecGID, leafDirMode)
|
|
}
|
|
|
|
// A completed migration is not an audit of subsequent nested writes. Operators
|
|
// can invalidate it explicitly to repair an external, in-place import.
|
|
func TestPermissionMigrationNestedAdditionsRequireInvalidationAsRoot(t *testing.T) {
|
|
f, file := permissionFixture(t)
|
|
late := filepath.Join(filepath.Dir(file), "imported", "private.txt")
|
|
if err := os.Mkdir(filepath.Dir(late), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(late, nil, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := shareRunDirs(f.data, DefaultExecUID, DefaultExecGID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for path, want := range map[string]os.FileMode{late: 0o600, filepath.Dir(late): 0o700} {
|
|
info, err := os.Stat(path)
|
|
if err != nil || info.Mode().Perm() != want {
|
|
t.Fatalf("warm start changed nested import %s: %v %v", path, info, err)
|
|
}
|
|
}
|
|
if err := os.RemoveAll(filepath.Join(f.data, execPermissionCacheDir)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := shareRunDirs(f.data, DefaultExecUID, DefaultExecGID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertSharedPermissionFile(t, late, DefaultExecGID)
|
|
}
|
|
|
|
func TestPermissionMigrationLegacyAndSessionDirectoriesAsRoot(t *testing.T) {
|
|
f := newExecRootFixture(t)
|
|
for _, dir := range []string{filepath.Join(f.data, "tasks", "1", "uploads"), filepath.Join(f.data, "sessions", "chat-1")} {
|
|
file := filepath.Join(dir, "nested", "old.txt")
|
|
if err := os.MkdirAll(filepath.Dir(file), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(file, nil, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := shareRunDirs(f.data, DefaultExecUID, DefaultExecGID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertSharedPermissionFile(t, file, DefaultExecGID)
|
|
before, err := os.Stat(file)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := shareRunDirs(f.data, DefaultExecUID, DefaultExecGID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
after, err := os.Stat(file)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if before.Sys().(*syscall.Stat_t).Ctim != after.Sys().(*syscall.Stat_t).Ctim {
|
|
t.Fatalf("warm startup rewrote nested legacy/session file %s", file)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPermissionMigrationRecordsAreProtectedAsRoot(t *testing.T) {
|
|
f, _ := permissionFixture(t)
|
|
dir := filepath.Join(f.data, execPermissionCacheDir)
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil || len(entries) != 1 {
|
|
t.Fatalf("records: %v, %v", entries, err)
|
|
}
|
|
c := exec.Command("sh", "-c", `test ! -r "$1" && test ! -w "$2"`, "probe", filepath.Join(dir, entries[0].Name()), dir)
|
|
applyExecCredential(c, f.priv)
|
|
if out, err := c.CombinedOutput(); err != nil {
|
|
t.Fatalf("executor can access migration state: %v %s", err, out)
|
|
}
|
|
}
|
|
|
|
func TestPermissionMigrationInvalidationAsRoot(t *testing.T) {
|
|
for _, reason := range []string{"group", "replacement", "new-entry", "root-mode", "missing-record", "corrupt-record", "record-symlink", "record-hardlink", "record-owner"} {
|
|
t.Run(reason, func(t *testing.T) {
|
|
f, file := permissionFixture(t)
|
|
if err := os.Chmod(file, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
gid := int(DefaultExecGID)
|
|
cacheDir := filepath.Join(f.data, execPermissionCacheDir)
|
|
records, err := os.ReadDir(cacheDir)
|
|
if err != nil || len(records) != 1 {
|
|
t.Fatalf("records: %v, %v", records, err)
|
|
}
|
|
record := filepath.Join(cacheDir, records[0].Name())
|
|
switch reason {
|
|
case "group":
|
|
gid++
|
|
case "replacement":
|
|
err = os.Rename(f.work, f.work+"-old")
|
|
if err == nil {
|
|
err = os.MkdirAll(filepath.Dir(file), 0o700)
|
|
}
|
|
if err == nil {
|
|
err = os.WriteFile(file, []byte("restored"), 0o600)
|
|
}
|
|
case "new-entry":
|
|
err = os.WriteFile(filepath.Join(f.work, "new"), nil, 0o600)
|
|
case "root-mode":
|
|
err = os.Chmod(f.work, 0o700)
|
|
case "missing-record":
|
|
err = os.Remove(record)
|
|
case "corrupt-record":
|
|
err = os.WriteFile(record, []byte("incomplete"), 0o600)
|
|
case "record-owner":
|
|
err = os.Chown(record, DefaultExecUID, DefaultExecGID)
|
|
case "record-symlink", "record-hardlink":
|
|
err = os.Rename(record, filepath.Join(f.data, "saved-record"))
|
|
if err == nil {
|
|
if reason == "record-symlink" {
|
|
err = os.Symlink(filepath.Join(f.data, "saved-record"), record)
|
|
} else {
|
|
err = os.Link(filepath.Join(f.data, "saved-record"), record)
|
|
}
|
|
}
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := shareRunDirs(f.data, DefaultExecUID, gid); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertSharedPermissionFile(t, file, uint32(gid))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPermissionMigrationFailureCannotLeaveCompletionAsRoot(t *testing.T) {
|
|
f, file := permissionFixture(t)
|
|
alias := filepath.Join(f.work, "hardlink")
|
|
if err := os.Link(f.secret, alias); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := shareRunDirs(f.data, DefaultExecUID, DefaultExecGID); err == nil {
|
|
t.Fatal("migration accepted a hard link")
|
|
}
|
|
entries, err := os.ReadDir(filepath.Join(f.data, execPermissionCacheDir))
|
|
if err != nil || len(entries) != 0 {
|
|
t.Fatalf("failed migration retained completion: %v, %v", entries, err)
|
|
}
|
|
if err := os.Remove(alias); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := shareRunDirs(f.data, DefaultExecUID, DefaultExecGID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertSharedPermissionFile(t, file, DefaultExecGID)
|
|
fi, err := os.Stat(f.secret)
|
|
if err != nil || fi.Mode().Perm() != 0o600 {
|
|
t.Fatalf("secret permissions changed: %v, %v", fi, err)
|
|
}
|
|
}
|
|
|
|
func TestPermissionMigrationRestoreBypassesCacheAsRoot(t *testing.T) {
|
|
f, file := permissionFixture(t)
|
|
if err := os.Chmod(file, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := shareRestoredTaskDir(f.priv, filepath.Dir(f.work)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertSharedPermissionFile(t, file, DefaultExecGID)
|
|
}
|
|
|
|
func TestPermissionMigrationChangedGroupCannotReuseOldRecordAsRoot(t *testing.T) {
|
|
f, file := permissionFixture(t)
|
|
if err := shareRunDirs(f.data, DefaultExecUID, DefaultExecGID+1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Recreating/reopening the run directory can restore only its top-level GID.
|
|
// An old completion for that group must not mask the still-different children.
|
|
if err := setDirOwner(f.work, DefaultExecGID, runDirMode); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := shareRunDirs(f.data, DefaultExecUID, DefaultExecGID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertSharedPermissionFile(t, file, DefaultExecGID)
|
|
}
|
|
|
|
func TestPermissionMigrationChangedUIDWithSameGroupAsRoot(t *testing.T) {
|
|
f, file := permissionFixture(t)
|
|
if err := os.Chown(file, DefaultExecUID, DefaultExecGID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(file, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
priv := f.priv
|
|
priv.UID++
|
|
if err := PrepareExecPrivilege(&priv, f.data, "", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertSharedPermissionFile(t, file, DefaultExecGID)
|
|
c := exec.Command("cat", file)
|
|
applyExecCredential(c, priv)
|
|
if out, err := c.CombinedOutput(); err != nil || string(out) != "fixture" {
|
|
t.Fatalf("new executor cannot read old executor's file: %v %s", err, out)
|
|
}
|
|
}
|
|
|
|
func TestPermissionMigrationDirectoryChangesDuringWalkAsRoot(t *testing.T) {
|
|
for _, change := range []string{"new-entry", "replacement"} {
|
|
t.Run(change, func(t *testing.T) {
|
|
f, _ := permissionFixture(t)
|
|
if err := os.Chmod(f.work, 0o700); err != nil { // Force a migration.
|
|
t.Fatal(err)
|
|
}
|
|
cache, err := openExecPermissionCache(f.data)
|
|
if err != nil || cache == nil {
|
|
t.Fatalf("cache: %v %v", cache, err)
|
|
}
|
|
defer cache.root.Close()
|
|
late := filepath.Join(f.work, "late.txt")
|
|
err = cache.share(f.work, DefaultExecGID, func() error {
|
|
if err := shareRunDir(f.work, DefaultExecGID); err != nil {
|
|
return err
|
|
}
|
|
// Model a concurrent writer arriving after the walk's directory
|
|
// snapshot. The resulting tree has not been fully migrated.
|
|
if change == "replacement" {
|
|
if err := os.Rename(f.work, f.work+"-old"); err != nil {
|
|
return err
|
|
}
|
|
if err := os.Mkdir(f.work, 0o700); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return os.WriteFile(late, nil, 0o600)
|
|
})
|
|
if (change == "replacement") != (err != nil) {
|
|
t.Fatalf("migration after %s: %v", change, err)
|
|
}
|
|
if err := cache.share(f.work, DefaultExecGID, func() error {
|
|
return shareRunDir(f.work, DefaultExecGID)
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertSharedPermissionFile(t, late, DefaultExecGID)
|
|
})
|
|
}
|
|
}
|
|
|
|
func duringPermissionDirectoryWrites(t *testing.T, nested string, migrate func() error) {
|
|
t.Helper()
|
|
for i := range 2000 {
|
|
if err := os.WriteFile(filepath.Join(nested, fmt.Sprint(i)), nil, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
stop, started := make(chan struct{}), make(chan struct{})
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
for n := int64(1); ; n++ {
|
|
// Change nested-directory metadata without removing entries the walk
|
|
// opens. This isolates incomplete snapshots from ordinary ENOENT races.
|
|
stamp := time.Unix(1800000000, n)
|
|
err := os.Chtimes(nested, stamp, stamp)
|
|
if n == 1 {
|
|
close(started)
|
|
}
|
|
if err != nil {
|
|
done <- err
|
|
return
|
|
}
|
|
select {
|
|
case <-stop:
|
|
done <- nil
|
|
return
|
|
default:
|
|
}
|
|
}
|
|
}()
|
|
<-started
|
|
err := migrate()
|
|
close(stop)
|
|
if writerErr := <-done; writerErr != nil {
|
|
t.Fatal(writerErr)
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestPermissionMigrationNestedChangesDoNotCreateCompletionAsRoot(t *testing.T) {
|
|
f, file := permissionFixture(t)
|
|
if err := os.Chmod(f.work, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cache, err := openExecPermissionCache(f.data)
|
|
if err != nil || cache == nil {
|
|
t.Fatalf("cache: %v %v", cache, err)
|
|
}
|
|
defer cache.root.Close()
|
|
duringPermissionDirectoryWrites(t, filepath.Dir(file), func() error {
|
|
return cache.share(f.work, DefaultExecGID, func() error { return shareRunDir(f.work, DefaultExecGID) })
|
|
})
|
|
entries, err := os.ReadDir(filepath.Join(f.data, execPermissionCacheDir))
|
|
if err != nil || len(entries) != 0 {
|
|
t.Fatalf("unstable nested tree received a completion record: %v %v", entries, err)
|
|
}
|
|
if err := cache.share(f.work, DefaultExecGID, func() error { return shareRunDir(f.work, DefaultExecGID) }); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertSharedPermissionFile(t, file, DefaultExecGID)
|
|
}
|
|
|
|
func TestPermissionMigrationUncachedConcurrentChangesAsRoot(t *testing.T) {
|
|
for _, mode := range []string{"startup", "restore"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
f, file := permissionFixture(t)
|
|
if err := os.Chmod(f.data, 0o777); err != nil { // Cache unavailable.
|
|
t.Fatal(err)
|
|
}
|
|
duringPermissionDirectoryWrites(t, filepath.Dir(file), func() error {
|
|
if mode == "restore" {
|
|
return shareRestoredTaskDir(f.priv, filepath.Dir(f.work))
|
|
}
|
|
return shareRunDirs(f.data, DefaultExecUID, DefaultExecGID)
|
|
})
|
|
assertSharedPermissionFile(t, file, DefaultExecGID)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPermissionMigrationCacheMustBeProtectedAsRoot(t *testing.T) {
|
|
for _, kind := range []string{"symlink", "writable-cache", "executor-owned-cache", "writable-data"} {
|
|
t.Run(kind, func(t *testing.T) {
|
|
f, file := permissionFixture(t)
|
|
cache := filepath.Join(f.data, execPermissionCacheDir)
|
|
var err error
|
|
switch kind {
|
|
case "symlink":
|
|
err = os.Rename(cache, cache+"-old")
|
|
if err == nil {
|
|
err = os.Symlink(cache+"-old", cache)
|
|
}
|
|
case "writable-cache":
|
|
err = os.Chmod(cache, 0o777)
|
|
case "executor-owned-cache":
|
|
err = os.Chown(cache, DefaultExecUID, DefaultExecGID)
|
|
case "writable-data":
|
|
err = os.Chmod(f.data, 0o777)
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(file, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = shareRunDirs(f.data, DefaultExecUID, DefaultExecGID)
|
|
if kind == "writable-data" {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertSharedPermissionFile(t, file, DefaultExecGID)
|
|
} else if err == nil {
|
|
t.Fatal("accepted unprotected migration cache")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func BenchmarkRunPermissionStartup(b *testing.B) {
|
|
if os.Geteuid() != 0 {
|
|
b.Skip("needs root")
|
|
}
|
|
data := b.TempDir()
|
|
task := filepath.Join(data, "tasks", "1")
|
|
dir := filepath.Join(task, "main", "nested")
|
|
if err := os.MkdirAll(dir, 0o700); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
for i := range 10000 {
|
|
if err := os.WriteFile(filepath.Join(dir, fmt.Sprintf("file-%d", i)), nil, 0o600); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
if err := shareRunDirs(data, DefaultExecUID, DefaultExecGID); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
b.Run("full-walk-10000-files", func(b *testing.B) {
|
|
for b.Loop() {
|
|
if err := shareTaskDir(task, DefaultExecGID); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
})
|
|
b.Run("warm-start-10000-files", func(b *testing.B) {
|
|
for b.Loop() {
|
|
if err := shareRunDirs(data, DefaultExecUID, DefaultExecGID); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
})
|
|
}
|