Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

fix(execd): address CI findings on the hosted-/tmp hardening

高然1ffc1508c673
diff --git a/components/execd/pkg/isolation/bwrap.go b/components/execd/pkg/isolation/bwrap.go
index f8d6bd36c..9c58e57ab 100644
--- a/components/execd/pkg/isolation/bwrap.go
+++ b/components/execd/pkg/isolation/bwrap.go
@@ -80,25 +80,9 @@ func buildArgvWithLifecycle(
}
argv = append(argv, ovArgv...)
- // Hide upper roots: Dir(Dir(upper|tmpDir)) derives the operator's upper
- // root for both overlay pairs and the /tmp backing dir. Without this,
- // the ro-bind of / below would expose other sessions' host state
- // in-namespace. Dedupe keeps arbitrary caller-supplied dirs safe.
- hideRoots := distinctUpperRoots(opts.Overlays)
- if opts.TmpDir != "" {
- root := filepath.Dir(filepath.Dir(opts.TmpDir))
- known := false
- for _, r := range hideRoots {
- if r == root {
- known = true
- break
- }
- }
- if !known {
- hideRoots = append(hideRoots, root)
- }
- }
- for _, upperRoot := range hideRoots {
+ // Hide upper roots so the ro-bind of / below cannot expose other
+ // sessions' host state in-namespace.
+ for _, upperRoot := range upperHideRoots(opts.Overlays, opts.TmpDir) {
argv = append(argv, "--tmpfs", upperRoot)
}
@@ -413,6 +397,23 @@ func sortOverlaysShallowFirst(overlays []OverlaySpec) []OverlaySpec {
return ordered
}
+// upperHideRoots returns the roots to hide in-namespace: one per overlay
+// upper root plus the /tmp backing dir's derived root, deduped. Dir(Dir(
+// upper|tmpDir)) yields the operator-configured upper root for both.
+func upperHideRoots(overlays []OverlaySpec, tmpDir string) []string {
+ roots := distinctUpperRoots(overlays)
+ if tmpDir == "" {
+ return roots
+ }
+ root := filepath.Dir(filepath.Dir(tmpDir))
+ for _, r := range roots {
+ if r == root {
+ return roots
+ }
+ }
+ return append(roots, root)
+}
+
// distinctUpperRoots returns one hidden root per distinct upper root
// containing a persistent overlay upper: filepath.Dir(filepath.Dir(upper)),
// i.e. the operator-configured upper_root itself.
diff --git a/components/execd/pkg/isolation/confined_view_test.go b/components/execd/pkg/isolation/confined_view_test.go
index fd6d7c519..799215814 100644
--- a/components/execd/pkg/isolation/confined_view_test.go
+++ b/components/execd/pkg/isolation/confined_view_test.go
@@ -17,7 +17,6 @@ package isolation
import (
"os"
"path/filepath"
- "syscall"
"testing"
)
@@ -147,31 +146,6 @@ func TestConfinedView_FollowsInRootSymlink(t *testing.T) {
}
}
-func TestConfinedView_ChownsCreatedFiles(t *testing.T) {
- if os.Geteuid() != 0 {
- t.Skip("chown assertion requires root")
- }
- root, err := filepath.EvalSymlinks(t.TempDir())
- if err != nil {
- t.Fatal(err)
- }
- view := NewConfinedView(root, 0, 0)
- if err := view.WriteFile("/tmp/owned.txt", []byte("x"), 0o644); err != nil {
- t.Fatal(err)
- }
- info, err := os.Stat(filepath.Join(root, "owned.txt"))
- if err != nil {
- t.Fatal(err)
- }
- stat, ok := info.Sys().(*syscall.Stat_t)
- if !ok {
- t.Skip("stat_t unavailable on this platform")
- }
- if stat.Uid != 0 {
- t.Fatalf("file uid = %d, want 0", stat.Uid)
- }
-}
-
func TestConfinedView_RejectsRootRemoval(t *testing.T) {
view, _ := newConfinedView(t)
if err := view.Remove("/tmp"); err == nil {
diff --git a/components/execd/pkg/runtime/bwrap_test/bwrap_tmp_files_test.go b/components/execd/pkg/runtime/bwrap_test/bwrap_tmp_files_test.go
index 381cac575..13777758e 100644
--- a/components/execd/pkg/runtime/bwrap_test/bwrap_tmp_files_test.go
+++ b/components/execd/pkg/runtime/bwrap_test/bwrap_tmp_files_test.go
@@ -203,7 +203,7 @@ func TestTmpFiles_SymlinkEscapeConfined(t *testing.T) {
assert.Equal(t, "host-secret", string(got), "the host file must be untouched")
// In-root symlinks keep working, matching the namespace.
- runInSession(t, r, id, "echo local > /tmp/target.txt && ln -s target.txt /tmp/link", nil)
+ runInSession(t, r, id, "echo local > /tmp/target.txt && ln -s target.txt /tmp/link.txt", nil)
data, err := mv.ReadFile("/tmp/link.txt")
require.NoError(t, err)
assert.Equal(t, "local\n", string(data))