Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Reject corrupt legacy installer agent identities

pulse-triage[bot]0b9bd4beb6aa
diff --git a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
index 74abaf3c67..f63cc04a05 100644
--- a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
+++ b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
@@ -15,6 +15,24 @@
## Purpose
+### Installer identity recovery accounts for the whole legacy file
+
+The descriptor-safe collector command remains the primary agent-ID reader. Its
+root-owned legacy compatibility path accepts only one 1–128 character ID, with
+or without a final newline, from an owned 0600 regular file in an owned
+non-writable parent. Portable size inspection and a bounded read must account
+for every byte; a valid prefix cannot hide oversized state, another identity or
+a discarded NUL. Symlink/FIFO rejection, descriptor ownership/privacy and the
+real collector executable remain unchanged. This does not enrol an agent or
+establish installed recovery.
+
+`TestInstallSHLegacyAgentIDRecoveryAccountsForEveryByte` pins that fallback.
+`TestInstallSHAgentIDRecoveryRejectsSymlinkFIFOAndOversizedState` covers both
+readers, including a 0400 positive control that cannot use the legacy path.
+Its two-second probes retain build-versus-probe timings and cancel their whole
+fixture process group; `TestAgentIDRecoveryCancellationStopsDescendants` pins
+child termination and inherited-pipe completion, not a longer acceptance budget.
+
### Shared origin and webhook edit boundaries
The referenced `internal/api/` settings boundary treats explicit empty
diff --git a/docs/release-control/v6/internal/subsystems/deployment-installability.md b/docs/release-control/v6/internal/subsystems/deployment-installability.md
index 10cd0c5077..ab83d236ed 100644
--- a/docs/release-control/v6/internal/subsystems/deployment-installability.md
+++ b/docs/release-control/v6/internal/subsystems/deployment-installability.md
@@ -15,6 +15,21 @@
## Purpose
+### Legacy installer identity recovery is a whole-file decision
+
+An installer recovering a pre-descriptor-helper agent-ID file accepts exactly
+one bounded ID and its optional final newline. It must not accept a plausible
+first line from an oversized, multi-line or NUL-bearing file. Portable byte-size
+inspection precedes the bounded shell read, and every file byte must be accounted
+for. Existing root-boundary ownership/mode/parent checks and descriptor-first
+selection remain in force; no signature, service, token or enrolment rule changes.
+
+`TestInstallSHLegacyAgentIDRecoveryAccountsForEveryByte` verifies normal and
+maximum-length compatibility alongside rejected valid-prefix corruptions. The
+Unix real-agent controls keep their independent two-second watchdog, with
+process-group cancellation and separate build/probe timings. These source
+fixtures do not establish signed native installation, persistence or recovery.
+
### Audit report cleanup belongs to the original invoking process
The npm audit wrapper retains a captured advisory until the parent classifies
diff --git a/scripts/install.sh b/scripts/install.sh
index 5d5cdebeed..2902338145 100755
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -4924,6 +4924,8 @@ read_agent_id_file_safely() {
local lifecycle_binary=""
local collector_uid=""
local identity=""
+ local identity_size=""
+ local consumed_bytes=0
local -a identity_args
[[ -e "$aid_path" || -L "$aid_path" || -p "$aid_path" ]] || return 1
@@ -4944,7 +4946,17 @@ read_agent_id_file_safely() {
# command. Their parent and file are not writable by the runtime, so a
# bounded shell read remains a boundary-only compatibility path.
if trusted_lifecycle_regular_file "$aid_path" 600; then
- IFS= read -r identity < "$aid_path" || true
+ identity_size=$(stat -c '%s' "$aid_path" 2>/dev/null || stat -f '%z' "$aid_path" 2>/dev/null) || return 1
+ [[ "$identity_size" =~ ^[0-9]+$ && "$identity_size" -ge 1 && "$identity_size" -le 129 ]] || return 1
+ # Accept exactly one bounded ID, with or without its final newline.
+ # Bash read discards NUL bytes and otherwise only reads the first line;
+ # matching all file bytes prevents either from hiding corrupt state.
+ if IFS= read -r -n 130 identity < "$aid_path"; then
+ consumed_bytes=$((${#identity} + 1))
+ else
+ consumed_bytes=${#identity}
+ fi
+ [[ "$identity_size" -eq "$consumed_bytes" ]] || return 1
if [[ ${#identity} -ge 1 && ${#identity} -le 128 && "$identity" =~ ^[A-Za-z0-9][A-Za-z0-9._:-]*$ ]]; then
printf '%s\n' "$identity"
return 0
diff --git a/scripts/installtests/agent_id_recovery_unix_test.go b/scripts/installtests/agent_id_recovery_unix_test.go
index b2b55ef133..60c63b87c4 100644
--- a/scripts/installtests/agent_id_recovery_unix_test.go
+++ b/scripts/installtests/agent_id_recovery_unix_test.go
@@ -3,29 +3,46 @@
package installtests
import (
+ "bytes"
"context"
+ "errors"
"os"
"os/exec"
"path/filepath"
+ "strconv"
"strings"
"syscall"
"testing"
"time"
)
+func agentIDRecoveryCommand(ctx context.Context, name string, args ...string) *exec.Cmd {
+ cmd := exec.CommandContext(ctx, name, args...)
+ // The two-second recovery budget must include descendants, not just Bash.
+ // A child retaining the output pipe must not hold the installer suite open.
+ cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
+ cmd.Cancel = func() error {
+ err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
+ if errors.Is(err, syscall.ESRCH) {
+ return os.ErrProcessDone
+ }
+ return err
+ }
+ cmd.WaitDelay = time.Second
+ return cmd
+}
+
func TestInstallSHAgentIDRecoveryRejectsSymlinkFIFOAndOversizedState(t *testing.T) {
+ buildStarted := time.Now()
binaryPath := buildLifecycleAgent(t)
+ t.Logf("built actual lifecycle agent in %s; recovery probes have an independent two-second deadline", time.Since(buildStarted))
root := t.TempDir()
validPath := filepath.Join(root, "valid-agent-id")
- oversizedPath := filepath.Join(root, "oversized-agent-id")
symlinkPath := filepath.Join(root, "symlink-agent-id")
fifoPath := filepath.Join(root, "fifo-agent-id")
if err := os.WriteFile(validPath, []byte("agent-safe-123\n"), 0600); err != nil {
t.Fatal(err)
}
- if err := os.WriteFile(oversizedPath, []byte(strings.Repeat("a", 5000)), 0600); err != nil {
- t.Fatal(err)
- }
if err := os.Symlink(validPath, symlinkPath); err != nil {
t.Fatal(err)
}
@@ -33,32 +50,111 @@ func TestInstallSHAgentIDRecoveryRejectsSymlinkFIFOAndOversizedState(t *testing.
t.Fatal(err)
}
- harness := func(path string) ([]byte, error) {
- script := `
- set -euo pipefail
- COLLECTOR_LIFECYCLE_BINARY_PATH="` + binaryPath + `"
- INSTALL_DIR="` + root + `"
- BINARY_NAME="pulse-agent"
- LEAST_PRIVILEGE_USER="pulse-agent-test-missing"
-` + extractLifecycleTrustShellFunctions(t) + `
-` + extractInstallShellFunction(t, "collector_lifecycle_binary") + `
-` + extractInstallShellFunction(t, "read_agent_id_file_safely") + `
- read_agent_id_file_safely "` + path + `"
- `
- ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
- defer cancel()
- return exec.CommandContext(ctx, "bash", "-c", script).CombinedOutput()
+ cases := []struct {
+ name, content, want string
+ }{
+ {"valid", "agent-safe-123\n", "agent-safe-123"},
+ {"without-newline", "agent-safe-123", "agent-safe-123"},
+ {"maximum-id", strings.Repeat("a", 128) + "\n", strings.Repeat("a", 128)},
+ {"oversized", strings.Repeat("a", 5000), ""},
+ {"valid-prefix-oversized", "agent-safe-123\n" + strings.Repeat("a", 5000), ""},
+ {"two-identities", "agent-safe-123\nother-agent\n", ""},
+ {"nul-before-newline", "agent-safe-123\x00\n", ""},
+ {"nul-at-eof", "agent-safe-123\x00", ""},
+ {"too-long-id", strings.Repeat("a", 129), ""},
+ {"empty", "", ""},
+ {"newline-only", "\n", ""},
+ {"invalid-id", "agent/safe\n", ""},
+ }
+ for _, mode := range []struct{ name, binary string }{{"descriptor", binaryPath}, {"legacy", ""}} {
+ t.Run(mode.name, func(t *testing.T) {
+ probe := func(t *testing.T, path, want string) {
+ t.Helper()
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+ started := time.Now()
+ out, err := agentIDRecoveryCommand(ctx, "bash", "-c", agentIDRecoveryShell(t, mode.binary, root, path)).CombinedOutput()
+ elapsed := time.Since(started)
+ if ctx.Err() != nil || elapsed >= 2*time.Second {
+ t.Fatalf("recovery probe exceeded its deadline: elapsed=%s context=%v exit=%v output=%q", elapsed, ctx.Err(), err, out)
+ }
+ if want != "" {
+ if err != nil || string(out) != want+"\n" {
+ t.Fatalf("valid agent ID recovery failed: elapsed=%s exit=%v output=%q", elapsed, err, out)
+ }
+ } else if err == nil || len(out) != 0 {
+ t.Fatalf("unsafe agent ID state was accepted or disclosed: elapsed=%s exit=%v output=%q", elapsed, err, out)
+ }
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ path := filepath.Join(root, mode.name+"-"+tc.name)
+ if err := os.WriteFile(path, []byte(tc.content), 0600); err != nil {
+ t.Fatal(err)
+ }
+ probe(t, path, tc.want)
+ })
+ }
+ t.Run("symlink", func(t *testing.T) { probe(t, symlinkPath, "") })
+ t.Run("fifo", func(t *testing.T) { probe(t, fifoPath, "") })
+ if mode.binary != "" {
+ // The legacy compatibility path requires 0600. A valid 0400
+ // file therefore proves the actual descriptor command ran.
+ t.Run("descriptor-not-masked-by-fallback", func(t *testing.T) {
+ if err := os.Chmod(validPath, 0400); err != nil {
+ t.Fatal(err)
+ }
+ probe(t, validPath, "agent-safe-123")
+ })
+ }
+ })
+ }
+}
+
+func TestAgentIDRecoveryCancellationStopsDescendants(t *testing.T) {
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ childFile := filepath.Join(t.TempDir(), "child-pid")
+ cmd := agentIDRecoveryCommand(ctx, "bash", "-c", `sleep 30 & child=$!; printf '%s\n' "$child" > "`+childFile+`"; wait`)
+ var output bytes.Buffer
+ cmd.Stdout, cmd.Stderr = &output, &output
+ finished := make(chan error, 1)
+ go func() { finished <- cmd.Run() }()
+ var childPID int
+ deadline := time.Now().Add(2 * time.Second)
+ for time.Now().Before(deadline) {
+ data, err := os.ReadFile(childFile)
+ if err == nil {
+ childPID, _ = strconv.Atoi(strings.TrimSpace(string(data)))
+ if childPID > 1 {
+ break
+ }
+ }
+ time.Sleep(10 * time.Millisecond)
+ }
+ if childPID <= 1 {
+ cancel()
+ <-finished
+ t.Fatal("descendant fixture did not start within two seconds")
+ }
+ defer syscall.Kill(childPID, syscall.SIGKILL)
+ cancelled := time.Now()
+ cancel()
+ if err := <-finished; err == nil || !errors.Is(ctx.Err(), context.Canceled) {
+ t.Fatalf("cancellation did not fail the command: exit=%v context=%v", err, ctx.Err())
}
- if out, err := harness(validPath); err != nil || strings.TrimSpace(string(out)) != "agent-safe-123" {
- t.Fatalf("valid descriptor-bound agent ID recovery failed: %v\n%s", err, out)
+ if elapsed := time.Since(cancelled); elapsed > time.Second {
+ t.Fatalf("cancellation held an inherited output pipe for %s", elapsed)
}
- for _, path := range []string{symlinkPath, fifoPath, oversizedPath} {
- started := time.Now()
- if out, err := harness(path); err == nil {
- t.Fatalf("unsafe agent ID path %s was accepted:\n%s", path, out)
+ deadline = time.Now().Add(time.Second)
+ for {
+ err := syscall.Kill(childPID, 0)
+ if errors.Is(err, syscall.ESRCH) {
+ break
}
- if elapsed := time.Since(started); elapsed >= 2*time.Second {
- t.Fatalf("unsafe agent ID path %s blocked for %s", path, elapsed)
+ if time.Now().After(deadline) {
+ t.Fatalf("descendant %d survived cancellation: %v", childPID, err)
}
+ time.Sleep(10 * time.Millisecond)
}
}
diff --git a/scripts/installtests/install_sh_test.go b/scripts/installtests/install_sh_test.go
index 55efa1a672..ac61d81b87 100644
--- a/scripts/installtests/install_sh_test.go
+++ b/scripts/installtests/install_sh_test.go
@@ -1,6 +1,7 @@
package installtests
import (
+ "context"
"crypto/sha256"
"encoding/base64"
"encoding/json"
@@ -8038,3 +8039,59 @@ grep -qx 'saved installer' "$PWD/state/install.sh"
t.Fatalf("lifecycle ownership: %v\n%s", err, out)
}
}
+
+func agentIDRecoveryShell(t *testing.T, binaryPath, root, path string) string {
+ t.Helper()
+ return `
+ set -euo pipefail
+ COLLECTOR_LIFECYCLE_BINARY_PATH="` + binaryPath + `"
+ INSTALL_DIR="` + root + `"
+ BINARY_NAME="absent-legacy-agent"
+ LEAST_PRIVILEGE_USER="pulse-agent-test-missing"
+` + extractLifecycleTrustShellFunctions(t) + `
+` + extractInstallShellFunction(t, "collector_lifecycle_binary") + `
+` + extractInstallShellFunction(t, "read_agent_id_file_safely") + `
+ read_agent_id_file_safely "` + path + `"
+ `
+}
+
+func TestInstallSHLegacyAgentIDRecoveryAccountsForEveryByte(t *testing.T) {
+ root := t.TempDir()
+ cases := []struct {
+ name string
+ body []byte
+ want string
+ }{
+ {"newline", []byte("agent-safe-123\n"), "agent-safe-123"},
+ {"no-newline", []byte("agent-safe-123"), "agent-safe-123"},
+ {"maximum", []byte(strings.Repeat("a", 128) + "\n"), strings.Repeat("a", 128)},
+ {"oversized-valid-prefix", []byte("agent-safe-123\n" + strings.Repeat("a", 5000)), ""},
+ {"second-identity", []byte("agent-safe-123\nother-agent\n"), ""},
+ {"nul-with-newline", []byte("agent-safe-123\x00\n"), ""},
+ {"nul-without-newline", []byte("agent-safe-123\x00"), ""},
+ {"carriage-return", []byte("agent-safe-123\r\n"), ""},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ path := filepath.Join(root, tc.name)
+ if err := os.WriteFile(path, tc.body, 0600); err != nil {
+ t.Fatal(err)
+ }
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+ cmd := exec.CommandContext(ctx, "bash", "-c", agentIDRecoveryShell(t, "", root, path))
+ cmd.WaitDelay = time.Second
+ out, err := cmd.CombinedOutput()
+ if ctx.Err() != nil {
+ t.Fatalf("legacy recovery deadline expired: %v exit=%v", ctx.Err(), err)
+ }
+ if tc.want != "" {
+ if err != nil || string(out) != tc.want+"\n" {
+ t.Fatalf("valid legacy identity rejected: %v output=%q", err, out)
+ }
+ } else if err == nil || len(out) != 0 {
+ t.Fatalf("legacy recovery accepted or disclosed corrupt state: %v output=%q", err, out)
+ }
+ })
+ }
+}