Bind authenticated snapshots to controller PID/starttime and validate process identity and held inode anchors before reporting current check health. Preserve identical signed foreign replacements and fail closed when live ownership is unavailable. Co-authored-by: GPT-6 <gpt-6@openai.com>
64 lines
2.2 KiB
Go
64 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
)
|
|
|
|
// A signed cache is not a live ownership claim. Inspect only procfs metadata,
|
|
// never the controller's private FD contents, and fail closed on uncertainty.
|
|
func resourceMonitorLiveOwner(store *resourceMonitorStore, state resourceMonitorState, config, proc string) error {
|
|
owner := state.Controller
|
|
if owner == nil || owner.PID <= 1 || owner.Start <= 0 || config == "" {
|
|
return errResourceState
|
|
}
|
|
base := filepath.Join(proc, strconv.Itoa(owner.PID))
|
|
identity := func() bool {
|
|
stat, err := os.ReadFile(filepath.Join(base, "stat"))
|
|
start, parseErr := parseMonitorProcessStat(owner.PID, string(stat))
|
|
if err != nil || parseErr != nil || start != owner.Start {
|
|
return false
|
|
}
|
|
exe, err := os.Readlink(filepath.Join(proc, "self/exe"))
|
|
self, selfErr := os.Stat(filepath.Join(proc, "self/exe"))
|
|
other, otherErr := os.Stat(filepath.Join(base, "exe"))
|
|
if err != nil || selfErr != nil || otherErr != nil || !os.SameFile(self, other) {
|
|
return false
|
|
}
|
|
return resourceProcessOwned(proc, owner.PID, exe, []string{exe, "managed", "monitor", "--resource-config", config}) ||
|
|
resourceProcessOwned(proc, owner.PID, exe, []string{exe, "managed", "monitor"})
|
|
}
|
|
if !identity() || store.snapshotGuard() != nil {
|
|
return errResourceState
|
|
}
|
|
// Include the singleton and directory so another target's controller cannot
|
|
// lend apparent liveness to a copied authenticated snapshot.
|
|
anchors := []string{store.path, store.entryPath(resourceKeyName), store.entryPath(resourceStateName), store.entryPath(managedMonitorLockName)}
|
|
fds, err := os.ReadDir(filepath.Join(base, "fd"))
|
|
if err != nil {
|
|
return errResourceState
|
|
}
|
|
for _, anchor := range anchors {
|
|
current, err := os.Lstat(anchor)
|
|
if err != nil {
|
|
return errResourceState
|
|
}
|
|
matched := false
|
|
for _, fd := range fds {
|
|
held, err := os.Stat(filepath.Join(base, "fd", fd.Name()))
|
|
if err == nil && os.SameFile(current, held) {
|
|
matched = true
|
|
break
|
|
}
|
|
}
|
|
if !matched {
|
|
return errResourceState
|
|
}
|
|
}
|
|
if !identity() || store.snapshotGuard() != nil || managedPrivateFile(store.entryPath(managedMonitorLockName)) != nil {
|
|
return errResourceState
|
|
}
|
|
return nil
|
|
}
|