From ccede2a0c74209ff17a5e1e95af47d108266cccc Mon Sep 17 00:00:00 2001 From: KKh <58800553+jamesKhor@users.noreply.github.com> Date: Mon, 30 Nov 2026 13:04:52 +0800 Subject: [PATCH 1/3] fix: verify secret reads in isolation probes --- server/isolation_read.go | 20 ++++++++++++++++++++ server/isolation_read_test.go | 31 +++++++++++++++++++++++++++++++ server/isolation_unix.go | 17 ++++++----------- server/isolation_unix_test.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 server/isolation_read.go create mode 100644 server/isolation_read_test.go diff --git a/server/isolation_read.go b/server/isolation_read.go new file mode 100644 index 000000000..4055205a6 --- /dev/null +++ b/server/isolation_read.go @@ -0,0 +1,20 @@ +package server + +import ( + "io" + "os" +) + +// isolationSecretReadable checks whether a secret's contents are accessible. +// Opening it alone may succeed even when reading is denied by the filesystem. +func isolationSecretReadable(path string) bool { + f, err := os.Open(path) + if err != nil { + return false + } + defer f.Close() + var byteRead [1]byte + n, err := f.Read(byteRead[:]) + // An empty file is accessible too; no bytes are needed to prove that. + return n > 0 || err == io.EOF +} diff --git a/server/isolation_read_test.go b/server/isolation_read_test.go new file mode 100644 index 000000000..57a1250fc --- /dev/null +++ b/server/isolation_read_test.go @@ -0,0 +1,31 @@ +package server + +import ( + "os" + "path/filepath" + "testing" +) + +func TestIsolationSecretReadableChecksContents(t *testing.T) { + dir := t.TempDir() + secret := filepath.Join(dir, "secret.key") + if err := os.WriteFile(secret, []byte("synthetic-key"), 0o600); err != nil { + t.Fatal(err) + } + if !isolationSecretReadable(secret) { + t.Fatal("a readable secret must fail isolation") + } + if isolationSecretReadable(filepath.Join(dir, "missing.key")) { + t.Fatal("a missing secret is not readable") + } + if isolationSecretReadable(dir) { + t.Fatal("opening a directory must not count as reading a secret") + } + empty := filepath.Join(dir, "empty.key") + if err := os.WriteFile(empty, nil, 0o600); err != nil { + t.Fatal(err) + } + if !isolationSecretReadable(empty) { + t.Fatal("an accessible empty secret is still readable") + } +} diff --git a/server/isolation_unix.go b/server/isolation_unix.go index 41f89fe09..a6c4b3597 100644 --- a/server/isolation_unix.go +++ b/server/isolation_unix.go @@ -226,8 +226,8 @@ func skillTreeFailures(root string, execUID, execGID uint32) []string { return failures } -// fileToolIsolationFailures opens each existing secret through the file tool -// identity (agent/filetools_identity.go): the open must be refused. +// fileToolIsolationFailures reads each existing secret through the file tool +// identity (agent/filetools_identity.go): reading must be refused. func fileToolIsolationFailures(secrets []string) []string { if !agent.FileToolIdentitySupported() { return []string{"the agent's file tools run with the server's identity on this platform"} @@ -237,18 +237,14 @@ func fileToolIsolationFailures(secrets []string) []string { if _, err := os.Stat(path); err != nil { continue } - var openErr error + var readable bool if err := agent.RunAsFileToolIdentity(func() { - f, err := os.Open(path) - if err == nil { - f.Close() - } - openErr = err + readable = isolationSecretReadable(path) }); err != nil { failures = append(failures, fmt.Sprintf("the file tool identity cannot be applied: %v", err)) break } - if openErr == nil { + if readable { failures = append(failures, fmt.Sprintf("the file tools can read %s", path)) } } @@ -318,8 +314,7 @@ func checkIsolationAsExecutor(spec isolationProbeSpec) []string { fail("Bash has the supplementary groups %v, want only the executor gid %d", groups, spec.GID) } for _, path := range spec.Unreadable { - if f, err := os.Open(path); err == nil { - f.Close() + if isolationSecretReadable(path) { fail("Bash can read %s", path) } } diff --git a/server/isolation_unix_test.go b/server/isolation_unix_test.go index 8f0a0cb8b..0ecc915e9 100644 --- a/server/isolation_unix_test.go +++ b/server/isolation_unix_test.go @@ -77,6 +77,36 @@ func hasFailure(failures []string, sub string) bool { return false } +// Opening a path is not proof that its contents can be read. A directory is an +// example of a path that opens successfully but refuses a byte read. Both agent +// probes must check a real read before reporting a leaked secret. +func TestIsolationSecretProbesRequireRead(t *testing.T) { + dir := t.TempDir() + secret := filepath.Join(dir, "readable.key") + if err := os.WriteFile(secret, []byte("synthetic-key"), 0o600); err != nil { + t.Fatal(err) + } + for _, probe := range []struct { + name string + run func([]string) []string + }{ + {"file tools", fileToolIsolationFailures}, + {"Bash", func(paths []string) []string { + return checkIsolationAsExecutor(isolationProbeSpec{UID: uint32(os.Getuid()), GID: uint32(os.Getgid()), Unreadable: paths}) + }}, + } { + t.Run(probe.name, func(t *testing.T) { + failures := probe.run([]string{dir, secret}) + if !hasFailure(failures, "can read "+secret) { + t.Errorf("readable secret was missed: %q", failures) + } + if hasFailure(failures, "can read "+dir) { + t.Errorf("openable but unreadable directory was reported as readable: %q", failures) + } + }) + } +} + // The probe's checks, run in the test process (not as the executor): each kind of // exposure is reported. The process can read the file, write the directory, carries // a variable outside the allowlist, is not the executor and reaches a database that From e941e7fa85d751fb0ce15113f79bb7b8a821536e Mon Sep 17 00:00:00 2001 From: KKh <58800553+jamesKhor@users.noreply.github.com> Date: Mon, 30 Nov 2026 13:17:59 +0800 Subject: [PATCH 2/3] test: match exact isolation failure paths --- server/isolation_unix_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/isolation_unix_test.go b/server/isolation_unix_test.go index 0ecc915e9..407e30588 100644 --- a/server/isolation_unix_test.go +++ b/server/isolation_unix_test.go @@ -10,6 +10,7 @@ import ( "net" "os" "path/filepath" + "slices" "strings" "testing" @@ -100,7 +101,7 @@ func TestIsolationSecretProbesRequireRead(t *testing.T) { if !hasFailure(failures, "can read "+secret) { t.Errorf("readable secret was missed: %q", failures) } - if hasFailure(failures, "can read "+dir) { + if slices.Contains(failures, "the file tools can read "+dir) || slices.Contains(failures, "Bash can read "+dir) { t.Errorf("openable but unreadable directory was reported as readable: %q", failures) } }) From c3dfa3cf27b42419e89c93a56c65aae04b969ff9 Mon Sep 17 00:00:00 2001 From: KKh <58800553+jamesKhor@users.noreply.github.com> Date: Mon, 30 Nov 2026 13:31:03 +0800 Subject: [PATCH 3/3] Report startup secret-open evidence under each probe identity --- server/isolation_read.go | 20 -------------- server/isolation_read_test.go | 31 --------------------- server/isolation_root_linux_test.go | 32 +++++++++++++++++++-- server/isolation_unix.go | 40 +++++++++++++++++++-------- server/isolation_unix_test.go | 43 ++++++++--------------------- 5 files changed, 70 insertions(+), 96 deletions(-) delete mode 100644 server/isolation_read.go delete mode 100644 server/isolation_read_test.go diff --git a/server/isolation_read.go b/server/isolation_read.go deleted file mode 100644 index 4055205a6..000000000 --- a/server/isolation_read.go +++ /dev/null @@ -1,20 +0,0 @@ -package server - -import ( - "io" - "os" -) - -// isolationSecretReadable checks whether a secret's contents are accessible. -// Opening it alone may succeed even when reading is denied by the filesystem. -func isolationSecretReadable(path string) bool { - f, err := os.Open(path) - if err != nil { - return false - } - defer f.Close() - var byteRead [1]byte - n, err := f.Read(byteRead[:]) - // An empty file is accessible too; no bytes are needed to prove that. - return n > 0 || err == io.EOF -} diff --git a/server/isolation_read_test.go b/server/isolation_read_test.go deleted file mode 100644 index 57a1250fc..000000000 --- a/server/isolation_read_test.go +++ /dev/null @@ -1,31 +0,0 @@ -package server - -import ( - "os" - "path/filepath" - "testing" -) - -func TestIsolationSecretReadableChecksContents(t *testing.T) { - dir := t.TempDir() - secret := filepath.Join(dir, "secret.key") - if err := os.WriteFile(secret, []byte("synthetic-key"), 0o600); err != nil { - t.Fatal(err) - } - if !isolationSecretReadable(secret) { - t.Fatal("a readable secret must fail isolation") - } - if isolationSecretReadable(filepath.Join(dir, "missing.key")) { - t.Fatal("a missing secret is not readable") - } - if isolationSecretReadable(dir) { - t.Fatal("opening a directory must not count as reading a secret") - } - empty := filepath.Join(dir, "empty.key") - if err := os.WriteFile(empty, nil, 0o600); err != nil { - t.Fatal(err) - } - if !isolationSecretReadable(empty) { - t.Fatal("an accessible empty secret is still readable") - } -} diff --git a/server/isolation_root_linux_test.go b/server/isolation_root_linux_test.go index dd5cb6d7f..942ccc5ad 100644 --- a/server/isolation_root_linux_test.go +++ b/server/isolation_root_linux_test.go @@ -10,6 +10,7 @@ import ( "strings" "syscall" "testing" + "time" ) // isolationRootFixture is newExecRootFixture with the Bash shim installed, a key @@ -73,9 +74,36 @@ func TestIsolationSelfTestDetectsReadableSecretAsRoot(t *testing.T) { if err := os.Chmod(key, 0o644); err != nil { t.Fatal(err) } + var st syscall.Stat_t + if err := syscall.Stat(key, &st); err != nil { + t.Fatal(err) + } err := IsolationSelfTest(f.priv, f.targets()) - if err == nil || !strings.Contains(err.Error(), "Bash can read "+key) || !strings.Contains(err.Error(), "the file tools can read "+key) { - t.Fatalf("self-test with a world-readable key: %v", err) + if err == nil { + t.Fatal("self-test accepted a world-readable key") + } + for _, actor := range []string{"Bash", "the file tools"} { + if !strings.Contains(err.Error(), actor+" opened "+key+" for reading (mode=-rw-r--r-- uid=0 gid="+strconv.FormatUint(uint64(st.Gid), 10)+")") { + t.Errorf("self-test with a world-readable key did not report its metadata for %s: %v", actor, err) + } + } +} + +func TestSecretOpenFailureOnFIFO(t *testing.T) { + path := filepath.Join(t.TempDir(), "key") + if err := syscall.Mkfifo(path, 0o600); err != nil { + t.Fatal(err) + } + // A FIFO with no writer must still be reported without waiting for bytes. + done := make(chan string, 1) + go func() { done <- secretOpenFailure("probe", path) }() + select { + case msg := <-done: + if !strings.Contains(msg, "opened "+path+" for reading (mode=p") { + t.Fatalf("unexpected FIFO diagnostic: %q", msg) + } + case <-time.After(5 * time.Second): + t.Fatal("probe blocked on a FIFO with no writer") } } diff --git a/server/isolation_unix.go b/server/isolation_unix.go index a6c4b3597..dabeba092 100644 --- a/server/isolation_unix.go +++ b/server/isolation_unix.go @@ -226,26 +226,44 @@ func skillTreeFailures(root string, execUID, execGID uint32) []string { return failures } -// fileToolIsolationFailures reads each existing secret through the file tool -// identity (agent/filetools_identity.go): reading must be refused. +// secretOpenFailure checks the open under the caller's identity. Inspect the +// descriptor so the diagnostic describes the object at the time of the probe. +func secretOpenFailure(actor, path string) string { + f, err := os.OpenFile(path, os.O_RDONLY|syscall.O_NONBLOCK|syscall.O_NOCTTY, 0) + if errors.Is(err, fs.ErrNotExist) || errors.Is(err, fs.ErrPermission) { + return "" + } + if err != nil { + return fmt.Sprintf("%s could not check %s: %v", actor, path, err) + } + defer f.Close() + msg := fmt.Sprintf("%s opened %s for reading", actor, path) + info, err := f.Stat() + if err != nil { + return fmt.Sprintf("%s (stat failed: %v)", msg, err) + } + if st, ok := info.Sys().(*syscall.Stat_t); ok { + return fmt.Sprintf("%s (mode=%s uid=%d gid=%d)", msg, info.Mode(), st.Uid, st.Gid) + } + return fmt.Sprintf("%s (mode=%s)", msg, info.Mode()) +} + +// fileToolIsolationFailures checks each secret through the file tool identity. func fileToolIsolationFailures(secrets []string) []string { if !agent.FileToolIdentitySupported() { return []string{"the agent's file tools run with the server's identity on this platform"} } var failures []string for _, path := range secrets { - if _, err := os.Stat(path); err != nil { - continue - } - var readable bool + var failure string if err := agent.RunAsFileToolIdentity(func() { - readable = isolationSecretReadable(path) + failure = secretOpenFailure("the file tools", path) }); err != nil { failures = append(failures, fmt.Sprintf("the file tool identity cannot be applied: %v", err)) break } - if readable { - failures = append(failures, fmt.Sprintf("the file tools can read %s", path)) + if failure != "" { + failures = append(failures, failure) } } return failures @@ -314,8 +332,8 @@ func checkIsolationAsExecutor(spec isolationProbeSpec) []string { fail("Bash has the supplementary groups %v, want only the executor gid %d", groups, spec.GID) } for _, path := range spec.Unreadable { - if isolationSecretReadable(path) { - fail("Bash can read %s", path) + if failure := secretOpenFailure("Bash", path); failure != "" { + failures = append(failures, failure) } } if spec.ServerPID > 0 { diff --git a/server/isolation_unix_test.go b/server/isolation_unix_test.go index 407e30588..dcb0bdbb6 100644 --- a/server/isolation_unix_test.go +++ b/server/isolation_unix_test.go @@ -10,7 +10,6 @@ import ( "net" "os" "path/filepath" - "slices" "strings" "testing" @@ -78,36 +77,6 @@ func hasFailure(failures []string, sub string) bool { return false } -// Opening a path is not proof that its contents can be read. A directory is an -// example of a path that opens successfully but refuses a byte read. Both agent -// probes must check a real read before reporting a leaked secret. -func TestIsolationSecretProbesRequireRead(t *testing.T) { - dir := t.TempDir() - secret := filepath.Join(dir, "readable.key") - if err := os.WriteFile(secret, []byte("synthetic-key"), 0o600); err != nil { - t.Fatal(err) - } - for _, probe := range []struct { - name string - run func([]string) []string - }{ - {"file tools", fileToolIsolationFailures}, - {"Bash", func(paths []string) []string { - return checkIsolationAsExecutor(isolationProbeSpec{UID: uint32(os.Getuid()), GID: uint32(os.Getgid()), Unreadable: paths}) - }}, - } { - t.Run(probe.name, func(t *testing.T) { - failures := probe.run([]string{dir, secret}) - if !hasFailure(failures, "can read "+secret) { - t.Errorf("readable secret was missed: %q", failures) - } - if slices.Contains(failures, "the file tools can read "+dir) || slices.Contains(failures, "Bash can read "+dir) { - t.Errorf("openable but unreadable directory was reported as readable: %q", failures) - } - }) - } -} - // The probe's checks, run in the test process (not as the executor): each kind of // exposure is reported. The process can read the file, write the directory, carries // a variable outside the allowlist, is not the executor and reaches a database that @@ -128,7 +97,7 @@ func TestCheckIsolationReportsEachExposure(t *testing.T) { }) for _, want := range []string{ "not the executor uid 10001", - "Bash can read " + secret, + "Bash opened " + secret + " for reading (mode=-rw-------", "Bash can create files in " + dir, "ARTEX_ISOLATION_CANARY reached Bash", `database session as "artex"`, @@ -145,6 +114,16 @@ func TestCheckIsolationReportsEachExposure(t *testing.T) { } } +func TestSecretOpenFailureReportsUnexpectedError(t *testing.T) { + link := filepath.Join(t.TempDir(), "key") + if err := os.Symlink(link, link); err != nil { + t.Fatal(err) + } + if msg := secretOpenFailure("probe", link); !strings.HasPrefix(msg, "probe could not check "+link) { + t.Fatalf("unexpected diagnostic for a symlink loop: %q", msg) + } +} + // A database that asks for a password, or none listening, is not an exposure. func TestConnectWithoutPasswordRefused(t *testing.T) { ln, err := net.Listen("tcp", "127.0.0.1:0")