opensnitch/daemon/procmon/checksum_cache_linux.go
trvon 8abb02a808
procmon: reuse verified executable checksums
Cache stable executable digests by file identity and algorithm, coalescing
concurrent requests within bounded limits. Revalidate opened files and
fall back to full hashing on misses or uncertainty.

Keep fallback lookups read-only and protect checksum map reads and logging.
Add regression coverage for reuse, mutation, failure and contention.
2026-11-03 15:05:53 -07:00

220 lines
7.5 KiB
Go

//go:build linux
package procmon
import (
"container/list"
"encoding/hex"
"hash"
"io"
"os"
"sync"
"syscall"
"time"
)
// This is an optimization of primary /proc/PID/exe reads, not an authority for
// paths or verdicts. Fallbacks may borrow a previously verified primary image,
// but never seed the cache. Memory dumps retain their original behavior.
// Entries hold no FDs, processes, goroutines, or policy state.
var executableChecksums = newImageChecksumCache(512, 64, 128, 100*time.Millisecond)
const imageChecksumSettle = 2 * time.Second
type imageChecksumKey struct {
algorithm string
dev, ino uint64
size int64
mtimeSec, mtimeNsec, ctimeSec, ctimeNsec int64
mode, uid, gid uint32
links uint64
}
type imageChecksumResult struct {
digest string
stable bool
err error
}
type imageChecksumEntry struct {
key imageChecksumKey
digest string
}
type imageChecksumFlight struct {
done chan struct{}
result imageChecksumResult
}
type imageChecksumCache struct {
mu sync.Mutex
entries map[imageChecksumKey]*list.Element
lru *list.List
flights map[imageChecksumKey]*imageChecksumFlight
capacity, maxFlights, maxWaiters, waiters int
wait time.Duration
}
func newImageChecksumCache(capacity, flights, waiters int, wait time.Duration) *imageChecksumCache {
return &imageChecksumCache{entries: make(map[imageChecksumKey]*list.Element), lru: list.New(), flights: make(map[imageChecksumKey]*imageChecksumFlight), capacity: capacity, maxFlights: flights, maxWaiters: waiters, wait: wait}
}
// lookup is read-only with respect to cache membership: a fallback cannot seed
// an arbitrary file or join an unverified computation.
func (c *imageChecksumCache) lookup(k imageChecksumKey) (imageChecksumResult, bool) {
c.mu.Lock()
defer c.mu.Unlock()
return c.lookupLocked(k)
}
func (c *imageChecksumCache) lookupLocked(k imageChecksumKey) (imageChecksumResult, bool) {
if e := c.entries[k]; e != nil {
c.lru.MoveToFront(e)
return imageChecksumResult{digest: e.Value.(imageChecksumEntry).digest, stable: true}, true
}
return imageChecksumResult{}, false
}
// get never holds mu during IO or starts a worker. Overload, timeout, failed or
// unstable owners fall back to the caller's own complete hash, never "clean".
// reused distinguishes a borrowed digest from one already read from our FD.
func (c *imageChecksumCache) get(k imageChecksumKey, compute func() imageChecksumResult) (imageChecksumResult, bool) {
c.mu.Lock()
if r, ok := c.lookupLocked(k); ok {
c.mu.Unlock()
return r, true
}
if f := c.flights[k]; f != nil {
if c.wait <= 0 || c.waiters >= c.maxWaiters {
c.mu.Unlock()
return compute(), false
}
c.waiters++
c.mu.Unlock()
defer func() { c.mu.Lock(); c.waiters--; c.mu.Unlock() }()
timer := time.NewTimer(c.wait)
defer timer.Stop()
select {
case <-f.done:
if f.result.err == nil && f.result.stable {
return f.result, true
}
case <-timer.C:
}
return compute(), false
}
if c.capacity <= 0 || c.maxFlights <= 0 || len(c.flights) >= c.maxFlights {
c.mu.Unlock()
return compute(), false
}
f := &imageChecksumFlight{done: make(chan struct{})}
c.flights[k] = f
c.mu.Unlock()
// Also release waiters on a panic. No digest is published until compute has
// returned success and explicitly confirmed the opened image stayed stable.
r := imageChecksumResult{}
defer func() {
c.mu.Lock()
if r.err == nil && r.stable {
e := c.lru.PushFront(imageChecksumEntry{key: k, digest: r.digest})
c.entries[k] = e
if len(c.entries) > c.capacity {
last := c.lru.Back()
delete(c.entries, last.Value.(imageChecksumEntry).key)
c.lru.Remove(last)
}
}
f.result = r
delete(c.flights, k)
close(f.done)
c.mu.Unlock()
}()
r = compute()
return r, false
}
type imageChecksumFile interface {
io.Reader
Stat() (os.FileInfo, error)
Fd() uintptr
}
// Local kernel filesystems with at most one-second ctime granularity. Exclude
// FUSE/network/overlay/unknown filesystems: their metadata is not this trust
// boundary. Refusing reuse must simply perform the original complete hash.
func nativeChecksumFilesystem(kind int64) bool {
switch uint64(kind) & 0xffffffff {
case 0xef53, 0x58465342, 0x9123683e, 0x01021994, 0xf2f52010, 0x2fc12fc1: // ext*, XFS, Btrfs, tmpfs, F2FS, ZFS
return true
}
return false
}
func imageChecksumIdentity(info os.FileInfo, algo string, fsKind int64, now time.Time) (imageChecksumKey, bool) {
if info == nil || !info.Mode().IsRegular() || !nativeChecksumFilesystem(fsKind) || (algo != HashMD5 && algo != HashSHA1) {
return imageChecksumKey{}, false
}
st, ok := info.Sys().(*syscall.Stat_t)
if !ok || st.Size < 0 || st.Nlink == 0 || st.Ctim.Nsec < 0 || st.Ctim.Nsec >= 1e9 || st.Mtim.Nsec < 0 || st.Mtim.Nsec >= 1e9 {
return imageChecksumKey{}, false
}
// A writable file can change twice within one coarse ctime tick. Never seed
// an entry from that tick. Primary executable images have kernel write
// exclusion, unlike arbitrary files with outstanding writable mmap mappings.
// Root/clock manipulation and lying filesystems are not metadata guarantees.
if time.Unix(int64(st.Ctim.Sec), int64(st.Ctim.Nsec)).After(now.Add(-imageChecksumSettle)) {
return imageChecksumKey{}, false
}
return imageChecksumKey{algorithm: algo, dev: uint64(st.Dev), ino: st.Ino, size: st.Size, mtimeSec: int64(st.Mtim.Sec), mtimeNsec: int64(st.Mtim.Nsec), ctimeSec: int64(st.Ctim.Sec), ctimeNsec: int64(st.Ctim.Nsec), mode: st.Mode, uid: st.Uid, gid: st.Gid, links: uint64(st.Nlink)}, true
}
func openedImageChecksumKey(f imageChecksumFile, algo string) (imageChecksumKey, bool) {
info, err := f.Stat()
if err != nil {
return imageChecksumKey{}, false
}
var fs syscall.Statfs_t
if syscall.Fstatfs(int(f.Fd()), &fs) != nil {
return imageChecksumKey{}, false
}
return imageChecksumIdentity(info, algo, int64(fs.Type), time.Now())
}
// checksumVerifiedFallback never publishes a fallback's bytes as a verified
// primary image. Without an existing success it performs the original IO.
func checksumVerifiedFallback(f imageChecksumFile, h hash.Hash, algo string, c *imageChecksumCache) (string, error) {
if k, eligible := openedImageChecksumKey(f, algo); eligible {
if r, found := c.lookup(k); found {
if after, ok := openedImageChecksumKey(f, algo); ok && after == k {
return r.digest, nil
}
}
}
if _, err := io.Copy(h, f); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func checksumPrimaryImage(f imageChecksumFile, h hash.Hash, algo string, c *imageChecksumCache) (string, error) {
k, eligible := openedImageChecksumKey(f, algo)
read := func() imageChecksumResult {
n, err := io.Copy(h, f)
if err != nil {
return imageChecksumResult{err: err}
}
digest := hex.EncodeToString(h.Sum(nil))
after, ok := openedImageChecksumKey(f, algo)
return imageChecksumResult{digest: digest, stable: eligible && ok && after == k && n == k.size}
}
if !eligible {
r := read()
return r.digest, r.err
}
r, reused := c.get(k, read)
if reused {
// Our separately opened FD may have changed while another owner was hashing
// or between lookup and return. It is still unread, so fall back normally.
after, ok := openedImageChecksumKey(f, algo)
if !ok || after != k {
r = read()
}
}
return r.digest, r.err
}