mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-25 17:30:56 +08:00
security(agent): restore group sharing for legacy task entries; resolve ask_operator paths from the run dir
The layout commit made tasks/<id> root:root 0755 and shared only the run directories. Entries that sat directly in a task directory before it (uploads/, files the main agent wrote there) were left as found and nothing shared them any more: after an archive restore, which keeps neither owners nor modes, the executor could not use them. ask_operator still resolved a relative image_path from tasks/<id>, not from the directory the agent works in. - shareTaskDir: after the task directory is set root:root 0755, each direct entry that is not a run directory (main, i<N>) is shared with the executor group again (shareWithExecGroup: group owner, group read/write, traverse and setgid on directories). Run directories are handled as before. The task directory stays root-only, so the executor works inside an old entry and cannot rename, remove or add one. A symlink among the entries is not followed. The pass runs at start-up and on archive restore (installTaskArchiveFiles calls shareRestoredTaskDir and rolls back on failure), with no operator step. - ask_operator: image_path is resolved inside the run directory of the agent that asks (agent.EnsureRunDir: tasks/<id>/i<N> for a worker, tasks/<id>/main without an intent). An absolute path outside it, "..", and a symlink leading out are refused as before. The copy is still staged in tasks/<id>/uploads. The tool description says so. Tests: root-only TestShareTaskDirLegacyEntriesAsRoot (owners and modes, an executor probe, links to a root-only directory and file left alone); TestRunDirLayoutOnExistingTreeAsRoot now expects the old file shared; TestTaskArchiveRestoreRunDirOwnershipAsRoot restores an entry of the task directory; TestStageAskOperatorImage uses the i<N> and main leaves. SECURITY.md states the sharing of the old entries and the empty main/.
This commit is contained in:
+1
-1
@@ -51,7 +51,7 @@ Operators running self-hosted LLM gateways or Ollama on a LAN or Docker bridge c
|
||||
|
||||
## Agent Process Isolation
|
||||
|
||||
The server runs as root, but the processes an agent starts (Bash, the interactive shell, custom tools and stdio MCP servers) run as the unprivileged uid and gid set by `-exec-uid` and `-exec-gid` (default 10001). Only the run directories and their own home (`<data>/exec-home`) are writable by them. No agent working directory is an ancestor of another: the main agent and the planner work in `tasks/<id>/main`, a worker in `tasks/<id>/i<N>`, a chat in `sessions/<id>`. Their parents (`tasks/`, `sessions/` and `tasks/<id>`) are root:root 0755, so the executor cannot create, rename or remove an entry there, and only the leaves are shared with the executor group (setgid, 0775); with the drop active the server creates each level without following a symlink. Files that a task wrote directly in `tasks/<id>/` before this layout are not moved and keep the owner and mode they had, so one that was group-writable stays writable by the executor. `keys/` (`jwt.key`, `secret.key`), the config file and the MITM CA key are root-only, and the server's environment block no longer carries `ARTEX_PG_DSN`, `ARTEX_SECRET_KEY`, the LLM API keys or the LLM proxy and base URLs (Linux). The agent's file tools (Read, Write, Edit, MultiEdit, LS, Glob, Grep) run inside the server but reach only the run's working directory: paths are opened through `os.Root`, so absolute paths elsewhere, `..` and symlinks leading out are refused, and a working directory reached through any symlink is refused. The executor cannot replace a run directory or one of its parents, so the race that remains is the swap between an `EvalSymlinks` check and the open of a file inside a run directory, which the executor can do only inside a leaf (every executor process has the same identity, so this is any leaf, never a leaf itself or its parents). `ingest_bbot` reads its file through the same confinement. Custom-tool scripts are protected only when the executor privilege drop is active: they are then written to `<data>/exec-scripts`, a root-owned directory the executor can read and cannot change; with the drop disabled they still go to `.tools/` under the run directory. Command-output spills (the full output of a tool call over the size cap, written by Norma inside the server) follow the same rule: with the drop active they go to a root-owned directory per task or chat session outside the run directory, `<data>/spill/tasks/<id>` or `<data>/spill/sessions/<id>` (root with the executor group, 0750, below root:root 0755 parents), which is deleted and archived with the task, or deleted with the conversation. The executor cannot create, rename or remove an entry there. Norma writes a spill file 0600 and the server makes it root with the executor group, 0640, when the tool call returns: the agent reads it with Bash, not with the file tools, which do not reach outside the run directory. With the drop disabled spills still go to `cmd-output/` under the run directory. Residual, not closed here: the other root reads and writes by path remain open, and a symlink the executor places can redirect them, so root may read or write outside the workspace. They are `ingest_scan_output` (a root read by path), the `ask_operator` image staging (a root read and write under the task `uploads/` directory), chat uploads under the `tasks/<id>/main/uploads/` or `sessions/<id>/uploads/` directories, and task archive packaging (`lstat` then open by path). The Docker image has no setuid programs and `docker-compose.yml` sets `no-new-privileges`. These are a containment layer, not a sandbox: an executor can still read world-readable files under the data directory, reach the loopback API and the database if the network allows it, and use any credential it was given through the platform. Persistence tools (custom tools, MCP servers, skills) need operator approval by default.
|
||||
The server runs as root, but the processes an agent starts (Bash, the interactive shell, custom tools and stdio MCP servers) run as the unprivileged uid and gid set by `-exec-uid` and `-exec-gid` (default 10001). Only the run directories and their own home (`<data>/exec-home`) are writable by them. No agent working directory is an ancestor of another: the main agent and the planner work in `tasks/<id>/main`, a worker in `tasks/<id>/i<N>`, a chat in `sessions/<id>`. Their parents (`tasks/`, `sessions/` and `tasks/<id>`) are root:root 0755, so the executor cannot create, rename or remove an entry there, and only the leaves are shared with the executor group (setgid, 0775); with the drop active the server creates each level without following a symlink. Entries that a task already had directly under `tasks/<id>/` before this layout (uploads, files the main agent wrote there) are not moved and are shared with the executor group again, at start-up and on archive restore, so for tasks in progress too, while `tasks/<id>` itself stays root:root 0755: the main agent's new run directory `main/` is empty on its first turn, and the earlier artifacts stay under `tasks/<id>/` and are reached through Bash only. `keys/` (`jwt.key`, `secret.key`), the config file and the MITM CA key are root-only, and the server's environment block no longer carries `ARTEX_PG_DSN`, `ARTEX_SECRET_KEY`, the LLM API keys or the LLM proxy and base URLs (Linux). The agent's file tools (Read, Write, Edit, MultiEdit, LS, Glob, Grep) run inside the server but reach only the run's working directory: paths are opened through `os.Root`, so absolute paths elsewhere, `..` and symlinks leading out are refused, and a working directory reached through any symlink is refused. The executor cannot replace a run directory or one of its parents, so the race that remains is the swap between an `EvalSymlinks` check and the open of a file inside a run directory, which the executor can do only inside a leaf (every executor process has the same identity, so this is any leaf, never a leaf itself or its parents). `ingest_bbot` reads its file through the same confinement. Custom-tool scripts are protected only when the executor privilege drop is active: they are then written to `<data>/exec-scripts`, a root-owned directory the executor can read and cannot change; with the drop disabled they still go to `.tools/` under the run directory. Command-output spills (the full output of a tool call over the size cap, written by Norma inside the server) follow the same rule: with the drop active they go to a root-owned directory per task or chat session outside the run directory, `<data>/spill/tasks/<id>` or `<data>/spill/sessions/<id>` (root with the executor group, 0750, below root:root 0755 parents), which is deleted and archived with the task, or deleted with the conversation. The executor cannot create, rename or remove an entry there. Norma writes a spill file 0600 and the server makes it root with the executor group, 0640, when the tool call returns: the agent reads it with Bash, not with the file tools, which do not reach outside the run directory. With the drop disabled spills still go to `cmd-output/` under the run directory. Residual, not closed here: the other root reads and writes by path remain open, and a symlink the executor places can redirect them, so root may read or write outside the workspace. They are `ingest_scan_output` (a root read by path), the `ask_operator` image staging (a root read and write under the task `uploads/` directory), chat uploads under the `tasks/<id>/main/uploads/` or `sessions/<id>/uploads/` directories, and task archive packaging (`lstat` then open by path). The Docker image has no setuid programs and `docker-compose.yml` sets `no-new-privileges`. These are a containment layer, not a sandbox: an executor can still read world-readable files under the data directory, reach the loopback API and the database if the network allows it, and use any credential it was given through the platform. Persistence tools (custom tools, MCP servers, skills) need operator approval by default.
|
||||
|
||||
## What to Expect
|
||||
|
||||
|
||||
+18
-15
@@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -37,7 +38,7 @@ func (s *Server) toolAskOperator() actool.CoreTool {
|
||||
},
|
||||
"image_path": map[string]any{
|
||||
"type": "string",
|
||||
"description": "Optional path to a screenshot (png/jpg/jpeg/webp/gif, max 10MB) under this task's working directory, e.g. the browser_take_screenshot output. It is copied into the task uploads and shown to the operator.",
|
||||
"description": "Optional path to a screenshot (png/jpg/jpeg/webp/gif, max 10MB) in your run directory, e.g. the browser_take_screenshot output. A relative path resolves from the run directory; an absolute path must be inside it. It is copied into the task uploads and shown to the operator.",
|
||||
},
|
||||
},
|
||||
"required": []any{"question"},
|
||||
@@ -61,7 +62,7 @@ func (s *Server) toolAskOperator() actool.CoreTool {
|
||||
if t == nil {
|
||||
return actool.Errorf("Error: task not found"), nil
|
||||
}
|
||||
att, err := stageAskOperatorImage(s.m.dir, taskID, req.ImagePath)
|
||||
att, err := stageAskOperatorImage(s.m.dir, ri.TaskID, ri.IntentID, req.ImagePath)
|
||||
if err != nil {
|
||||
return actool.Errorf("Error: " + err.Error()), nil
|
||||
}
|
||||
@@ -156,10 +157,10 @@ func askOperatorActivity(text string, att []chatAttachment, derived []string) db
|
||||
return a
|
||||
}
|
||||
|
||||
// resolveAskOperatorImage maps image_path onto a regular file inside taskDir.
|
||||
// Relative paths are resolved from the task working directory (the worker CWD).
|
||||
// ".." segments, paths that EvalSymlinks outside taskDir, and non-images are refused.
|
||||
func resolveAskOperatorImage(taskDir, src string) (string, error) {
|
||||
// resolveAskOperatorImage maps image_path onto a regular file inside runDir, the
|
||||
// run directory of the agent that asks (its CWD). Relative paths are resolved from it.
|
||||
// ".." segments, paths that EvalSymlinks outside runDir, and non-images are refused.
|
||||
func resolveAskOperatorImage(runDir, src string) (string, error) {
|
||||
src = strings.TrimSpace(src)
|
||||
ext := strings.ToLower(filepath.Ext(src))
|
||||
if !askOperatorImageExt[ext] {
|
||||
@@ -167,13 +168,13 @@ func resolveAskOperatorImage(taskDir, src string) (string, error) {
|
||||
}
|
||||
for _, el := range strings.Split(filepath.ToSlash(src), "/") {
|
||||
if el == ".." {
|
||||
return "", fmt.Errorf("image_path must stay inside the task working directory")
|
||||
return "", fmt.Errorf("image_path must stay inside the run directory")
|
||||
}
|
||||
}
|
||||
if !filepath.IsAbs(src) {
|
||||
src = filepath.Join(taskDir, src)
|
||||
src = filepath.Join(runDir, src)
|
||||
}
|
||||
realRoot, err := filepath.EvalSymlinks(taskDir)
|
||||
realRoot, err := filepath.EvalSymlinks(runDir)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("image not readable")
|
||||
}
|
||||
@@ -183,21 +184,23 @@ func resolveAskOperatorImage(taskDir, src string) (string, error) {
|
||||
}
|
||||
rel, err := filepath.Rel(realRoot, real)
|
||||
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||
return "", fmt.Errorf("image_path must stay inside the task working directory")
|
||||
return "", fmt.Errorf("image_path must stay inside the run directory")
|
||||
}
|
||||
return real, nil
|
||||
}
|
||||
|
||||
// stageAskOperatorImage copies the referenced image into the task's uploads dir
|
||||
// (the same place chat uploads land, so the transcript attachment card resolves)
|
||||
// and returns the attachment descriptor. Empty src returns no attachment.
|
||||
func stageAskOperatorImage(dataDir, taskID, src string) ([]chatAttachment, error) {
|
||||
// (tasks/<id>/uploads, where the transcript attachment card resolves) and returns
|
||||
// the attachment descriptor. The image is read from the run directory of the agent
|
||||
// that asks: tasks/<id>/i<intentID> for a worker, tasks/<id>/main without an intent.
|
||||
// Empty src returns no attachment.
|
||||
func stageAskOperatorImage(dataDir string, taskID, intentID int64, src string) ([]chatAttachment, error) {
|
||||
src = strings.TrimSpace(src)
|
||||
if src == "" {
|
||||
return nil, nil
|
||||
}
|
||||
taskDir := filepath.Join(dataDir, "tasks", taskID)
|
||||
resolved, err := resolveAskOperatorImage(taskDir, src)
|
||||
taskDir := filepath.Join(dataDir, "tasks", strconv.FormatInt(taskID, 10))
|
||||
resolved, err := resolveAskOperatorImage(agent.EnsureRunDir(dataDir, taskID, intentID), src)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
+62
-36
@@ -10,83 +10,109 @@ import (
|
||||
"github.com/Autumn-27/artex/intercept"
|
||||
)
|
||||
|
||||
// A worker's image_path is read from its run directory tasks/<id>/i<N>; the copy is
|
||||
// staged in tasks/<id>/uploads.
|
||||
func TestStageAskOperatorImage(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
taskDir := filepath.Join(dir, "tasks", "7")
|
||||
if err := os.MkdirAll(taskDir, 0o755); err != nil {
|
||||
runDir := filepath.Join(taskDir, "i3")
|
||||
if err := os.MkdirAll(runDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
src := filepath.Join(taskDir, "shot.png")
|
||||
src := filepath.Join(runDir, "shot.png")
|
||||
if err := os.WriteFile(src, []byte("fakepng"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// no image -> no attachment
|
||||
att, err := stageAskOperatorImage(dir, "7", "")
|
||||
att, err := stageAskOperatorImage(dir, 7, 3, "")
|
||||
if err != nil || att != nil {
|
||||
t.Fatalf("empty src: att=%v err=%v", att, err)
|
||||
}
|
||||
|
||||
// relative path from the task working directory (worker CWD)
|
||||
att, err = stageAskOperatorImage(dir, "7", "shot.png")
|
||||
// relative path from the run directory (worker CWD)
|
||||
att, err = stageAskOperatorImage(dir, 7, 3, "shot.png")
|
||||
if err != nil || len(att) != 1 {
|
||||
t.Fatalf("valid src: att=%v err=%v", att, err)
|
||||
}
|
||||
if !strings.HasPrefix(att[0].Path, "uploads/ask-operator-") || !strings.HasSuffix(att[0].Path, ".png") {
|
||||
t.Fatalf("unexpected attachment path %q", att[0].Path)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(taskDir, "uploads", att[0].Name)); err != nil {
|
||||
t.Fatalf("copy missing: %v", err)
|
||||
if got, err := os.ReadFile(filepath.Join(taskDir, "uploads", att[0].Name)); err != nil || string(got) != "fakepng" {
|
||||
t.Fatalf("copy under tasks/7/uploads: %q, %v", got, err)
|
||||
}
|
||||
if _, err := os.Lstat(filepath.Join(runDir, "uploads")); !os.IsNotExist(err) {
|
||||
t.Fatalf("the copy must not be staged in the run directory: %v", err)
|
||||
}
|
||||
|
||||
// absolute path still accepted when it stays inside the task dir
|
||||
att, err = stageAskOperatorImage(dir, "7", src)
|
||||
// absolute path still accepted when it stays inside the run dir
|
||||
att, err = stageAskOperatorImage(dir, 7, 3, src)
|
||||
if err != nil || len(att) != 1 {
|
||||
t.Fatalf("absolute inside task dir: att=%v err=%v", att, err)
|
||||
t.Fatalf("absolute inside run dir: att=%v err=%v", att, err)
|
||||
}
|
||||
|
||||
// wrong extension rejected
|
||||
bad := filepath.Join(taskDir, "payload.exe")
|
||||
bad := filepath.Join(runDir, "payload.exe")
|
||||
if err := os.WriteFile(bad, []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := stageAskOperatorImage(dir, "7", "payload.exe"); err == nil || !strings.Contains(err.Error(), "png/jpg") {
|
||||
if _, err := stageAskOperatorImage(dir, 7, 3, "payload.exe"); err == nil || !strings.Contains(err.Error(), "png/jpg") {
|
||||
t.Fatalf("expected extension rejection, got %v", err)
|
||||
}
|
||||
|
||||
// missing file rejected
|
||||
if _, err := stageAskOperatorImage(dir, "7", "nope.png"); err == nil {
|
||||
if _, err := stageAskOperatorImage(dir, 7, 3, "nope.png"); err == nil {
|
||||
t.Fatal("expected missing-file rejection")
|
||||
}
|
||||
|
||||
// The task directory, the main agent's run directory, another task and the data
|
||||
// directory are outside the worker's run directory, by ".." and by absolute path.
|
||||
outside := filepath.Join(dir, "outside.png")
|
||||
if err := os.WriteFile(outside, []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
inTask := filepath.Join(taskDir, "shot.png")
|
||||
inMain := filepath.Join(taskDir, "main", "shot.png")
|
||||
other := filepath.Join(dir, "tasks", "8", "i3", "shot.png")
|
||||
for _, file := range []string{outside, inTask, inMain, other} {
|
||||
if err := os.MkdirAll(filepath.Dir(file), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(file, []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := stageAskOperatorImage(dir, 7, 3, file); err == nil {
|
||||
t.Fatalf("expected outside-path rejection of %s", file)
|
||||
}
|
||||
}
|
||||
if _, err := stageAskOperatorImage(dir, "7", filepath.Join("..", "..", "outside.png")); err == nil {
|
||||
t.Fatal("expected .. rejection")
|
||||
}
|
||||
if _, err := stageAskOperatorImage(dir, "7", outside); err == nil {
|
||||
t.Fatal("expected outside-path rejection")
|
||||
for _, rel := range []string{
|
||||
filepath.Join("..", "shot.png"),
|
||||
filepath.Join("..", "main", "shot.png"),
|
||||
filepath.Join("..", "..", "..", "outside.png"),
|
||||
} {
|
||||
if _, err := stageAskOperatorImage(dir, 7, 3, rel); err == nil {
|
||||
t.Fatalf("expected .. rejection of %s", rel)
|
||||
}
|
||||
}
|
||||
|
||||
otherDir := filepath.Join(dir, "tasks", "8")
|
||||
if err := os.MkdirAll(otherDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other := filepath.Join(otherDir, "shot.png")
|
||||
if err := os.WriteFile(other, []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := stageAskOperatorImage(dir, "7", other); err == nil {
|
||||
t.Fatal("expected other-task rejection")
|
||||
}
|
||||
|
||||
link := filepath.Join(taskDir, "link.png")
|
||||
if err := os.Symlink(outside, link); err != nil {
|
||||
// A symlink leading out of the run directory is refused, as the last component
|
||||
// and as a directory on the way.
|
||||
if err := os.Symlink(outside, filepath.Join(runDir, "link.png")); err != nil {
|
||||
t.Logf("symlink test skipped: %v", err)
|
||||
} else if _, err := stageAskOperatorImage(dir, "7", "link.png"); err == nil {
|
||||
t.Fatal("expected symlink-out rejection")
|
||||
} else {
|
||||
if err := os.Symlink(taskDir, filepath.Join(runDir, "up")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, p := range []string{"link.png", filepath.Join(runDir, "link.png"), filepath.Join("up", "shot.png")} {
|
||||
if _, err := stageAskOperatorImage(dir, 7, 3, p); err == nil {
|
||||
t.Fatalf("expected symlink-out rejection of %s", p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Without an intent the image is read from the main agent's run directory.
|
||||
if _, err := stageAskOperatorImage(dir, 7, 0, "shot.png"); err != nil {
|
||||
t.Fatalf("relative path from tasks/7/main: %v", err)
|
||||
}
|
||||
if _, err := stageAskOperatorImage(dir, 7, 0, src); err == nil {
|
||||
t.Fatal("expected the worker's file to be outside the main agent's run directory")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+16
-7
@@ -143,7 +143,9 @@ func prepareExecScript(p ExecPrivilege, path string) error {
|
||||
// Their parents (tasks/, sessions/ and tasks/<id>) are root:root 0755, so the
|
||||
// executor cannot create, rename or remove a run directory, and no run directory is
|
||||
// an ancestor of another. Inside a run directory new subdirectories inherit the
|
||||
// group. Command-output spills leave the run directories for <data>/spill, which the
|
||||
// group. The entries a task directory held before this layout are shared with the
|
||||
// group too (shareTaskDir). Command-output spills leave the run directories for
|
||||
// <data>/spill, which the
|
||||
// executor group reads and cannot change (makeSpillDir). The key files and the
|
||||
// config file (which can hold the database DSN) are restricted to root. Nothing
|
||||
// else under the data directory changes; directories a
|
||||
@@ -306,7 +308,7 @@ func makeLeafDir(base, leaf string, gid int, leafMode os.FileMode) error {
|
||||
// shareRunDirs applies the run-directory layout to what is on disk: tasks/ and
|
||||
// sessions/ become root:root 0755, each task directory too, and each run directory
|
||||
// (sessions/<id>, tasks/<id>/main, tasks/<id>/i<N>) is shared with the executor
|
||||
// group with what it holds.
|
||||
// group with what it holds, like the older entries of a task directory.
|
||||
func shareRunDirs(dataDir string, gid int) error {
|
||||
if err := os.MkdirAll(dataDir, 0o755); err != nil {
|
||||
return err
|
||||
@@ -342,8 +344,12 @@ func shareRunDirs(dataDir string, gid int) error {
|
||||
}
|
||||
|
||||
// shareTaskDir makes a task directory root:root 0755 and shares the run directories
|
||||
// in it. Its other entries are left as they are: a task whose main agent worked in
|
||||
// the task directory itself keeps its files there, with the owner and mode they had.
|
||||
// in it. Its other entries date from before this layout, when the task directory was
|
||||
// the main agent's working directory (uploads/, files the agent wrote there): they
|
||||
// stay where they are and are shared with the executor group again, as the whole
|
||||
// tree was. The task directory itself stays root-only, so the executor can change
|
||||
// what is inside such an entry and cannot rename, remove or add one. A symlink
|
||||
// among them is not followed.
|
||||
func shareTaskDir(taskDir string, gid int) error {
|
||||
if err := setDirOwner(taskDir, 0, 0o755); err != nil {
|
||||
return err
|
||||
@@ -353,10 +359,13 @@ func shareTaskDir(taskDir string, gid int) error {
|
||||
return err
|
||||
}
|
||||
for _, e := range entries {
|
||||
if !e.IsDir() || !agent.IsRunDirName(e.Name()) {
|
||||
continue
|
||||
path := filepath.Join(taskDir, e.Name())
|
||||
if !agent.IsRunDirName(e.Name()) {
|
||||
err = shareWithExecGroup(path, gid)
|
||||
} else if e.IsDir() {
|
||||
err = shareRunDir(path, gid)
|
||||
}
|
||||
if err := shareRunDir(filepath.Join(taskDir, e.Name()), gid); err != nil {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,7 +177,7 @@ attempt("chmod_worker", lambda: os.chmod(worker, 0o777))
|
||||
|
||||
// A data directory written before this layout: the task directory was the main
|
||||
// agent's working directory and was shared with the executor group. The start-up
|
||||
// pass takes the task directory back, shares its run directories and leaves the
|
||||
// pass takes the task directory back, shares its run directories and shares the
|
||||
// files of the task directory where they are. A symlink planted where a run
|
||||
// directory goes is refused and its target is not changed.
|
||||
func TestRunDirLayoutOnExistingTreeAsRoot(t *testing.T) {
|
||||
@@ -223,7 +223,6 @@ func TestRunDirLayoutOnExistingTreeAsRoot(t *testing.T) {
|
||||
}
|
||||
return fi.Mode(), fi.Sys().(*syscall.Stat_t).Gid
|
||||
}
|
||||
oldMode, oldGid := stat(oldFile)
|
||||
|
||||
if err := PrepareExecPrivilege(&f.priv, f.data, "", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -231,12 +230,11 @@ func TestRunDirLayoutOnExistingTreeAsRoot(t *testing.T) {
|
||||
checkDir(t, task, 0, parentDirMode)
|
||||
checkDir(t, oldWorker, DefaultExecGID, leafDirMode)
|
||||
checkDir(t, nested, DefaultExecGID, 0o770|os.ModeSetgid)
|
||||
if mode, g := stat(workerFile); mode.Perm() != 0o660 || g != DefaultExecGID {
|
||||
t.Errorf("%s: %v gid %d, want 0660 with gid %d", workerFile, mode, g, DefaultExecGID)
|
||||
}
|
||||
// The file of the task directory is not moved and keeps its mode and group.
|
||||
if mode, g := stat(oldFile); mode != oldMode || g != oldGid {
|
||||
t.Errorf("%s: %v gid %d, want it left %v gid %d", oldFile, mode, g, oldMode, oldGid)
|
||||
// The file of the task directory is not moved and is shared like the worker's.
|
||||
for _, file := range []string{workerFile, oldFile} {
|
||||
if mode, g := stat(file); mode.Perm() != 0o660 || g != DefaultExecGID {
|
||||
t.Errorf("%s: %v gid %d, want 0660 with gid %d", file, mode, g, DefaultExecGID)
|
||||
}
|
||||
}
|
||||
// The planted link is still a link; creating the main run directory through it
|
||||
// fails, and the directory it points to keeps its owner and mode.
|
||||
@@ -264,6 +262,8 @@ func TestTaskArchiveRestoreRunDirOwnershipAsRoot(t *testing.T) {
|
||||
files := map[string]string{
|
||||
filepath.Join(f.work, "uploads", "evidence.txt"): "task evidence",
|
||||
filepath.Join(worker, "notes.txt"): "worker notes",
|
||||
// An entry from before the layout, directly in the task directory.
|
||||
filepath.Join(task, "uploads", "old.txt"): "old upload",
|
||||
}
|
||||
for path, body := range files {
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
@@ -346,3 +346,114 @@ func TestTaskArchiveRestoreRunDirOwnershipAsRoot(t *testing.T) {
|
||||
t.Fatalf("executor write in the restored run directory: %v\n%s", err, out)
|
||||
}
|
||||
}
|
||||
|
||||
// A task directory holds entries from before this layout: uploads/ and a file the
|
||||
// main agent wrote, here root-only as an archive restore leaves them. shareTaskDir
|
||||
// shares them with the executor group again and keeps the task directory root-only:
|
||||
// the executor works inside the old entries and cannot move a run directory. A
|
||||
// symlink among the old entries is not followed.
|
||||
func TestShareTaskDirLegacyEntriesAsRoot(t *testing.T) {
|
||||
f := newExecRootFixture(t)
|
||||
py, err := exec.LookPath("python3")
|
||||
if err != nil {
|
||||
t.Fatalf("python3 is needed for the executor probe: %v", err)
|
||||
}
|
||||
task := filepath.Join(f.data, "tasks", "3")
|
||||
uploads := filepath.Join(task, "uploads")
|
||||
worker := filepath.Join(task, "i4")
|
||||
trap := filepath.Join(f.data, "trap")
|
||||
for _, dir := range []string{task, uploads, worker, trap} {
|
||||
if err := os.Mkdir(dir, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
upload := filepath.Join(uploads, "x.txt")
|
||||
notes := filepath.Join(task, "notes.txt")
|
||||
trapped := filepath.Join(trap, "inside.txt")
|
||||
for _, file := range []string{upload, notes, trapped} {
|
||||
if err := os.WriteFile(file, []byte("old"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// Links planted directly in the task directory, to a root-only directory and to
|
||||
// a root-only file.
|
||||
links := map[string]string{filepath.Join(task, "old-dir"): trap, filepath.Join(task, "old-key"): f.secret}
|
||||
for link, target := range links {
|
||||
if err := os.Symlink(target, link); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := shareTaskDir(task, int(DefaultExecGID)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
checkDir(t, task, 0, parentDirMode)
|
||||
checkDir(t, worker, DefaultExecGID, leafDirMode)
|
||||
checkDir(t, uploads, DefaultExecGID, 0o770|os.ModeSetgid)
|
||||
checkFile := func(path string, gid uint32, mode os.FileMode) {
|
||||
t.Helper()
|
||||
fi, err := os.Lstat(path)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if st := fi.Sys().(*syscall.Stat_t); fi.Mode() != mode || st.Uid != 0 || st.Gid != gid {
|
||||
t.Errorf("%s: %v uid %d gid %d, want %v root:%d", path, fi.Mode(), st.Uid, st.Gid, mode, gid)
|
||||
}
|
||||
}
|
||||
checkFile(upload, DefaultExecGID, 0o660)
|
||||
checkFile(notes, DefaultExecGID, 0o660)
|
||||
// The links are still links and what they point to is unchanged.
|
||||
for link := range links {
|
||||
if fi, err := os.Lstat(link); err != nil || fi.Mode()&os.ModeSymlink == 0 {
|
||||
t.Errorf("%s: %v %v, want the symlink left in place", link, fi, err)
|
||||
}
|
||||
}
|
||||
checkDir(t, trap, 0, 0o700)
|
||||
checkFile(trapped, 0, 0o600)
|
||||
checkFile(f.secret, 0, 0o600)
|
||||
|
||||
code := `import errno, os
|
||||
task = os.environ["TASK"]
|
||||
worker = os.path.join(task, "i4")
|
||||
print("uid=%d gid=%d" % (os.getuid(), os.getgid()))
|
||||
def attempt(name, fn):
|
||||
try:
|
||||
fn()
|
||||
print(name + "=done")
|
||||
except OSError as e:
|
||||
print(name + "=" + errno.errorcode.get(e.errno, str(e.errno)))
|
||||
attempt("write_in_uploads", lambda: open(os.path.join(task, "uploads", "y.txt"), "w").close())
|
||||
attempt("append_upload", lambda: open(os.path.join(task, "uploads", "x.txt"), "a").close())
|
||||
attempt("read_notes", lambda: print("notes=" + open(os.path.join(task, "notes.txt")).read()))
|
||||
attempt("rename_worker", lambda: os.rename(worker, os.path.join(task, "i4.moved")))
|
||||
attempt("rmdir_worker", lambda: os.rmdir(worker))
|
||||
attempt("unlink_worker", lambda: os.unlink(worker))
|
||||
attempt("rename_uploads", lambda: os.rename(os.path.join(task, "uploads"), os.path.join(task, "uploads.moved")))
|
||||
attempt("unlink_notes", lambda: os.unlink(os.path.join(task, "notes.txt")))
|
||||
attempt("create_in_task", lambda: open(os.path.join(task, "planted"), "w").close())
|
||||
attempt("read_trap", lambda: open(os.path.join(task, "old-dir", "inside.txt")).close())
|
||||
attempt("read_key", lambda: open(os.path.join(task, "old-key")).close())
|
||||
`
|
||||
out, err := execPython(context.Background(), py, "probe", code, nil, f.work, []string{"TASK=" + task}, 30*time.Second, f.priv)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Logf("executor probe output:\n%s", out)
|
||||
for _, want := range []string{
|
||||
fmt.Sprintf("uid=%d gid=%d\n", DefaultExecUID, DefaultExecGID),
|
||||
"write_in_uploads=done\n", "append_upload=done\n", "notes=old\n", "read_notes=done\n",
|
||||
"rename_worker=EACCES\n", "rmdir_worker=EACCES\n", "unlink_worker=EACCES\n",
|
||||
"rename_uploads=EACCES\n", "unlink_notes=EACCES\n", "create_in_task=EACCES\n",
|
||||
"read_trap=EACCES\n", "read_key=EACCES\n",
|
||||
} {
|
||||
if !strings.Contains(out, want) {
|
||||
t.Errorf("want %q in the probe output:\n%s", want, out)
|
||||
}
|
||||
}
|
||||
if strings.Contains(out, "[exit:") {
|
||||
t.Errorf("the probe failed:\n%s", out)
|
||||
}
|
||||
checkDir(t, task, 0, parentDirMode)
|
||||
checkDir(t, worker, DefaultExecGID, leafDirMode)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user