From c1e17ad93443a2adbe0eff7aa8c3241793bb540a Mon Sep 17 00:00:00 2001 From: James Khor Date: Mon, 23 Nov 2026 12:39:32 +0800 Subject: [PATCH] server: secure executor run-directory sharing --- server/execpriv_share_race_root_linux_test.go | 138 ++++++++++++++++++ server/execpriv_unix.go | 83 ++++++++--- 2 files changed, 204 insertions(+), 17 deletions(-) create mode 100644 server/execpriv_share_race_root_linux_test.go diff --git a/server/execpriv_share_race_root_linux_test.go b/server/execpriv_share_race_root_linux_test.go new file mode 100644 index 0000000..1a4deba --- /dev/null +++ b/server/execpriv_share_race_root_linux_test.go @@ -0,0 +1,138 @@ +//go:build linux + +package server + +import ( + "os" + "path/filepath" + "syscall" + "testing" + "time" + + "github.com/Autumn-27/artex/agent" +) + +// An executor can write in a shared run directory while a restore walks it. +// Replacing a regular entry with a symlink must never change a file outside it. +func TestShareWithExecGroupCannotChmodOutsideAsRoot(t *testing.T) { + if os.Geteuid() != 0 { + t.Skip("needs root") + } + base, err := os.MkdirTemp("", "artex-share-race-") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = os.RemoveAll(base) }) + if err := os.Chmod(base, 0o755); err != nil { + t.Fatal(err) + } + run := filepath.Join(base, "run") + if err := os.Mkdir(run, 0o775); err != nil { + t.Fatal(err) + } + if err := os.Chown(run, 0, DefaultExecGID); err != nil { + t.Fatal(err) + } + if err := os.Chmod(run, 0o775); err != nil { + t.Fatal(err) + } + secret := filepath.Join(base, "root-secret") + if err := os.WriteFile(secret, []byte("synthetic secret"), 0o600); err != nil { + t.Fatal(err) + } + if err := os.Chmod(secret, 0o600); err != nil { + t.Fatal(err) + } + agent.SetFileToolIdentity(&agent.FileToolIdentity{UID: DefaultExecUID, GID: DefaultExecGID}) + t.Cleanup(func() { agent.SetFileToolIdentity(nil) }) + var denied error + probe := filepath.Join(run, "write-probe") + if err := agent.RunAsFileToolIdentity(func() { denied = os.WriteFile(probe, []byte("x"), 0o600) }); err != nil { + t.Fatal(err) + } + if denied != nil { + t.Fatalf("executor cannot write in the shared run directory: %v", denied) + } + if err := os.Remove(probe); err != nil { + t.Fatal(err) + } + if err := agent.RunAsFileToolIdentity(func() { _, denied = os.ReadFile(secret) }); err != nil { + t.Fatal(err) + } + if denied == nil { + t.Fatal("executor identity can already read root-only file") + } + swap := filepath.Join(run, "swap") + stop := make(chan struct{}) + done := make(chan error, 1) + go func() { + done <- agent.RunAsFileToolIdentity(func() { + for { + select { + case <-stop: + return + default: + } + _ = os.Remove(swap) + _ = os.Symlink(secret, swap) + _ = os.Remove(swap) + _ = os.WriteFile(swap, []byte("ordinary"), 0o644) + } + }) + }() + defer func() { + close(stop) + if err := <-done; err != nil { + t.Error(err) + } + }() + + deadline := time.Now().Add(3 * time.Second) + for time.Now().Before(deadline) { + // Concurrent replacement may make a pass fail; it must never affect secret. + _ = shareWithExecGroup(run, DefaultExecGID) + fi, err := os.Stat(secret) + if err != nil { + t.Fatal(err) + } + if fi.Mode().Perm() != 0o600 { + t.Fatalf("executor swap changed root-only file mode to %04o", fi.Mode().Perm()) + } + } + if err := agent.RunAsFileToolIdentity(func() { _, denied = os.ReadFile(secret) }); err != nil { + t.Fatal(err) + } + if denied == nil { + t.Fatal("executor read root-only file after the walk") + } +} + +// A hard link is an alias for an inode outside the run directory. Sharing it +// would change that outside inode even though the walk never follows a symlink. +func TestShareWithExecGroupRejectsHardLinkAsRoot(t *testing.T) { + if os.Geteuid() != 0 { + t.Skip("needs root") + } + base := t.TempDir() + run := filepath.Join(base, "run") + if err := os.Mkdir(run, 0o755); err != nil { + t.Fatal(err) + } + secret := filepath.Join(base, "root-secret") + if err := os.WriteFile(secret, []byte("synthetic secret"), 0o600); err != nil { + t.Fatal(err) + } + if err := os.Link(secret, filepath.Join(run, "alias")); err != nil { + t.Fatal(err) + } + if err := shareWithExecGroup(run, DefaultExecGID); err == nil { + t.Fatal("shared an inode with a hard link outside the run directory") + } + fi, err := os.Stat(secret) + if err != nil { + t.Fatal(err) + } + if fi.Mode().Perm() != 0o600 || fi.Sys().(*syscall.Stat_t).Gid != 0 { + t.Fatalf("changed outside file: mode %04o, gid %d", fi.Mode().Perm(), fi.Sys().(*syscall.Stat_t).Gid) + } +} diff --git a/server/execpriv_unix.go b/server/execpriv_unix.go index f28bad9..df04053 100644 --- a/server/execpriv_unix.go +++ b/server/execpriv_unix.go @@ -15,6 +15,7 @@ import ( "syscall" "github.com/Autumn-27/artex/agent" + "golang.org/x/sys/unix" ) // Environment names the shims read. They carry no secret: the executor id, the real @@ -542,28 +543,76 @@ func shareRestoredTaskDir(p ExecPrivilege, taskDir string) error { // shareWithExecGroup gives the executor group the tree under root: the group owns // each entry and gets read/write (and traverse on directories), and directories set -// the setgid bit so anything created below them keeps the group. +// the setgid bit so anything created below them keeps the group. The executor can +// change entries in a run directory concurrently, so each entry is opened relative +// to its already-open parent without following a symlink. Ownership and mode changes +// use the descriptor: replacing a name after the open cannot redirect them to a +// root-only key outside the run directory. func shareWithExecGroup(root string, gid int) error { - return filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + f, err := os.OpenFile(root, os.O_RDONLY|unix.O_NONBLOCK|unix.O_NOFOLLOW, 0) + if errors.Is(err, syscall.ELOOP) { + // Older task directories can contain symlinks. Like WalkDir, leave the + // target alone; only the link itself receives the executor group. + return os.Lchown(root, -1, gid) + } + if err != nil { + return err + } + defer f.Close() + return shareOpenedExecEntry(f, gid) +} + +func shareOpenedExecEntry(f *os.File, gid int) error { + info, err := f.Stat() + if err != nil { + return err + } + if info.Mode().IsRegular() { + st, ok := info.Sys().(*syscall.Stat_t) + if !ok || st.Nlink > 1 { + return fmt.Errorf("refusing to share a file with another hard link") + } + } + if err := f.Chown(-1, gid); err != nil { + return err + } + mode := info.Mode().Perm() | 0o060 + if info.IsDir() { + mode = (info.Mode().Perm() | 0o070) | os.ModeSetgid + } + if err := f.Chmod(mode); err != nil { + return err + } + if !info.IsDir() { + return nil + } + entries, err := f.ReadDir(-1) + if err != nil { + return err + } + for _, entry := range entries { + name := entry.Name() // one component, relative to the open directory + fd, err := unix.Openat(int(f.Fd()), name, unix.O_RDONLY|unix.O_NONBLOCK|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0) + if errors.Is(err, syscall.ELOOP) { + if err := unix.Fchownat(int(f.Fd()), name, -1, gid, unix.AT_SYMLINK_NOFOLLOW); err != nil { + return fmt.Errorf("%s: %w", name, err) + } + continue + } if err != nil { - return err + return fmt.Errorf("%s: %w", name, err) } - if err := os.Lchown(path, -1, gid); err != nil { - return err + child := os.NewFile(uintptr(fd), name) + childErr := shareOpenedExecEntry(child, gid) + closeErr := child.Close() + if childErr != nil { + return fmt.Errorf("%s: %w", name, childErr) } - if d.Type()&fs.ModeSymlink != 0 { - return nil + if closeErr != nil { + return fmt.Errorf("%s: %w", name, closeErr) } - info, err := d.Info() - if err != nil { - return err - } - mode := info.Mode().Perm() | 0o060 - if d.IsDir() { - mode = (info.Mode().Perm() | 0o070) | os.ModeSetgid - } - return os.Chmod(path, mode) - }) + } + return nil } // InstallBashShim makes the Bash tool, the interactive shell and background tasks