diff --git a/.gitea/workflows/pin-check.yml b/.gitea/workflows/pin-check.yml index c1bc923..9422d52 100644 --- a/.gitea/workflows/pin-check.yml +++ b/.gitea/workflows/pin-check.yml @@ -3,14 +3,16 @@ # # Weekly check that the releases this installer pins (internal/release) have # not fallen behind the newest release of inbuxa-server, inbuxa-admin and -# inbuxa-webmail. A fresh install ships whatever release.go names, so a pin -# left behind is a fresh install shipped stale -- and, if the skipped release -# fixed a vulnerability, shipped vulnerable. +# inbuxa-webmail, nor Caddy behind Docker Hub's newest patch of its series or +# a rebuild of its own version. A fresh install ships whatever release.go +# names, so a pin left behind is a fresh install shipped stale -- and, if the +# skipped release fixed a vulnerability, shipped vulnerable. # # A stale pin fails this run (exit 10) and opens or updates a tracking issue; -# the fix is a reviewed change to release.go with the new checksums, never an -# automatic edit. The release reads are public, so they need no token; --issue -# uses the run token to write the issue in this repository. +# the fix is a reviewed change to release.go with the new checksums (and +# Caddy's new digest), never an automatic edit. The release reads are public, +# so they need no token; --issue uses the run token to write the issue in +# this repository. name: pin-check on: diff --git a/cmd/pin-check/main.go b/cmd/pin-check/main.go index 2aa915b..3237cea 100644 --- a/cmd/pin-check/main.go +++ b/cmd/pin-check/main.go @@ -10,13 +10,19 @@ // on an error -- so a scheduled job turns a stale pin into a red run (and, with // --issue, an open issue) instead of a fresh install shipped stale. // +// Caddy, pinned by the digest of its image on Docker Hub, is read from Docker +// Hub's tags instead: it is behind when a newer X.Y.Z-alpine of its series is +// there, or when its own version's tag now names another image (Docker Hub +// rebuilds an image for a new Alpine under the same tag). +// // It only reads; it never edits release.go. The move itself stays a reviewed // change: the report names the version to move to, and release.go's checksums -// are still taken from each release's signed SHA256SUMS by hand. +// are still taken from each release's signed SHA256SUMS by hand, and Caddy's +// digest from its image index. // // Usage: // -// pin-check [--base-url URL] [--timeout D] [--issue] +// pin-check [--base-url URL] [--hub-url URL] [--timeout D] [--issue] // // GITEA_TOKEN, if set, authenticates the API reads (needed while the // repositories are private). --issue opens or updates a tracking issue in this @@ -55,6 +61,7 @@ func components() []component { func main() { baseURL := flag.String("base-url", "https://git.coffeylabs.org", "the Gitea base URL whose releases to read") + hubURL := flag.String("hub-url", "https://hub.docker.com", "the Docker Hub base URL whose caddy tags to read") timeout := flag.Duration("timeout", 30*time.Second, "overall deadline for the API reads") openIssue := flag.Bool("issue", false, "open or update a tracking issue in this repo when a pin is stale") flag.Parse() @@ -63,7 +70,7 @@ func main() { ctx, cancel := context.WithTimeout(context.Background(), *timeout) defer cancel() - code, err := run(ctx, http.DefaultClient, *baseURL, token, *openIssue) + code, err := run(ctx, http.DefaultClient, *baseURL, *hubURL, token, *openIssue) if err != nil { fmt.Fprintf(os.Stderr, "pin-check: %v\n", err) os.Exit(1) @@ -71,8 +78,16 @@ func main() { os.Exit(code) } -func run(ctx context.Context, client *http.Client, baseURL, token string, openIssue bool) (int, error) { +func run(ctx context.Context, client *http.Client, baseURL, hubURL, token string, openIssue bool) (int, error) { var findings []pins.Finding + report := func(f pins.Finding, stale bool, name, current string) { + if stale { + findings = append(findings, f) + fmt.Printf("STALE %-24s %s -> %s\n", name, f.Current, f.Latest) + } else { + fmt.Printf("ok %-24s %s\n", name, current) + } + } for _, c := range components() { rels, err := fetchReleases(ctx, client, baseURL, token, c.repo) if err != nil { @@ -82,13 +97,13 @@ func run(ctx context.Context, client *http.Client, baseURL, token string, openIs if err != nil { return 0, err } - if stale { - findings = append(findings, f) - fmt.Printf("STALE %-24s %s -> %s\n", c.Name, f.Current, f.Latest) - } else { - fmt.Printf("ok %-24s %s\n", c.Name, c.Current) - } + report(f, stale, c.Name, c.Current) } + f, stale, err := checkCaddy(ctx, client, hubURL) + if err != nil { + return 0, fmt.Errorf("caddy: %w", err) + } + report(f, stale, caddyName, release.CaddyVersion) if len(findings) == 0 { fmt.Println("\nAll pins are current.") @@ -96,7 +111,8 @@ func run(ctx context.Context, client *http.Client, baseURL, token string, openIs } fmt.Printf("\n%d pin(s) behind. Move release.go to the versions above, taking each\n", len(findings)) - fmt.Println("checksum from that release's signed SHA256SUMS, and open a pull request.") + fmt.Println("checksum from that release's signed SHA256SUMS (Caddy's digest from its image index),") + fmt.Println("and open a pull request.") if openIssue { if token == "" { return 0, fmt.Errorf("--issue needs GITEA_TOKEN") @@ -185,7 +201,7 @@ func issueBody(findings []pins.Finding) string { for _, f := range findings { fmt.Fprintf(&b, "- **%s**: `%s` -> `%s`\n", f.Component, f.Current, f.Latest) } - b.WriteString("\nMove each pin, taking its checksum from that release's signed `SHA256SUMS`, and open a pull request. This issue is maintained by `pin-check`; it updates itself and can be closed once the pins are current.\n") + b.WriteString("\nMove each pin, taking its checksum from that release's signed `SHA256SUMS` (Caddy's digest from its image index: `docker buildx imagetools inspect docker.io/library/caddy:-alpine`), and open a pull request. This issue is maintained by `pin-check`; it updates itself and can be closed once the pins are current.\n") return b.String() } @@ -241,3 +257,49 @@ func apiWrite(ctx context.Context, client *http.Client, method, u, token string, } return nil } + +const caddyName = "caddy (Docker Hub)" + +// hubTag is the slice of a Docker Hub tag the check needs. +type hubTag struct { + Name string `json:"name"` + Digest string `json:"digest"` +} + +// checkCaddy compares the Caddy pin with Docker Hub's caddy tags: a newer +// X.Y.Z-alpine in the pin's series is a move, and so is the pin's own tag +// naming an image other than the pinned digest, which is a rebuild. +func checkCaddy(ctx context.Context, client *http.Client, hubURL string) (pins.Finding, bool, error) { + u := strings.TrimRight(hubURL, "/") + "/v2/repositories/library/caddy/tags?page_size=100&name=-alpine" + body, err := apiGet(ctx, client, u, "") + if err != nil { + return pins.Finding{}, false, err + } + var page struct { + Results []hubTag `json:"results"` + } + if err := json.Unmarshal(body, &page); err != nil { + return pins.Finding{}, false, fmt.Errorf("decoding tags: %w", err) + } + var rels []pins.Release + rebuilt := "" + for _, t := range page.Results { + v, ok := strings.CutSuffix(t.Name, "-alpine") + if !ok { + continue + } + rels = append(rels, pins.Release{Tag: v}) + if v == release.CaddyVersion && t.Digest != "" && t.Digest != release.CaddyDigest { + rebuilt = t.Digest + } + } + f, stale, err := pins.Check(pins.Component{Name: caddyName, Current: release.CaddyVersion}, rels) + if err != nil || stale { + return f, stale, err + } + if rebuilt != "" { + return pins.Finding{Component: caddyName, Current: release.CaddyVersion, + Latest: release.CaddyVersion + " rebuilt, now " + rebuilt}, true, nil + } + return pins.Finding{}, false, nil +} diff --git a/cmd/pin-check/main_test.go b/cmd/pin-check/main_test.go index 701406e..63fd0a8 100644 --- a/cmd/pin-check/main_test.go +++ b/cmd/pin-check/main_test.go @@ -12,6 +12,7 @@ import ( "strings" "testing" + "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/pins" "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/release" ) @@ -50,9 +51,32 @@ func releasesServer(t *testing.T, bump bool) *httptest.Server { _ = json.NewEncoder(w).Encode(rels) }) } + mux.HandleFunc("/v2/repositories/library/caddy/tags", func(w http.ResponseWriter, r *http.Request) { + _ = json.NewEncoder(w).Encode(caddyTags(bump, release.CaddyDigest)) + }) return httptest.NewServer(mux) } +// caddyTags is a page of Docker Hub's caddy tags: the pinned version's at +// digest, a newer patch when bump is true, and the moving and builder tags, +// which are not versions to move to. +func caddyTags(bump bool, digest string) map[string][]hubTag { + tags := []hubTag{ + {Name: "alpine", Digest: "sha256:moving"}, + {Name: "2-alpine", Digest: "sha256:moving"}, + {Name: "2.11-alpine", Digest: "sha256:moving"}, + {Name: release.CaddyVersion + "-alpine", Digest: digest}, + {Name: "9.9.9-builder-alpine", Digest: "sha256:builder"}, + {Name: "2.99.0-beta.1-alpine", Digest: "sha256:beta"}, + } + if bump { + v, _ := pins.Parse(release.CaddyVersion) + v.Patch++ + tags = append(tags, hubTag{Name: v.String() + "-alpine", Digest: "sha256:newer"}) + } + return map[string][]hubTag{"results": tags} +} + func bumpNum(s string) string { var n int fmt.Sscanf(s, "%d", &n) @@ -62,7 +86,7 @@ func bumpNum(s string) string { func TestRunReportsAllCurrentWhenNothingIsNewer(t *testing.T) { srv := releasesServer(t, false) defer srv.Close() - code, err := run(context.Background(), srv.Client(), srv.URL, "", false) + code, err := run(context.Background(), srv.Client(), srv.URL, srv.URL, "", false) if err != nil { t.Fatal(err) } @@ -74,7 +98,7 @@ func TestRunReportsAllCurrentWhenNothingIsNewer(t *testing.T) { func TestRunExits10WhenAPinIsBehind(t *testing.T) { srv := releasesServer(t, true) defer srv.Close() - code, err := run(context.Background(), srv.Client(), srv.URL, "", false) + code, err := run(context.Background(), srv.Client(), srv.URL, srv.URL, "", false) if err != nil { t.Fatal(err) } @@ -88,7 +112,7 @@ func TestRunErrorsOnABadStatus(t *testing.T) { http.Error(w, "nope", http.StatusInternalServerError) })) defer srv.Close() - if _, err := run(context.Background(), srv.Client(), srv.URL, "", false); err == nil { + if _, err := run(context.Background(), srv.Client(), srv.URL, srv.URL, "", false); err == nil { t.Errorf("a 500 from the releases API should be an error") } } @@ -113,3 +137,37 @@ func TestFetchReleasesSkipsDraftsAndKeepsPrereleaseFlag(t *testing.T) { t.Errorf("releases not carried through faithfully: %+v", got) } } + +// Caddy is behind when Docker Hub has a newer patch of its series, or when +// its own version's tag now names another image; the moving tags, the +// builder images and betas are never a move. +func TestCheckCaddy(t *testing.T) { + for _, c := range []struct { + name string + bump bool + digest string + stale bool + latest string + }{ + {"current", false, release.CaddyDigest, false, ""}, + {"newer patch", true, release.CaddyDigest, true, ""}, + {"rebuilt", false, "sha256:rebuilt", true, release.CaddyVersion + " rebuilt, now sha256:rebuilt"}, + } { + t.Run(c.name, func(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = json.NewEncoder(w).Encode(caddyTags(c.bump, c.digest)) + })) + defer srv.Close() + f, stale, err := checkCaddy(context.Background(), srv.Client(), srv.URL) + if err != nil { + t.Fatal(err) + } + if stale != c.stale || (c.latest != "" && f.Latest != c.latest) { + t.Errorf("stale = %v, finding %+v", stale, f) + } + if c.bump && (f.Latest == release.CaddyVersion || strings.Contains(f.Latest, "beta") || strings.HasPrefix(f.Latest, "9.")) { + t.Errorf("moved to %s", f.Latest) + } + }) + } +} diff --git a/internal/apply/apply.go b/internal/apply/apply.go index 10bf453..0e01b6e 100644 --- a/internal/apply/apply.go +++ b/internal/apply/apply.go @@ -23,9 +23,11 @@ import ( "io" "math/big" "net/http" + "net/url" "os" "os/exec" "path/filepath" + "strconv" "strings" "time" @@ -40,15 +42,15 @@ import ( "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/state" ) -// Images are the defaults. They are tags rather than digests for now: the -// registry is ours and the tags are immutable releases, and pinning digests -// here would mean this program needing a release of its own for every one of -// theirs. +// Images are the defaults. The suite's own name one release each, by tag: +// the registry is ours, but its tags can be pushed again by anyone holding a +// push token, so they are a release's name rather than a guarantee of its +// contents. Caddy, from Docker Hub, is pinned by digest (release.CaddyImage). const ( DefaultServerImage = release.ServerImage DefaultConsoleImage = "registry.coffeylabs.org/inbuxa/inbuxa-admin:" + release.ConsoleVersion DefaultWebmailImage = "registry.coffeylabs.org/inbuxa/inbuxa-webmail:" + release.WebmailVersion - DefaultCaddyImage = "docker.io/library/caddy:2-alpine" + DefaultCaddyImage = release.CaddyImage DefaultSubnet = "172.31.253.0/24" // DefaultIPv6Subnet is a unique local /64 for the same network, on a // machine with IPv6 (#65). Nothing outside the machine ever sees it. @@ -322,7 +324,7 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro // listener on the host, so the router's is fitted again after one. if stack.PassThrough && (fitRouter || mv != nil) { log.Step("fitting the server's HTTPS to your web server's name router") - srv := &jmap.Client{BaseURL: "http://" + hostFinalBind, Token: o.Key} + srv := setupClient(onHost, jmap.Client{BaseURL: "http://" + hostFinalBind, Token: o.Key}) if err := routeByName(ctx, srv, stack, onHost); err != nil { return nil, fmt.Errorf("fitting the server's HTTPS listener: %w", err) } @@ -343,7 +345,7 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro log.Info("%s", LogFilesHint) } else { log.Step("turning on the server's log files") - srv := &jmap.Client{BaseURL: "http://" + hostFinalBind, Token: o.Key} + srv := setupClient(onHost, jmap.Client{BaseURL: "http://" + hostFinalBind, Token: o.Key}) if lerr := turnOnLogFiles(ctx, srv, onHost, mv != nil, cmp, log); lerr != nil { log.Info("could not turn on the log files the console's Logs page reads: %v", lerr) } else if !existing.LogFiles { @@ -382,12 +384,12 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro if err != nil { return nil, err } - override := filepath.Join(os.TempDir(), "inbuxa-bootstrap.yaml") - if err := os.WriteFile(override, []byte( - "services:\n server:\n environment:\n INBUXA_RECOVERY_ADMIN: ${INBUXA_BOOTSTRAP_ADMIN:?}\n"), 0o600); err != nil { + override, cleanup, err := setupOverride("inbuxa-bootstrap.yaml", + "services:\n server:\n environment:\n INBUXA_RECOVERY_ADMIN: ${INBUXA_BOOTSTRAP_ADMIN:?}\n") + if err != nil { return nil, err } - defer os.Remove(override) + defer cleanup() log.Step("starting the mail server in bootstrap mode") serverURL := "http://" + stack.ServerBind @@ -416,7 +418,7 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro return nil, withLogs(ctx, err, srvLogs, "server") } - bootClient := &jmap.Client{BaseURL: serverURL, Username: "admin", Password: bootPass} + bootClient := setupClient(onHost, jmap.Client{BaseURL: serverURL, Username: "admin", Password: bootPass}) if err := bootClient.CheckBootstrapMode(ctx); err != nil { return nil, err } @@ -449,12 +451,12 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro // does the rest of the setup, is removed when the setup ends, and expires // within the hour anyway, so a setup that stops halfway leaves nothing // that still works. - basicOverride := filepath.Join(os.TempDir(), "inbuxa-basic-auth.yaml") - if err := os.WriteFile(basicOverride, []byte( - "services:\n server:\n environment:\n INBUXA_HTTP_BASIC_AUTH: all\n"), 0o600); err != nil { + basicOverride, cleanupBasic, err := setupOverride("inbuxa-basic-auth.yaml", + "services:\n server:\n environment:\n INBUXA_HTTP_BASIC_AUTH: all\n") + if err != nil { return res, err } - defer os.Remove(basicOverride) + defer cleanupBasic() log.Step("restarting the mail server configured, to give the setup an API key") if onHost { if err := hs.Start(ctx, modeBasic, ""); err != nil { @@ -467,7 +469,7 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro return res, err } } - adminBasic := &jmap.Client{BaseURL: serverURL, Username: admin.Username, Password: admin.Secret} + adminBasic := setupClient(onHost, jmap.Client{BaseURL: serverURL, Username: admin.Username, Password: admin.Secret}) var keyID, key string if err := waitFor(ctx, log, "the server to come back configured", 120*time.Second, func(ctx context.Context) error { var err error @@ -476,7 +478,7 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro }); err != nil { return res, withLogs(ctx, fmt.Errorf("an API key for the setup: %w", err), srvLogs, "server") } - srv := &jmap.Client{BaseURL: serverURL, Token: key} + srv := setupClient(onHost, jmap.Client{BaseURL: serverURL, Token: key}) defer func() { ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) defer cancel() @@ -1044,3 +1046,78 @@ func password(n int) (string, error) { } return string(b), nil } + +// setupOverride writes a compose override for one step of first boot, and +// returns its path and what removes it. It goes in a new directory only root +// can reach, never at a fixed name in the shared temporary directory, where +// another local user could have put a file or a link of its own first. +func setupOverride(name, content string) (string, func(), error) { + dir, err := os.MkdirTemp("", "inbuxa-setup-") + if err != nil { + return "", nil, err + } + cleanup := func() { os.RemoveAll(dir) } + path := filepath.Join(dir, name) + f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) + if err == nil { + _, err = f.WriteString(content) + if cerr := f.Close(); err == nil { + err = cerr + } + } + if err != nil { + cleanup() + return "", nil, err + } + return path, cleanup, nil +} + +// setupClient is a client for the server at a loopback address, carrying +// the administrator's password or an API key. On the host those are ports +// 8080 and 8081, which any local user may listen on while the installer +// restarts the server: so before each request, whatever listens there must +// be the server the unit started, or nothing is sent. In a container the +// runtime publishes the port and nobody else can take it. +// +// The check is of the client's address at the time of each request, as +// first boot moves its client from 8080 to 8081 once the listeners are +// fitted. +func setupClient(onHost bool, c jmap.Client) *jmap.Client { + cl := &c + if onHost { + cl.Guard = func(ctx context.Context) error { return serversOwnListener(ctx, cl.BaseURL) } + } + return cl +} + +// serversOwnListener is nil when what listens on base's port is the host +// server unit's main process. +func serversOwnListener(ctx context.Context, base string) error { + u, err := url.Parse(base) + if err != nil { + return err + } + port, err := strconv.Atoi(u.Port()) + if err != nil { + return fmt.Errorf("%s names no port", base) + } + pid := serverMainPID(ctx) + if pid <= 0 { + return fmt.Errorf("%s is not running", HostServerUnit) + } + if err := host.PortHeldBy(port, pid); err != nil { + return fmt.Errorf("%w; the mail server is pid %d, so nothing was sent there", err, pid) + } + return nil +} + +// serverMainPID is the host server unit's main process, or 0; a variable so +// a test can stand in for systemd. +var serverMainPID = func(ctx context.Context) int { + out, err := exec.CommandContext(ctx, "systemctl", "show", "-p", "MainPID", "--value", HostServerUnit).Output() + if err != nil { + return 0 + } + pid, _ := strconv.Atoi(strings.TrimSpace(string(out))) + return pid +} diff --git a/internal/apply/hostdirs_test.go b/internal/apply/hostdirs_test.go new file mode 100644 index 0000000..0ebed5d --- /dev/null +++ b/internal/apply/hostdirs_test.go @@ -0,0 +1,75 @@ +// SPDX-FileCopyrightText: 2026 Coffey Labs LLC +// SPDX-License-Identifier: AGPL-3.0-or-later + +package apply + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" +) + +// The server's user can write its home, so it can swap data/ for a symbolic +// link to a directory of root's. Install runs again as root on every +// converge or move, and must not then give that directory to the server's +// user (INSTALLER-1). +func TestServerDirsRefusesALinkedDataDir(t *testing.T) { + deploy := t.TempDir() + victim := t.TempDir() // a directory of root's, as far as the installer knows + h := hostServer{deploy: deploy} + if err := os.MkdirAll(h.home(), 0o750); err != nil { + t.Fatal(err) + } + if err := os.Symlink(victim, filepath.Join(h.home(), "data")); err != nil { + t.Fatal(err) + } + + var chowned []string + defer func(f func(context.Context, ...string) error) { chownServer = f }(chownServer) + chownServer = func(_ context.Context, args ...string) error { + chowned = append(chowned, strings.Join(args, " ")) + return nil + } + + err := h.serverDirs(context.Background()) + if err == nil || !strings.Contains(err.Error(), "symbolic link") { + t.Errorf("a data directory that is a symbolic link should be refused, got %v", err) + } + for _, c := range chowned { + // chown follows a link it is given unless told -h, and always + // through a trailing slash. + if !strings.HasPrefix(c, "-h ") || strings.HasSuffix(c, "/") || strings.Contains(c, "/data") { + t.Errorf("chown %s: would reach the link's target", c) + } + } +} + +// A first install, and a converge over directories already right, make +// both and give them to the server's user without following anything. +func TestServerDirsMakesBoth(t *testing.T) { + h := hostServer{deploy: t.TempDir()} + var chowned []string + defer func(f func(context.Context, ...string) error) { chownServer = f }(chownServer) + chownServer = func(_ context.Context, args ...string) error { + chowned = append(chowned, strings.Join(args, " ")) + return nil + } + for range 2 { + chowned = nil + if err := h.serverDirs(context.Background()); err != nil { + t.Fatal(err) + } + want := []string{ + "-h inbuxa:inbuxa " + h.home(), + "-h inbuxa:inbuxa " + filepath.Join(h.home(), "data"), + } + if strings.Join(chowned, "|") != strings.Join(want, "|") { + t.Errorf("chown %v, want %v", chowned, want) + } + if fi, err := os.Lstat(filepath.Join(h.home(), "data")); err != nil || !fi.IsDir() { + t.Errorf("data/ is not a directory: %v", err) + } + } +} diff --git a/internal/apply/hostserver.go b/internal/apply/hostserver.go index da5c35e..45e2474 100644 --- a/internal/apply/hostserver.go +++ b/internal/apply/hostserver.go @@ -27,6 +27,7 @@ package apply import ( "bytes" "context" + "errors" "fmt" "os" "os/exec" @@ -122,17 +123,65 @@ func (h hostServer) Install(ctx context.Context) (replaced bool, err error) { if err := os.Chmod(h.deploy, 0o711); err != nil { return false, err } - for _, d := range []string{h.home(), h.DataPath()} { - if err := os.MkdirAll(d, 0o750); err != nil { - return false, err - } - if err := runQuiet(ctx, "chown", HostServerUser+":"+HostServerUser, d); err != nil { - return false, err - } + if err := h.serverDirs(ctx); err != nil { + return false, err } return replaced, nil } +// chownServer runs chown; a variable so a test can watch it without root. +var chownServer = func(ctx context.Context, args ...string) error { + return runQuiet(ctx, "chown", args...) +} + +// serverDirs makes the server's home and its data directory and gives them +// to the server's user. The home is in the deployment directory, which is +// root's, but data/ is in the home, which that user can write: something +// running as it could have put a symbolic link there, and following the +// link would give it whatever directory the link names. So a data/ that is +// not a plain directory is refused, and chown is given -h and no trailing +// slash, so a link swapped in after the check is what changes owner, not +// what it points at. +func (h hostServer) serverDirs(ctx context.Context) error { + for _, d := range []string{h.home(), filepath.Join(h.home(), "data")} { + if err := plainDir(d, 0o750); err != nil { + return err + } + if err := chownServer(ctx, "-h", HostServerUser+":"+HostServerUser, d); err != nil { + return err + } + } + return nil +} + +// plainDir makes dir when it is not there, and refuses one that is a +// symbolic link or not a directory at all. +func plainDir(dir string, mode os.FileMode) error { + fi, err := os.Lstat(dir) + if errors.Is(err, os.ErrNotExist) { + if err := os.Mkdir(dir, mode); err != nil && !errors.Is(err, os.ErrExist) { + return err + } + fi, err = os.Lstat(dir) + } + if err != nil { + return err + } + return isPlainDir(dir, fi) +} + +func isPlainDir(dir string, fi os.FileInfo) error { + switch { + case fi.Mode()&os.ModeSymlink != 0: + return fmt.Errorf("%s is a symbolic link, where the installer keeps a directory of the server's; "+ + "nothing was changed through it. Something running as %s may have put it there: look at where it points, "+ + "then move it aside and run this again", dir, HostServerUser) + case !fi.IsDir(): + return fmt.Errorf("%s is not a directory", dir) + } + return nil +} + func (h hostServer) fetchBinary(ctx context.Context) error { arch := runtime.GOARCH sum, ok := release.ServerSHA256[arch] diff --git a/internal/apply/images_test.go b/internal/apply/images_test.go new file mode 100644 index 0000000..b921d88 --- /dev/null +++ b/internal/apply/images_test.go @@ -0,0 +1,29 @@ +// SPDX-FileCopyrightText: 2026 Coffey Labs LLC +// SPDX-License-Identifier: AGPL-3.0-or-later + +package apply + +import ( + "regexp" + "testing" +) + +// Everything this installer downloads as a file is checked against a +// checksum in its source. Caddy, the proxy that holds the TLS keys, was +// pulled by a floating tag from Docker Hub instead, so whoever could push +// that tag chose what ran (INSTALLER-5). It is pinned by its digest now. +func TestCaddyIsPinnedByDigest(t *testing.T) { + if !regexp.MustCompile(`@sha256:[0-9a-f]{64}$`).MatchString(DefaultCaddyImage) { + t.Errorf("DefaultCaddyImage = %q, not pinned by digest", DefaultCaddyImage) + } +} + +// The suite's own images name one exact release, never a moving tag. +func TestSuiteImagesNameAnExactRelease(t *testing.T) { + exact := regexp.MustCompile(`:\d+\.\d+(\.\d+)*(-rc\.\d+)?$`) + for _, img := range []string{DefaultServerImage, DefaultConsoleImage, DefaultWebmailImage} { + if !exact.MatchString(img) { + t.Errorf("%s does not name an exact release", img) + } + } +} diff --git a/internal/apply/listener_test.go b/internal/apply/listener_test.go new file mode 100644 index 0000000..8c9f07f --- /dev/null +++ b/internal/apply/listener_test.go @@ -0,0 +1,102 @@ +// SPDX-FileCopyrightText: 2026 Coffey Labs LLC +// SPDX-License-Identifier: AGPL-3.0-or-later + +package apply + +import ( + "context" + "net/http" + "net/http/httptest" + "os" + "sync" + "testing" + "time" + + "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/jmap" +) + +// squatter listens on loopback as this test's own process and records the +// Authorization header of anything sent to it. +func squatter(t *testing.T) (url string, seen func() []string) { + t.Helper() + var mu sync.Mutex + var got []string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + got = append(got, r.Header.Get("Authorization")) + mu.Unlock() + w.WriteHeader(http.StatusServiceUnavailable) + })) + t.Cleanup(srv.Close) + return srv.URL, func() []string { + mu.Lock() + defer mu.Unlock() + return append([]string(nil), got...) + } +} + +// During a host first boot or a move the server is restarted and then sent +// the administrator's password or an API key on 127.0.0.1:8080 or :8081, +// ports any local user may listen on while the server is down. What answers +// there must be the server the unit started before anything is sent to it +// (INSTALLER-4). Here the listener is this test's, and the unit's main +// process is another one. +func TestSetupCredentialsGoOnlyToTheServersOwnListener(t *testing.T) { + url, seen := squatter(t) + defer func(f func(context.Context) int) { serverMainPID = f }(serverMainPID) + serverMainPID = func(context.Context) int { return os.Getppid() } + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + admin := setupClient(true, jmap.Client{BaseURL: url, Username: "admin", Password: "the-admin-password"}) + if _, _, err := admin.CreateAPIKey(ctx, "setup", time.Hour); err == nil { + t.Error("an API key was made through a listener that isn't the server's") + } + keyed := setupClient(true, jmap.Client{BaseURL: url, Token: "the-api-key"}) + if err := keyed.HostListeners(ctx, hostFinalBind, false); err == nil { + t.Error("the listeners were fitted through a listener that isn't the server's") + } + if got := seen(); len(got) != 0 { + t.Errorf("credentials went to a listener that isn't the server's: %q", got) + } +} + +// When the listener is the server's, the request goes through; and a +// container's server, reached through the runtime's port, is not checked. +func TestSetupCredentialsGoToTheServer(t *testing.T) { + url, seen := squatter(t) + defer func(f func(context.Context) int) { serverMainPID = f }(serverMainPID) + serverMainPID = func(context.Context) int { return os.Getpid() } + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + setupClient(true, jmap.Client{BaseURL: url, Token: "k"}).Listeners(ctx) + serverMainPID = func(context.Context) int { return os.Getppid() } + setupClient(false, jmap.Client{BaseURL: url, Token: "k"}).Listeners(ctx) + if got := seen(); len(got) != 2 { + t.Errorf("want both requests sent, got %q", got) + } +} + +// First boot moves its client from 8080 to 8081 once the listeners are +// fitted; the check follows it there. +func TestSetupCredentialsCheckTheClientsCurrentAddress(t *testing.T) { + old, _ := squatter(t) + url, seen := squatter(t) + defer func(f func(context.Context) int) { serverMainPID = f }(serverMainPID) + serverMainPID = func(context.Context) int { return os.Getpid() } + srv := setupClient(true, jmap.Client{BaseURL: old, Token: "k"}) + srv.BaseURL = url + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + srv.Listeners(ctx) + if got := seen(); len(got) != 1 { + t.Errorf("want the request sent to the new address, got %q", got) + } + // Both of the test's listeners are its own, so stand in a listener + // that is not: a closed one, where nothing listens any more. + srv.BaseURL = "http://127.0.0.1:1" + if err := serversOwnListener(ctx, srv.BaseURL); err == nil { + t.Error("a port nothing listens on passed the check") + } +} diff --git a/internal/apply/move.go b/internal/apply/move.go index b2d6b30..1a3b7d9 100644 --- a/internal/apply/move.go +++ b/internal/apply/move.go @@ -34,6 +34,7 @@ import ( "os/exec" "path/filepath" "strings" + "syscall" "time" "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/compose" @@ -313,7 +314,7 @@ func (m *serverMove) toHost(ctx context.Context, stack compose.Stack, snippets b if err := m.cameBack(ctx, hostSetupBind, func() string { return hs.Logs(ctx, "server", 400) }); err != nil { return back(withLogs(ctx, err, hs, "server")) } - srv := &jmap.Client{BaseURL: hostSetupBind, Token: m.key} + srv := setupClient(true, jmap.Client{BaseURL: hostSetupBind, Token: m.key}) if err := srv.HostListeners(ctx, hostFinalBind, m.local); err != nil { return back(fmt.Errorf("fitting the server's listeners to the host: %w", err)) } @@ -459,6 +460,12 @@ func (m *serverMove) restoreHost(ctx context.Context, why error) error { // copied into it: what an earlier install on the host left there is // archived, never mixed with this. func (m *serverMove) clearForCopy(ctx context.Context, hs hostServer) error { + data := filepath.Join(hs.home(), "data") + if fi, err := os.Lstat(data); err == nil { + if err := isPlainDir(data, fi); err != nil { + return err + } + } entries, _ := os.ReadDir(hs.DataPath()) if len(entries) == 0 && !exists(hs.configPath()) { return nil @@ -507,8 +514,17 @@ func allowOnce(ctx context.Context, srv *jmap.Client, address, reason string) er // rewriteDataPath points the data store's description at its new home. Only // a path under the old home is changed: a store kept elsewhere (PostgreSQL, // a path the operator chose) is left alone. +// +// config.json is in the server's home, which the server's user can write, so +// it is read without following a link and replaced by a rename, never +// written through whatever is there. func rewriteDataPath(configPath, from, to string) error { - b, err := os.ReadFile(configPath) + f, err := openNoFollow(configPath) + if err != nil { + return fmt.Errorf("the server's configuration: %w", err) + } + b, err := io.ReadAll(f) + f.Close() if err != nil { return fmt.Errorf("the server's configuration: %w", err) } @@ -525,11 +541,42 @@ func rewriteDataPath(configPath, from, to string) error { if err != nil { return err } - info, err := os.Stat(configPath) + info, err := os.Lstat(configPath) if err != nil { return err } - return os.WriteFile(configPath, out, info.Mode().Perm()) + return replaceFile(configPath, out, info.Mode().Perm()) +} + +// openNoFollow opens a file for reading, refusing a symbolic link. +func openNoFollow(path string) (*os.File, error) { + f, err := os.OpenFile(path, os.O_RDONLY|syscall.O_NOFOLLOW, 0) + if errors.Is(err, syscall.ELOOP) { + return nil, fmt.Errorf("%s is a symbolic link, which the installer will not follow there", path) + } + return f, err +} + +// replaceFile puts content at path by writing a new file beside it and +// renaming it over the old one: a link at path is replaced, not followed. +func replaceFile(path string, content []byte, mode os.FileMode) error { + tmp, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".") + if err != nil { + return err + } + defer os.Remove(tmp.Name()) + if _, err := tmp.Write(content); err != nil { + tmp.Close() + return err + } + if err := tmp.Chmod(mode); err != nil { + tmp.Close() + return err + } + if err := tmp.Close(); err != nil { + return err + } + return os.Rename(tmp.Name(), path) } // moveContents renames everything in src into dest, which is made if need @@ -553,7 +600,7 @@ func moveContents(src, dest string) error { } func copyFile(src, dest string, mode os.FileMode) error { - in, err := os.Open(src) + in, err := openNoFollow(src) if err != nil { return err } diff --git a/internal/apply/move_test.go b/internal/apply/move_test.go index 325f7b4..9b143a6 100644 --- a/internal/apply/move_test.go +++ b/internal/apply/move_test.go @@ -108,3 +108,28 @@ func TestMovedContentsArriveAndLeaveTheStageEmpty(t *testing.T) { t.Errorf("nothing to move is not an error: %v", err) } } + +// config.json is in the server's home, which its user can write. A move runs +// as root and must not read or write through a link planted there +// (INSTALLER-1). +func TestRewriteDataPathRefusesALink(t *testing.T) { + dir := t.TempDir() + victim := filepath.Join(t.TempDir(), "root.json") + body := `{"path":"/var/lib/inbuxa/x","secret":"root's"}` + if err := os.WriteFile(victim, []byte(body), 0o600); err != nil { + t.Fatal(err) + } + cfg := filepath.Join(dir, "config.json") + if err := os.Symlink(victim, cfg); err != nil { + t.Fatal(err) + } + if err := rewriteDataPath(cfg, containerDataPath, "/var/lib/inbuxa/server/data/"); err == nil { + t.Error("a config.json that is a symbolic link should be refused") + } + if b, _ := os.ReadFile(victim); string(b) != body { + t.Errorf("written through the link: %s", b) + } + if err := copyFile(cfg, filepath.Join(dir, "staged.json"), 0o640); err == nil { + t.Error("copying a config.json that is a symbolic link should be refused") + } +} diff --git a/internal/apply/tempfiles_test.go b/internal/apply/tempfiles_test.go new file mode 100644 index 0000000..3f19569 --- /dev/null +++ b/internal/apply/tempfiles_test.go @@ -0,0 +1,51 @@ +// SPDX-FileCopyrightText: 2026 Coffey Labs LLC +// SPDX-License-Identifier: AGPL-3.0-or-later + +package apply + +import ( + "os" + "path/filepath" + "testing" +) + +// The setup's compose overrides are given to docker compose as root. Put at a +// fixed name in the shared temporary directory, they could be planted +// beforehand by any local user: as a link, to have root write over another +// file, or as their own file, to rewrite it after root wrote it +// (INSTALLER-3). Each now goes in a directory of its own that only root can +// reach, and is gone after. +func TestSetupOverrideIsNotAtAFixedNameInTmp(t *testing.T) { + shared := t.TempDir() + t.Setenv("TMPDIR", shared) + victim := filepath.Join(t.TempDir(), "victim") + if err := os.WriteFile(victim, []byte("root's\n"), 0o600); err != nil { + t.Fatal(err) + } + for _, name := range []string{"inbuxa-bootstrap.yaml", "inbuxa-basic-auth.yaml"} { + if err := os.Symlink(victim, filepath.Join(shared, name)); err != nil { + t.Fatal(err) + } + } + + path, cleanup, err := setupOverride("inbuxa-bootstrap.yaml", "services: {}\n") + if err != nil { + t.Fatal(err) + } + if b, _ := os.ReadFile(victim); string(b) != "root's\n" { + t.Errorf("written through a planted link: %q", b) + } + if b, _ := os.ReadFile(path); string(b) != "services: {}\n" { + t.Errorf("the override holds %q", b) + } + dir := filepath.Dir(path) + if dir == shared { + t.Errorf("%s is straight in the shared temporary directory", path) + } else if fi, err := os.Lstat(dir); err != nil || !fi.IsDir() || fi.Mode().Perm() != 0o700 { + t.Errorf("%s should be a directory only its owner can reach: %v %v", dir, fi, err) + } + cleanup() + if _, err := os.Lstat(dir); err == nil && dir != shared { + t.Errorf("%s is left behind", dir) + } +} diff --git a/internal/cluster/join.go b/internal/cluster/join.go index 2ab221f..e859eb5 100644 --- a/internal/cluster/join.go +++ b/internal/cluster/join.go @@ -24,11 +24,16 @@ package cluster import ( "context" "encoding/json" + "errors" "fmt" + "io" "os" "os/exec" + "os/user" "path/filepath" + "strconv" "strings" + "syscall" "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/topology" ) @@ -142,7 +147,10 @@ func BuildPlan(c Config) (*Plan, error) { if _, err := os.Stat(dst); err != nil { return nil, fmt.Errorf("%s is not at %s and no secret_from says where to install it from", s.what, dst) } - continue // already in place, left as it is + continue // already in place, left as it is, and only the server reads it + } + if err := c.notALink(dst); err != nil { + return nil, err } if _, err := os.Stat(s.from); err != nil { return nil, fmt.Errorf("%s: the source %s is not there", s.what, s.from) @@ -152,9 +160,14 @@ func BuildPlan(c Config) (*Plan, error) { // The existing config.json, kept aside once so a node can be put back on // its old store. Only when there is one and no copy has been made before. - if _, err := os.Stat(c.configPath()); err == nil { - backup := c.configPath() + ".prev" - if _, err := os.Stat(backup); err != nil { + backup := c.configPath() + ".prev" + for _, f := range []string{c.configPath(), backup} { + if err := c.notALink(f); err != nil { + return nil, err + } + } + if _, err := os.Lstat(c.configPath()); err == nil { + if _, err := os.Lstat(backup); err != nil { p.Backup = backup } } @@ -239,18 +252,26 @@ func orderDropIn(after []string) string { // Apply carries out the plan: the secrets, then the store config, then the // drop-ins, then a reload and restart. The order matters -- the config and its // secret are in place before the server is told to read them. +// +// The server's directory is the server user's, so whatever is in it may have +// been put there by that user. Nothing is read or written there through a +// symbolic link: config.json is read without following one, and every file +// is written beside its place, given to the server's user by its descriptor, +// and renamed into place, which replaces a link instead of writing through it. func Apply(ctx context.Context, p *Plan) error { c := p.Config run := c.run() + owner := c.owner() if p.Backup != "" { c.log("keeping the old store config at %s", p.Backup) if !c.DryRun { - if err := copyFile(c.configPath(), p.Backup, 0o600); err != nil { - return fmt.Errorf("saving the old config aside: %w", err) + b, err := readNoFollow(c.configPath()) + if err == nil { + err = putFile(p.Backup, b, 0o600, owner) } - if c.User != "" { - chown(ctx, run, c.User, p.Backup) + if err != nil { + return fmt.Errorf("saving the old config aside: %w", err) } } } @@ -274,17 +295,22 @@ func Apply(ctx context.Context, p *Plan) error { if err := os.MkdirAll(filepath.Dir(w.Path), 0o750); err != nil { return err } - if w.From != "" { - if err := copyFile(w.From, w.Path, w.Mode); err != nil { - return fmt.Errorf("installing %s from %s: %w", w.Path, w.From, err) - } - } else if err := os.WriteFile(w.Path, []byte(w.Content), w.Mode); err != nil { - return err - } // Secrets and the store config are the server's to read; the drop-ins // are root's, as systemd wants them. - if c.User != "" && withinEtc(c, w.Path) { - chown(ctx, run, c.User, w.Path) + var to *ids + if withinEtc(c, w.Path) { + to = owner + } + if w.From != "" { + b, err := os.ReadFile(w.From) + if err == nil { + err = putFile(w.Path, b, w.Mode, to) + } + if err != nil { + return fmt.Errorf("installing %s from %s: %w", w.Path, w.From, err) + } + } else if err := putFile(w.Path, []byte(w.Content), w.Mode, to); err != nil { + return err } } @@ -312,21 +338,77 @@ func withinEtc(c Config, path string) bool { return err == nil && !strings.HasPrefix(rel, "..") } -func chown(ctx context.Context, run func(context.Context, string, ...string) error, user, path string) { - // Best effort: ownership needs root, and a dry run or a test without it - // should not fail over it. The mode is what keeps a secret unreadable. - _ = run(ctx, "chown", user+":"+user, path) +// notALink refuses a symbolic link at a path in the server's directory, +// which the join would read or write as root. +func (c Config) notALink(path string) error { + if !withinEtc(c, path) { + return nil // a path the operator chose, outside the server's directory + } + if fi, err := os.Lstat(path); err == nil && fi.Mode()&os.ModeSymlink != 0 { + return fmt.Errorf("%s is a symbolic link, in a directory the server's user can write; the join "+ + "will not read or write through it. Look at where it points, then move it aside and run this again", path) + } + return nil } -func copyFile(src, dst string, mode os.FileMode) error { - b, err := os.ReadFile(src) +// ids are the server user's, for the files written for it. +type ids struct{ uid, gid int } + +// owner is the server's user, or nil when none is named or it can't be +// found. Ownership is best effort, as it needs root, and a dry run or a test +// without it should not fail over it: the mode is what keeps a secret +// unreadable. +func (c Config) owner() *ids { + if c.User == "" { + return nil + } + u, err := user.Lookup(c.User) + if err != nil { + return nil + } + uid, err1 := strconv.Atoi(u.Uid) + gid, err2 := strconv.Atoi(u.Gid) + if err1 != nil || err2 != nil { + return nil + } + return &ids{uid, gid} +} + +// readNoFollow reads a file, refusing a symbolic link. +func readNoFollow(path string) ([]byte, error) { + f, err := os.OpenFile(path, os.O_RDONLY|syscall.O_NOFOLLOW, 0) + if err != nil { + if errors.Is(err, syscall.ELOOP) { + return nil, fmt.Errorf("%s is a symbolic link, which the join will not follow", path) + } + return nil, err + } + defer f.Close() + return io.ReadAll(f) +} + +// putFile writes content to a new file beside path, gives it its mode and +// owner through the open file, and renames it over path. +func putFile(path string, content []byte, mode os.FileMode, owner *ids) error { + tmp, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".") if err != nil { return err } - if err := os.WriteFile(dst, b, mode); err != nil { + defer os.Remove(tmp.Name()) + _, err = tmp.Write(content) + if err == nil { + err = tmp.Chmod(mode) + } + if err == nil && owner != nil { + _ = tmp.Chown(owner.uid, owner.gid) // best effort, as above + } + if cerr := tmp.Close(); err == nil { + err = cerr + } + if err != nil { return err } - return os.Chmod(dst, mode) + return os.Rename(tmp.Name(), path) } // String renders the plan the way --dry-run shows it, in the installer's diff --git a/internal/cluster/join_test.go b/internal/cluster/join_test.go index 5df1a26..64a9f07 100644 --- a/internal/cluster/join_test.go +++ b/internal/cluster/join_test.go @@ -7,8 +7,11 @@ import ( "context" "encoding/json" "os" + "os/user" "path/filepath" + "strconv" "strings" + "syscall" "testing" "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/topology" @@ -226,3 +229,141 @@ func TestNoServerDir(t *testing.T) { t.Error("a join before the server is installed should fail") } } + +// victimFile is a file of root's outside the server's directory, which a +// link planted in that directory points at. +func victimFile(t *testing.T) string { + t.Helper() + p := filepath.Join(t.TempDir(), "shadow") + if err := os.WriteFile(p, []byte("root:secret-hash\n"), 0o600); err != nil { + t.Fatal(err) + } + return p +} + +func untouched(t *testing.T, path string) { + t.Helper() + if b, _ := os.ReadFile(path); string(b) != "root:secret-hash\n" { + t.Errorf("%s was written through a link: %q", path, b) + } +} + +// The server's directory is its user's, so that user can replace config.json +// or a secret with a symbolic link. A join runs as root and must neither +// write through such a link nor copy what it points at into the rollback +// copy (INSTALLER-1). +func TestJoinRefusesLinksInTheServersDirectory(t *testing.T) { + for _, name := range []string{"config.json", "config.json.prev", "pg.pw"} { + t.Run(name, func(t *testing.T) { + cfg, _ := fixture(t, clusterNode(t, t.TempDir())) + victim := victimFile(t) + // A link to a file not there yet, for the one a join only + // writes when it is missing. + absent := filepath.Join(filepath.Dir(victim), "cron.d-x") + link := filepath.Join(cfg.EtcDir, name) + os.Remove(link) + target := victim + if name == "config.json.prev" { + target = absent + } + if err := os.Symlink(target, link); err != nil { + t.Fatal(err) + } + p, err := BuildPlan(cfg) + if err == nil { + err = Apply(context.Background(), p) + } + if err == nil || !strings.Contains(err.Error(), "symbolic link") { + t.Errorf("a link at %s should be refused, got %v", name, err) + } + untouched(t, victim) + if _, err := os.Stat(absent); err == nil { + t.Errorf("%s was created through a link", absent) + } + prev := cfg.configPath() + ".prev" + if fi, err := os.Lstat(prev); err == nil && fi.Mode().IsRegular() { + if b, _ := os.ReadFile(prev); strings.Contains(string(b), "secret-hash") { + t.Error("the rollback copy holds the link's target") + } + } + }) + } +} + +// A link swapped in after the plan was made, while the join runs, is +// replaced, never written through. +func TestJoinDoesNotWriteThroughALinkPlantedAfterThePlan(t *testing.T) { + cfg, _ := fixture(t, clusterNode(t, t.TempDir())) + p, err := BuildPlan(cfg) + if err != nil { + t.Fatal(err) + } + victim := victimFile(t) + for _, name := range []string{"pg.pw", "nats.pw"} { + if err := os.Symlink(victim, filepath.Join(cfg.EtcDir, name)); err != nil { + t.Fatal(err) + } + } + if err := Apply(context.Background(), p); err != nil { + t.Fatal(err) + } + untouched(t, victim) + for _, name := range []string{"pg.pw", "nats.pw"} { + fi, err := os.Lstat(filepath.Join(cfg.EtcDir, name)) + if err != nil || !fi.Mode().IsRegular() || fi.Mode().Perm() != 0o400 { + t.Errorf("%s should be a plain file, mode 0400: %v %v", name, fi, err) + } + } +} + +// With the server's user named, what a join writes beside config.json is +// given to it. The current user stands in for it, so this runs without root. +func TestJoinGivesItsFilesToTheServersUser(t *testing.T) { + me, err := user.Current() + if err != nil { + t.Skip(err) + } + cfg, _ := fixture(t, clusterNode(t, t.TempDir())) + cfg.User = me.Username + p, err := BuildPlan(cfg) + if err != nil { + t.Fatal(err) + } + if err := Apply(context.Background(), p); err != nil { + t.Fatal(err) + } + for _, name := range []string{"config.json", "config.json.prev", "pg.pw"} { + fi, err := os.Lstat(filepath.Join(cfg.EtcDir, name)) + if err != nil { + t.Fatal(err) + } + if st := fi.Sys().(*syscall.Stat_t); strconv.Itoa(int(st.Uid)) != me.Uid { + t.Errorf("%s belongs to uid %d, want %s", name, st.Uid, me.Uid) + } + } +} + +// A secret already in place that the join leaves alone may be a link the +// operator made: only the server reads it, so it is not refused. +func TestJoinLeavesALinkedSecretItDoesNotWrite(t *testing.T) { + node := clusterNode(t, t.TempDir()) + cfg, _ := fixture(t, node) + kept := filepath.Join(t.TempDir(), "pg.pw") + if err := os.WriteFile(kept, []byte("pgpw"), 0o400); err != nil { + t.Fatal(err) + } + if err := os.Symlink(kept, filepath.Join(cfg.EtcDir, "pg.pw")); err != nil { + t.Fatal(err) + } + cfg.Node.Store.SecretFrom = "" + p, err := BuildPlan(cfg) + if err != nil { + t.Fatal(err) + } + if err := Apply(context.Background(), p); err != nil { + t.Fatal(err) + } + if fi, err := os.Lstat(filepath.Join(cfg.EtcDir, "pg.pw")); err != nil || fi.Mode()&os.ModeSymlink == 0 { + t.Errorf("the operator's link should be left as it is: %v %v", fi, err) + } +} diff --git a/internal/deps/deps.go b/internal/deps/deps.go index fd9f1a6..94c1878 100644 --- a/internal/deps/deps.go +++ b/internal/deps/deps.go @@ -31,6 +31,7 @@ import ( "crypto/sha256" "encoding/hex" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -348,10 +349,11 @@ func nodeNeed(f host.Facts) Need { } n.steps = []step{ {"fetching node " + nodeVersion, func(ctx context.Context, log io.Writer) error { - tmp := filepath.Join(os.TempDir(), "inbuxa-node.tar.xz") - if err := fetchVerified(ctx, log, url, sum, tmp, 0o644); err != nil { + tmp, cleanup, err := fetchToTemp(ctx, log, url, sum, "inbuxa-node.tar.xz") + if err != nil { return err } + defer cleanup() // A different version unpacked over the old one would leave // the old one's files behind it. if err := os.RemoveAll("/opt/inbuxa/node"); err != nil { @@ -482,8 +484,13 @@ func fetchVerified(ctx context.Context, log io.Writer, url, want, dest string, m if err := os.MkdirAll(filepath.Dir(dest), 0o755); err != nil { return err } + // A new file, never one already at the name: what a failed run left is + // removed first, and a link there is removed, not written through. tmp := dest + ".part" - f, err := os.OpenFile(tmp, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, mode) + if err := os.Remove(tmp); err != nil && !errors.Is(err, os.ErrNotExist) { + return err + } + f, err := os.OpenFile(tmp, os.O_CREATE|os.O_EXCL|os.O_WRONLY, mode) if err != nil { return err } @@ -569,3 +576,21 @@ func describeOS(o host.OSInfo) string { } return "this distribution" } + +// fetchToTemp downloads url, checked against sum, to a temporary file named +// name, and returns its path and what removes it. The file is in a new +// directory only root can reach, so nobody else can put it there first or +// change it between the checksum and its use. +func fetchToTemp(ctx context.Context, log io.Writer, url, sum, name string) (string, func(), error) { + dir, err := os.MkdirTemp("", "inbuxa-fetch-") + if err != nil { + return "", nil, err + } + cleanup := func() { os.RemoveAll(dir) } + tmp := filepath.Join(dir, name) + if err := fetchVerified(ctx, log, url, sum, tmp, 0o644); err != nil { + cleanup() + return "", nil, err + } + return tmp, cleanup, nil +} diff --git a/internal/deps/fetch_test.go b/internal/deps/fetch_test.go new file mode 100644 index 0000000..5371457 --- /dev/null +++ b/internal/deps/fetch_test.go @@ -0,0 +1,91 @@ +// SPDX-FileCopyrightText: 2026 Coffey Labs LLC +// SPDX-License-Identifier: AGPL-3.0-or-later + +package deps + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" +) + +func serve(t *testing.T, body string) (url, sum string) { + t.Helper() + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + io.WriteString(w, body) + })) + t.Cleanup(srv.Close) + h := sha256.Sum256([]byte(body)) + return srv.URL, hex.EncodeToString(h[:]) +} + +func plantedVictim(t *testing.T) string { + t.Helper() + p := filepath.Join(t.TempDir(), "victim") + if err := os.WriteFile(p, []byte("root's\n"), 0o600); err != nil { + t.Fatal(err) + } + return p +} + +// Node is downloaded and then unpacked as root. At a fixed name in the +// shared temporary directory, a local user could plant the file, or a link, +// before the install, and change what root unpacks after its checksum +// passed (INSTALLER-3). It now goes in a directory of its own, removed after. +func TestNodeTarballIsNotAtAFixedNameInTmp(t *testing.T) { + shared := t.TempDir() + t.Setenv("TMPDIR", shared) + victim := plantedVictim(t) + for _, name := range []string{"inbuxa-node.tar.xz", "inbuxa-node.tar.xz.part"} { + if err := os.Symlink(victim, filepath.Join(shared, name)); err != nil { + t.Fatal(err) + } + } + url, sum := serve(t, "a node tarball") + path, cleanup, err := fetchToTemp(context.Background(), io.Discard, url, sum, "inbuxa-node.tar.xz") + if err != nil { + t.Fatal(err) + } + if b, _ := os.ReadFile(victim); string(b) != "root's\n" { + t.Errorf("written through a planted link: %q", b) + } + if fi, err := os.Lstat(path); err != nil || !fi.Mode().IsRegular() { + t.Errorf("%s should be a plain file: %v %v", path, fi, err) + } + dir := filepath.Dir(path) + if dir == shared { + t.Errorf("%s is straight in the shared temporary directory", path) + } else if fi, err := os.Lstat(dir); err != nil || fi.Mode().Perm() != 0o700 { + t.Errorf("%s should be a directory only its owner can reach: %v %v", dir, fi, err) + } + cleanup() + if _, err := os.Lstat(path); err == nil { + t.Errorf("%s is left behind", path) + } +} + +// A download is written to a new file, never through whatever is at its +// .part name already. +func TestFetchVerifiedDoesNotWriteThroughAPartLink(t *testing.T) { + dest := filepath.Join(t.TempDir(), "file") + victim := plantedVictim(t) + if err := os.Symlink(victim, dest+".part"); err != nil { + t.Fatal(err) + } + url, sum := serve(t, "the download") + if err := fetchVerified(context.Background(), io.Discard, url, sum, dest, 0o644); err != nil { + t.Fatal(err) + } + if b, _ := os.ReadFile(victim); string(b) != "root's\n" { + t.Errorf("written through a planted link: %q", b) + } + if fi, err := os.Lstat(dest); err != nil || !fi.Mode().IsRegular() { + t.Errorf("%s should be a plain file: %v %v", dest, fi, err) + } +} diff --git a/internal/host/host.go b/internal/host/host.go index 0dd14cc..737f823 100644 --- a/internal/host/host.go +++ b/internal/host/host.go @@ -477,6 +477,34 @@ func surveyPort(n int, for_ string) Port { // /proc, which means it only sees the whole truth as root; a guess is better // than nothing and the caller treats it as one. func holderOf(port int) string { + inodes := listenInodes(port) + if len(inodes) == 0 { + return "" + } + procs, err := filepath.Glob("/proc/[0-9]*/fd/*") + if err != nil { + return "" + } + for _, fd := range procs { + link, err := os.Readlink(fd) + if err != nil || !strings.HasPrefix(link, "socket:[") { + continue + } + inode := strings.TrimSuffix(strings.TrimPrefix(link, "socket:["), "]") + if !inodes[inode] { + continue + } + pid := strings.Split(fd, "/")[2] + if comm, err := os.ReadFile("/proc/" + pid + "/comm"); err == nil { + return strings.TrimSpace(string(comm)) + " (pid " + pid + ")" + } + } + return "" +} + +// listenInodes are the socket inodes listening on a TCP port, on any +// address, IPv4 or IPv6. +func listenInodes(port int) map[string]bool { inodes := map[string]bool{} for _, tab := range []string{"/proc/net/tcp", "/proc/net/tcp6"} { file, err := os.Open(tab) @@ -505,28 +533,35 @@ func holderOf(port int) string { } file.Close() } + return inodes +} + +// portHeldBy is nil when every socket listening on port is the process +// pid's own, and says why not otherwise. +func portHeldBy(port, pid int) error { + inodes := listenInodes(port) if len(inodes) == 0 { - return "" + return fmt.Errorf("nothing listens on port %d yet", port) } - procs, err := filepath.Glob("/proc/[0-9]*/fd/*") + fds, err := os.ReadDir(fmt.Sprintf("/proc/%d/fd", pid)) if err != nil { - return "" + return fmt.Errorf("the sockets of pid %d: %w", pid, err) } - for _, fd := range procs { - link, err := os.Readlink(fd) + for _, fd := range fds { + link, err := os.Readlink(fmt.Sprintf("/proc/%d/fd/%s", pid, fd.Name())) if err != nil || !strings.HasPrefix(link, "socket:[") { continue } - inode := strings.TrimSuffix(strings.TrimPrefix(link, "socket:["), "]") - if !inodes[inode] { - continue - } - pid := strings.Split(fd, "/")[2] - if comm, err := os.ReadFile("/proc/" + pid + "/comm"); err == nil { - return strings.TrimSpace(string(comm)) + " (pid " + pid + ")" - } + delete(inodes, strings.TrimSuffix(strings.TrimPrefix(link, "socket:["), "]")) } - return "" + if len(inodes) == 0 { + return nil + } + who := holderOf(port) + if who == "" { + who = "another process" + } + return fmt.Errorf("port %d is held by %s, not pid %d", port, who, pid) } func memoryMB() int { @@ -603,3 +638,7 @@ func Systemd() bool { return isSystemd() } // HolderOf names the process listening on a port, as well as /proc lets it: // "postfix (pid 812)", or "" when it can't tell. func HolderOf(port int) string { return holderOf(port) } + +// PortHeldBy is nil when everything listening on a TCP port is process +// pid's, and says what else is there otherwise. +func PortHeldBy(port, pid int) error { return portHeldBy(port, pid) } diff --git a/internal/jmap/client.go b/internal/jmap/client.go index 1f90857..92e8b77 100644 --- a/internal/jmap/client.go +++ b/internal/jmap/client.go @@ -36,6 +36,9 @@ type Client struct { // /jmap (contract C-23), so a configured server takes an API key only. Token string HTTP *http.Client + // Guard, when set, is asked before every request: an error from it + // stops the request before any credential is sent. + Guard func(context.Context) error } // Call is one method call in a request. @@ -77,6 +80,13 @@ func (e *HTTPError) Error() string { return fmt.Sprintf("HTTP %d: %s", e.Status, strings.TrimSpace(e.Body)) } +func (c *Client) guard(ctx context.Context) error { + if c.Guard == nil { + return nil + } + return c.Guard(ctx) +} + func (c *Client) httpClient() *http.Client { if c.HTTP != nil { return c.HTTP @@ -105,6 +115,9 @@ func (c *Client) do(ctx context.Context, using []string, calls ...Call) ([]Respo if err != nil { return nil, err } + if err := c.guard(ctx); err != nil { + return nil, err + } if c.Token != "" { req.Header.Set("Authorization", "Bearer "+c.Token) } else { diff --git a/internal/jmap/frontends.go b/internal/jmap/frontends.go index 99fb363..c24b30e 100644 --- a/internal/jmap/frontends.go +++ b/internal/jmap/frontends.go @@ -39,6 +39,9 @@ func (c *Client) AccountID(ctx context.Context) (string, error) { if err != nil { return "", err } + if err := c.guard(ctx); err != nil { + return "", err + } if c.Token != "" { req.Header.Set("Authorization", "Bearer "+c.Token) } else { diff --git a/internal/relay/relay.go b/internal/relay/relay.go index bddc002..b72c6d4 100644 --- a/internal/relay/relay.go +++ b/internal/relay/relay.go @@ -243,8 +243,7 @@ func Install(ctx context.Context, p Plan, o Options, log Log) error { if p.Join { log.Step("joining") - out, err := exec.CommandContext(ctx, "runuser", "-u", User, "--", - Binary, "join", o.Token, "--state", StateDir).CombinedOutput() + out, err := asRelay(ctx, "join", o.Token, "--state", StateDir).CombinedOutput() if err != nil { return fmt.Errorf("the join was refused: %s", strings.TrimSpace(strings.TrimPrefix(string(out), "inbuxa-relay: "))) } @@ -320,8 +319,8 @@ func putBinary(ctx context.Context, p Plan, log Log) error { fmt.Fprintln(log.Out(), " signed by an inbuxa release key") } // Run before it is put in place, as the relay tries a binary before an - // upgrade. - got := installedVersion(ctx, path) + // upgrade. Run as root: it is the checked file, in root's own directory. + got := versionOf(exec.CommandContext(ctx, path, "--version")) if got == "" { out, _ := exec.CommandContext(ctx, path, "--version").CombinedOutput() return fmt.Errorf("the relay binary does not run here: %s", strings.TrimSpace(string(out))) @@ -356,7 +355,7 @@ func Status(ctx context.Context, out io.Writer) bool { enabled, _ := exec.CommandContext(ctx, "systemctl", "is-enabled", Unit).Output() fmt.Fprintf(out, "%s: %s, %s at boot\n", Unit, state, strings.TrimSpace(string(enabled))) fmt.Fprintf(out, "%s: inbuxa-relay %s (this installer installs %s)\n\n", Binary, v, release.RelayVersion()) - cmd := exec.CommandContext(ctx, "runuser", "-u", User, "--", Binary, "status", "--state", StateDir) + cmd := asRelay(ctx, "status", "--state", StateDir) cmd.Stdout, cmd.Stderr = out, out cmd.Run() return true @@ -394,11 +393,11 @@ func Uninstall(ctx context.Context, purge, discardHeld bool, log Log) error { } if purge { if _, err := os.Stat(Binary); err == nil { - args := []string{"-u", User, "--", Binary, "reset", "--state", StateDir} + args := []string{"reset", "--state", StateDir} if discardHeld { args = append(args, "--discard-held") } - out, err := exec.CommandContext(ctx, "runuser", args...).CombinedOutput() + out, err := asRelay(ctx, args...).CombinedOutput() if err != nil { msg := strings.TrimSpace(strings.TrimPrefix(string(out), "inbuxa-relay: ")) msg = strings.ReplaceAll(msg, "Run `inbuxa-relay reset --discard-held`", "Add --discard-held") @@ -509,16 +508,36 @@ func unitLinkListen(unit string) string { return "" } -// installedVersion is the version a relay binary says it is, or "" when it -// isn't there or doesn't run. +// installedVersion is the version the installed relay binary at path says +// it is, or "" when it isn't there or doesn't run. It is asked as the relay +// user: the binary is that user's to replace, so root never runs it. func installedVersion(ctx context.Context, path string) string { - out, err := exec.CommandContext(ctx, path, "--version").Output() + return versionOf(asRelayRun(ctx, path, "--version")) +} + +func versionOf(cmd *exec.Cmd) string { + out, err := cmd.Output() if err != nil { return "" } return parseVersionOutput(string(out)) } +// asRelay runs the installed binary with args as the relay user, the one +// that owns it and may have replaced it, never as root. +func asRelay(ctx context.Context, args ...string) *exec.Cmd { + return asRelayRun(ctx, Binary, args...) +} + +// asRelayRun is asRelay for the binary at path. It runs in a session of its +// own, without root's terminal as its controlling one, so it can't push +// input into the shell that ran the installer. +func asRelayRun(ctx context.Context, path string, args ...string) *exec.Cmd { + cmd := exec.CommandContext(ctx, "runuser", append([]string{"-u", User, "--", path}, args...)...) + cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true} + return cmd +} + func parseVersionOutput(out string) string { f := strings.Fields(out) if len(f) != 2 || f[0] != "inbuxa-relay" { diff --git a/internal/relay/version_test.go b/internal/relay/version_test.go new file mode 100644 index 0000000..eec25ed --- /dev/null +++ b/internal/relay/version_test.go @@ -0,0 +1,50 @@ +// SPDX-FileCopyrightText: 2026 Coffey Labs LLC +// SPDX-License-Identifier: AGPL-3.0-or-later + +package relay + +import ( + "context" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" +) + +// The installed binary is the relay user's to replace (an upgrade from the +// console does), so whatever is there may be that user's own program. relay +// status and relay install, which run as root, ask it its version, and must +// not run it as root to do so (INSTALLER-2). Here the stand-in records that +// it ran: run as the relay user through runuser, it never does, since this +// test is not root or the relay user cannot reach a file in its temp dir. +func TestInstalledVersionDoesNotRunTheBinaryItself(t *testing.T) { + dir := t.TempDir() + ran := filepath.Join(dir, "ran") + bin := filepath.Join(dir, "inbuxa-relay") + script := "#!/bin/sh\ntouch " + ran + "\necho inbuxa-relay 27.1.0\n" + if err := os.WriteFile(bin, []byte(script), 0o755); err != nil { + t.Fatal(err) + } + installedVersion(context.Background(), bin) + if _, err := os.Stat(ran); err == nil { + t.Error("installedVersion ran the relay user's binary as the installer's own user") + } +} + +// Everything the installer runs of the installed binary goes through +// runuser as the relay user, in a session of its own. +func TestTheInstalledBinaryRunsAsTheRelayUser(t *testing.T) { + for _, cmd := range []*exec.Cmd{ + asRelay(context.Background(), "status", "--state", StateDir), + asRelayRun(context.Background(), Binary, "--version"), + } { + if len(cmd.Args) < 5 || filepath.Base(cmd.Path) != "runuser" || + strings.Join(cmd.Args[:5], " ") != "runuser -u "+User+" -- "+Binary { + t.Errorf("runs %v, not the binary as %s", cmd.Args, User) + } + if cmd.SysProcAttr == nil || !cmd.SysProcAttr.Setsid { + t.Errorf("%v keeps the installer's controlling terminal", cmd.Args) + } + } +} diff --git a/internal/release/release.go b/internal/release/release.go index 9b0f5e6..f711f26 100644 --- a/internal/release/release.go +++ b/internal/release/release.go @@ -30,6 +30,23 @@ const ServerVersion = "27.1-rc.25" // ServerImage is that release as a container image. const ServerImage = "registry.coffeylabs.org/inbuxa/inbuxa-server:" + ServerVersion +// Caddy, the proxy in front of the console and the webmail, which holds +// their TLS keys, from Docker Hub. It is pinned by the digest of its image +// index, as the binaries are by their checksums: a tag there moves, and even +// a full version's tag is pushed again when its image is rebuilt (for a new +// Alpine, say). CaddyVersion is the tag the digest was read from, X.Y.Z of +// caddy:X.Y.Z-alpine, which pin-check watches for a newer one or a rebuild. +// The digest is read with +// `docker buildx imagetools inspect docker.io/library/caddy:-alpine`. +const ( + CaddyVersion = "2.11.9" + CaddyDigest = "sha256:003a97808e781aa9f7e75cc2f43d34d461099031faf5e20faefb9f61126836f0" +) + +// CaddyImage is that image, by its digest alone, which every runtime the +// installer drives reads the same way. +const CaddyImage = "docker.io/library/caddy@" + CaddyDigest + // ServerSHA256 is the SHA-256 of each architecture's release tarball, copied // from the release's SHA256SUMS when the version was moved and checked here // rather than fetched beside the download: a checksum that arrives with the