Skip to content

armgit / ARTEX

Public Imported from GitHub
0

ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)

fix(server): lock OS thread and set PR_SET_NO_NEW_PRIVS in shims before exec

Adrian Martinez Garciab6748f8ec08e
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 38cb4bf..ea549d9 100644
--- a/server/execpriv_unix.go
+++ b/server/execpriv_unix.go
@@ -270,11 +270,6 @@ 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)
@@ -684,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)
@@ -698,10 +696,11 @@ 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.
+// 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)
@@ -722,6 +721,7 @@ func enterExecutor(uid, gid int) error {
// runDropShim handles: <uid> <gid> -- <command> <args...>.
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 <uid> <gid> -- <command> [args]"))
}