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.

Harden helper quarantine file reads

rcourtmanac47e67878f0
diff --git a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
index 711b362554..669148b60d 100644
--- a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
+++ b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
@@ -142,6 +142,12 @@ retains the previous-version update evidence from that handoff.
Before staging or activation, the helper must establish that the signed target
is the Pulse agent Go command, that its declared and operational version equals
the requested target, and that the target advances the installed version.
+Every collector-owned quarantine artifact is opened exactly once with
+no-follow, close-on-exec, and nonblocking semantics. The helper derives file
+type, ownership, mode, and bounded size only from that descriptor, rejects
+FIFO, device, socket, symlink, oversized, and size-unstable inputs without
+mutating staging or activation state, and releases update admission so a
+subsequent valid stage can proceed.
Commit is additionally bound to the activating process ID and to the digest of
that process's `/proc/<pid>/exe`, preventing an unrelated collector process
from finalizing a pending replacement.
diff --git a/internal/agenthelper/file_security_unix.go b/internal/agenthelper/file_security_unix.go
index de884687b4..b8a77cf012 100644
--- a/internal/agenthelper/file_security_unix.go
+++ b/internal/agenthelper/file_security_unix.go
@@ -6,10 +6,20 @@ import (
"errors"
"os"
"syscall"
+
+ "golang.org/x/sys/unix"
)
func openFileNoFollow(path string) (*os.File, error) {
- return os.OpenFile(path, os.O_RDONLY|syscall.O_NOFOLLOW, 0)
+ // Quarantine files are collector-owned. O_NONBLOCK ensures a FIFO or
+ // device swap cannot wedge the privileged helper before descriptor metadata
+ // rejects the object as non-regular. The returned descriptor is the sole
+ // authority for all subsequent type, ownership, mode, and size checks.
+ fd, err := unix.Open(path, unix.O_RDONLY|unix.O_CLOEXEC|unix.O_NOFOLLOW|unix.O_NONBLOCK, 0)
+ if err != nil {
+ return nil, err
+ }
+ return os.NewFile(uintptr(fd), path), nil
}
func StrictRootOwnedFile(file *os.File) error {
diff --git a/internal/agenthelper/file_security_unix_test.go b/internal/agenthelper/file_security_unix_test.go
index e379c03a62..7c1c75e5f7 100644
--- a/internal/agenthelper/file_security_unix_test.go
+++ b/internal/agenthelper/file_security_unix_test.go
@@ -3,9 +3,13 @@
package agenthelper
import (
+ "context"
"os"
"path/filepath"
"testing"
+ "time"
+
+ "golang.org/x/sys/unix"
)
func TestStrictRootOwnedFileRejectsUnprivilegedOwner(t *testing.T) {
@@ -25,3 +29,108 @@ func TestStrictRootOwnedFileRejectsUnprivilegedOwner(t *testing.T) {
t.Fatal("unprivileged artifact owner accepted")
}
}
+
+func TestOpenFileNoFollowDoesNotBlockOnFIFO(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "artifact.fifo")
+ if err := unix.Mkfifo(path, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ type openResult struct {
+ file *os.File
+ err error
+ }
+ opened := make(chan openResult, 1)
+ go func() {
+ file, err := openFileNoFollow(path)
+ opened <- openResult{file: file, err: err}
+ }()
+ select {
+ case result := <-opened:
+ if result.file != nil {
+ result.file.Close()
+ }
+ if result.err != nil {
+ t.Fatalf("openFileNoFollow FIFO: %v", result.err)
+ }
+ case <-time.After(250 * time.Millisecond):
+ // Pair a writer with a legacy blocking reader so the regression fails
+ // without leaking a blocked goroutine or file descriptor.
+ writerDone := make(chan *os.File, 1)
+ go func() {
+ writer, _ := os.OpenFile(path, os.O_WRONLY, 0)
+ writerDone <- writer
+ }()
+ result := <-opened
+ writer := <-writerDone
+ if result.file != nil {
+ result.file.Close()
+ }
+ if writer != nil {
+ writer.Close()
+ }
+ t.Fatal("openFileNoFollow blocked on a FIFO")
+ }
+}
+
+func TestUpdateStageRejectsFIFOWithoutBlockingAndRecovers(t *testing.T) {
+ for _, fifoName := range []string{"pulse-agent", "pulse-agent.sig"} {
+ t.Run(fifoName, func(t *testing.T) {
+ provider, quarantine, _, statePath := testUpdateActivator(t, func([]byte, string) error { return nil })
+ artifactID := "fifo-release"
+ artifact := testELF("new-signed-binary")
+ artifactDir := filepath.Join(quarantine, artifactID)
+ if err := os.Mkdir(artifactDir, 0o700); err != nil {
+ t.Fatal(err)
+ }
+ for name, data := range map[string][]byte{
+ "pulse-agent": artifact,
+ "pulse-agent.sig": []byte("valid-signature"),
+ } {
+ path := filepath.Join(artifactDir, name)
+ if name == fifoName {
+ if err := unix.Mkfifo(path, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ continue
+ }
+ if err := os.WriteFile(path, data, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ request := UpdateStageRequest{ArtifactID: artifactID, SHA256: sha256Hex(artifact), Version: "1.1.0"}
+ result := make(chan error, 1)
+ go func() {
+ _, err := provider.Stage(context.Background(), request)
+ result <- err
+ }()
+ select {
+ case err := <-result:
+ if err == nil {
+ t.Fatal("FIFO quarantine artifact was accepted")
+ }
+ case <-time.After(time.Second):
+ t.Fatal("FIFO quarantine artifact blocked the privileged update helper")
+ }
+ if _, err := os.Stat(filepath.Join(filepath.Dir(quarantine), "staging", artifactID)); !os.IsNotExist(err) {
+ t.Fatalf("invalid FIFO artifact mutated staging: %v", err)
+ }
+ if _, err := os.Stat(statePath); !os.IsNotExist(err) {
+ t.Fatalf("invalid FIFO artifact mutated durable state: %v", err)
+ }
+
+ fifoPath := filepath.Join(artifactDir, fifoName)
+ if err := os.Remove(fifoPath); err != nil {
+ t.Fatal(err)
+ }
+ replacement := artifact
+ if fifoName == "pulse-agent.sig" {
+ replacement = []byte("valid-signature")
+ }
+ if err := os.WriteFile(fifoPath, replacement, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ promoteUpdate(t, provider, artifactID, request.SHA256)
+ })
+ }
+}
diff --git a/internal/agenthelper/update_activation.go b/internal/agenthelper/update_activation.go
index 21bcf1ca77..a9edbb4edb 100644
--- a/internal/agenthelper/update_activation.go
+++ b/internal/agenthelper/update_activation.go
@@ -565,13 +565,6 @@ func (u *updateActivator) readOwnedBounded(path string, limit int64) ([]byte, er
}
func (u *updateActivator) readOwnedBoundedWith(path string, limit int64, validate func(*os.File) error) ([]byte, error) {
- before, err := os.Lstat(path)
- if err != nil {
- return nil, err
- }
- if before.Mode()&os.ModeSymlink != 0 || !before.Mode().IsRegular() {
- return nil, errors.New("unsafe file type")
- }
file, err := openFileNoFollow(path)
if err != nil {
return nil, err
@@ -580,14 +573,18 @@ func (u *updateActivator) readOwnedBoundedWith(path string, limit int64, validat
if err := validate(file); err != nil {
return nil, err
}
- after, err := file.Stat()
- if err != nil || !os.SameFile(before, after) || !after.Mode().IsRegular() || after.Mode().Perm()&0o022 != 0 {
- return nil, errors.New("file identity or ownership changed")
+ before, err := file.Stat()
+ if err != nil || !before.Mode().IsRegular() || before.Mode().Perm()&0o022 != 0 || before.Size() < 0 || before.Size() > limit {
+ return nil, errors.New("file descriptor is not a bounded private regular file")
}
data, err := io.ReadAll(io.LimitReader(file, limit+1))
if err != nil || int64(len(data)) > limit {
return nil, errors.New("file exceeds bounded size")
}
+ after, err := file.Stat()
+ if err != nil || !after.Mode().IsRegular() || after.Mode().Perm()&0o022 != 0 || after.Size() != before.Size() || int64(len(data)) != after.Size() {
+ return nil, errors.New("file descriptor changed while being read")
+ }
return data, nil
}