package runnersetup import ( "context" "crypto/sha256" "encoding/hex" "encoding/json" "io" "os" "os/exec" "strconv" "strings" "time" "amadan.net/oficina/equipo/internal/release" ) type probeHost struct { read func(string) ([]byte, error) version func(context.Context) (string, error) namespace func(context.Context, string, []string) error } func containmentPreflight(ctx context.Context, binary string, m release.InstallManifest) []string { path, e := exec.LookPath("bwrap") if e != nil { return []string{"runner.install.bwrap"} } file, e := os.Open(path) if e != nil { return []string{"runner.install.bwrap"} } hash := sha256.New() _, e = io.Copy(hash, file) closeErr := file.Close() if e != nil || closeErr != nil { return []string{"runner.install.bwrap"} } matched := false for _, entry := range m.Release.Binaries { if entry.Name == "bwrap" && entry.Version == "0.11.0" && entry.SHA256 == hex.EncodeToString(hash.Sum(nil)) { matched = true } } if !matched { return []string{"runner.install.bwrap"} } return probeContainment(ctx, binary, m, probeHost{read: os.ReadFile, version: func(ctx context.Context) (string, error) { b, e := command(ctx, "bwrap", "--version").Output() return string(b), e }, namespace: namespaceProbe}) } func probeContainment(ctx context.Context, binary string, m release.InstallManifest, h probeHost) []string { version, e := h.version(ctx) if e != nil || version != "bubblewrap 0.11.0\n" { return []string{"runner.install.bwrap"} } for _, path := range []string{"/proc/sys/kernel/unprivileged_userns_clone", "/proc/sys/user/max_user_namespaces"} { b, e := h.read(path) if e == nil && strings.TrimSpace(string(b)) == "0" { return []string{"runner.install.userns"} } if e != nil && !os.IsNotExist(e) { return []string{"runner.install.userns"} } } if e = h.namespace(ctx, binary, m.NamespaceArgs); e != nil { if e == errPIDFD { return []string{"runner.install.pidfd"} } restriction, _ := h.read("/proc/sys/kernel/apparmor_restrict_unprivileged_userns") if strings.TrimSpace(string(restriction)) == "1" { return []string{"runner.install.apparmor"} } return []string{"runner.install.namespace"} } return nil } var errPIDFD = &probeError{} type probeError struct{} func (*probeError) Error() string { return "runner.install.pidfd" } func namespaceProbe(ctx context.Context, binary string, policy []string) error { ctx, cancel := context.WithTimeout(ctx, 10*time.Second) defer cancel() statusR, statusW, e := os.Pipe() if e != nil { return e } defer statusR.Close() defer statusW.Close() args := append([]string{}, policy...) args = append(args, "--ro-bind", binary, "/equipo-probe", "--proc", "/proc", "--dev", "/dev", "--tmpfs", "/tmp", "--", "/equipo-probe", "__install_probe") cmd := command(ctx, "bwrap", args...) cmd.Env = []string{} cmd.ExtraFiles = []*os.File{statusW} input, e := cmd.StdinPipe() if e != nil { return e } defer input.Close() output, e := cmd.StdoutPipe() if e != nil { return e } if e = cmd.Start(); e != nil { return e } statusW.Close() defer func() { cancel(); _ = cmd.Wait() }() done := make(chan error, 1) go func() { var status struct { PID int `json:"child-pid"` } if json.NewDecoder(io.LimitReader(statusR, 4096)).Decode(&status) != nil || status.PID < 1 { done <- errUnavailable return } var ready struct{ PID int } if json.NewDecoder(io.LimitReader(output, 4096)).Decode(&ready) != nil || ready.PID != 1 { done <- errUnavailable return } for _, ns := range []string{"pid", "user", "net"} { host, e := os.Readlink("/proc/self/ns/" + ns) child, ce := os.Readlink("/proc/" + strconv.Itoa(status.PID) + "/ns/" + ns) if e != nil || ce != nil || host == child { done <- errUnavailable return } } if pidfdAlive(status.PID) != nil { done <- errPIDFD return } _, e = input.Write([]byte{1}) done <- e }() select { case e = <-done: case <-ctx.Done(): statusR.Close() output.Close() e = <-done if e == nil { e = ctx.Err() } } if e != nil { return e } return cmd.Wait() } // InstallProbeMain is a no-provider child of the release's namespace policy. func InstallProbeMain(input io.Reader, output io.Writer) int { if os.Getpid() != 1 { return 125 } if json.NewEncoder(output).Encode(struct{ PID int }{os.Getpid()}) != nil { return 125 } var token [1]byte if _, e := io.ReadFull(input, token[:]); e != nil || token[0] != 1 { return 125 } return 0 }