//go:build linux package containment import ( "fmt" "net" "os" "path/filepath" "testing" ) func TestDirectToolMount(t *testing.T) { dir := t.TempDir() if e := os.Chmod(dir, 0700); e != nil { t.Fatal(e) } fd, e := os.Open(dir) if e != nil { t.Fatal(e) } defer fd.Close() path := filepath.Join(dir, "equipo.sock") ln, e := net.ListenUnix("unix", &net.UnixAddr{Name: fmt.Sprintf("/proc/self/fd/%d/equipo.sock", fd.Fd()), Net: "unix"}) if e != nil { t.Fatal(e) } ln.SetUnlinkOnClose(false) defer ln.Close() if e = os.Chmod(path, 0600); e != nil { t.Fatal(e) } p := &mounts{} defer p.close() if e = p.bindTool(path); e != nil { t.Fatal(e) } if len(p.files) != 1 || len(p.args) != 3 || p.args[0] != "--ro-bind-fd" || p.args[2] != "/tool/equipo.sock" { t.Fatal("tool mount missing", p.args) } for _, change := range []string{"public", "symlink", "regular"} { t.Run(change, func(t *testing.T) { q := &mounts{} defer q.close() bad := path switch change { case "public": os.Chmod(path, 0666) defer os.Chmod(path, 0600) case "symlink": bad = filepath.Join(t.TempDir(), "equipo.sock") os.Symlink(path, bad) case "regular": bad = filepath.Join(t.TempDir(), "equipo.sock") os.WriteFile(bad, []byte("x"), 0600) } if e := os.Chmod(filepath.Dir(bad), 0700); e != nil { t.Fatal(e) } if q.bindTool(bad) == nil { t.Fatal("unsafe socket mount", change) } }) } }