The server's home (<dir>/server) belongs to its user, and the server runs as that user with write access there. Install, cluster join and move then worked on paths inside it as root in ways that follow symbolic links. Code running as the server's user (a compromised server, for example) could put a link at data/, config.json, config.json.prev or a secret file, and the next install, converge, move or cluster join would chown the link's target to that user, write over it, or copy a root-only file into an inbuxa-owned rollback copy: the server's user became root. Now install refuses a data/ that is not a plain directory and changes ownership with chown -h and no trailing slash. Cluster join refuses a link at config.json, its rollback copy or a secret in the server's directory, reads config.json without following a link, and writes each file beside its place, gives it to the server's user through the open file and renames it into place. A move reads config.json without following a link and replaces it by a rename, and refuses a data/ that is a link before emptying it. Test: TestServerDirsRefusesALinkedDataDir, TestJoinRefusesLinksInTheServersDirectory, TestJoinDoesNotWriteThroughALinkPlantedAfterThePlan and TestRewriteDataPathRefusesALink failed before (the link was chowned or written through) and now pass.
76 lines
2.4 KiB
Go
76 lines
2.4 KiB
Go
// 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)
|
|
}
|
|
}
|
|
}
|