| 1 | package runnersetup |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "encoding/json" |
| 8 | "io" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "strconv" |
| 12 | "strings" |
| 13 | "time" |
| 14 | |
| 15 | "amadan.net/oficina/equipo/internal/release" |
| 16 | ) |
| 17 | |
| 18 | type probeHost struct { |
| 19 | read func(string) ([]byte, error) |
| 20 | version func(context.Context) (string, error) |
| 21 | namespace func(context.Context, string, []string) error |
| 22 | } |
| 23 | |
| 24 | func containmentPreflight(ctx context.Context, binary string, m release.InstallManifest) []string { |
| 25 | path, e := exec.LookPath("bwrap") |
| 26 | if e != nil { |
| 27 | return []string{"runner.install.bwrap"} |
| 28 | } |
| 29 | file, e := os.Open(path) |
| 30 | if e != nil { |
| 31 | return []string{"runner.install.bwrap"} |
| 32 | } |
| 33 | hash := sha256.New() |
| 34 | _, e = io.Copy(hash, file) |
| 35 | closeErr := file.Close() |
| 36 | if e != nil || closeErr != nil { |
| 37 | return []string{"runner.install.bwrap"} |
| 38 | } |
| 39 | matched := false |
| 40 | for _, entry := range m.Release.Binaries { |
| 41 | if entry.Name == "bwrap" && entry.Version == "0.11.0" && entry.SHA256 == hex.EncodeToString(hash.Sum(nil)) { |
| 42 | matched = true |
| 43 | } |
| 44 | } |
| 45 | if !matched { |
| 46 | return []string{"runner.install.bwrap"} |
| 47 | } |
| 48 | |
| 49 | return probeContainment(ctx, binary, m, probeHost{read: os.ReadFile, version: func(ctx context.Context) (string, error) { |
| 50 | b, e := command(ctx, "bwrap", "--version").Output() |
| 51 | return string(b), e |
| 52 | }, namespace: namespaceProbe}) |
| 53 | } |
| 54 | func probeContainment(ctx context.Context, binary string, m release.InstallManifest, h probeHost) []string { |
| 55 | version, e := h.version(ctx) |
| 56 | if e != nil || version != "bubblewrap 0.11.0\n" { |
| 57 | return []string{"runner.install.bwrap"} |
| 58 | } |
| 59 | for _, path := range []string{"/proc/sys/kernel/unprivileged_userns_clone", "/proc/sys/user/max_user_namespaces"} { |
| 60 | b, e := h.read(path) |
| 61 | if e == nil && strings.TrimSpace(string(b)) == "0" { |
| 62 | return []string{"runner.install.userns"} |
| 63 | } |
| 64 | if e != nil && !os.IsNotExist(e) { |
| 65 | return []string{"runner.install.userns"} |
| 66 | } |
| 67 | } |
| 68 | if e = h.namespace(ctx, binary, m.NamespaceArgs); e != nil { |
| 69 | if e == errPIDFD { |
| 70 | return []string{"runner.install.pidfd"} |
| 71 | } |
| 72 | restriction, _ := h.read("/proc/sys/kernel/apparmor_restrict_unprivileged_userns") |
| 73 | if strings.TrimSpace(string(restriction)) == "1" { |
| 74 | return []string{"runner.install.apparmor"} |
| 75 | } |
| 76 | return []string{"runner.install.namespace"} |
| 77 | } |
| 78 | return nil |
| 79 | } |
| 80 | |
| 81 | var errPIDFD = &probeError{} |
| 82 | |
| 83 | type probeError struct{} |
| 84 | |
| 85 | func (*probeError) Error() string { return "runner.install.pidfd" } |
| 86 | func namespaceProbe(ctx context.Context, binary string, policy []string) error { |
| 87 | ctx, cancel := context.WithTimeout(ctx, 10*time.Second) |
| 88 | defer cancel() |
| 89 | statusR, statusW, e := os.Pipe() |
| 90 | if e != nil { |
| 91 | return e |
| 92 | } |
| 93 | defer statusR.Close() |
| 94 | defer statusW.Close() |
| 95 | args := append([]string{}, policy...) |
| 96 | args = append(args, "--ro-bind", binary, "/equipo-probe", "--proc", "/proc", "--dev", "/dev", "--tmpfs", "/tmp", "--", "/equipo-probe", "__install_probe") |
| 97 | cmd := command(ctx, "bwrap", args...) |
| 98 | cmd.Env = []string{} |
| 99 | cmd.ExtraFiles = []*os.File{statusW} |
| 100 | input, e := cmd.StdinPipe() |
| 101 | if e != nil { |
| 102 | return e |
| 103 | } |
| 104 | defer input.Close() |
| 105 | output, e := cmd.StdoutPipe() |
| 106 | if e != nil { |
| 107 | return e |
| 108 | } |
| 109 | if e = cmd.Start(); e != nil { |
| 110 | return e |
| 111 | } |
| 112 | statusW.Close() |
| 113 | defer func() { cancel(); _ = cmd.Wait() }() |
| 114 | done := make(chan error, 1) |
| 115 | go func() { |
| 116 | var status struct { |
| 117 | PID int `json:"child-pid"` |
| 118 | } |
| 119 | if json.NewDecoder(io.LimitReader(statusR, 4096)).Decode(&status) != nil || status.PID < 1 { |
| 120 | done <- errUnavailable |
| 121 | return |
| 122 | } |
| 123 | var ready struct{ PID int } |
| 124 | if json.NewDecoder(io.LimitReader(output, 4096)).Decode(&ready) != nil || ready.PID != 1 { |
| 125 | done <- errUnavailable |
| 126 | return |
| 127 | } |
| 128 | for _, ns := range []string{"pid", "user", "net"} { |
| 129 | host, e := os.Readlink("/proc/self/ns/" + ns) |
| 130 | child, ce := os.Readlink("/proc/" + strconv.Itoa(status.PID) + "/ns/" + ns) |
| 131 | if e != nil || ce != nil || host == child { |
| 132 | done <- errUnavailable |
| 133 | return |
| 134 | } |
| 135 | } |
| 136 | if pidfdAlive(status.PID) != nil { |
| 137 | done <- errPIDFD |
| 138 | return |
| 139 | } |
| 140 | _, e = input.Write([]byte{1}) |
| 141 | done <- e |
| 142 | }() |
| 143 | select { |
| 144 | case e = <-done: |
| 145 | case <-ctx.Done(): |
| 146 | statusR.Close() |
| 147 | output.Close() |
| 148 | e = <-done |
| 149 | if e == nil { |
| 150 | e = ctx.Err() |
| 151 | } |
| 152 | } |
| 153 | if e != nil { |
| 154 | return e |
| 155 | } |
| 156 | return cmd.Wait() |
| 157 | } |
| 158 | |
| 159 | // InstallProbeMain is a no-provider child of the release's namespace policy. |
| 160 | func InstallProbeMain(input io.Reader, output io.Writer) int { |
| 161 | if os.Getpid() != 1 { |
| 162 | return 125 |
| 163 | } |
| 164 | if json.NewEncoder(output).Encode(struct{ PID int }{os.Getpid()}) != nil { |
| 165 | return 125 |
| 166 | } |
| 167 | var token [1]byte |
| 168 | if _, e := io.ReadFull(input, token[:]); e != nil || token[0] != 1 { |
| 169 | return 125 |
| 170 | } |
| 171 | return 0 |
| 172 | } |
| 173 | |