Download this file
| 1 | //go:build linux |
| 2 | |
| 3 | package containment |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "net" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestDirectToolMount(t *testing.T) { |
| 14 | dir := t.TempDir() |
| 15 | if e := os.Chmod(dir, 0700); e != nil { |
| 16 | t.Fatal(e) |
| 17 | } |
| 18 | fd, e := os.Open(dir) |
| 19 | if e != nil { |
| 20 | t.Fatal(e) |
| 21 | } |
| 22 | defer fd.Close() |
| 23 | path := filepath.Join(dir, "equipo.sock") |
| 24 | ln, e := net.ListenUnix("unix", &net.UnixAddr{Name: fmt.Sprintf("/proc/self/fd/%d/equipo.sock", fd.Fd()), Net: "unix"}) |
| 25 | if e != nil { |
| 26 | t.Fatal(e) |
| 27 | } |
| 28 | ln.SetUnlinkOnClose(false) |
| 29 | defer ln.Close() |
| 30 | if e = os.Chmod(path, 0600); e != nil { |
| 31 | t.Fatal(e) |
| 32 | } |
| 33 | p := &mounts{} |
| 34 | defer p.close() |
| 35 | if e = p.bindTool(path); e != nil { |
| 36 | t.Fatal(e) |
| 37 | } |
| 38 | if len(p.files) != 1 || len(p.args) != 3 || p.args[0] != "--ro-bind-fd" || p.args[2] != "/tool/equipo.sock" { |
| 39 | t.Fatal("tool mount missing", p.args) |
| 40 | } |
| 41 | for _, change := range []string{"public", "symlink", "regular"} { |
| 42 | t.Run(change, func(t *testing.T) { |
| 43 | q := &mounts{} |
| 44 | defer q.close() |
| 45 | bad := path |
| 46 | switch change { |
| 47 | case "public": |
| 48 | os.Chmod(path, 0666) |
| 49 | defer os.Chmod(path, 0600) |
| 50 | case "symlink": |
| 51 | bad = filepath.Join(t.TempDir(), "equipo.sock") |
| 52 | os.Symlink(path, bad) |
| 53 | case "regular": |
| 54 | bad = filepath.Join(t.TempDir(), "equipo.sock") |
| 55 | os.WriteFile(bad, []byte("x"), 0600) |
| 56 | } |
| 57 | if e := os.Chmod(filepath.Dir(bad), 0700); e != nil { |
| 58 | t.Fatal(e) |
| 59 | } |
| 60 | if q.bindTool(bad) == nil { |
| 61 | t.Fatal("unsafe socket mount", change) |
| 62 | } |
| 63 | }) |
| 64 | } |
| 65 | } |
| 66 | |