Files
jcoffey-dev 2f458c8911 Host server: root steps no longer follow links in the server's own directory
The server's home (<dir>/server) belongs to its user, and the server
runs as that user with write access there. Install 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/ or config.json, and the next install, converge or
move 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. 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.

Backport to the 2026 line. The cluster join part of the original change
is left out: the 2026 line has no cluster join.

Test: TestServerDirsRefusesALinkedDataDir and TestRewriteDataPathRefusesALink
failed before (the link was chowned or written through) and now pass.

(cherry picked from commit 82771a7ed7)
2026-11-17 16:11:32 -07:00

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