| 1 | package containment |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "io/fs" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "sort" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/runner/egress" |
| 14 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 15 | "amadan.net/oficina/equipo/internal/runner/provider/gotoolchain" |
| 16 | "amadan.net/oficina/equipo/internal/specialist" |
| 17 | "golang.org/x/sys/unix" |
| 18 | ) |
| 19 | |
| 20 | var errMount = errors.New("namespace_mount_rejected") |
| 21 | |
| 22 | type mounts struct { |
| 23 | binaryFD int |
| 24 | // secretFD is the pipe bubblewrap reads the secret environment from |
| 25 | // (--args), 0 when the launch has none. |
| 26 | secretFD int |
| 27 | proxy *egress.Proxy |
| 28 | args []string |
| 29 | files []*os.File |
| 30 | // own are files containment itself supplies (a Research step's CA |
| 31 | // bundle and passwd and group stubs): digested into the namespace |
| 32 | // manifest, but not policy files. |
| 33 | own map[*os.File]bool |
| 34 | } |
| 35 | |
| 36 | // secretEnvKeys are the only variables a launch may receive outside its |
| 37 | // policy: a token connection's token, and the CLI's subprocess scrub; a |
| 38 | // Pi or OpenCode launch's key, in its provider's variable |
| 39 | // (provider.PiKeyEnvs, provider.OpenCodeKeyEnvs). |
| 40 | var secretEnvKeys = func() map[string]bool { |
| 41 | keys := map[string]bool{provider.OAuthTokenEnv: true, provider.ScrubEnv: true} |
| 42 | for _, name := range provider.PiKeyEnvs { |
| 43 | keys[name] = true |
| 44 | } |
| 45 | for _, name := range provider.OpenCodeKeyEnvs { |
| 46 | keys[name] = true |
| 47 | } |
| 48 | return keys |
| 49 | }() |
| 50 | |
| 51 | // addSecretEnv passes env to bubblewrap through a pipe it reads as |
| 52 | // further arguments (--args FD, after --clearenv), so the values are in |
| 53 | // no argv, no manifest and no record: the namespace's environment only. |
| 54 | func (p *mounts) addSecretEnv(env map[string]string) error { |
| 55 | if len(env) == 0 { |
| 56 | return nil |
| 57 | } |
| 58 | keys := make([]string, 0, len(env)) |
| 59 | for k := range env { |
| 60 | keys = append(keys, k) |
| 61 | } |
| 62 | sort.Strings(keys) |
| 63 | var b []byte |
| 64 | for _, k := range keys { |
| 65 | v := env[k] |
| 66 | if !secretEnvKeys[k] || v == "" || strings.ContainsAny(v, "\x00\n") { |
| 67 | return errMount |
| 68 | } |
| 69 | b = append(b, "--setenv\x00"+k+"\x00"...) |
| 70 | b = append(b, v...) |
| 71 | b = append(b, 0) |
| 72 | } |
| 73 | defer clear(b) |
| 74 | r, w, err := os.Pipe() |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | // The whole message fits the pipe's buffer: a token is at most 4 KB. |
| 79 | _, err = w.Write(b) |
| 80 | if cerr := w.Close(); err == nil { |
| 81 | err = cerr |
| 82 | } |
| 83 | if err != nil { |
| 84 | r.Close() |
| 85 | return err |
| 86 | } |
| 87 | p.secretFD = 6 + len(p.files) |
| 88 | p.files = append(p.files, r) |
| 89 | return nil |
| 90 | } |
| 91 | |
| 92 | func (p *mounts) close() { |
| 93 | for _, f := range p.files { |
| 94 | _ = f.Close() |
| 95 | } |
| 96 | } |
| 97 | func cleanAbsolute(path string) bool { |
| 98 | return filepath.IsAbs(path) && filepath.Clean(path) == path && path != "/" |
| 99 | } |
| 100 | func within(path, root string) bool { return path == root || strings.HasPrefix(path, root+"/") } |
| 101 | func overlaps(a, b string) bool { return within(a, b) || within(b, a) } |
| 102 | |
| 103 | // openat2 rejects symlinks in every component. The retained descriptor, rather |
| 104 | // than the submitted path, is the source bubblewrap mounts after validation. |
| 105 | func pin(path string) (*os.File, error) { |
| 106 | if !cleanAbsolute(path) { |
| 107 | return nil, errMount |
| 108 | } |
| 109 | fd, err := pathNoLinks(path) |
| 110 | if err != nil { |
| 111 | return nil, fmt.Errorf("%w: %v", errMount, err) |
| 112 | } |
| 113 | return os.NewFile(uintptr(fd), path), nil |
| 114 | } |
| 115 | func (p *mounts) bind(source, target string, ro, tree bool) error { |
| 116 | f, err := pin(source) |
| 117 | if err != nil { |
| 118 | return err |
| 119 | } |
| 120 | return p.bindPinned(f, source, target, ro, tree) |
| 121 | } |
| 122 | |
| 123 | // bindPinned binds an already pinned descriptor, and owns it from here: |
| 124 | // it is closed on refusal. |
| 125 | func (p *mounts) bindPinned(f *os.File, source, target string, ro, tree bool) error { |
| 126 | info, err := f.Stat() |
| 127 | if err != nil { |
| 128 | f.Close() |
| 129 | return err |
| 130 | } |
| 131 | if (tree && !info.IsDir()) || (!tree && source != "/usr" && !info.Mode().IsRegular()) { |
| 132 | f.Close() |
| 133 | return errMount |
| 134 | } |
| 135 | if tree { |
| 136 | err = filepath.WalkDir(fmt.Sprintf("/proc/self/fd/%d/.", f.Fd()), func(path string, d fs.DirEntry, e error) error { |
| 137 | if e != nil { |
| 138 | return e |
| 139 | } |
| 140 | if d.Type()&(os.ModeSocket|os.ModeNamedPipe|os.ModeDevice) != 0 { |
| 141 | return errMount |
| 142 | } |
| 143 | return nil |
| 144 | }) |
| 145 | if err != nil { |
| 146 | f.Close() |
| 147 | return err |
| 148 | } |
| 149 | } |
| 150 | flag := "--bind-fd" |
| 151 | if ro { |
| 152 | flag = "--ro-bind-fd" |
| 153 | } |
| 154 | // FDs 3, 4 and 5 are status, release and readiness, respectively. |
| 155 | p.args = append(p.args, flag, strconv.Itoa(6+len(p.files)), target) |
| 156 | p.files = append(p.files, f) |
| 157 | return nil |
| 158 | } |
| 159 | |
| 160 | // signInTmpfs caps each of a sign-in's in-memory home and /tmp. |
| 161 | const signInTmpfs = 64 << 20 |
| 162 | |
| 163 | func (m *Manager) mountPlan(l provider.Launch, dir string) (_ *mounts, err error) { |
| 164 | p := &mounts{} |
| 165 | defer func() { |
| 166 | if err != nil { |
| 167 | p.close() |
| 168 | } |
| 169 | }() |
| 170 | // A sign-in has no workspace: an empty read-only one. |
| 171 | signIn := l.Policy.IsSignIn() |
| 172 | if signIn && (l.Workspace != "" || l.Binding.Home != "" || l.ToolSocket != "") { |
| 173 | return nil, errMount |
| 174 | } |
| 175 | if !signIn && (!cleanAbsolute(l.Workspace) || overlaps(l.Workspace, m.cfg.ScratchRoot)) { |
| 176 | return nil, errMount |
| 177 | } |
| 178 | for _, root := range []string{"/proc", "/sys", "/run", "/dev", "/etc", "/usr"} { |
| 179 | if !signIn && overlaps(l.Workspace, root) { |
| 180 | return nil, errMount |
| 181 | } |
| 182 | } |
| 183 | home, err := os.UserHomeDir() |
| 184 | if err != nil { |
| 185 | return nil, err |
| 186 | } |
| 187 | if !signIn && (within(home, l.Workspace) || l.Workspace == "/home" || filepath.Dir(l.Workspace) == "/home" || l.Workspace == "/tmp" || l.Workspace == "/root") { |
| 188 | return nil, errMount |
| 189 | } |
| 190 | if err = p.bind("/usr", "/usr", true, false); err != nil { |
| 191 | return nil, err |
| 192 | } |
| 193 | research := l.Policy.Research() |
| 194 | if research { |
| 195 | // A Research step never sees the host /etc (spec §4): a fresh |
| 196 | // tmpfs with the CA bundle and passwd and group stubs only. |
| 197 | p.args = append(p.args, "--tmpfs", "/etc") |
| 198 | if err = p.researchEtc(dir); err != nil { |
| 199 | return nil, err |
| 200 | } |
| 201 | } else { |
| 202 | p.args = append(p.args, "--overlay-src", "/etc", "--tmp-overlay", "/etc") |
| 203 | } |
| 204 | p.args = append(p.args, "--symlink", "usr/bin", "/bin", "--symlink", "usr/lib", "/lib", "--symlink", "usr/lib64", "/lib64", "--proc", "/proc", "--dev", "/dev", "--dir", "/run", "--dir", "/sys", "--dir", "/home/runner", "--tmpfs", "/etc/claude-code", "--tmpfs", "/etc/codex") |
| 205 | if l.Policy.IsOpenCode() { |
| 206 | // OpenCode reads /etc/opencode over every other configuration |
| 207 | // (connectors spec §3, isolated configuration): an empty one. |
| 208 | p.args = append(p.args, "--tmpfs", "/etc/opencode") |
| 209 | } |
| 210 | if l.Policy.Converse() { |
| 211 | // The thin coordinator's desk: its own notes, read-only to it. |
| 212 | if err = p.bind(l.Workspace, "/workspace", true, true); err != nil { |
| 213 | return nil, err |
| 214 | } |
| 215 | } else if l.Policy.Coordinator() || signIn { |
| 216 | p.args = append(p.args, "--tmpfs", "/workspace", "--remount-ro", "/workspace") |
| 217 | } else if research { |
| 218 | // The runner's empty scratch directory, never a clone or a desk. |
| 219 | if err = p.bindEmpty(l.Workspace); err != nil { |
| 220 | return nil, err |
| 221 | } |
| 222 | } else if err = p.bind(l.Workspace, "/workspace", l.Policy.ReadOnlyWorkspace(), true); err != nil { |
| 223 | return nil, err |
| 224 | } |
| 225 | runtimeHome := filepath.Join(dir, "home") |
| 226 | if l.Binding.Home != "" { |
| 227 | runtimeHome = l.Binding.Home |
| 228 | homes := filepath.Join(m.cfg.ScratchRoot, "homes") |
| 229 | if !cleanAbsolute(runtimeHome) || filepath.Dir(runtimeHome) != homes || overlaps(runtimeHome, l.Workspace) { |
| 230 | return nil, errMount |
| 231 | } |
| 232 | info, e := os.Stat(runtimeHome) |
| 233 | if e != nil || !info.IsDir() || info.Mode().Perm() != 0700 { |
| 234 | return nil, errMount |
| 235 | } |
| 236 | } |
| 237 | if signIn { |
| 238 | // A sign-in writes nothing to disk: its home and /tmp are the |
| 239 | // namespace's own memory, capped, gone with it. The runner takes |
| 240 | // the token from the CLI's output, never from a file here. |
| 241 | p.args = append(p.args, "--size", strconv.Itoa(signInTmpfs), "--perms", "0700", "--tmpfs", "/home/runner", "--size", strconv.Itoa(signInTmpfs), "--perms", "1777", "--tmpfs", "/tmp") |
| 242 | } else { |
| 243 | if err = p.bind(runtimeHome, "/home/runner", false, true); err != nil { |
| 244 | return nil, err |
| 245 | } |
| 246 | if err = p.bind(filepath.Join(dir, "tmp"), "/tmp", false, true); err != nil { |
| 247 | return nil, err |
| 248 | } |
| 249 | } |
| 250 | if err = p.bind(m.cfg.RunnerBinary, "/equipo-namespace", true, false); err != nil { |
| 251 | return nil, err |
| 252 | } |
| 253 | if l.ResearchDir != "" { |
| 254 | // Kept research images reach Engineering and Review only, never a |
| 255 | // Research step or any other launch. |
| 256 | if research || !l.Policy.ResearchReader() { |
| 257 | return nil, fmt.Errorf("%w: research directory refused for this launch", errMount) |
| 258 | } |
| 259 | near := []string{l.Workspace, m.cfg.ScratchRoot, dir, l.Binding.Home, l.Binding.CredentialHome, l.Binding.RuntimeRoot} |
| 260 | if l.ToolSocket != "" { |
| 261 | near = append(near, filepath.Dir(l.ToolSocket)) |
| 262 | } |
| 263 | if err = p.bindResearch(l.ResearchDir, near); err != nil { |
| 264 | return nil, err |
| 265 | } |
| 266 | } |
| 267 | if l.RenderDir != "" { |
| 268 | // The step's render_page captures reach the Design step only, |
| 269 | // read-only: render_page writes them from outside. |
| 270 | if !l.Policy.RenderReader() { |
| 271 | return nil, fmt.Errorf("%w: render directory refused for this launch", errMount) |
| 272 | } |
| 273 | near := []string{l.Workspace, m.cfg.ScratchRoot, dir, l.Binding.Home, l.Binding.CredentialHome, l.Binding.RuntimeRoot} |
| 274 | if l.ToolSocket != "" { |
| 275 | near = append(near, filepath.Dir(l.ToolSocket)) |
| 276 | } |
| 277 | if err = p.bindKept(l.RenderDir, near, specialist.RenderMount, "render"); err != nil { |
| 278 | return nil, err |
| 279 | } |
| 280 | } |
| 281 | targets := map[string]bool{} |
| 282 | for _, mount := range l.Policy.Mounts { |
| 283 | gateMount := l.Policy.CheckGateMount(mount) |
| 284 | if !gateMount && (l.Policy.IsCheck() || gotoolchain.Target(mount.Target)) && (overlaps(mount.Source, m.cfg.ScratchRoot) || mount.Source == "/" || within(home, mount.Source)) { |
| 285 | return nil, errMount |
| 286 | } |
| 287 | target := mount.Target |
| 288 | if !mount.ReadOnly || !cleanAbsolute(target) || targets[target] { |
| 289 | return nil, errMount |
| 290 | } |
| 291 | if !gateMount && !(strings.HasPrefix(target, "/opt/") || strings.HasPrefix(target, "/config/") || strings.HasPrefix(target, "/etc/claude-code/") || strings.HasPrefix(target, "/etc/codex/")) { |
| 292 | return nil, errMount |
| 293 | } |
| 294 | for _, existing := range l.Policy.Mounts { |
| 295 | if existing.Target != target && overlaps(existing.Target, target) { |
| 296 | return nil, errMount |
| 297 | } |
| 298 | } |
| 299 | for _, protected := range []string{"/proc", "/sys", "/run", "/dev"} { |
| 300 | if overlaps(mount.Source, protected) { |
| 301 | return nil, errMount |
| 302 | } |
| 303 | } |
| 304 | // bind validates and retains the same descriptor; no second path lookup. |
| 305 | // A Pi launch's package tree is a directory, its digest checked by |
| 306 | // the attestation; every other mount is a pinned file. |
| 307 | tree := l.Policy.CheckGateTree(mount) || gotoolchain.Target(target) || l.Policy.IsPi() && target == provider.PiTree |
| 308 | |
| 309 | if gotoolchain.Target(target) { |
| 310 | f, e := l.Policy.OpenGoTree(l.Binding, mount) |
| 311 | if e != nil { |
| 312 | return nil, e |
| 313 | } |
| 314 | err = p.bindPinned(f, mount.Source, target, true, tree) |
| 315 | } else { |
| 316 | err = p.bind(mount.Source, target, true, tree) |
| 317 | } |
| 318 | if err != nil { |
| 319 | return nil, err |
| 320 | } |
| 321 | |
| 322 | if l.Policy.CheckGateTree(mount) { |
| 323 | mounted := p.files[len(p.files)-1] |
| 324 | if sum, err := provider.TreeSHA256(fmt.Sprintf("/proc/self/fd/%d/.", mounted.Fd())); err != nil || sum != l.Policy.CheckGateTreeSHA(mount) { |
| 325 | return nil, errMount |
| 326 | } |
| 327 | } |
| 328 | if l.Policy.IsPi() && target == provider.PiTree { |
| 329 | // The tree as mounted, through the descriptor bubblewrap |
| 330 | // mounts, not by its path again: one replaced after the |
| 331 | // attestation is refused here. |
| 332 | source, pin, ok := l.Policy.PiTree() |
| 333 | mounted := p.files[len(p.files)-1] |
| 334 | if !ok || source != mount.Source { |
| 335 | return nil, errMount |
| 336 | } |
| 337 | if sum, err := provider.TreeSHA256(fmt.Sprintf("/proc/self/fd/%d/.", mounted.Fd())); err != nil || sum != pin { |
| 338 | return nil, errMount |
| 339 | } |
| 340 | } |
| 341 | if l.Policy.IsOpenCode() && target == provider.OpenCodeBinary { |
| 342 | // The binary as mounted, hashed through the descriptor |
| 343 | // bubblewrap mounts: one replaced after the attestation is |
| 344 | // refused here. |
| 345 | source, pin, ok := l.Policy.OpenCodeBinaryPin() |
| 346 | mounted := p.files[len(p.files)-1] |
| 347 | if !ok || source != mount.Source { |
| 348 | return nil, errMount |
| 349 | } |
| 350 | // The pinned descriptor is a path one: read the same file |
| 351 | // through it, not by its path again. |
| 352 | f, err := os.Open(fmt.Sprintf("/proc/self/fd/%d", mounted.Fd())) |
| 353 | if err != nil { |
| 354 | return nil, errMount |
| 355 | } |
| 356 | // Held to its seal as well as its bytes: owner, one name, no |
| 357 | // write permission, safe ancestors (security round finding 2). |
| 358 | sum, err := provider.SealedBinarySHA256(f, source) |
| 359 | f.Close() |
| 360 | if err != nil || sum != pin { |
| 361 | return nil, errMount |
| 362 | } |
| 363 | } |
| 364 | targets[target] = true |
| 365 | } |
| 366 | if l.ToolSocket != "" { |
| 367 | if err = p.bindTool(l.ToolSocket); err != nil { |
| 368 | return nil, err |
| 369 | } |
| 370 | } |
| 371 | p.args = append(p.args, "--clearenv", "--setenv", "PATH", "/usr/bin:/bin:/opt", "--setenv", "HOME", "/home/runner", "--setenv", "TMPDIR", "/tmp", "--setenv", "LANG", "C.UTF-8") |
| 372 | allowed := map[string]bool{"GIT_CONFIG_GLOBAL": l.Policy.Env["GIT_CONFIG_GLOBAL"] == "/dev/null", "GIT_CONFIG_NOSYSTEM": l.Policy.Env["GIT_CONFIG_NOSYSTEM"] == "1", "LANG": true, "LC_ALL": true, "TERM": true, "CODEX_HOME": true, "CLAUDE_CONFIG_DIR": true, "CLAUDE_SECURESTORAGE_CONFIG_DIR": true, "CLAUDE_TMPDIR": true, "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": true, "DISABLE_AUTOUPDATER": true} |
| 373 | if signIn { |
| 374 | // The sign-in's browser opens nothing. |
| 375 | if l.Policy.Env["BROWSER"] != provider.SignInBrowser { |
| 376 | return nil, errMount |
| 377 | } |
| 378 | allowed["BROWSER"] = true |
| 379 | } |
| 380 | if l.Policy.IsPi() { |
| 381 | // Pi's config directory in the runtime home, its startup fetches |
| 382 | // off, and the extension's gate (provider.BuildPiPolicy). |
| 383 | for k, v := range map[string]string{"PI_CODING_AGENT_DIR": provider.PiAgentDir, "PI_OFFLINE": "1", "PI_SKIP_VERSION_CHECK": "1", "PI_TELEMETRY": "0"} { |
| 384 | if l.Policy.Env[k] != v { |
| 385 | return nil, errMount |
| 386 | } |
| 387 | allowed[k] = true |
| 388 | } |
| 389 | for _, name := range []string{"settings.json", "models.json", "mcp.json"} { |
| 390 | if err = p.bind(filepath.Join(runtimeHome, "agent", name), "/home/runner/agent/"+name, true, false); err != nil { |
| 391 | return nil, err |
| 392 | } |
| 393 | } |
| 394 | allowed["EQUIPO_PI_TOOLS"] = true |
| 395 | allowed["EQUIPO_PI_CONFINE"] = l.Policy.Env["EQUIPO_PI_CONFINE"] == "/workspace" |
| 396 | allowed["NODE_EXTRA_CA_CERTS"] = provider.TestCAAllowed() |
| 397 | } |
| 398 | if l.Policy.IsOpenCode() { |
| 399 | // OpenCode's configuration, data and state in the runtime home, |
| 400 | // and its switches: no project configuration, no Claude Code |
| 401 | // compatibility, no catalogue fetch, no update check, no free |
| 402 | // models, no file watcher (provider.BuildOpenCodePolicy). |
| 403 | for k, v := range provider.OpenCodeEnvSwitches() { |
| 404 | if l.Policy.Env[k] != v { |
| 405 | return nil, errMount |
| 406 | } |
| 407 | allowed[k] = true |
| 408 | } |
| 409 | allowed["NODE_EXTRA_CA_CERTS"] = provider.TestCAAllowed() |
| 410 | } |
| 411 | if l.Policy.IsCheck() || l.Policy.GoToolchain() { |
| 412 | for k, v := range gotoolchain.Environment(l.Selection.Provider) { |
| 413 | if l.Policy.Env[k] != v { |
| 414 | return nil, errMount |
| 415 | } |
| 416 | allowed[k] = true |
| 417 | } |
| 418 | } |
| 419 | keys := make([]string, 0, len(l.Policy.Env)) |
| 420 | for k := range l.Policy.Env { |
| 421 | keys = append(keys, k) |
| 422 | } |
| 423 | sort.Strings(keys) |
| 424 | for _, k := range keys { |
| 425 | v := l.Policy.Env[k] |
| 426 | if !allowed[k] || strings.ContainsRune(v, 0) { |
| 427 | return nil, errMount |
| 428 | } |
| 429 | if (k == "CODEX_HOME" || k == "CLAUDE_CONFIG_DIR" || k == "CLAUDE_SECURESTORAGE_CONFIG_DIR") && (!cleanAbsolute(v) || !within(v, "/home/runner")) { |
| 430 | return nil, errMount |
| 431 | } |
| 432 | if k == "CLAUDE_TMPDIR" && (!cleanAbsolute(v) || !within(v, "/tmp")) { |
| 433 | return nil, errMount |
| 434 | } |
| 435 | p.args = append(p.args, "--setenv", k, v) |
| 436 | } |
| 437 | if l.Policy.IsOpenCode() { |
| 438 | p.args = append(p.args, "--remount-ro", "/etc/opencode") |
| 439 | } |
| 440 | p.args = append(p.args, "--remount-ro", "/etc/claude-code", "--remount-ro", "/etc/codex", "--remount-ro", "/etc", "--chdir", "/workspace") |
| 441 | return p, nil |
| 442 | } |
| 443 | |
| 444 | // hostCABundle is the host's CA bundle, the one file of the host /etc a |
| 445 | // Research step sees. A variable so a test can point it at a symlink. |
| 446 | var hostCABundle = "/etc/ssl/certs/ca-certificates.crt" |
| 447 | |
| 448 | // researchEtc fills a Research step's tmpfs /etc: the host CA bundle, |
| 449 | // resolved on the host and pinned as the regular file it names, and |
| 450 | // passwd and group stubs written into the attempt directory. All three |
| 451 | // are read-only; the masked provider directories follow as for every |
| 452 | // launch. |
| 453 | func (p *mounts) researchEtc(dir string) error { |
| 454 | ca, err := filepath.EvalSymlinks(hostCABundle) |
| 455 | if err != nil { |
| 456 | return fmt.Errorf("%w: CA bundle: %v", errMount, err) |
| 457 | } |
| 458 | if err = p.bindOwn(ca, "/etc/ssl/certs/ca-certificates.crt"); err != nil { |
| 459 | return err |
| 460 | } |
| 461 | stubs := filepath.Join(dir, "etc") |
| 462 | if err = os.Mkdir(stubs, 0700); err != nil { |
| 463 | return err |
| 464 | } |
| 465 | uid, gid := os.Getuid(), os.Getgid() |
| 466 | for name, body := range map[string]string{ |
| 467 | "passwd": fmt.Sprintf("runner:x:%d:%d:runner:/home/runner:/usr/sbin/nologin\nnobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin\n", uid, gid), |
| 468 | "group": fmt.Sprintf("runner:x:%d:\nnogroup:x:65534:\n", gid), |
| 469 | } { |
| 470 | path := filepath.Join(stubs, name) |
| 471 | f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL|unix.O_NOFOLLOW, 0644) |
| 472 | if err != nil { |
| 473 | return err |
| 474 | } |
| 475 | _, err = f.WriteString(body) |
| 476 | if cerr := f.Close(); err == nil { |
| 477 | err = cerr |
| 478 | } |
| 479 | if err != nil { |
| 480 | return err |
| 481 | } |
| 482 | if err = p.bindOwn(path, "/etc/"+name); err != nil { |
| 483 | return err |
| 484 | } |
| 485 | } |
| 486 | return nil |
| 487 | } |
| 488 | |
| 489 | // bindOwn binds a regular file containment supplies itself, read-only. |
| 490 | func (p *mounts) bindOwn(source, target string) error { |
| 491 | if err := p.bind(source, target, true, false); err != nil { |
| 492 | return err |
| 493 | } |
| 494 | if p.own == nil { |
| 495 | p.own = map[*os.File]bool{} |
| 496 | } |
| 497 | p.own[p.files[len(p.files)-1]] = true |
| 498 | return nil |
| 499 | } |
| 500 | |
| 501 | // bindEmpty binds a Research step's scratch workspace read-write after |
| 502 | // checking, through the pinned descriptor, that it is empty. |
| 503 | func (p *mounts) bindEmpty(path string) error { |
| 504 | f, err := pin(path) |
| 505 | if err != nil { |
| 506 | return err |
| 507 | } |
| 508 | entries, err := os.ReadDir(fmt.Sprintf("/proc/self/fd/%d", f.Fd())) |
| 509 | if err != nil || len(entries) != 0 { |
| 510 | f.Close() |
| 511 | return fmt.Errorf("%w: research workspace is not an empty directory", errMount) |
| 512 | } |
| 513 | return p.bindPinned(f, path, "/workspace", false, true) |
| 514 | } |
| 515 | |
| 516 | // bindResearch binds a task's kept research images read-only at |
| 517 | // /research. The directory must be the runner's own, 0700, apart from |
| 518 | // every other mount source, and hold nothing but regular files with one |
| 519 | // link each (no subdirectory, symlink, device, socket, fifo or hard |
| 520 | // link to a file elsewhere). It is checked and bound through one pinned |
| 521 | // descriptor, so a path swapped after the checks is not what is bound. |
| 522 | func (p *mounts) bindResearch(path string, near []string) error { |
| 523 | return p.bindKept(path, near, "/research", "research") |
| 524 | } |
| 525 | |
| 526 | // bindKept binds a runner-made flat directory of regular files |
| 527 | // read-only at target, as bindResearch describes; what names it in a |
| 528 | // refusal. |
| 529 | func (p *mounts) bindKept(path string, near []string, target, what string) error { |
| 530 | if !cleanAbsolute(path) { |
| 531 | return errMount |
| 532 | } |
| 533 | for _, other := range near { |
| 534 | if other != "" && overlaps(path, other) { |
| 535 | return fmt.Errorf("%w: %s directory overlaps another mount", errMount, what) |
| 536 | } |
| 537 | } |
| 538 | for _, root := range []string{"/proc", "/sys", "/run", "/dev", "/etc", "/usr"} { |
| 539 | if overlaps(path, root) { |
| 540 | return fmt.Errorf("%w: %s directory overlaps another mount", errMount, what) |
| 541 | } |
| 542 | } |
| 543 | f, err := pin(path) |
| 544 | if err != nil { |
| 545 | return err |
| 546 | } |
| 547 | fd := int(f.Fd()) |
| 548 | var st unix.Stat_t |
| 549 | if err = unix.Fstat(fd, &st); err != nil || st.Mode&unix.S_IFMT != unix.S_IFDIR { |
| 550 | f.Close() |
| 551 | return fmt.Errorf("%w: %s directory is not a directory", errMount, what) |
| 552 | } |
| 553 | if st.Uid != uint32(os.Getuid()) { |
| 554 | f.Close() |
| 555 | return fmt.Errorf("%w: %s directory owner is not the runner", errMount, what) |
| 556 | } |
| 557 | if st.Mode&0o7777 != 0o700 { |
| 558 | f.Close() |
| 559 | return fmt.Errorf("%w: %s directory mode is not 0700", errMount, what) |
| 560 | } |
| 561 | entries, err := os.ReadDir(fmt.Sprintf("/proc/self/fd/%d", fd)) |
| 562 | if err != nil { |
| 563 | f.Close() |
| 564 | return fmt.Errorf("%w: %s directory: %v", errMount, what, err) |
| 565 | } |
| 566 | for _, e := range entries { |
| 567 | var est unix.Stat_t |
| 568 | if unix.Fstatat(fd, e.Name(), &est, unix.AT_SYMLINK_NOFOLLOW) != nil || est.Mode&unix.S_IFMT != unix.S_IFREG || est.Nlink != 1 || est.Uid != uint32(os.Getuid()) { |
| 569 | f.Close() |
| 570 | return fmt.Errorf("%w: %s directory entry %q is not a single regular file", errMount, what, e.Name()) |
| 571 | } |
| 572 | } |
| 573 | return p.bindPinned(f, path, target, true, true) |
| 574 | } |
| 575 | |