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

52 lines
1.7 KiB
Go

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