ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)
Go aiai-agentsautonomous-agentsbug-bountydockergolanglinuxllmmcpnextjspenetration-testingpentestpostgresqlreareactred-teamsecuritysecurity-automationsqlitetypescript
security(server): refuse to start when the executor can write the Norma temp directory
BaaRai992fcf703053
diff --git a/server/execpriv_root_linux_test.go b/server/execpriv_root_linux_test.goindex 882de6a..e5f2054 100644--- a/server/execpriv_root_linux_test.go+++ b/server/execpriv_root_linux_test.go@@ -3,9 +3,11 @@package serverimport (+ "bytes""context""encoding/json""fmt"+ "log""os""os/exec""path/filepath"@@ -249,8 +251,9 @@ func TestPrepareNormaTaskDir(t *testing.T) {t.Cleanup(func() { syscall.Umask(old) })base := t.TempDir()dir := filepath.Join(base, "norma")+ priv := ExecPrivilege{Enabled: true, UID: DefaultExecUID, GID: DefaultExecGID}for _, state := range []string{"absent", "already there"} {- if err := prepareNormaTaskDir(dir); err != nil {+ if err := prepareNormaTaskDir(dir, priv); err != nil {t.Fatalf("%s: %v", state, err)}if fi, err := os.Lstat(dir); err != nil || !fi.IsDir() || fi.Mode().Perm() != 0o755 {@@ -262,7 +265,7 @@ func TestPrepareNormaTaskDir(t *testing.T) {if err := os.Symlink(outside, link); err != nil {t.Fatal(err)}- if err := prepareNormaTaskDir(link); err == nil {+ if err := prepareNormaTaskDir(link, priv); err == nil {t.Fatal("a symlink to a directory was accepted")}if entries, err := os.ReadDir(outside); err != nil || len(entries) != 0 {@@ -272,11 +275,104 @@ func TestPrepareNormaTaskDir(t *testing.T) {if err := os.WriteFile(file, nil, 0o644); err != nil {t.Fatal(err)}- if err := prepareNormaTaskDir(file); err == nil {+ if err := prepareNormaTaskDir(file, priv); err == nil {t.Fatal("a file was accepted")}}+// The executor can write in a directory it owns, in one its group can write in and+// in one others can write in. No other directory counts. Runs without root.+func TestNormaTaskDirExecWrite(t *testing.T) {+ const execUID, execGID = 10001, 10002+ for _, tc := range []struct {+ name string+ perm os.FileMode+ uid, gid uint32+ writable bool+ }{+ {"owned by the executor uid", 0o755, execUID, 0, true},+ {"owned by the executor uid, no write bit", 0o555, execUID, 0, true},+ {"group-writable for the executor gid", 0o775, 0, execGID, true},+ {"other-writable", 0o757, 0, 0, true},+ {"root-owned 0755", 0o755, 0, 0, false},+ {"another user, not writable by the executor", 0o755, 1000, 1000, false},+ {"group-writable for another gid", 0o775, 1000, 1000, false},+ {"the executor gid without the group write bit", 0o755, 1000, execGID, false},+ } {+ why := normaTaskDirExecWrite(tc.perm, tc.uid, tc.gid, execUID, execGID)+ if (why != "") != tc.writable {+ t.Errorf("%s (mode %v, uid %d, gid %d): reason %q, want writable=%v", tc.name, tc.perm, tc.uid, tc.gid, why, tc.writable)+ }+ }+}++// prepareNormaTaskDir applies that decision to a directory that is already there:+// one the executor can write in fails the start with its path in the error, and one+// of another user that the executor cannot write in is used with a warning. Either+// way the directory keeps its owner and mode. Runs with and without root: the+// executor ids are chosen around the ids of the directory.+func TestPrepareNormaTaskDirAlreadyThere(t *testing.T) {+ for _, tc := range []struct {+ name string+ perm os.FileMode+ sameUID, sameGID bool // the executor id is the directory's+ refused bool+ }{+ {"owned by the executor uid", 0o755, true, false, true},+ {"group-writable for the executor gid", 0o775, false, true, true},+ {"other-writable", 0o757, false, false, true},+ {"another user, not writable by the executor", 0o755, false, false, false},+ } {+ t.Run(tc.name, func(t *testing.T) {+ dir := filepath.Join(t.TempDir(), "norma")+ if err := os.Mkdir(dir, 0o700); err != nil {+ t.Fatal(err)+ }+ if os.Geteuid() == 0 { // not root's: without root it is the test user's+ if err := os.Lchown(dir, 20001, 20002); err != nil {+ t.Fatal(err)+ }+ }+ if err := os.Chmod(dir, tc.perm); err != nil {+ t.Fatal(err)+ }+ before, err := os.Lstat(dir)+ if err != nil {+ t.Fatal(err)+ }+ st := before.Sys().(*syscall.Stat_t)+ priv := ExecPrivilege{Enabled: true, UID: st.Uid + 1, GID: st.Gid + 1}+ if tc.sameUID {+ priv.UID = st.Uid+ }+ if tc.sameGID {+ priv.GID = st.Gid+ }+ var logged bytes.Buffer+ prev := log.Writer()+ log.SetOutput(&logged)+ err = prepareNormaTaskDir(dir, priv)+ log.SetOutput(prev)+ if tc.refused && (err == nil || !strings.Contains(err.Error(), dir)) {+ t.Fatalf("err = %v, want a refusal that names %s", err, dir)+ }+ if !tc.refused && err != nil {+ t.Fatalf("a directory the executor cannot write in was refused: %v", err)+ }+ if warned := strings.Contains(logged.String(), dir); warned == tc.refused {+ t.Fatalf("warning logged = %v, want %v:\n%s", warned, !tc.refused, logged.String())+ }+ after, err := os.Lstat(dir)+ if err != nil {+ t.Fatalf("the directory is gone: %v", err)+ }+ if at := after.Sys().(*syscall.Stat_t); at.Uid != st.Uid || at.Gid != st.Gid || after.Mode() != before.Mode() {+ t.Fatalf("the directory changed: uid %d gid %d mode %v, was uid %d gid %d mode %v", at.Uid, at.Gid, after.Mode(), st.Uid, st.Gid, before.Mode())+ }+ })+ }+}+// With the drop enabled, PrepareExecPrivilege creates <temp>/norma before any agent// process runs: root:root 0755 in a world-writable sticky temp directory, where the// executor can neither add an entry nor rename it.diff --git a/server/execpriv_unix.go b/server/execpriv_unix.goindex b3790db..753b1d3 100644--- a/server/execpriv_unix.go+++ b/server/execpriv_unix.go@@ -132,9 +132,12 @@ func prepareExecScriptDir(p ExecPrivilege, dataDir string) (string, error) {// before that first session could create dir itself and place symlinks in it for// the server to write through. Created here, before any agent process starts, it is// root:root 0755. A symlink or a file in its place is refused. A directory that is-// already there is used as found, and reported when root does not own it or others-// can write in it: it may belong to another user of this host.-func prepareNormaTaskDir(dir string) error {+// already there and that the executor p can write in is refused too+// (normaTaskDirExecWrite): the executor could have placed the symlinks before this+// start, and can place them at any time. Any other directory already there is used+// as found, and reported when root does not own it or others can write in it: it+// may belong to another user of this host. It is never chowned or removed.+func prepareNormaTaskDir(dir string, p ExecPrivilege) error {if err := os.Mkdir(dir, 0o755); err != nil && !errors.Is(err, fs.ErrExist) {return err}@@ -145,12 +148,35 @@ func prepareNormaTaskDir(dir string) error {if !fi.IsDir() {return fmt.Errorf("%s is not a directory", dir)}- if st, ok := fi.Sys().(*syscall.Stat_t); ok && (st.Uid != 0 || fi.Mode().Perm()&0o022 != 0) {- log.Printf("[exec] %s (uid %d, mode %v) is not a root-owned directory only root can write in: background-task output written there is not protected from its owner", dir, st.Uid, fi.Mode().Perm())+ st, ok := fi.Sys().(*syscall.Stat_t)+ if !ok {+ return nil+ }+ perm := fi.Mode().Perm()+ if why := normaTaskDirExecWrite(perm, st.Uid, st.Gid, p.UID, p.GID); why != "" {+ return fmt.Errorf("%s (uid %d, gid %d, mode %v) can be written by the executor: %s. Background-task output written there by the server would follow a symlink the executor places; remove the directory or make it root-owned with mode 0755, then start again", dir, st.Uid, st.Gid, perm, why)+ }+ if st.Uid != 0 || perm&0o022 != 0 {+ log.Printf("[exec] %s (uid %d, mode %v) is not a root-owned directory only root can write in: background-task output written there is not protected from its owner", dir, st.Uid, perm)}return nil}+// normaTaskDirExecWrite reports why the executor (execUID, execGID) can write in a+// directory with the permission bits perm, the owner uid and the group gid, or ""+// when it cannot. The owner counts whatever the bits say: it can change them.+func normaTaskDirExecWrite(perm fs.FileMode, uid, gid, execUID, execGID uint32) string {+ switch {+ case uid == execUID:+ return fmt.Sprintf("it is owned by the executor uid %d", execUID)+ case gid == execGID && perm&0o020 != 0:+ return fmt.Sprintf("its group is the executor gid %d and it is group-writable", execGID)+ case perm&0o002 != 0:+ return "it is writable by others"+ }+ return ""+}+// prepareExecScript lets the executor read a script the server wrote: the file stays// root-owned and becomes readable by the executor group only.func prepareExecScript(p ExecPrivilege, path string) error {@@ -177,7 +203,8 @@ func prepareExecScript(p ExecPrivilege, path string) error {// else under the data directory changes; directories a// root-only path needs are left as they are. It sets p.ScriptDir to the script// directory it creates, and creates the directory of Norma's background-task output-// under the temp directory (prepareNormaTaskDir).+// under the temp directory (prepareNormaTaskDir); it fails when that directory is+// already there and the executor can write in it.func PrepareExecPrivilege(p *ExecPrivilege, dataDir, keyDir, configPath string) error {if !p.Enabled {return nil@@ -199,7 +226,7 @@ func PrepareExecPrivilege(p *ExecPrivilege, dataDir, keyDir, configPath string)return fmt.Errorf("prepare the executor script directory: %w", err)}p.ScriptDir = scriptDir- if err := prepareNormaTaskDir(filepath.Join(os.TempDir(), "norma")); err != nil {+ if err := prepareNormaTaskDir(filepath.Join(os.TempDir(), "norma"), *p); err != nil {return fmt.Errorf("prepare the background-task output directory: %w", err)}tasks := filepath.Join(dataDir, "tasks")