fix(server): fail-closed privilege drop and set PR_SET_NO_NEW_PRIVS on linux (#423)

This commit is contained in:
Adrian Martinez Garcia
2026-11-23 19:27:26 +00:00
parent d235be850e
commit 528e5230cd
4 changed files with 105 additions and 0 deletions
+26
View File
@@ -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
+51
View File
@@ -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)
}
}
+15
View File
@@ -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
+13
View File
@@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
@@ -269,6 +270,11 @@ func PrepareExecPrivilege(p *ExecPrivilege, dataDir, keyDir, configPath string)
if !p.Enabled {
return nil
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if err := setNoNewPrivileges(); err != nil {
return fmt.Errorf("set no_new_privs before the agent processes start: %w", err)
}
gid := int(p.GID)
if err := shareRunDirs(dataDir, gid); err != nil {
return fmt.Errorf("share the run directories with the executor group: %w", err)
@@ -692,7 +698,14 @@ 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. The flag is
// inherited by the real program 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