Commit Graph
7 Commits
Author SHA1 Message Date
BaaRai f483ecec83 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.
2026-11-20 06:36:48 +01:00
BaaRai 992fcf7030 security(server): refuse to start when the executor can write the Norma temp directory 2026-11-20 06:36:48 +01:00
BaaRai ebb90199c5 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).
2026-11-20 05:35:57 +01:00
BaaRai 7520c6e588 security(agent): command-output spills go to a root-owned per-task dir
Norma's tool.Capture writes an oversized tool output to a file in
ToolOutputDir, from the server process. ARTEX passed <run dir>/cmd-output:
the executor can write in the run directory and could turn cmd-output into
a symlink, so root wrote the spill where the link pointed.

- With the privilege drop on, ToolOutputDir is <data>/spill/tasks/<id> for
  the worker, the main agent and the planner of a task, and
  <data>/spill/sessions/<id> for a chat. The server creates it when the
  run starts (agent.SetSpillDirMaker, same style as SetRunDirMaker):
  root:<exec gid> 0750 below root:root 0755 parents, each level through a
  descriptor opened with O_NOFOLLOW. A directory that cannot be created
  leaves the run without a spill directory (output is truncated), never
  with the run directory. Without the drop the path stays
  <run dir>/cmd-output.
- Norma v0.4.11 writes the spill file 0600 (directory 0700), so the
  executor could not read it. The runs wrap their hooks (agent.SpillHooks):
  after a tool call whose result carries the spill marker, the server makes
  the files of the directory root:<exec gid> 0640. The agent reads a spill
  with Bash; the file tools do not reach outside the run directory.
- The spill directory follows the task: stageTaskFiles moves it to the
  delete staging with tasks/<id>, stageTaskArchiveFiles packs it as
  files/spill/tasks/<id>, and installTaskArchiveFiles restores it and puts
  its ownership back. Deleting a conversation removes
  spill/sessions/conv-<id>.

Tests: TestToolOutputDir (agent); TestDeleteTaskFilesRemovesSpillDir,
TestRemoveConversationSpill and the archive round trip (server); root-only
TestSpillDirAsRoot, and the spill directory in
TestTaskArchiveRestoreRunDirOwnershipAsRoot. SECURITY.md drops the spill
from the residual list.
2026-11-20 05:35:55 +01:00
BaaRai e73cdeda93 security(agent): share only run-dir leaves; main agent gets its own leaf
The task and chat workspace trees were shared whole with the executor
group, and the main agent and the planner worked in tasks/<id>, the parent
of every worker directory: the executor could rename, remove or replace a
run directory, its own included, and plant entries beside them.

- Layout: 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>. With the privilege drop on,
  tasks/, sessions/ and tasks/<id> are root:root 0755 and only the leaves
  are root:<exec gid> 2775.
- PrepareExecPrivilege no longer shares tasks/ and sessions/. It sets the
  parents, shares the leaves found on disk with what they hold, and
  registers the run-directory maker (agent.SetRunDirMaker). ensureRunDir
  and the chat session directory go through it: each level is created
  with Mkdir and set through a descriptor opened with O_NOFOLLOW, parents
  before the leaf, so a link planted where a run directory goes is
  refused. Without the drop both stay a plain MkdirAll.
- Uploads for the main agent land in tasks/<id>/main/uploads, and the
  attachment paths given to it use the same directory; session uploads
  create sessions/<id> by the same rule.
- Task archives still pack and delete the whole task directory, so the
  package holds files/tasks/<id>/main and i<N>. The package keeps neither
  owners nor directory modes: the restore puts the layout back
  (installTaskArchiveFiles takes the privilege).
- Existing data is not migrated. Files a task wrote directly in
  tasks/<id>/ stay there with their owner and mode; the main agent starts
  in an empty tasks/<id>/main and its file tools no longer reach them.

Tests: TestRunDirLayout and TestIsRunDirName (agent); root-only
TestRunDirLayoutAsRoot, TestRunDirSiblingsProtectedAsRoot,
TestRunDirLayoutOnExistingTreeAsRoot and
TestTaskArchiveRestoreRunDirOwnershipAsRoot (server). The archive tests
use the main/ and i<N> leaves. SECURITY.md states the invariant, names the
race that remains and drops ensureRunDir from the residual list.
2026-11-20 05:35:55 +01:00
BaaRai c1c1fe9141 security(agent): confine ingest_bbot; write tool scripts outside the run dir
Two root file operations took a path the agent controls.

- ingest_bbot opened the path it was given with os.Open in the server
  process, which is root in the image: any root-readable file could be
  named, and the result counts told its line structure. The tool now opens
  the file through the file tools' confinement (openWorkRoot on the run's
  working directory under the data directory, then os.Root): an absolute
  path elsewhere, "..", a symlink, a hard-linked file and a run directory
  reached by a symlink are refused. It fails closed without a data
  directory or a working directory. The worker passes its data directory
  to the ToolSet (SetWorkBase); the tool reads the ToolContext for the
  working directory, like ingest_scan_output.
- execPython wrote the script of a custom tool as root to
  <run dir>/.tools and then chowned it. The executor can write the run
  directory, so it could turn .tools into a symlink and have root create
  and chown a file elsewhere. With the privilege drop on, the script now
  goes to <data>/exec-scripts, created by PrepareExecPrivilege as
  root:<exec gid> 0750, outside tasks/ and sessions/: the executor reads
  the 0640 script and cannot create, rename or replace an entry. An
  enabled drop without that directory is an error, not a fallback to the
  run directory. Without the drop nothing changes. PrepareExecPrivilege
  takes the privilege by pointer to record ExecPrivilege.ScriptDir.

Tests: TestOpenBbotOutputConfined and TestIngestBbotToolConfined (no
Postgres), TestExecPythonNeedsScriptDirWhenDropping, and the root-only
TestExecPythonScriptDirAsRoot (.tools planted as a symlink to a root-only
directory). TestIngestBbotScopeSplit now places its file in a run
directory. SECURITY.md: the two paths leave the residual list; the
command-output spill stays.
2026-11-20 05:35:54 +01:00
BaaRai eb5d3d73c1 security(agent): confine the file tools and close the executor review
The SDK file tools Read, Write, Edit, MultiEdit, LS, Glob and Grep run in
the server process, which is root in the image, and took any absolute or
relative path: Read returned /app/keys/jwt.key or the config file, and
Write could plant a skill or rewrite the config past the approval gate.

- agent/filetools.go: defaultTools replaces the seven tools for every
  agent kind (chat, main agent, planner, worker). Same names, prompts and
  schemas; every path goes through an os.Root on the run's working
  directory, opened component by component below the data directory, so
  absolute paths elsewhere, "..", symlinks leading out, a run directory
  swapped for a symlink and files with extra hard links are refused. Grep
  no longer starts ripgrep. The LLM connection test passes an empty tool
  list instead of nil (nil meant the unconfined defaults).
- traffic: chmod the go-mitmproxy CA private key to 0600 after the proxy
  creates or loads it; the CA certificate stays readable.
- server: the executor gets HOME and XDG_CACHE_HOME in <data>/exec-home,
  owned by it, on the bash shim, the MCP drop shim and the custom-tool
  credential path.
- server: ARTEX_LLM_PROXY and ARTEX_LLM_BASE_URL can carry user:pass@;
  they are scrubbed and sealed with the other startup secrets.
- docker: no-new-privileges for the artex service, setuid/setgid bits
  stripped in the image, PLAYWRIGHT_BROWSERS_PATH kept at root's cache.
- db: the persistence approval seed inserts each rule only when no rule
  of that name exists, so a re-run does not duplicate rules.
- Root-gated tests run the bash shim, a custom tool and an MCP stdio
  command as the executor and check the uid, a 0600 root file, the
  server's /proc environ and a cache write.
2026-11-20 05:35:54 +01:00