mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-03 18:43:38 +01:00
Retain protected-state differences before native rollback assertions
Capture bounded before/after file hashes, full type/mode/ownership and absence at the existing installed-profile reverse check. Preserve every strict state, lifecycle, history and cleanup assertion; no installer behavior or permissions change. Exercise all differing fields, missing entries, unsupported types, no-follow links and the read cap in the ordinary Linux install tests. Contract-Neutral: Native regression observation only; no installer, profile, API, rendered behavior or authority contract change. Change-source: pulse-maintainer
This commit is contained in:
150
scripts/installtests/agent_installed_profile_readback_test.go
Normal file
150
scripts/installtests/agent_installed_profile_readback_test.go
Normal file
@@ -0,0 +1,150 @@
|
||||
//go:build linux
|
||||
|
||||
package installtests
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAgentInstalledProfileRestoreReadback(t *testing.T) {
|
||||
for _, name := range []string{"unchanged", "bytes", "permissions", "owner", "group", "special-mode", "required-absent", "lifecycle-added", "type", "root-mode", "multiple"} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
root := filepath.Join(t.TempDir(), "state")
|
||||
if err := os.Mkdir(root, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
file, optional := filepath.Join(root, "token"), filepath.Join(root, "connection.env")
|
||||
const secret = "fixture-protected-bytes-not-for-receipts"
|
||||
if err := os.WriteFile(file, []byte(secret), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before := nativeProfileObserveProtected(file, optional, root)
|
||||
original := nativeProfileFiles(t, file)[file]
|
||||
if got := before[file]; got.Type != "regular" || !got.Present || got.Error != "" ||
|
||||
got.Hash != original.Hash || got.Mode != original.Mode || got.UID != original.UID || got.GID != original.GID {
|
||||
t.Fatalf("diagnostic lost the strict file identity: %+v", got)
|
||||
}
|
||||
if before[optional] != (nativeProfileProtectedIdentity{Type: "absent"}) ||
|
||||
before[root].Type != "directory" || before[root].Mode&os.ModeDir == 0 {
|
||||
t.Fatal("diagnostic lost absence or directory type")
|
||||
}
|
||||
want := []string{}
|
||||
change := func(path string, err error) {
|
||||
t.Helper()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want = append(want, path)
|
||||
}
|
||||
switch name {
|
||||
case "bytes":
|
||||
change(file, os.WriteFile(file, []byte("changed"), 0o600))
|
||||
case "permissions":
|
||||
change(file, os.Chmod(file, 0o640))
|
||||
case "owner", "group", "special-mode":
|
||||
// Witness each comparison without requiring privileged chown.
|
||||
identity := before[file]
|
||||
switch name {
|
||||
case "owner":
|
||||
identity.UID++
|
||||
case "group":
|
||||
identity.GID++
|
||||
case "special-mode":
|
||||
identity.Mode |= os.ModeSetuid
|
||||
}
|
||||
before[file] = identity
|
||||
want = append(want, file)
|
||||
case "required-absent":
|
||||
change(file, os.Remove(file))
|
||||
case "lifecycle-added":
|
||||
change(optional, os.WriteFile(optional, []byte("unexpected"), 0o600))
|
||||
case "type":
|
||||
if err := os.Remove(file); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
change(file, os.Mkdir(file, 0o600))
|
||||
case "root-mode":
|
||||
change(root, os.Chmod(root, 0o750))
|
||||
case "multiple":
|
||||
change(file, os.WriteFile(file, []byte("changed"), 0o600))
|
||||
change(optional, os.WriteFile(optional, []byte("unexpected"), 0o600))
|
||||
change(root, os.Chmod(root, 0o750))
|
||||
}
|
||||
sort.Strings(want)
|
||||
evidence := nativeProfileRestoreReadback(before)
|
||||
if !reflect.DeepEqual(evidence.ChangedPaths, want) || !reflect.DeepEqual(evidence.Before, before) || len(evidence.After) != 3 {
|
||||
t.Fatalf("incomplete before/after difference: %+v; want changed %v", evidence, want)
|
||||
}
|
||||
receipt := nativeProfileReceipt{Phases: []nativeProfilePhase{{Name: "reverse-protected-state-readback", Evidence: evidence}}, Passed: false}
|
||||
data, err := json.Marshal(receipt)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(data), secret) || !strings.Contains(string(data), original.Hash) || !strings.Contains(string(data), `"passed":false`) {
|
||||
t.Fatal("failed-journey receipt lost digest evidence or exposed protected contents")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentInstalledProfileProtectedReadbackBounds(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
secretTarget := filepath.Join(t.TempDir(), "target-must-not-appear")
|
||||
if err := os.WriteFile(secretTarget, []byte("unread-target"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
link, dangling, fifo := filepath.Join(root, "link"), filepath.Join(root, "dangling"), filepath.Join(root, "fifo")
|
||||
if err := os.Symlink(secretTarget, link); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(secretTarget+"-absent", dangling); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := syscall.Mkfifo(fifo, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
large := filepath.Join(root, "oversized")
|
||||
if err := os.WriteFile(large, []byte{}, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Truncate(large, nativeProfileReadbackMaxBytes+1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name, path, kind, diagnostic string
|
||||
}{
|
||||
{"symlink", link, "symlink", ""},
|
||||
{"dangling-link", dangling, "symlink", ""},
|
||||
{"fifo-no-open", fifo, "unsupported", ""},
|
||||
{"oversized-no-partial-hash", large, "regular", "file-too-large"},
|
||||
{"lstat-error-not-absence", filepath.Join(secretTarget, "child"), "unknown", "lstat-failed"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := nativeProfileObserveProtectedPath(tc.path)
|
||||
if got.Type != tc.kind || got.Error != tc.diagnostic || got.Hash != "" {
|
||||
t.Fatalf("unsafe or incomplete readback: %+v", got)
|
||||
}
|
||||
data, err := json.Marshal(got)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(data), secretTarget) || strings.Contains(string(data), "unread-target") {
|
||||
t.Fatal("diagnostic exposed a symlink target, raw error or contents")
|
||||
}
|
||||
})
|
||||
}
|
||||
boundary := filepath.Join(root, "exact-limit")
|
||||
if err := os.WriteFile(boundary, []byte(strings.Repeat("x", nativeProfileReadbackMaxBytes)), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := nativeProfileObserveProtectedPath(boundary); got.Error != "" || got.Hash != secureRuntimeHash([]byte(strings.Repeat("x", nativeProfileReadbackMaxBytes))) {
|
||||
t.Fatal("exact bounded file was not completely hashed")
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/big"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -29,6 +30,7 @@ import (
|
||||
"reflect"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -194,6 +196,114 @@ func nativeProfileOptionalFiles(t *testing.T, paths ...string) secureRuntimeStab
|
||||
return result
|
||||
}
|
||||
|
||||
// Diagnostic identities are separate from the strict acceptance snapshots above.
|
||||
// Observe every fixed protected path before an assertion can stop the journey;
|
||||
// never publish contents, symlink targets, raw errors or directory descendants.
|
||||
// The installer is the largest protected file; cap even a corrupt replacement.
|
||||
const nativeProfileReadbackMaxBytes = 1 << 20
|
||||
|
||||
type nativeProfileProtectedIdentity struct {
|
||||
Present bool `json:"present"`
|
||||
Type string `json:"type"`
|
||||
Mode os.FileMode `json:"mode"`
|
||||
UID uint32 `json:"uid"`
|
||||
GID uint32 `json:"gid"`
|
||||
Hash string `json:"sha256,omitempty"`
|
||||
Error string `json:"observation_error,omitempty"`
|
||||
}
|
||||
|
||||
type nativeProfileRestoreEvidence struct {
|
||||
Before map[string]nativeProfileProtectedIdentity `json:"before"`
|
||||
After map[string]nativeProfileProtectedIdentity `json:"after"`
|
||||
ChangedPaths []string `json:"changed_paths"`
|
||||
}
|
||||
|
||||
func nativeProfileObserveProtected(paths ...string) map[string]nativeProfileProtectedIdentity {
|
||||
result := make(map[string]nativeProfileProtectedIdentity, len(paths))
|
||||
for _, path := range paths {
|
||||
result[path] = nativeProfileObserveProtectedPath(path)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func nativeProfileObserveProtectedPath(path string) nativeProfileProtectedIdentity {
|
||||
info, err := os.Lstat(path)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return nativeProfileProtectedIdentity{Type: "absent"}
|
||||
}
|
||||
if err != nil {
|
||||
return nativeProfileProtectedIdentity{Type: "unknown", Error: "lstat-failed"}
|
||||
}
|
||||
identity := nativeProfileProtectedIdentity{Present: true, Mode: info.Mode(), Type: "unsupported"}
|
||||
stat, ok := info.Sys().(*syscall.Stat_t)
|
||||
if !ok {
|
||||
identity.Error = "ownership-unavailable"
|
||||
return identity
|
||||
}
|
||||
identity.UID, identity.GID = stat.Uid, stat.Gid
|
||||
switch {
|
||||
case info.Mode().IsRegular():
|
||||
identity.Type = "regular"
|
||||
case info.IsDir():
|
||||
identity.Type = "directory"
|
||||
return identity
|
||||
case info.Mode()&os.ModeSymlink != 0:
|
||||
identity.Type = "symlink"
|
||||
return identity
|
||||
default:
|
||||
return identity
|
||||
}
|
||||
// Lstat alone cannot prevent a swapped link or FIFO from being opened.
|
||||
fd, err := syscall.Open(path, syscall.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK|syscall.O_CLOEXEC, 0)
|
||||
if err != nil {
|
||||
identity.Error = "open-failed"
|
||||
return identity
|
||||
}
|
||||
file := os.NewFile(uintptr(fd), "protected-state")
|
||||
defer file.Close()
|
||||
opened, err := file.Stat()
|
||||
if err != nil || !opened.Mode().IsRegular() || !os.SameFile(info, opened) {
|
||||
identity.Error = "file-changed"
|
||||
return identity
|
||||
}
|
||||
data, err := io.ReadAll(io.LimitReader(file, nativeProfileReadbackMaxBytes+1))
|
||||
if err != nil {
|
||||
identity.Error = "read-failed"
|
||||
return identity
|
||||
}
|
||||
if len(data) > nativeProfileReadbackMaxBytes {
|
||||
identity.Error = "file-too-large"
|
||||
return identity
|
||||
}
|
||||
finished, err := file.Stat()
|
||||
current, pathErr := os.Lstat(path)
|
||||
if err != nil || pathErr != nil || !os.SameFile(opened, current) || opened.Size() != finished.Size() ||
|
||||
!opened.ModTime().Equal(finished.ModTime()) || opened.Mode() != finished.Mode() ||
|
||||
stat.Uid != finished.Sys().(*syscall.Stat_t).Uid || stat.Gid != finished.Sys().(*syscall.Stat_t).Gid ||
|
||||
len(data) != int(finished.Size()) {
|
||||
identity.Error = "file-changed"
|
||||
return identity
|
||||
}
|
||||
identity.Hash = secureRuntimeHash(data)
|
||||
return identity
|
||||
}
|
||||
|
||||
func nativeProfileRestoreReadback(before map[string]nativeProfileProtectedIdentity) nativeProfileRestoreEvidence {
|
||||
paths := make([]string, 0, len(before))
|
||||
for path := range before {
|
||||
paths = append(paths, path)
|
||||
}
|
||||
sort.Strings(paths)
|
||||
after := nativeProfileObserveProtected(paths...)
|
||||
evidence := nativeProfileRestoreEvidence{Before: before, After: after, ChangedPaths: []string{}}
|
||||
for _, path := range paths {
|
||||
if before[path] != after[path] {
|
||||
evidence.ChangedPaths = append(evidence.ChangedPaths, path)
|
||||
}
|
||||
}
|
||||
return evidence
|
||||
}
|
||||
|
||||
// Mutable descendants are handed to the collector, not restored recursively
|
||||
// to root on rollback. Still bind their bytes, mode and exact service owner;
|
||||
// protected identity/connection files and the state root are checked separately.
|
||||
@@ -501,6 +611,8 @@ func TestAgentInstalledProfileSystemd(t *testing.T) {
|
||||
lifecyclePaths := []string{"/etc/pulse-agent/connection.env", "/etc/pulse-agent/install.sh", "/etc/pulse-agent/install.sh.sha256"}
|
||||
baselineLifecycle := nativeProfileOptionalFiles(t, lifecyclePaths...)
|
||||
baselineRoot := nativeProfileDirectory(t, "/var/lib/pulse-agent")
|
||||
protectedPaths := append(append(append([]string{}, statePaths...), lifecyclePaths...), "/var/lib/pulse-agent")
|
||||
baselineReadback := nativeProfileObserveProtected(protectedPaths...)
|
||||
baselineHistory := nativeProfileFiles(t, historyPath)[historyPath]
|
||||
phase("signed-TLS-root-install", map[string]any{"units": nativeProfileState(t, receipt), "state": baselineState, "lifecycle": baselineLifecycle, "state_root": baselineRoot, "history": baselineHistory})
|
||||
// The installer correctly forbids a legacy command collector and a new
|
||||
@@ -529,6 +641,9 @@ func TestAgentInstalledProfileSystemd(t *testing.T) {
|
||||
return history
|
||||
}
|
||||
assertRestoredState := func() {
|
||||
// Retain all paths, including absence/type changes, before the unchanged
|
||||
// strict checks can fail. A diagnostic read error never grants a pass.
|
||||
phase("reverse-protected-state-readback", nativeProfileRestoreReadback(baselineReadback))
|
||||
if !reflect.DeepEqual(baselineState, nativeProfileFiles(t, statePaths...)) ||
|
||||
!reflect.DeepEqual(baselineLifecycle, nativeProfileOptionalFiles(t, lifecyclePaths...)) ||
|
||||
baselineRoot != nativeProfileDirectory(t, "/var/lib/pulse-agent") {
|
||||
|
||||
Reference in New Issue
Block a user