mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 01:53:42 +01:00
436 lines
13 KiB
Go
436 lines
13 KiB
Go
package install
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// layoutTest sets up the hooks for layout tests: the folders of the test
|
|
// user count as root's, the service user is the test user (or uid when
|
|
// set), the new binary holds NEW and systemctl calls are recorded
|
|
type layoutTest struct {
|
|
t *testing.T
|
|
calls []string
|
|
failing map[string]int
|
|
}
|
|
|
|
func newLayoutTest(t *testing.T, serviceUID int) *layoutTest {
|
|
t.Helper()
|
|
trustTestUser(t)
|
|
lt := &layoutTest{t: t, failing: map[string]int{}}
|
|
savedIDs, savedNew, savedCtl, savedWait := lookupServiceIDs, newBinaryPath, systemctl, stopWait
|
|
t.Cleanup(func() { lookupServiceIDs, newBinaryPath, systemctl, stopWait = savedIDs, savedNew, savedCtl, savedWait })
|
|
lookupServiceIDs = func() (int, int, error) { return serviceUID, os.Getgid(), nil }
|
|
newBin := filepath.Join(t.TempDir(), "new")
|
|
if err := os.WriteFile(newBin, []byte("NEW"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
newBinaryPath = func() (string, error) { return newBin, nil }
|
|
stopWait = 0
|
|
systemctl = func(action string) ([]byte, error) {
|
|
lt.calls = append(lt.calls, action)
|
|
if lt.failing[action] > 0 {
|
|
lt.failing[action]--
|
|
return []byte("failed"), errors.New("exit status 1")
|
|
}
|
|
return nil, nil
|
|
}
|
|
return lt
|
|
}
|
|
|
|
// installDirWith makes an install folder with a binary holding content
|
|
func installDirWith(t *testing.T, content string) (string, string) {
|
|
t.Helper()
|
|
dir := filepath.Join(t.TempDir(), "phishingclub")
|
|
if err := os.Mkdir(dir, 0750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bin := filepath.Join(dir, binaryName)
|
|
if content != "" {
|
|
if err := os.WriteFile(bin, []byte(content), 0750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
return dir, bin
|
|
}
|
|
|
|
func readFile(t *testing.T, path string) (string, os.FileMode) {
|
|
t.Helper()
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
info, err := os.Lstat(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return string(b), info.Mode()
|
|
}
|
|
|
|
func noTempFiles(t *testing.T, dirs ...string) {
|
|
t.Helper()
|
|
for _, dir := range dirs {
|
|
entries, _ := os.ReadDir(dir)
|
|
for _, e := range entries {
|
|
if strings.Contains(e.Name(), ".new-") {
|
|
t.Fatalf("left %s in %s", e.Name(), dir)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestBackupBinary checks the backup reads only a binary it can read
|
|
// safely and never writes through what is at the backup path
|
|
func TestBackupBinary(t *testing.T) {
|
|
newLayoutTest(t, os.Getuid())
|
|
outside := func(content string, mode os.FileMode) string {
|
|
p := filepath.Join(t.TempDir(), "outside")
|
|
if err := os.WriteFile(p, []byte(content), mode); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return p
|
|
}
|
|
|
|
// a regular binary is copied, the backup is private
|
|
dir, _ := installDirWith(t, "BINARY")
|
|
l, err := checkLayoutAt(dir, false, true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := l.backup(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l.close()
|
|
if got, mode := readFile(t, filepath.Join(dir, binaryName+".backup")); got != "BINARY" || mode != 0600 {
|
|
t.Fatalf("backup %q %v", got, mode)
|
|
}
|
|
|
|
// a symlink at the backup path is replaced, its target is not written
|
|
dir, _ = installDirWith(t, "BINARY")
|
|
target := outside("KEEP", 0644)
|
|
if err := os.Symlink(target, filepath.Join(dir, binaryName+".backup")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l, err = checkLayoutAt(dir, false, true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := l.backup(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l.close()
|
|
if got, mode := readFile(t, target); got != "KEEP" || mode != 0644 {
|
|
t.Fatalf("backup symlink target changed: %q %v", got, mode)
|
|
}
|
|
if _, mode := readFile(t, filepath.Join(dir, binaryName+".backup")); !mode.IsRegular() {
|
|
t.Fatal("backup is not a regular file")
|
|
}
|
|
|
|
// a symlink as the binary in a folder others can change is refused
|
|
dir, bin := installDirWith(t, "")
|
|
secret := outside("SECRET", 0600)
|
|
if err := os.Symlink(secret, bin); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(dir, 0777); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := checkLayoutAt(dir, false, true); err == nil || !errors.Is(err, errLinkNotTrusted) {
|
|
t.Fatalf("symlink binary of others: %v", err)
|
|
}
|
|
if _, err := os.Lstat(filepath.Join(dir, binaryName+".backup")); !os.IsNotExist(err) {
|
|
t.Fatalf("backup written for a symlink binary: %v", err)
|
|
}
|
|
|
|
// a FIFO as the binary is refused without blocking
|
|
dir, bin = installDirWith(t, "")
|
|
if err := syscall.Mkfifo(bin, 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
_, err := checkLayoutAt(dir, false, true)
|
|
done <- err
|
|
}()
|
|
select {
|
|
case err := <-done:
|
|
if err == nil || !strings.Contains(err.Error(), "not a regular file") {
|
|
t.Fatalf("FIFO binary: %v", err)
|
|
}
|
|
case <-time.After(5 * time.Second):
|
|
if f, err := os.OpenFile(bin, os.O_WRONLY|syscall.O_NONBLOCK, 0); err == nil {
|
|
_ = f.Close()
|
|
}
|
|
t.Fatal("the check blocked on a FIFO")
|
|
}
|
|
if got, _ := readFile(t, secret); got != "SECRET" {
|
|
t.Fatal("secret changed")
|
|
}
|
|
noTempFiles(t, dir)
|
|
}
|
|
|
|
// TestBinaryHardLinks checks a hard linked binary is read when it belongs
|
|
// to the service user or the service user can read it anyway, and refused
|
|
// for an update when it could be a file of root
|
|
func TestBinaryHardLinks(t *testing.T) {
|
|
// owned by the service user
|
|
newLayoutTest(t, os.Getuid())
|
|
dir, bin := installDirWith(t, "BINARY")
|
|
other := filepath.Join(t.TempDir(), "other")
|
|
if err := os.Link(bin, other); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l, err := checkLayoutAt(dir, false, true)
|
|
if err != nil {
|
|
t.Fatalf("own hard linked binary: %v", err)
|
|
}
|
|
if err := l.backup(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := l.writeBinary(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l.close()
|
|
// the name is replaced, the other link keeps the old file
|
|
if got, _ := readFile(t, bin); got != "NEW" {
|
|
t.Fatalf("binary %q", got)
|
|
}
|
|
if got, _ := readFile(t, other); got != "BINARY" {
|
|
t.Fatalf("other link %q", got)
|
|
}
|
|
|
|
// owned by another user (the service user is someone else): only when
|
|
// others can read it
|
|
newLayoutTest(t, os.Getuid()+1)
|
|
for mode, ok := range map[os.FileMode]bool{0700: false, 0755: true} {
|
|
dir, bin := installDirWith(t, "BINARY")
|
|
if err := os.Chmod(bin, mode); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Link(bin, filepath.Join(t.TempDir(), "other")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l, err := checkLayoutAt(dir, false, true)
|
|
if ok != (err == nil) {
|
|
t.Fatalf("mode %o: %v", mode, err)
|
|
}
|
|
if err == nil {
|
|
l.close()
|
|
} else if !strings.Contains(err.Error(), "hard link") {
|
|
t.Fatalf("mode %o: %v", mode, err)
|
|
}
|
|
// an install replaces it without reading it
|
|
l, err = checkLayoutAt(dir, false, false)
|
|
if err != nil {
|
|
t.Fatalf("install, mode %o: %v", mode, err)
|
|
}
|
|
l.close()
|
|
}
|
|
}
|
|
|
|
// TestTrustedSymlinks checks the install folder, the binary and the data
|
|
// folder are followed when only root could have made the link
|
|
func TestTrustedSymlinks(t *testing.T) {
|
|
newLayoutTest(t, os.Getuid())
|
|
real, _ := installDirWith(t, "OLD")
|
|
link := filepath.Join(t.TempDir(), "phishingclub")
|
|
if err := os.Symlink(real, link); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// the binary is a link to a file in a folder of root
|
|
binDir := t.TempDir()
|
|
binTarget := filepath.Join(binDir, "pc")
|
|
if err := os.WriteFile(binTarget, []byte("OLD"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Remove(filepath.Join(real, binaryName)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Symlink(binTarget, filepath.Join(real, binaryName)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l, err := checkLayoutAt(link, false, true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if l.dir != real || l.binary != binTarget {
|
|
t.Fatalf("layout %s %s", l.dir, l.binary)
|
|
}
|
|
if err := l.replace(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l.close()
|
|
if got, mode := readFile(t, binTarget); got != "NEW" || mode.Perm() != 0755 {
|
|
t.Fatalf("binary target %q %v", got, mode)
|
|
}
|
|
if got, _ := readFile(t, filepath.Join(real, binaryName+".backup")); got != "OLD" {
|
|
t.Fatalf("backup %q", got)
|
|
}
|
|
if info, err := os.Lstat(filepath.Join(real, binaryName)); err != nil || info.Mode()&os.ModeSymlink == 0 {
|
|
t.Fatal("the binary symlink was replaced")
|
|
}
|
|
|
|
// a data folder that is a link of root to a folder of root
|
|
data := t.TempDir()
|
|
if err := os.Symlink(data, filepath.Join(real, dataDir)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := createDirectories(l); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if info, err := os.Stat(filepath.Join(data, "certs", "own-managed")); err != nil || !info.IsDir() {
|
|
t.Fatalf("folders not made in the linked data folder: %v", err)
|
|
}
|
|
noTempFiles(t, real, binDir)
|
|
}
|
|
|
|
// TestUntrustedSymlinks checks symlinks others could have made are not
|
|
// followed and nothing is made or changed through them
|
|
func TestUntrustedSymlinks(t *testing.T) {
|
|
newLayoutTest(t, os.Getuid())
|
|
// the install folder a link in a folder others can write
|
|
real, _ := installDirWith(t, "OLD")
|
|
open := t.TempDir()
|
|
link := filepath.Join(open, "phishingclub")
|
|
if err := os.Symlink(real, link); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(open, 0777); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := checkLayoutAt(link, false, false); !errors.Is(err, errLinkNotTrusted) {
|
|
t.Fatalf("install folder link of others: %v", err)
|
|
}
|
|
// a link of root to a folder that others can replace
|
|
elsewhere := filepath.Join(open, "real")
|
|
if err := os.Mkdir(elsewhere, 0750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
link2 := filepath.Join(t.TempDir(), "phishingclub")
|
|
if err := os.Symlink(elsewhere, link2); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := checkLayoutAt(link2, false, false); !errors.Is(err, errLinkNotTrusted) {
|
|
t.Fatalf("link to a folder of others: %v", err)
|
|
}
|
|
// a hard link to a link of root
|
|
link3 := filepath.Join(t.TempDir(), "phishingclub")
|
|
if err := os.Link(link2, link3); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := checkLayoutAt(link3, false, false); !errors.Is(err, errLinkNotTrusted) {
|
|
t.Fatalf("hard linked symlink: %v", err)
|
|
}
|
|
|
|
// the data folder a link in an install folder others can write: no
|
|
// folder is made there, the install goes on
|
|
dir, _ := installDirWith(t, "OLD")
|
|
target := t.TempDir()
|
|
if err := os.Symlink(target, filepath.Join(dir, dataDir)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(dir, 0777); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l, err := checkLayoutAt(dir, false, false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer l.close()
|
|
if err := createDirectories(l); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if entries, _ := os.ReadDir(target); len(entries) != 0 {
|
|
t.Fatalf("folders made through a link of others: %v", entries)
|
|
}
|
|
// a loop of links ends
|
|
loop := filepath.Join(t.TempDir(), "loop")
|
|
if err := os.Symlink(loop, loop); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := resolveTrusted(loop); err == nil || !strings.Contains(err.Error(), "too many") {
|
|
t.Fatalf("loop: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestUpdateStartsServiceOnFailure checks a failure after the stop starts
|
|
// the service again, with the previous binary once the new one was written,
|
|
// and a refusal of the checks does not stop it
|
|
func TestUpdateStartsServiceOnFailure(t *testing.T) {
|
|
lt := newLayoutTest(t, os.Getuid())
|
|
|
|
// the start fails once: the old binary is put back and started
|
|
dir, bin := installDirWith(t, "OLD")
|
|
if err := os.Chmod(bin, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l, err := checkLayoutAt(dir, false, true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
lt.failing["start"] = 1
|
|
err = l.replace()
|
|
l.close()
|
|
if err == nil || !strings.Contains(err.Error(), "start service") {
|
|
t.Fatalf("replace: %v", err)
|
|
}
|
|
if got, mode := readFile(t, bin); got != "OLD" || mode.Perm() != 0755 {
|
|
t.Fatalf("binary after a failed start %q %v", got, mode)
|
|
}
|
|
if strings.Join(lt.calls, ",") != "stop,start,start" {
|
|
t.Fatalf("systemctl calls %v", lt.calls)
|
|
}
|
|
|
|
// the backup fails after the stop: the binary is untouched and started
|
|
lt.calls = nil
|
|
dir, bin = installDirWith(t, "OLD")
|
|
if err := os.MkdirAll(filepath.Join(dir, binaryName+".backup", "x"), 0750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l, err = checkLayoutAt(dir, false, true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = l.replace()
|
|
l.close()
|
|
if err == nil || !strings.Contains(err.Error(), "backup") {
|
|
t.Fatalf("replace: %v", err)
|
|
}
|
|
if got, _ := readFile(t, bin); got != "OLD" {
|
|
t.Fatalf("binary after a failed backup %q", got)
|
|
}
|
|
if strings.Join(lt.calls, ",") != "stop,start" {
|
|
t.Fatalf("systemctl calls %v", lt.calls)
|
|
}
|
|
noTempFiles(t, dir)
|
|
|
|
// the install steps put the binary back and start the service when a
|
|
// step after the binary write fails on a rerun
|
|
lt.calls = nil
|
|
dir, bin = installDirWith(t, "OLD")
|
|
l, err = checkLayoutAt(dir, false, false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
steps := []installStep{
|
|
{name: "install binary", fn: l.writeBinary, writesBinary: true},
|
|
{name: "set permissions", fn: func() error { return errors.New("broken") }},
|
|
}
|
|
err = l.runInstallSteps(steps)
|
|
l.close()
|
|
if err == nil || !strings.Contains(err.Error(), "set permissions") {
|
|
t.Fatalf("install steps: %v", err)
|
|
}
|
|
if got, _ := readFile(t, bin); got != "OLD" {
|
|
t.Fatalf("binary after a failed rerun %q", got)
|
|
}
|
|
if strings.Join(lt.calls, ",") != "start" {
|
|
t.Fatalf("systemctl calls %v", lt.calls)
|
|
}
|
|
}
|