diff --git a/server/execpriv_linux.go b/server/execpriv_linux.go new file mode 100644 index 0000000..3a83868 --- /dev/null +++ b/server/execpriv_linux.go @@ -0,0 +1,26 @@ +//go:build linux + +package server + +import ( + "fmt" + "syscall" +) + +// prSetNoNewPrivs is PR_SET_NO_NEW_PRIVS from linux/prctl.h. With the flag set, execve +// cannot grant privileges (setuid bits, file capabilities), so a process that dropped +// to the executor identity cannot regain root through a setuid binary. +const prSetNoNewPrivs = 38 + +// setNoNewPrivileges sets PR_SET_NO_NEW_PRIVS on the calling thread. The flag is per +// thread and is inherited by what that thread forks or execs. +func setNoNewPrivileges() error { + if _, _, errno := syscall.RawSyscall6(syscall.SYS_PRCTL, prSetNoNewPrivs, 1, 0, 0, 0, 0); errno != 0 { + return fmt.Errorf("no_new_privs: prctl(PR_SET_NO_NEW_PRIVS): %w", errno) + } + return nil +} + +// setNoNewPrivsFn is the step the executor shims run before they drop identity. A +// variable so a test can make it fail. +var setNoNewPrivsFn = setNoNewPrivileges diff --git a/server/execpriv_nnp_linux_test.go b/server/execpriv_nnp_linux_test.go new file mode 100644 index 0000000..d2a2f90 --- /dev/null +++ b/server/execpriv_nnp_linux_test.go @@ -0,0 +1,51 @@ +//go:build linux + +package server + +import ( + "errors" + "os" + "runtime" + "strings" + "testing" +) + +// noNewPrivsOfThread reads the NoNewPrivs flag of the calling thread from /proc. +func noNewPrivsOfThread(t *testing.T) string { + t.Helper() + b, err := os.ReadFile("/proc/thread-self/status") + if err != nil { + t.Fatal(err) + } + for _, line := range strings.Split(string(b), "\n") { + if v, ok := strings.CutPrefix(line, "NoNewPrivs:"); ok { + return strings.TrimSpace(v) + } + } + t.Fatal("no NoNewPrivs line in /proc/thread-self/status") + return "" +} + +// PR_SET_NO_NEW_PRIVS is per thread: the check reads the thread that set it. +func TestSetNoNewPrivilegesSetsTheFlag(t *testing.T) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + if err := setNoNewPrivileges(); err != nil { + t.Fatal(err) + } + if got := noNewPrivsOfThread(t); got != "1" { + t.Fatalf("NoNewPrivs = %s, want 1", got) + } +} + +// The executor shims fail closed: no identity change and no exec when the flag cannot +// be set. The error names the step so the operator can see why the start stopped. +func TestEnterExecutorFailsClosedWithoutNoNewPrivs(t *testing.T) { + old := setNoNewPrivsFn + setNoNewPrivsFn = func() error { return errors.New("prctl refused") } + t.Cleanup(func() { setNoNewPrivsFn = old }) + err := enterExecutor(DefaultExecUID, DefaultExecGID) + if err == nil || !strings.Contains(err.Error(), "no_new_privs") { + t.Fatalf("enterExecutor = %v, want a no_new_privs failure", err) + } +} diff --git a/server/execpriv_nonlinux.go b/server/execpriv_nonlinux.go new file mode 100644 index 0000000..c515358 --- /dev/null +++ b/server/execpriv_nonlinux.go @@ -0,0 +1,15 @@ +//go:build unix && !linux + +package server + +import "errors" + +// PR_SET_NO_NEW_PRIVS is Linux only. The privilege drop is never enabled elsewhere +// (execPrivilegeSupported is false), so this only keeps the unix code compiling and +// fails closed if reached. + +var errNoNewPrivsUnsupported = errors.New("no_new_privs: PR_SET_NO_NEW_PRIVS is not supported on this platform") + +func setNoNewPrivileges() error { return errNoNewPrivsUnsupported } + +var setNoNewPrivsFn = setNoNewPrivileges diff --git a/server/execpriv_shim_nnp_linux_test.go b/server/execpriv_shim_nnp_linux_test.go new file mode 100644 index 0000000..2524aae --- /dev/null +++ b/server/execpriv_shim_nnp_linux_test.go @@ -0,0 +1,74 @@ +//go:build linux + +package server + +import ( + "os" + "os/exec" + "runtime" + "strconv" + "strings" + "testing" +) + +const nnpShimHelperEnv = "ARTEX_NNP_SHIM_HELPER" + +// PR_SET_NO_NEW_PRIVS is per thread: the thread that sets it must be the one that +// execs. This runs the real drop shim as root with GOMAXPROCS=1 and wraps +// setNoNewPrivsFn so that right after the prctl on thread M1 a helper goroutine +// steals and locks M1. If runDropShim did not call runtime.LockOSThread(), the shim +// resumes on a new thread M2 where NoNewPrivs is 0 and the exec'd program reports 0; +// with the lock it stays on M1 and reports 1. A plain "run the shim and read the +// flag" test passes without the lock, so it does not guard this regression. +func TestExecDropShimInheritsNoNewPrivs(t *testing.T) { + if os.Geteuid() != 0 { + t.Skip("needs root: the shim drops to the executor identity before it execs") + } + cmd := exec.Command(os.Args[0], "-test.run=^TestExecDropShimNoNewPrivsHelper$") + cmd.Env = append(os.Environ(), nnpShimHelperEnv+"=1", "GOMAXPROCS=1") + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("shim helper failed: %v\n%s", err, out) + } + for _, line := range strings.Split(string(out), "\n") { + if v, ok := strings.CutPrefix(line, "NoNewPrivs:"); ok { + if got := strings.TrimSpace(v); got != "1" { + t.Fatalf("exec'd program NoNewPrivs = %q, want 1 (thread migrated after prctl)\n%s", got, out) + } + return + } + } + t.Fatalf("no NoNewPrivs line from the exec'd program\n%s", out) +} + +// TestExecDropShimNoNewPrivsHelper is the child side: it wraps the flag setter and +// runs the drop shim. On success the shim execs /bin/sh, which prints its own status. +func TestExecDropShimNoNewPrivsHelper(t *testing.T) { + if os.Getenv(nnpShimHelperEnv) != "1" { + t.Skip("helper process only") + } + orig := setNoNewPrivsFn + setNoNewPrivsFn = func() error { + if err := orig(); err != nil { + return err + } + // With GOMAXPROCS=1, blocking on <-locked lets this goroutine run on M1 + // (when the shim is not locked) and lock it, forcing the shim onto M2. + // When the shim is locked to M1, the helper takes M2 and the shim resumes + // on M1 with the flag set. + locked := make(chan struct{}) + hold := make(chan struct{}) + go func() { + runtime.LockOSThread() + close(locked) + <-hold + }() + <-locked + return nil + } + RunExecShimIfRequested([]string{ + "artex", execDropArg, strconv.Itoa(DefaultExecUID), strconv.Itoa(DefaultExecGID), "--", + "/bin/sh", "-c", "grep NoNewPrivs /proc/self/status", + }) + t.Fatal("the drop shim returned instead of exec-ing the target") +} diff --git a/server/execpriv_unix.go b/server/execpriv_unix.go index df04053..ea549d9 100644 --- a/server/execpriv_unix.go +++ b/server/execpriv_unix.go @@ -10,6 +10,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strconv" "strings" "syscall" @@ -678,6 +679,9 @@ func RunExecShimIfRequested(argv []string) { } func runBashShim(argv []string) { + // PR_SET_NO_NEW_PRIVS is per thread, so the thread that sets it must be the one + // that execs. The lock is never released: the process is replaced by exec. + runtime.LockOSThread() uid, gid, err := execIDsFromEnv() if err != nil { shimFail(err) @@ -692,7 +696,15 @@ func runBashShim(argv []string) { // nested shell already running as the executor, whose environment the agent may // have set), the environment is first rebuilt from the allowlist (execenv_unix.go) // and HOME and XDG_CACHE_HOME move to the executor's home. +// PR_SET_NO_NEW_PRIVS is set first, before the identity changes, on the calling +// thread. The shims call this with their thread locked (runBashShim, runDropShim), +// so the same thread goes on to exec the target and the flag is inherited by it and +// everything it forks. Custom tools take SysProcAttr.Credential in the server and do +// not pass through this shim, so the flag is a known gap there. func enterExecutor(uid, gid int) error { + if err := setNoNewPrivsFn(); err != nil { + return fmt.Errorf("no_new_privs must be set before the executor identity is applied: %w", err) + } if os.Getuid() != uid || os.Getgid() != gid { if err := sealExecutorEnv(); err != nil { return err @@ -709,6 +721,7 @@ func enterExecutor(uid, gid int) error { // runDropShim handles: -- . func runDropShim(args []string) { + runtime.LockOSThread() // same reason as runBashShim: the flag is per thread if len(args) < 3 || args[2] != "--" { shimFail(errors.New("drop shim needs -- [args]")) }