mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-26 01:41:04 +08:00
test(server): cover the Norma temp directory rules in a private temp directory
TestCheckNormaTaskDir runs every rule of checkNormaTaskDir on stat facts alone, without root and without a file system. On a real directory, with and without root: - TestPrepareNormaTaskDirAlreadyThere: a directory the executor uid owns, one its gid can write in and an other-writable one fail the start, and the error names the path, the owner and group, the mode, the executor and the remedy; one of another user or of root that the executor cannot write in is used as found. Owners, modes, times and contents of the directory and of what it holds are hashed before and after: nothing changes in either case. - TestPrepareNormaTaskDirParent: a parent the executor can write in and that is not sticky fails the start; the same parent with the sticky bit passes. - TestPrepareNormaTaskDirNotADirectory: a symlink, a dangling symlink and a regular file fail the start, and nothing is created through the link. - TestPrepareNormaTaskDirDropDisabled: with the drop disabled nothing is checked or created. As root, TestNormaTaskDirPlantedByExecutorAsRoot lets an executor process create <temp>/norma with a symlink in it, as it could before a start: PrepareExecPrivilege fails and leaves the directory as it is. The root fixture now gives each test its own temp directory, 1777 like /tmp. PrepareExecPrivilege and Norma's Bash tool reach <temp>/norma, and before this every root test went to the one of the host. The new tests set TMPDIR to t.TempDir() for the same reason.
This commit is contained in:
@@ -5,8 +5,12 @@ package server
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -27,8 +31,8 @@ import (
|
||||
// writable HOME and cache.
|
||||
|
||||
type execRootFixture struct {
|
||||
priv ExecPrivilege
|
||||
data, home, secret, work string
|
||||
priv ExecPrivilege
|
||||
tmp, data, home, secret, work string
|
||||
}
|
||||
|
||||
func newExecRootFixture(t *testing.T) *execRootFixture {
|
||||
@@ -43,6 +47,19 @@ func newExecRootFixture(t *testing.T) *execRootFixture {
|
||||
for _, k := range []string{"PATH", envExecBash, envExecUID, envExecGID, envExecHome} {
|
||||
t.Setenv(k, os.Getenv(k))
|
||||
}
|
||||
// PrepareExecPrivilege and Norma's Bash tool reach <temp>/norma. The temp
|
||||
// directory of the test is its own, world-writable and sticky like /tmp, so the
|
||||
// host's <temp>/norma is never read or written. It cannot be t.TempDir(): the
|
||||
// executor has to traverse it to reach the data directory below.
|
||||
tmp, err := os.MkdirTemp("", "artex-exec-tmp-")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { os.RemoveAll(tmp) })
|
||||
if err := os.Chmod(tmp, 0o777|os.ModeSticky); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("TMPDIR", tmp)
|
||||
// A data directory like /app/data: root-owned and traversable.
|
||||
dataDir, err := os.MkdirTemp("", "artex-exec-root-")
|
||||
if err != nil {
|
||||
@@ -70,7 +87,7 @@ func newExecRootFixture(t *testing.T) *execRootFixture {
|
||||
}
|
||||
// The run directory of task 1's main agent, created as the agent creates it.
|
||||
work := agent.EnsureRunDir(dataDir, 1, 0)
|
||||
return &execRootFixture{priv: priv, data: dataDir, home: filepath.Join(dataDir, execHomeDir), secret: secret, work: work}
|
||||
return &execRootFixture{priv: priv, tmp: tmp, data: dataDir, home: filepath.Join(dataDir, execHomeDir), secret: secret, work: work}
|
||||
}
|
||||
|
||||
// probe is the shell script each path runs as the executor.
|
||||
@@ -306,29 +323,185 @@ func TestNormaTaskDirExecWrite(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// normaTempDir gives the test its own temp directory and returns <temp>/norma, the
|
||||
// path PrepareExecPrivilege checks. The host's <temp>/norma is never touched.
|
||||
func normaTempDir(t *testing.T) string {
|
||||
t.Helper()
|
||||
t.Setenv("TMPDIR", t.TempDir())
|
||||
return filepath.Join(os.TempDir(), "norma")
|
||||
}
|
||||
|
||||
// fillNormaDir puts below dir what a used <temp>/norma holds, a session directory
|
||||
// with a task output file, and a symlink.
|
||||
func fillNormaDir(t *testing.T, dir string) {
|
||||
t.Helper()
|
||||
tasks := filepath.Join(dir, "session-1", "tasks")
|
||||
if err := os.MkdirAll(tasks, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(tasks, "task_1.output"), []byte("output\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink("/etc/passwd", filepath.Join(dir, "link")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// treeDigest hashes path and everything below it without following a symlink: each
|
||||
// name with its type, mode, owner and modification time, the bytes of a file and
|
||||
// the target of a symlink. Two equal digests mean nothing was added, removed,
|
||||
// rewritten, chmodded or chowned in between.
|
||||
func treeDigest(t *testing.T, path string) string {
|
||||
t.Helper()
|
||||
h := sha256.New()
|
||||
err := filepath.WalkDir(path, func(p string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fi, err := d.Info()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rel, err := filepath.Rel(path, p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
st := fi.Sys().(*syscall.Stat_t)
|
||||
fmt.Fprintf(h, "%q %v %d:%d %d\n", rel, fi.Mode(), st.Uid, st.Gid, fi.ModTime().UnixNano())
|
||||
switch {
|
||||
case fi.Mode()&fs.ModeSymlink != 0:
|
||||
target, err := os.Readlink(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(h, "-> %q\n", target)
|
||||
case fi.Mode().IsRegular():
|
||||
b, err := os.ReadFile(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
h.Write(b)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("digest of %s: %v", path, err)
|
||||
}
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
// wantNormaRefusal checks the error of a refused start: it names the path, the
|
||||
// owner and group, the mode, the executor uid and gid, and the remedy.
|
||||
func wantNormaRefusal(t *testing.T, err error, dir string, priv ExecPrivilege) {
|
||||
t.Helper()
|
||||
if err == nil {
|
||||
t.Fatalf("%s was accepted for the executor %d:%d", dir, priv.UID, priv.GID)
|
||||
}
|
||||
fi, lerr := os.Lstat(dir)
|
||||
if lerr != nil {
|
||||
t.Fatal(lerr)
|
||||
}
|
||||
st := fi.Sys().(*syscall.Stat_t)
|
||||
for _, want := range []string{
|
||||
dir,
|
||||
fmt.Sprintf("owner %d:%d", st.Uid, st.Gid),
|
||||
fmt.Sprintf("mode %v", fi.Mode()),
|
||||
fmt.Sprintf("uid %d gid %d", priv.UID, priv.GID),
|
||||
"remove the directory or set TMPDIR to a directory the executor cannot write",
|
||||
} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("the error does not hold %q:\n%v", want, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// checkNormaTaskDir decides from the stat facts and the executor ids alone, so every
|
||||
// rule is tested here without root and without a file system.
|
||||
func TestCheckNormaTaskDir(t *testing.T) {
|
||||
const execUID, execGID = 10001, 10002
|
||||
const path = "/tmp/norma" // named in the errors only: nothing is read from it
|
||||
const dir, sticky = fs.ModeDir, fs.ModeSticky
|
||||
const tmpMode = dir | sticky | 0o777 // a parent like /tmp
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
f normaTaskDirFacts
|
||||
refused string // a part of the reason in the error, "" when the start goes on
|
||||
warned bool
|
||||
}{
|
||||
{"created by the server: root 0755 in /tmp", normaTaskDirFacts{mode: dir | 0o755, parentMode: tmpMode}, "", false},
|
||||
{"a symlink", normaTaskDirFacts{mode: fs.ModeSymlink | 0o777, parentMode: tmpMode}, "symbolic link", false},
|
||||
{"a regular file", normaTaskDirFacts{mode: 0o644, parentMode: tmpMode}, "not a directory", false},
|
||||
{"owned by the executor uid", normaTaskDirFacts{mode: dir | 0o755, uid: execUID, gid: execGID, parentMode: tmpMode}, "owned by the executor uid", false},
|
||||
{"owned by the executor uid, no write bit", normaTaskDirFacts{mode: dir | 0o555, uid: execUID, parentMode: tmpMode}, "owned by the executor uid", false},
|
||||
{"group-writable for the executor gid", normaTaskDirFacts{mode: dir | 0o775, gid: execGID, parentMode: tmpMode}, "group-writable", false},
|
||||
{"other-writable", normaTaskDirFacts{mode: dir | 0o757, parentMode: tmpMode}, "writable by others", false},
|
||||
{"another user, not writable by the executor", normaTaskDirFacts{mode: dir | 0o755, uid: 1000, gid: 1000, parentMode: tmpMode}, "", true},
|
||||
{"another user, the executor gid without the group write bit", normaTaskDirFacts{mode: dir | 0o755, uid: 1000, gid: execGID, parentMode: tmpMode}, "", true},
|
||||
{"root-owned, writable by the root group", normaTaskDirFacts{mode: dir | 0o775, parentMode: tmpMode}, "", true},
|
||||
{"parent world-writable, not sticky", normaTaskDirFacts{mode: dir | 0o755, parentMode: dir | 0o777}, "not sticky", false},
|
||||
{"parent group-writable for the executor gid, not sticky", normaTaskDirFacts{mode: dir | 0o755, parentMode: dir | 0o775, parentGID: execGID}, "not sticky", false},
|
||||
{"parent group-writable for the executor gid, sticky", normaTaskDirFacts{mode: dir | 0o755, parentMode: dir | sticky | 0o775, parentGID: execGID}, "", false},
|
||||
{"parent owned by the executor uid", normaTaskDirFacts{mode: dir | 0o755, parentMode: dir | 0o700, parentUID: execUID}, "owned by the executor", false},
|
||||
{"parent owned by the executor uid, sticky", normaTaskDirFacts{mode: dir | 0o755, parentMode: tmpMode, parentUID: execUID}, "owned by the executor", false},
|
||||
{"parent root 0755, not sticky", normaTaskDirFacts{mode: dir | 0o755, parentMode: dir | 0o755}, "", false},
|
||||
{"parent of another user, not writable by the executor", normaTaskDirFacts{mode: dir | 0o755, parentMode: dir | 0o755, parentUID: 1000, parentGID: 1000}, "", false},
|
||||
} {
|
||||
warning, err := checkNormaTaskDir(path, tc.f, execUID, execGID)
|
||||
if tc.refused == "" {
|
||||
if err != nil {
|
||||
t.Errorf("%s: refused: %v", tc.name, err)
|
||||
}
|
||||
if (warning != "") != tc.warned {
|
||||
t.Errorf("%s: warning %q, want one: %v", tc.name, warning, tc.warned)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err == nil || warning != "" {
|
||||
t.Errorf("%s: err %v, warning %q, want a refusal", tc.name, err, warning)
|
||||
continue
|
||||
}
|
||||
for _, want := range []string{
|
||||
path, tc.refused,
|
||||
fmt.Sprintf("owner %d:%d", tc.f.uid, tc.f.gid),
|
||||
fmt.Sprintf("mode %v", tc.f.mode),
|
||||
fmt.Sprintf("uid %d gid %d", execUID, execGID),
|
||||
"remove the directory or set TMPDIR to a directory the executor cannot write",
|
||||
} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("%s: the error does not hold %q:\n%v", tc.name, want, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
// one the executor can write in fails the start, and the error names the path, its
|
||||
// owner, its mode, the executor and the remedy. One the executor cannot write in is
|
||||
// used as found, with a warning unless root owns it. Either way the directory and
|
||||
// what it holds are the same afterwards: owners, modes, times and contents are
|
||||
// hashed before and after. 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
|
||||
otherUser bool // as root, the directory is given to 20001:20002
|
||||
sameUID, sameGID bool // the executor id is the directory's
|
||||
refused string // the reason in the error, "" when the start goes on
|
||||
}{
|
||||
{"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},
|
||||
{"owned by the executor uid", 0o755, true, true, false, "owned by the executor uid"},
|
||||
{"group-writable for the executor gid", 0o775, true, false, true, "group-writable"},
|
||||
{"other-writable", 0o757, true, false, false, "writable by others"},
|
||||
{"another user, not writable by the executor", 0o755, true, false, false, ""},
|
||||
{"root-owned as root, not writable by the executor", 0o755, false, false, false, ""},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "norma")
|
||||
dir := normaTempDir(t)
|
||||
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
|
||||
fillNormaDir(t, dir)
|
||||
if tc.otherUser && os.Geteuid() == 0 { // without root it is the test user's
|
||||
if err := os.Lchown(dir, 20001, 20002); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -336,11 +509,11 @@ func TestPrepareNormaTaskDirAlreadyThere(t *testing.T) {
|
||||
if err := os.Chmod(dir, tc.perm); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before, err := os.Lstat(dir)
|
||||
fi, err := os.Lstat(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st := before.Sys().(*syscall.Stat_t)
|
||||
st := fi.Sys().(*syscall.Stat_t)
|
||||
priv := ExecPrivilege{Enabled: true, UID: st.Uid + 1, GID: st.Gid + 1}
|
||||
if tc.sameUID {
|
||||
priv.UID = st.Uid
|
||||
@@ -348,48 +521,175 @@ func TestPrepareNormaTaskDirAlreadyThere(t *testing.T) {
|
||||
if tc.sameGID {
|
||||
priv.GID = st.Gid
|
||||
}
|
||||
before := treeDigest(t, dir)
|
||||
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 {
|
||||
if tc.refused != "" {
|
||||
wantNormaRefusal(t, err, dir, priv)
|
||||
if !strings.Contains(err.Error(), tc.refused) {
|
||||
t.Errorf("the error does not give the reason %q: %v", tc.refused, err)
|
||||
}
|
||||
} else if 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())
|
||||
wantWarning := tc.refused == "" && st.Uid != 0
|
||||
if warned := strings.Contains(logged.String(), dir); warned != wantWarning {
|
||||
t.Fatalf("warning logged = %v, want %v:\n%s", warned, wantWarning, 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())
|
||||
if after := treeDigest(t, dir); after != before {
|
||||
t.Fatalf("the directory or what it holds changed: digest %s, was %s", after, before)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// The parent directory decides too. In a parent the executor can write in and that
|
||||
// is not sticky, the executor can rename <temp>/norma and put its own in its place:
|
||||
// the start fails, for a directory that is already there and for one created here.
|
||||
// The same parent with the sticky bit, or one the executor cannot write in, passes.
|
||||
// Runs with and without root.
|
||||
func TestPrepareNormaTaskDirParent(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
mode os.FileMode
|
||||
refused bool
|
||||
}{
|
||||
{"world-writable, not sticky", 0o777, true},
|
||||
{"world-writable, sticky", 0o777 | os.ModeSticky, false},
|
||||
{"not writable by the executor", 0o755, false},
|
||||
} {
|
||||
for _, state := range []string{"already there", "absent"} {
|
||||
t.Run(tc.name+", "+state, func(t *testing.T) {
|
||||
parent := filepath.Join(t.TempDir(), "tmp")
|
||||
if err := os.Mkdir(parent, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Chmod(parent, tc.mode); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("TMPDIR", parent)
|
||||
dir := filepath.Join(os.TempDir(), "norma")
|
||||
pfi, err := os.Stat(parent)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pst := pfi.Sys().(*syscall.Stat_t)
|
||||
// Not the owner of the parent or of the directory, not in their group.
|
||||
priv := ExecPrivilege{Enabled: true, UID: pst.Uid + 1, GID: pst.Gid + 1}
|
||||
before := ""
|
||||
if state == "already there" {
|
||||
if err := os.Mkdir(dir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fillNormaDir(t, dir)
|
||||
before = treeDigest(t, dir)
|
||||
}
|
||||
err = prepareNormaTaskDir(dir, priv)
|
||||
if tc.refused {
|
||||
wantNormaRefusal(t, err, dir, priv)
|
||||
if !strings.Contains(err.Error(), "parent directory") || !strings.Contains(err.Error(), "not sticky") {
|
||||
t.Errorf("the error does not name the parent directory: %v", err)
|
||||
}
|
||||
} else if err != nil {
|
||||
t.Fatalf("refused in a parent the executor cannot rename in: %v", err)
|
||||
}
|
||||
if before != "" {
|
||||
if after := treeDigest(t, dir); after != before {
|
||||
t.Fatalf("the directory or what it holds changed: digest %s, was %s", after, before)
|
||||
}
|
||||
}
|
||||
if now, err := os.Stat(parent); err != nil || now.Mode() != pfi.Mode() {
|
||||
t.Fatalf("the parent changed: %v %v, was %v", now, err, pfi.Mode())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A symlink (to a directory, or dangling) or a regular file at <temp>/norma fails
|
||||
// the start. The entry stays what it was and nothing is created through the link.
|
||||
// Runs with and without root.
|
||||
func TestPrepareNormaTaskDirNotADirectory(t *testing.T) {
|
||||
for _, kind := range []string{"symlink to a directory", "dangling symlink", "regular file"} {
|
||||
t.Run(kind, func(t *testing.T) {
|
||||
dir := normaTempDir(t)
|
||||
target := filepath.Join(t.TempDir(), "target")
|
||||
switch kind {
|
||||
case "symlink to a directory":
|
||||
if err := os.Mkdir(target, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fallthrough
|
||||
case "dangling symlink":
|
||||
if err := os.Symlink(target, dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
default:
|
||||
if err := os.WriteFile(dir, []byte("not a directory\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
before := treeDigest(t, dir)
|
||||
priv := ExecPrivilege{Enabled: true, UID: DefaultExecUID, GID: DefaultExecGID}
|
||||
wantNormaRefusal(t, prepareNormaTaskDir(dir, priv), dir, priv)
|
||||
if after := treeDigest(t, dir); after != before {
|
||||
t.Fatalf("the entry changed: digest %s, was %s", after, before)
|
||||
}
|
||||
switch entries, err := os.ReadDir(target); {
|
||||
case kind == "symlink to a directory" && (err != nil || len(entries) != 0):
|
||||
t.Fatalf("the symlink target holds %v (%v), want it empty", entries, err)
|
||||
case kind == "dangling symlink" && !errors.Is(err, fs.ErrNotExist):
|
||||
t.Fatalf("something was created through the dangling symlink: %v %v", entries, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// With the privilege drop disabled nothing is checked: a missing <temp>/norma is not
|
||||
// created, and one the executor owns and everybody can write in does not fail the
|
||||
// start and stays as it is. The same directory fails it once the drop is enabled.
|
||||
// Runs with and without root.
|
||||
func TestPrepareNormaTaskDirDropDisabled(t *testing.T) {
|
||||
dir := normaTempDir(t)
|
||||
priv := ExecPrivilege{UID: uint32(os.Getuid()), GID: uint32(os.Getgid())} // Enabled is false
|
||||
if err := prepareNormaTaskDir(dir, priv); err != nil {
|
||||
t.Fatalf("absent, drop disabled: %v", err)
|
||||
}
|
||||
if _, err := os.Lstat(dir); !errors.Is(err, fs.ErrNotExist) {
|
||||
t.Fatalf("the directory was created with the drop disabled: %v", err)
|
||||
}
|
||||
if err := os.Mkdir(dir, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fillNormaDir(t, dir)
|
||||
if err := os.Chmod(dir, 0o777); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before := treeDigest(t, dir)
|
||||
if err := prepareNormaTaskDir(dir, priv); err != nil {
|
||||
t.Fatalf("executor-writable, drop disabled: %v", err)
|
||||
}
|
||||
// The same through the entry point, which reads the path from the temp directory.
|
||||
entry := priv
|
||||
if err := PrepareExecPrivilege(&entry, t.TempDir(), "", ""); err != nil {
|
||||
t.Fatalf("PrepareExecPrivilege, drop disabled: %v", err)
|
||||
}
|
||||
priv.Enabled = true
|
||||
wantNormaRefusal(t, prepareNormaTaskDir(dir, priv), dir, priv)
|
||||
if after := treeDigest(t, dir); after != before {
|
||||
t.Fatalf("the directory or what it holds changed: digest %s, was %s", after, before)
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
func TestNormaTaskDirAsRoot(t *testing.T) {
|
||||
if os.Geteuid() != 0 {
|
||||
t.Skip("needs root")
|
||||
}
|
||||
tmp, err := os.MkdirTemp("", "artex-norma-tmp-")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { os.RemoveAll(tmp) })
|
||||
if err := os.Chmod(tmp, 0o777|os.ModeSticky); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("TMPDIR", tmp)
|
||||
f := newExecRootFixture(t)
|
||||
f := newExecRootFixture(t) // skips without root; its temp directory is 1777
|
||||
tmp := f.tmp
|
||||
fi, err := os.Lstat(filepath.Join(tmp, "norma"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -413,6 +713,31 @@ id -u`, "sh", tmp)
|
||||
}
|
||||
}
|
||||
|
||||
// The case the check is for, through the entry point: an executor process creates
|
||||
// <temp>/norma in the sticky temp directory before a start and places a symlink in
|
||||
// it. PrepareExecPrivilege then fails, and leaves what the executor made as it is.
|
||||
func TestNormaTaskDirPlantedByExecutorAsRoot(t *testing.T) {
|
||||
f := newExecRootFixture(t) // skips without root
|
||||
dir := filepath.Join(f.tmp, "norma")
|
||||
if err := os.Remove(dir); err != nil { // the one this start created, still empty
|
||||
t.Fatal(err)
|
||||
}
|
||||
c := exec.Command("sh", "-c", `mkdir "$1" && ln -s /etc/passwd "$1/task_1.output"`, "sh", dir)
|
||||
applyExecCredential(c, f.priv)
|
||||
if out, err := c.CombinedOutput(); err != nil {
|
||||
t.Fatalf("%v\n%s", err, out)
|
||||
}
|
||||
before := treeDigest(t, dir)
|
||||
err := PrepareExecPrivilege(&f.priv, f.data, "", "")
|
||||
wantNormaRefusal(t, err, dir, f.priv)
|
||||
if !strings.Contains(err.Error(), "prepare the background-task output directory") {
|
||||
t.Errorf("the error does not say which step failed: %v", err)
|
||||
}
|
||||
if after := treeDigest(t, dir); after != before {
|
||||
t.Fatalf("the directory or what it holds changed: digest %s, was %s", after, before)
|
||||
}
|
||||
}
|
||||
|
||||
// applyExecCredential sets the credential and the executor's HOME; the later env
|
||||
// entry wins in exec.Cmd. Runs without root: nothing is started.
|
||||
func TestApplyExecCredentialSetsExecutorHome(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user