mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-25 17:30:56 +08:00
security(agent): name the Norma background output directory
Norma keeps the output of background tasks in os.TempDir()/norma (<tmp>/norma/<session>/tasks/task_<n>.output), written from the server process with MkdirAll and an open that follows symlinks. ARTEX does not choose that path, and the temp directory is world-writable: an executor process that runs before the first session creates the directory (a stdio MCP server, a process left from an earlier start) could create it first and place symlinks in it for the server to write through. - PrepareExecPrivilege, only when the drop is enabled, now creates <TempDir>/norma itself (prepareNormaTaskDir): Mkdir 0755 as root, then Lstat. A symlink or a file in its place is refused and the server does not start. - A directory that is already there is used as found. It is not chowned: it may belong to another user of the host. When root does not own it or others can write in it, a warning is logged and nothing else changes. - SECURITY.md adds this output to the residual list of the Agent Process Isolation paragraph, with what the start-up step does and does not cover. Norma is unchanged. Tests: TestPrepareNormaTaskDir (created when absent, kept when there, symlink and file refused, nothing created through the symlink); root-only TestNormaTaskDirAsRoot (PrepareExecPrivilege leaves <TMPDIR>/norma root:root 0755 in a sticky world-writable directory, and an executor process can neither add an entry nor rename it).
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. 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.
|
||||
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, task archive packaging (`lstat` then open by path), and the background-task output that Norma writes inside the server under `os.TempDir()/norma` (`<tmp>/norma/<session>/tasks/`), a path ARTEX does not choose: with the drop active the server creates that directory root:root 0755 at start-up, before any agent process runs, and refuses to start when it is a symlink or not a directory, but a directory already there under another owner is used as found, with a warning in the log. 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
|
||||
|
||||
|
||||
@@ -241,6 +241,82 @@ func TestExecutorMCPStdioAsRoot(t *testing.T) {
|
||||
f.check(t, "mcp stdio", string(out))
|
||||
}
|
||||
|
||||
// The directory of Norma's background-task output is created when absent. A symlink
|
||||
// or a file in its place is refused, and nothing is created through the symlink.
|
||||
// Runs without root.
|
||||
func TestPrepareNormaTaskDir(t *testing.T) {
|
||||
old := syscall.Umask(0o022)
|
||||
t.Cleanup(func() { syscall.Umask(old) })
|
||||
base := t.TempDir()
|
||||
dir := filepath.Join(base, "norma")
|
||||
for _, state := range []string{"absent", "already there"} {
|
||||
if err := prepareNormaTaskDir(dir); err != nil {
|
||||
t.Fatalf("%s: %v", state, err)
|
||||
}
|
||||
if fi, err := os.Lstat(dir); err != nil || !fi.IsDir() || fi.Mode().Perm() != 0o755 {
|
||||
t.Fatalf("%s: %v %v, want a 0755 directory", state, fi, err)
|
||||
}
|
||||
}
|
||||
outside := t.TempDir()
|
||||
link := filepath.Join(base, "link")
|
||||
if err := os.Symlink(outside, link); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := prepareNormaTaskDir(link); err == nil {
|
||||
t.Fatal("a symlink to a directory was accepted")
|
||||
}
|
||||
if entries, err := os.ReadDir(outside); err != nil || len(entries) != 0 {
|
||||
t.Fatalf("the symlink target holds %v (%v), want it empty", entries, err)
|
||||
}
|
||||
file := filepath.Join(base, "file")
|
||||
if err := os.WriteFile(file, nil, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := prepareNormaTaskDir(file); err == nil {
|
||||
t.Fatal("a file was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
fi, err := os.Lstat(filepath.Join(tmp, "norma"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if st := fi.Sys().(*syscall.Stat_t); !fi.IsDir() || st.Uid != 0 || st.Gid != 0 || fi.Mode().Perm() != 0o755 {
|
||||
t.Fatalf("%s/norma: mode %v uid %d gid %d, want a directory 0755 root:root", tmp, fi.Mode(), st.Uid, st.Gid)
|
||||
}
|
||||
c := exec.Command("sh", "-c", `cd "$1" || exit 1
|
||||
mkdir norma/x 2>/dev/null || echo mkdir=denied
|
||||
ln -s / norma/l 2>/dev/null || echo symlink=denied
|
||||
mv norma moved 2>/dev/null || echo rename=denied
|
||||
id -u`, "sh", tmp)
|
||||
applyExecCredential(c, f.priv)
|
||||
out, err := c.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("%v\n%s", err, out)
|
||||
}
|
||||
want := fmt.Sprintf("mkdir=denied\nsymlink=denied\nrename=denied\n%d\n", DefaultExecUID)
|
||||
if string(out) != want {
|
||||
t.Fatalf("executor probe in the temp directory:\n%s\nwant:\n%s", out, want)
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
|
||||
+31
-1
@@ -125,6 +125,32 @@ func prepareExecScriptDir(p ExecPrivilege, dataDir string) (string, error) {
|
||||
return dir, nil
|
||||
}
|
||||
|
||||
// prepareNormaTaskDir creates dir, the directory Norma keeps background-task output
|
||||
// in: os.TempDir()/norma, a path ARTEX does not choose. Norma creates it with the
|
||||
// first session and writes <session>/tasks/task_<n>.output below it from the server
|
||||
// process. The temp directory is world-writable: an executor process that runs
|
||||
// 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 {
|
||||
if err := os.Mkdir(dir, 0o755); err != nil && !errors.Is(err, fs.ErrExist) {
|
||||
return err
|
||||
}
|
||||
fi, err := os.Lstat(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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 {
|
||||
@@ -150,7 +176,8 @@ func prepareExecScript(p ExecPrivilege, path string) error {
|
||||
// config file (which can hold the database DSN) are restricted to root. Nothing
|
||||
// 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.
|
||||
// directory it creates, and creates the directory of Norma's background-task output
|
||||
// under the temp directory (prepareNormaTaskDir).
|
||||
func PrepareExecPrivilege(p *ExecPrivilege, dataDir, keyDir, configPath string) error {
|
||||
if !p.Enabled {
|
||||
return nil
|
||||
@@ -172,6 +199,9 @@ 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 {
|
||||
return fmt.Errorf("prepare the background-task output directory: %w", err)
|
||||
}
|
||||
tasks := filepath.Join(dataDir, "tasks")
|
||||
for _, name := range []string{"jwt.key", "secret.key"} {
|
||||
if keyDir == "" {
|
||||
|
||||
Reference in New Issue
Block a user