server: fail the isolation self-test on a skill tree the agent can write

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).
This commit is contained in:
BaaRai
2026-11-22 09:50:09 +01:00
parent 7f145965b2
commit fd1ccb2f6c
3 changed files with 142 additions and 1 deletions
+43
View File
@@ -183,6 +183,49 @@ func TestIsolationSelfTestDetectsReplaceableParentAsRoot(t *testing.T) {
}
}
// A skill tree an agent process can write fails the self-test: a skill written there
// directly would skip the approval of create_skill and update_skill_file. A tree
// only root can write passes.
func TestIsolationSelfTestDetectsWritableSkillsAsRoot(t *testing.T) {
f := newIsolationRootFixture(t)
skills := filepath.Join(f.tmp, "skills")
skill := filepath.Join(skills, "recon")
if err := os.MkdirAll(skill, 0o755); err != nil {
t.Fatal(err)
}
md := filepath.Join(skill, "SKILL.md")
if err := os.WriteFile(md, []byte("---\nname: recon\n---\n"), 0o644); err != nil {
t.Fatal(err)
}
tg := f.targets()
tg.SkillDir = skills
if err := IsolationSelfTest(f.priv, tg); err != nil {
t.Fatalf("self-test with a root-only skill tree: %v", err)
}
if err := os.Chown(md, DefaultExecUID, DefaultExecGID); err != nil {
t.Fatal(err)
}
err := IsolationSelfTest(f.priv, tg)
if err == nil || !strings.Contains(err.Error(), "can change the skill entry "+md) {
t.Fatalf("self-test with a skill file owned by the executor: %v", err)
}
if err := os.Chown(md, 0, 0); err != nil {
t.Fatal(err)
}
if err := os.Chmod(skills, 0o777); err != nil {
t.Fatal(err)
}
err = IsolationSelfTest(f.priv, tg)
if err == nil || !strings.Contains(err.Error(), "Bash can create files in "+skills) || !strings.Contains(err.Error(), "can change the skill entry "+skills) {
t.Fatalf("self-test with a world-writable skill directory: %v", err)
}
if entries, _ := os.ReadDir(skills); len(entries) != 1 {
t.Fatalf("the probe left an entry in the skill directory: %v", entries)
}
}
// A database that lets the server's user in without a password fails the self-test:
// the probe, as the executor, opens a session on it.
func TestIsolationSelfTestDetectsPasswordlessDatabaseAsRoot(t *testing.T) {
+46 -1
View File
@@ -66,6 +66,7 @@ func IsolationSelfTest(p ExecPrivilege, t IsolationTargets) error {
spec := isolationSpec(p, t)
failures := fileToolIsolationFailures(spec.Unreadable)
failures = append(failures, replaceableDirFailures(spec.Unwritable, p.UID, p.GID)...)
failures = append(failures, skillTreeFailures(t.SkillDir, p.UID, p.GID)...)
rep, err := runIsolationProbe(spec)
if err != nil {
failures = append(failures, fmt.Sprintf("the probe did not run through the Bash shim: %v", err))
@@ -93,7 +94,9 @@ func isolationSpec(p ExecPrivilege, t IsolationTargets) isolationProbeSpec {
// The directories of the keys, the config and the data: an agent process that
// can create an entry there can plant a key, a config or a whole data tree that
// the server uses at its next start.
for _, dir := range []string{t.DataDir, t.KeyDir, configDir(t.ConfigPath)} {
// The skill directory too: a skill an agent process writes there directly would
// skip the approval of create_skill (see skillTreeFailures).
for _, dir := range []string{t.DataDir, t.KeyDir, configDir(t.ConfigPath), t.SkillDir} {
if dir != "" && !slices.Contains(spec.Unwritable, dir) {
spec.Unwritable = append(spec.Unwritable, dir)
}
@@ -154,6 +157,48 @@ func parentReplaceable(mode fs.FileMode, uid, gid, execUID, execGID uint32) stri
return ""
}
// skillTreeMaxFailures caps what skillTreeFailures reports for one tree.
const skillTreeMaxFailures = 20
// skillTreeFailures reports each file and directory under the skill directory that
// an agent process could change: agents load skill files as instructions, so a skill
// written there directly would bypass the operator approval that create_skill and
// update_skill_file need (intercept/persistence.go). It decides from the owners and
// mode bits, without writing anything: an entry owned by the executor uid, or
// writable by the executor gid or by others, is reported. A symlink is judged by its
// target and not descended. The probe checks the directory itself for writes and
// replaceableDirFailures its parent.
func skillTreeFailures(root string, execUID, execGID uint32) []string {
if root == "" {
return nil
}
if _, err := os.Stat(root); err != nil {
return nil
}
var failures []string
_ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return nil
}
fi, err := os.Stat(path)
if err != nil {
return nil
}
st, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return nil
}
if why := normaTaskDirExecWrite(fi.Mode().Perm(), st.Uid, st.Gid, execUID, execGID); why != "" {
failures = append(failures, fmt.Sprintf("an agent process can change the skill entry %s, bypassing the skill approval: %s", path, why))
}
if len(failures) >= skillTreeMaxFailures {
return fs.SkipAll
}
return nil
})
return failures
}
// fileToolIsolationFailures opens each existing secret through the file tool
// identity (agent/filetools_identity.go): the open must be refused.
func fileToolIsolationFailures(secrets []string) []string {
+53
View File
@@ -168,6 +168,59 @@ func TestParentReplaceable(t *testing.T) {
}
}
// A skill tree is judged from owners and mode bits: an entry the executor owns, or
// can write through its group or as other, is reported; a symlink by its target.
// Runs without root: the test user stands in for the executor or for root.
func TestSkillTreeFailures(t *testing.T) {
root := t.TempDir()
skill := filepath.Join(root, "recon")
if err := os.Mkdir(skill, 0o755); err != nil {
t.Fatal(err)
}
md := filepath.Join(skill, "SKILL.md")
if err := os.WriteFile(md, []byte("---\nname: recon\n---\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Chmod(root, 0o755); err != nil {
t.Fatal(err)
}
me := uint32(os.Getuid())
other := me + 54321 // an executor that owns nothing here
if got := skillTreeFailures(root, other, other); len(got) != 0 {
t.Fatalf("a tree only its owner can write: %q", got)
}
if got := skillTreeFailures(root, me, other); len(got) != 3 || !hasFailure(got, md) {
t.Fatalf("a tree owned by the executor uid: %q, want all three entries", got)
}
if err := os.Chmod(md, 0o666); err != nil {
t.Fatal(err)
}
if got := skillTreeFailures(root, other, other); len(got) != 1 || !hasFailure(got, md) || !hasFailure(got, "writable by others") {
t.Fatalf("a world-writable skill file: %q", got)
}
if err := os.Chmod(md, 0o644); err != nil {
t.Fatal(err)
}
outside := filepath.Join(t.TempDir(), "evil.md")
if err := os.WriteFile(outside, []byte("x"), 0o666); err != nil {
t.Fatal(err)
}
if err := os.Chmod(outside, 0o666); err != nil {
t.Fatal(err)
}
link := filepath.Join(skill, "linked.md")
if err := os.Symlink(outside, link); err != nil {
t.Fatal(err)
}
if got := skillTreeFailures(root, other, other); len(got) != 1 || !hasFailure(got, link) {
t.Fatalf("a symlink to a world-writable file: %q", got)
}
if got := skillTreeFailures(filepath.Join(root, "absent"), me, me); got != nil {
t.Fatalf("a missing skill directory: %q", got)
}
}
// The spec carries the server's paths and the database name, never its password.
func TestIsolationSpecHasNoPassword(t *testing.T) {
spec := isolationSpec(ExecPrivilege{Enabled: true, UID: 10001, GID: 10002}, IsolationTargets{