Files
ARTEX/server/execpriv_shim_nnp_linux_test.go
T
Adrian Martinez Garcia b6748f8ec0 fix(server): lock OS thread and set PR_SET_NO_NEW_PRIVS in shims before exec
PR_SET_NO_NEW_PRIVS is per thread. PrepareExecPrivilege set it on a server thread that never execs, so the flag was not inherited by agent processes. The shims set it in enterExecutor, but runBashShim and runDropShim did not lock their goroutine, so it could migrate between prctl and exec.

Move the prctl out of PrepareExecPrivilege. Lock the thread at the top of runBashShim and runDropShim; the lock is never released because exec replaces the process.

Replace the test with a deterministic one: the drop shim runs as root with GOMAXPROCS=1, and a wrapped setNoNewPrivsFn forces the goroutine off its thread. Without the lock the exec'd program reports NoNewPrivs 0; with it, 1.
2026-11-23 22:28:50 +00:00

75 lines
2.5 KiB
Go

//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")
}