Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions server/isolation_root_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"syscall"
"testing"
"time"
)

// isolationRootFixture is newExecRootFixture with the Bash shim installed, a key
Expand Down Expand Up @@ -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")
}
}

Expand Down
45 changes: 29 additions & 16 deletions server/isolation_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,30 +226,44 @@ 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.
// 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 openErr error
var failure string
if err := agent.RunAsFileToolIdentity(func() {
f, err := os.Open(path)
if err == nil {
f.Close()
}
openErr = err
failure = secretOpenFailure("the file tools", path)
}); err != nil {
failures = append(failures, fmt.Sprintf("the file tool identity cannot be applied: %v", err))
break
}
if openErr == nil {
failures = append(failures, fmt.Sprintf("the file tools can read %s", path))
if failure != "" {
failures = append(failures, failure)
}
}
return failures
Expand Down Expand Up @@ -318,9 +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 f, err := os.Open(path); err == nil {
f.Close()
fail("Bash can read %s", path)
if failure := secretOpenFailure("Bash", path); failure != "" {
failures = append(failures, failure)
}
}
if spec.ServerPID > 0 {
Expand Down
12 changes: 11 additions & 1 deletion server/isolation_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,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"`,
Expand All @@ -114,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")
Expand Down
Loading