Skip to content

Run agent processes under the executor identity with a sealed environment - #478

Merged
BaaRai merged 11 commits into
mainfrom
feat/agent-uid-isolation-rebased
Nov 23, 2026
Merged

BaaRai merged 11 commits into
mainfrom
feat/agent-uid-isolation-rebased

Conversation

@BaaRai

@BaaRai BaaRai commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Summary

Extends agent process isolation on top of main (565f448). Agent processes (Bash, the interactive shell, custom tools, stdio MCP servers and the file tools) run under the executor uid and gid (default 10001), with a fixed environment and a startup self-test that fails closed. Branch head: 60a1ffc (tree 6a0ede0b).

Includes #505 by @jamesKhor: the run-directory sharing walk now opens each entry relative to its already-open parent with O_NOFOLLOW and changes ownership and mode through that descriptor, and refuses regular files with another hard link. It also drops the LC_ prefix from the environment allowlist, so only the fixed POSIX locale names cross.

Changes and controls

  1. Executor identity for file tools. Read, Write, Edit, MultiEdit, LS, Glob and Grep run as the executor identity on a thread locked to it, in addition to the existing os.Root confinement to the run directory.
  2. Environment allowlist. Agent processes receive a fixed set of variable names. Server secrets (the database DSN, the secret key, LLM API keys, LLM proxy and base URLs, archive storage credentials) are not in their environment.
  3. Startup self-test, fail closed. On start the server checks that agent processes run as the executor and cannot reach the server's secrets. If isolation cannot be built, the server starts in degraded mode and /health reports isolation.mode and isolation.reason. -dev-insecure-no-isolation is the explicit opt-out.
  4. Root-owned keys, config and data. The JWT and secret keys, the configuration file and the MITM CA key are root-only.
  5. Skill trees not writable by the executor. The self-test fails if a skill tree is writable by the executor.
  6. Database host probing. The self-test probes every database host in the DSN. docker-compose.yml uses scram-sha-256 for TCP connections to PostgreSQL.
  7. Run-directory sharing race closed. The executor can replace entries while a restore shares the tree. Ownership and mode now go through opened descriptors, a symlink is never followed, and a hard-linked file alias is refused, so the walk cannot reach a root-only file outside the run directory.

Verification

Run on the tested tree (head 303acd7, tree d1272e01), rebased on main 565f448, against PostgreSQL 16 with ARTEX_PG_DSN set.

  • gofmt clean; go build for linux, windows and darwin; go vet clean.
  • Non-root run (go test ./...): 1748 PASS, 0 FAIL, 33 SKIP.
  • Root run (21 packages, *AsRoot tests run as root): 1776 PASS, 0 FAIL, 5 SKIP. All 30 *AsRoot tests ran and passed.
  • Skips in both runs: the two tests known to fail on a fresh database (Add CI: go build/vet/tests with postgres + web typecheck and static build #9), and TestLiveContextReview, which needs an external model configuration. The root run also skips two tests that expect a non-root process.
  • After Fix executor run-directory permission race in #478 #505 and the locale fix (60a1ffc): go test ./server as root from the package directory with an executable test binary: PASS, including TestShareWithExecGroupCannotChmodOutsideAsRoot, TestShareWithExecGroupRejectsHardLinkAsRoot and TestExecutorEnvAllowlist with an LC_REVIEW_SECRET value that no longer crosses. The full module go test -race runs in CI on this head.
  • Container check, using an image built from the tested tree (head 303acd7, tree d1272e01) with the repository's Dockerfile and compose file:
    • The self-test passes on first start and on restart; /health reports isolation.mode isolated.
    • Bash, a custom tool, a stdio MCP server and the file tools run as uid 10001.
    • The file tools refuse paths outside the run directory.
    • From the executor shell, the keys, the MITM CA key and /proc/1/environ are refused.
    • Playwright runs headless under the allowlist.
  • Real-model gate: @jamesKhor ran a live ARTEX instance at 303acd7 with local Ollama qwen3:1.7b. The startup self-test passed, the model made real shell_open tool calls, and the captured shell output showed gid 10001 inside the confined run directory. A wrong synthetic database password was refused; keys and config were root-owned with mode 0600. Coverage limit: the model did not poll its first asynchronous id -u session, so the numeric uid was covered by the root tests and the live gid output, not by that call.

Not covered

  • A database connection probe from the executor shell (the image has no psql; the self-test performs that probe itself).

The confined file tools (Read, Write, Edit, MultiEdit, LS, Glob, Grep) and
the opens of ingest_bbot and ingest_scan_output ran inside the server, as
root, and relied on the os.Root path confinement alone. With the executor
privilege drop active, PrepareExecPrivilege now gives them the executor
uid and gid: each call runs on a thread of its own whose file system uid,
file system gid and supplementary groups are the executor's (setfsuid,
setfsgid and the per-thread setgroups of x/sys/unix). The thread is never
unlocked, so the runtime ends it with the call and no other goroutine
runs with those ids. The kernel then checks every open, create and
directory read against the rights of the agent's shell, on top of the
path confinement. Output over the cap is still spilled, after the call,
with the server's identity.

Linux only. Elsewhere the identity is not set and a log line says the
file tools keep the server's identity. golang.org/x/sys becomes a direct
dependency (already in the module graph at the same version).
An agent process inherited the server's environment minus the names that
ScrubSecretEnv removes: a deny list, so a credential under any other name
(one an operator or an image adds) reached the agent's shell, its custom
tools and its MCP stdio servers. When a process crosses from the server's
identity to the executor's, its environment is now rebuilt from a fixed
allowlist: the search path and locale, the temp directory, the
non-interactive defaults, the proxy and CA trust variables the agents pass
to their shells, PLAYWRIGHT_BROWSERS_PATH and the shim's own variables.
The Bash shim and the MCP drop shim rebuild it before the drop; a custom
tool starts from it. What the caller passes on purpose still crosses: the
agent's Bash env, the TOOL_* parameters, and the configured env of an MCP
server, which the drop shim keeps because connectMCP names it in
ARTEX_EXEC_KEEP. A nested shell is not filtered again.

The startup scrub and the environment seal stay as they are; they are no
longer the boundary for agent processes. With the drop disabled nothing
changes.
When the privilege drop could not be applied (the server not root, or
-exec-uid negative), the start went on with one log line, and nothing
checked that an applied drop really isolated the agent. Before the stores
open, the server now runs a self-test: a probe started the way the
agent's Bash starts ("bash" on PATH, so through the shim) checks from
inside the executor process that it runs as the executor uid, gid and
only group, cannot read jwt.key, secret.key, the config file or the MITM
CA key, cannot read the server's /proc environ or memory, sees only the
allowlisted environment, and cannot open a session on the server's
database with the server's user and no password (only host, port, user
and database name reach the probe). The file tools are checked too: the
secrets must not open through their identity.

A failed or impossible self-test stops the start. The development escape
-dev-insecure-no-isolation, off by default, lets the start go on and logs
a banner that says what is exposed. This changes the behaviour of a
non-root start (go run, the systemd example unit, Windows, macOS): it now
needs that flag.
PrepareExecPrivilege chmodded jwt.key, secret.key and the config file to
0600 but left their owner as it was: a file owned by the executor uid
(created by an agent process, or on the host under the uid the executor
has in the container) stayed readable by the agent. It now makes root
their owner first.

The startup self-test also checks the directories: the probe, as the
executor, must not be able to create an entry in the data directory, the
key directory or the config file's directory (it could plant a key, a
config or a data tree for the next start), and none of their parents may
let the executor replace them (owned by it, or writable by it without the
sticky bit). For the database, a test against PostgreSQL shows the
server's role refused without its password (SQLSTATE 28P01); the probe
already fails the start when the database lets that role in.

No Unix socket is introduced: in the compose deployment PostgreSQL runs
in its own container and is reached over TCP with a password that the
executor cannot read (sealed environment, root-only config).
The six platform tools that durably change skills, custom tools and MCP
servers already need operator approval (intercept/persistence.go, the
built-in ask rules of db/intercept_persistence_seed.go, guard.go). Custom
tools and MCP servers live in the database, which the agent's shell
cannot reach. Skills live on disk: an agent process that can write the
skill directory adds or changes a skill without create_skill or
update_skill_file, and every agent then loads it as instructions.

The startup self-test now checks the skill directory: the probe, as the
executor, must not be able to create an entry in it, its parent must not
let the executor replace it, and no file or directory below it may be
owned by the executor uid or writable by the executor gid or by others
(judged from owners and mode bits, symlinks by their target, nothing
written).
The new root tests hold a file tool call open and check, at that moment,
that only the call's thread has file system uid 10001: the caller, 32
goroutines locked on other threads and every other thread listed in
/proc keep uid 0, root's groups and root's access, and goroutines the
call starts do not inherit the ids. A panic and runtime.Goexit inside
the call reach the caller and end the thread instead of returning it
to the runtime. Calls under load with live and cancelled contexts never
show the executor's ids on an observer thread, and no thread keeps
them afterwards.

Both mutations that would leak the identity are caught: unlocking the
thread without restoring its ids, and a process-wide setgroups.

The comment of runWithFileIdentity now states the limits: the ids cover
fn's goroutine only, and fn must not start goroutines or programs.
The isolation self-test stopped every start it failed, so a non-root
server (the systemd unit), dev.sh, Windows and macOS no longer started
without -dev-insecure-no-isolation.

EnforceIsolation now decides from the platform and the privilege drop:

- self-test passed: isolated;
- Linux with the privilege drop on (root, the Docker image): a failure
  stops the start, unless -dev-insecure-no-isolation is set;
- elsewhere (not Linux, not root, -exec-uid=-1): the server starts
  degraded, without a flag.

A degraded start logs a banner with the self-test failure and what is
exposed, and GET /api/health reports the isolation status to a
signed-in caller; the web UI shows a red banner on every page while it
is degraded. The systemd deployment guide and dev.sh describe the mode.
The self-test probe tried only the first host of the DSN. It now tries
each host and port the DSN lists, fallbacks included, and nothing else,
and a DSN it cannot parse fails the self-test instead of skipping the
database check.

docker-compose.yml sets POSTGRES_HOST_AUTH_METHOD=scram-sha-256: a TCP
connection from another container, artex's included, needs the
password. It takes effect when a new pgdata volume is initialised.
@BaaRai
BaaRai force-pushed the feat/agent-uid-isolation-rebased branch from b42bffc to 303acd7 Compare November 22, 2026 22:00
@jamesKhor

jamesKhor commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Independent re-test of PR #478 at commit 303acd75875f1efb258c4cfeb08fb737ebc00e71 on Ubuntu 24.04 under WSL2, using an isolated ARTEX instance, scratch PostgreSQL 16, synthetic credentials, and local Ollama qwen3:1.7b.

Results

  • Targeted isolation tests passed for executor UID/GID setup, environment sealing and scrubbing, denial of root-owned secrets, protected file tools, self-test failure detection, and file-tool concurrency. In the 200-call file-tool load/cancel check, all 962 observer samples retained the server's filesystem UID.
  • The live ARTEX startup logged a passing isolation self-test. The model made real shell_open tool calls in a conversation. Captured shell output included group ID 10001 and an unprivileged prompt inside the confined run directory.
  • A direct connection with the wrong synthetic database password was rejected. Test keys and config were root-owned with mode 0600.
  • No product security defect was observed. The test services and temporary data were cleaned up.

Coverage limit: The model did not poll its first asynchronous id -u shell session, so that particular numeric UID output was not captured. UID/GID behavior was covered by the passing root-run isolation tests and the live shell's group-ID output. This is a test limitation, not a claim that the live id -u check passed.

@jamesKhor jamesKhor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed a security vulnerability at head 303acd7: an executor process can race the permission-sharing walk and cause it to make a root-only file readable. I tested the production shareWithExecGroup function directly with a root process and a UID/GID 10001 child on Ubuntu 24.04 under WSL2. In five of five runs, a synthetic root-owned 0600 file outside the run directory became 0777, and the UID 10001 child read its contents. This was a focused helper-level reproduction; I did not run an end-to-end archive restore or use real secrets. The restore path calls this helper after sharing the run directory. Please fix the race before merging. A separate lower-severity allowlist observation is noted inline.

The vulnerable call is in server/execpriv_unix.go at line 565. Archive restore reaches it through shareRestoredTaskDir in server/task_archive_package.go at line 304. Use directory-descriptor-anchored no-follow traversal and descriptor-based ownership/permission changes.

Comment thread server/execenv_unix.go Outdated
}

// executorEnvPrefixes are kept by prefix: the locale categories.
var executorEnvPrefixes = []string{"LC_"}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3, conditional] This prefix admits arbitrary LC_... variable names, so the executor environment is not a fixed set of names. A synthetic LC_REVIEW_SECRET value passed executorEnv in a focused test, while OPENAI_API_KEY was filtered. I did not observe a standard ARTEX secret leaking; exposure requires a secret supplied under an LC_ name. Enumerating the locale variables actually needed would make the stated fixed-name guarantee accurate.

@jamesKhor

Copy link
Copy Markdown
Contributor

Follow-up to my changes-requested security review: I submitted the tested permission-race fix as PR #505, targeting this PR's feat/agent-uid-isolation-rebased branch.

The patch makes the run-directory sharing walk operate on opened descriptors with no-follow child opens and refuses hard-linked file aliases. The concurrent UID 10001 regression failed on this PR's original code (a synthetic root-owned 0600 file became 0777) and passes with the patch. The full non-root Go suite and root server suite passed; the latter did not use a live PostgreSQL DSN.

PR #505 is open for review and has not been merged into #478. The separate conditional LC_ environment-allowlist finding remains open.

BaaRai and others added 2 commits November 23, 2026 12:40
Fix executor run-directory permission race in #478
The executor environment kept every name starting with LC_, so a secret supplied under an LC_ name crossed the allowlist even though the comment promised a fixed set of names. The POSIX locale categories are now enumerated and the prefix rule is gone.

Found by jamesKhor in the review of #478.
@BaaRai
BaaRai merged commit a4276b4 into main Nov 23, 2026
5 checks passed
@BaaRai

BaaRai commented Nov 23, 2026

Copy link
Copy Markdown
Owner Author

Thanks @jamesKhor. Merged. Your review changed the PR before merge: the sharing walk now works on opened descriptors and refuses hard-linked aliases (#505), and only the fixed POSIX locale names cross the environment allowlist. Your local Ollama run with a real task is the model gate, and the numeric uid coverage limit you noted is recorded in the body.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants