| 1 | //go:build linux |
| 2 | |
| 3 | package containment |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "syscall" |
| 11 | "testing" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 14 | ) |
| 15 | |
| 16 | // renderDir makes a runner-style render directory: 0700, the runner's, |
| 17 | // regular files only (the step's captures so far). |
| 18 | func renderDir(t *testing.T) string { |
| 19 | t.Helper() |
| 20 | dir := filepath.Join(t.TempDir(), "render") |
| 21 | if err := os.Mkdir(dir, 0700); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | if err := os.WriteFile(filepath.Join(dir, "1-work-1.png"), []byte("capture bytes"), 0600); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | return dir |
| 28 | } |
| 29 | |
| 30 | // The Design step sees its captures read-only at /render, including one |
| 31 | // the runner writes after the launch (render_page runs outside the |
| 32 | // namespace, into the same directory); nothing inside writes there. |
| 33 | func TestNamespaceRenderDirReadOnly(t *testing.T) { |
| 34 | m, l := fixture(t) |
| 35 | l = withAction(t, m, l, provider.DesignStepAction) |
| 36 | l.RenderDir = renderDir(t) |
| 37 | script := `import os, sys |
| 38 | print(sorted(os.listdir("/render")), open("/render/1-work-1.png").read()) |
| 39 | for path in ("/render/1-work-1.png", "/render/planted.png"): |
| 40 | try: |
| 41 | open(path, "a").write("x") |
| 42 | print("wrote", path) |
| 43 | except OSError as e: |
| 44 | print("refused", e.errno) |
| 45 | print("research", os.path.exists("/research")) |
| 46 | ` |
| 47 | out := runPlanned(t, m, l, []string{"/usr/bin/python3", "-u", "-c", script}, "01234567890123456789012345678913") |
| 48 | want := "['1-work-1.png'] capture bytes\nrefused 30\nrefused 30\nresearch False\n" |
| 49 | if out != want { |
| 50 | t.Fatalf("/render inside the namespace:\n%s\nwant\n%s", out, want) |
| 51 | } |
| 52 | if raw, _ := os.ReadFile(filepath.Join(l.RenderDir, "1-work-1.png")); string(raw) != "capture bytes" { |
| 53 | t.Fatalf("capture changed: %q", raw) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // The /render bind is refused for any launch but the Design step's |
| 58 | // (Engineering and the legacy design action above all), and for a |
| 59 | // directory that is not the runner's own flat directory of regular |
| 60 | // files apart from every other mount. |
| 61 | func TestRenderDirRefusals(t *testing.T) { |
| 62 | m, base := fixture(t) |
| 63 | for _, c := range []struct { |
| 64 | name, reason string |
| 65 | setup func(t *testing.T, l *provider.Launch) |
| 66 | }{ |
| 67 | {"engineering", "launch", func(t *testing.T, l *provider.Launch) { *l = withAction(t, m, *l, "implement") }}, |
| 68 | {"legacy design", "launch", func(t *testing.T, l *provider.Launch) { *l = withAction(t, m, *l, "design") }}, |
| 69 | {"review", "launch", func(t *testing.T, l *provider.Launch) { *l = withAction(t, m, *l, "inspect") }}, |
| 70 | {"research step", "launch", func(t *testing.T, l *provider.Launch) { *l = withAction(t, m, *l, provider.ResearchStepAction) }}, |
| 71 | {"coordinator", "launch", func(t *testing.T, l *provider.Launch) { *l = withAction(t, m, *l, "converse") }}, |
| 72 | {"no action", "launch", func(t *testing.T, l *provider.Launch) { l.Policy.Manifest, l.Policy.ManifestHash = []byte(`{}`), "" }}, |
| 73 | {"tampered manifest", "launch", func(t *testing.T, l *provider.Launch) { l.Policy.ManifestHash = provider.ManifestDigest([]byte(`{}`)) }}, |
| 74 | {"with research images", "launch", func(t *testing.T, l *provider.Launch) { l.ResearchDir = researchDir(t) }}, |
| 75 | {"symlinked directory", "", func(t *testing.T, l *provider.Launch) { |
| 76 | link := filepath.Join(t.TempDir(), "render") |
| 77 | os.Symlink(l.RenderDir, link) |
| 78 | l.RenderDir = link |
| 79 | }}, |
| 80 | {"relative path", "", func(t *testing.T, l *provider.Launch) { l.RenderDir = "render" }}, |
| 81 | {"group readable", "mode", func(t *testing.T, l *provider.Launch) { os.Chmod(l.RenderDir, 0750) }}, |
| 82 | {"subdirectory", "entry", func(t *testing.T, l *provider.Launch) { os.Mkdir(filepath.Join(l.RenderDir, "nested"), 0700) }}, |
| 83 | {"symlink entry", "entry", func(t *testing.T, l *provider.Launch) { |
| 84 | os.Symlink("/etc/hostname", filepath.Join(l.RenderDir, "2-work-1.png")) |
| 85 | }}, |
| 86 | {"fifo entry", "entry", func(t *testing.T, l *provider.Launch) { |
| 87 | syscall.Mkfifo(filepath.Join(l.RenderDir, "2-work-1.png"), 0600) |
| 88 | }}, |
| 89 | {"inside the workspace", "overlap", func(t *testing.T, l *provider.Launch) { |
| 90 | dir := filepath.Join(l.Workspace, "render") |
| 91 | os.Mkdir(dir, 0700) |
| 92 | l.RenderDir = dir |
| 93 | }}, |
| 94 | {"is the workspace", "overlap", func(t *testing.T, l *provider.Launch) { l.RenderDir = l.Workspace }}, |
| 95 | {"inside the scratch root", "overlap", func(t *testing.T, l *provider.Launch) { |
| 96 | dir := filepath.Join(m.cfg.ScratchRoot, "render") |
| 97 | os.Mkdir(dir, 0700) |
| 98 | l.RenderDir = dir |
| 99 | }}, |
| 100 | } { |
| 101 | t.Run(c.name, func(t *testing.T) { |
| 102 | l := withAction(t, m, base, provider.DesignStepAction) |
| 103 | ws := filepath.Join(t.TempDir(), "workspace") |
| 104 | os.Mkdir(ws, 0700) |
| 105 | l.Workspace = ws |
| 106 | l.RenderDir = renderDir(t) |
| 107 | c.setup(t, &l) |
| 108 | plan, err := m.mountPlan(l, attemptDir(t, m)) |
| 109 | if err == nil { |
| 110 | plan.close() |
| 111 | t.Fatal("render directory accepted") |
| 112 | } |
| 113 | if !errors.Is(err, errMount) || !strings.Contains(err.Error(), c.reason) { |
| 114 | t.Fatalf("refused for the wrong reason: %v (want %q)", err, c.reason) |
| 115 | } |
| 116 | }) |
| 117 | } |
| 118 | // The Design step's launch with no render directory has no /render. |
| 119 | l := withAction(t, m, base, provider.DesignStepAction) |
| 120 | plan, err := m.mountPlan(l, attemptDir(t, m)) |
| 121 | if err != nil { |
| 122 | t.Fatalf("design launch without captures: %v", err) |
| 123 | } |
| 124 | defer plan.close() |
| 125 | for _, arg := range plan.args { |
| 126 | if arg == "/render" { |
| 127 | t.Fatal("/render bound with no directory") |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |