Files
jcoffey-dev f8aa224747 Install: setup files and the Node download no longer use fixed names in /tmp
First boot wrote its two compose overrides to /tmp/inbuxa-bootstrap.yaml
and /tmp/inbuxa-basic-auth.yaml, and a host webmail install downloaded
Node to /tmp/inbuxa-node.tar.xz(.part), all opened by root with no
O_EXCL and following links. A local user could create those names
first: with fs.protected_regular off, root then wrote into the user's
own file, which the user could rewrite before docker compose read it
(an override that mounts / into the server) or before tar unpacked it
as root (after the checksum had passed), or a link made root overwrite
another file. With the sysctl on, it made the install fail. The Node
tarball was also never removed.

Each override now goes in a new directory from os.MkdirTemp (0700),
created with O_EXCL and removed with its directory when the step ends.
Node is downloaded the same way and removed once unpacked. A download's
.part file is always created new: anything already at the name is
removed first, and a link there is never written through.

Test: TestSetupOverrideIsNotAtAFixedNameInTmp,
TestNodeTarballIsNotAtAFixedNameInTmp and
TestFetchVerifiedDoesNotWriteThroughAPartLink failed before (a planted
link was written through) and now pass.

(cherry picked from commit c734ebd4b2)
2026-11-17 16:11:54 -07:00

92 lines
2.8 KiB
Go

// 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)
}
}