server: secure executor run-directory sharing

This commit is contained in:
James Khor
2026-11-23 12:39:32 +08:00
parent 303acd7587
commit c1e17ad934
2 changed files with 204 additions and 17 deletions
@@ -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)
}
}
+66 -17
View File
@@ -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