| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "syscall" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 12 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 13 | ) |
| 14 | |
| 15 | // Only locally recorded launch paths are considered, after all owned namespaces |
| 16 | // have been proved dead. Claims and tool arguments never supply cleanup paths. |
| 17 | func (r *Runtime) cleanupCoordinatorFiles(ctx context.Context, c runnerproto.CoordinatorClaim) error { |
| 18 | var states []namespaceState |
| 19 | if err := r.journal.readStates(ctx, "namespace", &states); err != nil { |
| 20 | return err |
| 21 | } |
| 22 | paths := map[string]string{} |
| 23 | for _, n := range states { |
| 24 | if n.Owner != attemptKey(c.Scope, c.Handle) { |
| 25 | continue |
| 26 | } |
| 27 | var raw []byte |
| 28 | var contained int |
| 29 | var l provider.Launch |
| 30 | if err := r.journal.db.QueryRowContext(ctx, "SELECT launch,contained FROM direct_attempts WHERE attempt=?", n.Record.AttemptKey).Scan(&raw, &contained); err != nil { |
| 31 | return err |
| 32 | } |
| 33 | if contained != 1 || json.Unmarshal(raw, &l) != nil || !directManifestIntact(l) { |
| 34 | return provider.ErrContainment |
| 35 | } |
| 36 | if filepath.Dir(l.Workspace) != r.cfg.WorkspaceRoot || !strings.HasPrefix(filepath.Base(l.Workspace), "coordinator-") { |
| 37 | return provider.ErrContainment |
| 38 | } |
| 39 | paths[l.Workspace] = r.cfg.WorkspaceRoot |
| 40 | if l.Binding.RuntimeRoot != filepath.Join(r.cfg.ScratchRoot, "homes") || filepath.Dir(l.Binding.Home) != l.Binding.RuntimeRoot || !strings.HasPrefix(filepath.Base(l.Binding.Home), "account-") { |
| 41 | return provider.ErrContainment |
| 42 | } |
| 43 | paths[l.Binding.Home] = l.Binding.RuntimeRoot |
| 44 | var manifest struct{ Scratch string } |
| 45 | if json.Unmarshal(l.Policy.Manifest, &manifest) != nil { |
| 46 | return provider.ErrContainment |
| 47 | } |
| 48 | if filepath.Dir(manifest.Scratch) == r.cfg.ScratchRoot && strings.HasPrefix(filepath.Base(manifest.Scratch), "policy-") { |
| 49 | paths[manifest.Scratch] = r.cfg.ScratchRoot |
| 50 | } |
| 51 | for _, m := range l.Policy.Mounts { |
| 52 | dir := filepath.Dir(m.Source) |
| 53 | if filepath.Dir(dir) == l.Binding.RuntimeRoot && strings.HasPrefix(filepath.Base(dir), "config-") { |
| 54 | paths[dir] = l.Binding.RuntimeRoot |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | for path, root := range paths { |
| 59 | if !filepath.IsAbs(path) || filepath.Clean(path) != path || filepath.Dir(path) != root { |
| 60 | return provider.ErrContainment |
| 61 | } |
| 62 | info, err := os.Lstat(path) |
| 63 | if os.IsNotExist(err) { |
| 64 | continue |
| 65 | } |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | stat, ok := info.Sys().(*syscall.Stat_t) |
| 70 | if !ok || !info.IsDir() || info.Mode().Perm() != 0700 || stat.Uid != uint32(os.Geteuid()) { |
| 71 | return provider.ErrContainment |
| 72 | } |
| 73 | if err = os.RemoveAll(path); err != nil { |
| 74 | return err |
| 75 | } |
| 76 | } |
| 77 | return nil |
| 78 | } |
| 79 | |