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.
465 lines
13 KiB
Go
465 lines
13 KiB
Go
//go:build linux
|
|
|
|
package procmon
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"crypto/sha1"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"sync/atomic"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func cacheOK(s string) imageChecksumResult { return imageChecksumResult{digest: s, stable: true} }
|
|
|
|
func TestImageChecksumCacheBounded(t *testing.T) {
|
|
c := newImageChecksumCache(3, 2, 2, time.Second)
|
|
for i := 0; i < 1000; i++ {
|
|
k := imageChecksumKey{ino: uint64(i), algorithm: HashMD5}
|
|
r, reused := c.get(k, func() imageChecksumResult {
|
|
// Calling out under the global lock would deadlock here.
|
|
c.mu.Lock()
|
|
c.mu.Unlock()
|
|
return cacheOK(fmt.Sprint(i))
|
|
})
|
|
if r.err != nil || reused {
|
|
t.Fatal("unexpected initial reuse")
|
|
}
|
|
if len(c.entries) > 3 || len(c.flights) != 0 || c.waiters != 0 {
|
|
t.Fatal("unbounded cache state")
|
|
}
|
|
}
|
|
k := imageChecksumKey{ino: 999, algorithm: HashMD5}
|
|
r, reused := c.get(k, func() imageChecksumResult { t.Fatal("hot entry rehashed"); return imageChecksumResult{} })
|
|
if !reused || r.digest != "999" {
|
|
t.Fatal("missing hot entry")
|
|
}
|
|
k.algorithm = HashSHA1
|
|
r, reused = c.get(k, func() imageChecksumResult { return cacheOK("sha1") })
|
|
if reused || r.digest != "sha1" {
|
|
t.Fatal("algorithm key collision")
|
|
}
|
|
k.ino = 0
|
|
_, reused = c.get(k, func() imageChecksumResult { return cacheOK("evicted") })
|
|
if reused {
|
|
t.Fatal("old entry was not evicted")
|
|
}
|
|
}
|
|
|
|
func TestImageChecksumFlightDedup(t *testing.T) {
|
|
c := newImageChecksumCache(4, 2, 16, time.Second)
|
|
k := imageChecksumKey{ino: 1}
|
|
started, release := make(chan struct{}), make(chan struct{})
|
|
var calls atomic.Int32
|
|
compute := func() imageChecksumResult {
|
|
if calls.Add(1) == 1 {
|
|
close(started)
|
|
}
|
|
<-release
|
|
return cacheOK("digest")
|
|
}
|
|
var done sync.WaitGroup
|
|
done.Add(1)
|
|
go func() { defer done.Done(); c.get(k, compute) }()
|
|
<-started
|
|
var ready sync.WaitGroup
|
|
ready.Add(8)
|
|
done.Add(8)
|
|
for i := 0; i < 8; i++ {
|
|
go func() {
|
|
defer done.Done()
|
|
ready.Done()
|
|
r, _ := c.get(k, compute)
|
|
if r.digest != "digest" {
|
|
t.Error("bad shared result")
|
|
}
|
|
}()
|
|
}
|
|
ready.Wait()
|
|
close(release)
|
|
done.Wait()
|
|
if calls.Load() != 1 || len(c.flights) != 0 || c.waiters != 0 {
|
|
t.Fatal("single-flight did not coalesce or clean up")
|
|
}
|
|
}
|
|
|
|
func TestImageChecksumFlightTimeoutAndOverload(t *testing.T) {
|
|
c := newImageChecksumCache(4, 1, 1, 5*time.Millisecond)
|
|
k := imageChecksumKey{ino: 1}
|
|
started, release, done := make(chan struct{}), make(chan struct{}), make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
c.get(k, func() imageChecksumResult { close(started); <-release; return cacheOK("owner") })
|
|
}()
|
|
<-started
|
|
r, reused := c.get(imageChecksumKey{ino: 2}, func() imageChecksumResult { return cacheOK("overload") })
|
|
if reused || r.digest != "overload" || len(c.entries) != 0 {
|
|
t.Fatal("inflight limit not honored")
|
|
}
|
|
r, reused = c.get(k, func() imageChecksumResult { return cacheOK("timeout own hash") })
|
|
if reused || r.digest != "timeout own hash" {
|
|
t.Fatal("wait timeout must hash, not claim clean")
|
|
}
|
|
close(release)
|
|
<-done
|
|
if len(c.flights) != 0 || c.waiters != 0 {
|
|
t.Fatal("timeout leaked flight/waiter")
|
|
}
|
|
}
|
|
|
|
func TestImageChecksumFailureAndPanicCleanup(t *testing.T) {
|
|
c := newImageChecksumCache(4, 2, 2, time.Second)
|
|
k := imageChecksumKey{ino: 1}
|
|
for _, result := range []imageChecksumResult{{err: syscall.EIO}, {digest: "unstable", stable: false}} {
|
|
c.get(k, func() imageChecksumResult { return result })
|
|
if len(c.entries) != 0 || len(c.flights) != 0 {
|
|
t.Fatal("failure/unstable result cached")
|
|
}
|
|
}
|
|
func() {
|
|
defer func() {
|
|
if recover() == nil {
|
|
t.Error("expected callback panic")
|
|
}
|
|
}()
|
|
c.get(k, func() imageChecksumResult { panic("test-only callback panic") })
|
|
}()
|
|
if len(c.flights) != 0 || len(c.entries) != 0 {
|
|
t.Fatal("panic stranded flight")
|
|
}
|
|
r, _ := c.get(k, func() imageChecksumResult { return cacheOK("recovered") })
|
|
if r.digest != "recovered" {
|
|
t.Fatal("failed owner poisoned key")
|
|
}
|
|
}
|
|
|
|
func TestImageChecksumFailedOwnerDoesNotPoisonWaiter(t *testing.T) {
|
|
c := newImageChecksumCache(4, 2, 2, time.Second)
|
|
k := imageChecksumKey{ino: 1}
|
|
started, release, done := make(chan struct{}), make(chan struct{}), make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
c.get(k, func() imageChecksumResult { close(started); <-release; return imageChecksumResult{err: syscall.EIO} })
|
|
}()
|
|
<-started
|
|
result := make(chan imageChecksumResult, 1)
|
|
go func() { r, _ := c.get(k, func() imageChecksumResult { return cacheOK("own success") }); result <- r }()
|
|
close(release)
|
|
<-done
|
|
r := <-result
|
|
if r.err != nil || r.digest != "own success" {
|
|
t.Fatal("failed owner propagated success/error to independent reader")
|
|
}
|
|
if len(c.flights) != 0 || c.waiters != 0 {
|
|
t.Fatal("failed owner leaked state")
|
|
}
|
|
}
|
|
|
|
type checksumTestInfo struct {
|
|
os.FileInfo
|
|
st syscall.Stat_t
|
|
}
|
|
|
|
func (i checksumTestInfo) Sys() interface{} { return &i.st }
|
|
|
|
type checksumTestFile struct {
|
|
File *os.File
|
|
info checksumTestInfo
|
|
reads int
|
|
maxRead int
|
|
readError error
|
|
statError error
|
|
mutate bool
|
|
earlyEOF bool
|
|
statCalls int
|
|
mutateOnStat int
|
|
}
|
|
|
|
func (f *checksumTestFile) Fd() uintptr { return f.File.Fd() }
|
|
func (f *checksumTestFile) Stat() (os.FileInfo, error) {
|
|
f.statCalls++
|
|
if f.statError != nil {
|
|
return nil, f.statError
|
|
}
|
|
if f.statCalls == f.mutateOnStat {
|
|
f.info.st.Ctim.Sec++
|
|
}
|
|
return f.info, nil
|
|
}
|
|
func (f *checksumTestFile) Read(p []byte) (int, error) {
|
|
f.reads++
|
|
if f.earlyEOF && f.reads > 1 {
|
|
return 0, io.EOF
|
|
}
|
|
if f.maxRead > 0 && len(p) > f.maxRead {
|
|
p = p[:f.maxRead]
|
|
}
|
|
n, err := f.File.Read(p)
|
|
if f.mutate {
|
|
f.info.st.Ctim.Sec++
|
|
f.mutate = false
|
|
}
|
|
if f.readError != nil {
|
|
return n, f.readError
|
|
}
|
|
return n, err
|
|
}
|
|
func checksumOpenTestFile(t *testing.T, path string) *checksumTestFile {
|
|
t.Helper()
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { f.Close() })
|
|
info, err := f.Stat()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st := *info.Sys().(*syscall.Stat_t)
|
|
// Unit-only old metadata models a settled primary image. End-to-end tests
|
|
// separately exercise real /proc/self/exe without metadata substitution.
|
|
st.Ctim.Sec -= 10
|
|
return &checksumTestFile{File: f, info: checksumTestInfo{FileInfo: info, st: st}}
|
|
}
|
|
|
|
func TestImageChecksumEligibility(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "image")
|
|
if err := os.WriteFile(path, []byte("content"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f := checksumOpenTestFile(t, path)
|
|
now := time.Now()
|
|
old := f.info
|
|
base, ok := imageChecksumIdentity(old, HashMD5, 0x2fc12fc1, now)
|
|
if !ok {
|
|
t.Fatal("settled regular native image rejected")
|
|
}
|
|
for _, kind := range []int64{0x65735546, 0x6969, 0xff534d42, 0x794c7630, 0} { // FUSE,NFS,CIFS,overlay,unknown
|
|
if _, ok := imageChecksumIdentity(old, HashMD5, kind, now); ok {
|
|
t.Fatal("untrusted filesystem accepted")
|
|
}
|
|
}
|
|
young := old
|
|
young.st.Ctim.Sec = now.Unix()
|
|
young.st.Ctim.Nsec = int64(now.Nanosecond())
|
|
if _, ok := imageChecksumIdentity(young, HashMD5, 0x2fc12fc1, now); ok {
|
|
t.Fatal("fresh ctime accepted")
|
|
}
|
|
deleted := old
|
|
deleted.st.Nlink = 0
|
|
if _, ok := imageChecksumIdentity(deleted, HashMD5, 0x2fc12fc1, now); ok {
|
|
t.Fatal("deleted inode accepted")
|
|
}
|
|
if _, ok := imageChecksumIdentity(old, "unsupported", 0x2fc12fc1, now); ok {
|
|
t.Fatal("unknown algorithm accepted")
|
|
}
|
|
for i := 0; i < 64; i++ {
|
|
changed := old
|
|
switch i % 8 {
|
|
case 0:
|
|
changed.st.Ino++
|
|
case 1:
|
|
changed.st.Dev++
|
|
case 2:
|
|
changed.st.Size++
|
|
case 3:
|
|
changed.st.Mtim.Nsec++
|
|
case 4:
|
|
changed.st.Ctim.Nsec++
|
|
case 5:
|
|
changed.st.Uid++
|
|
case 6:
|
|
changed.st.Mode ^= 0200
|
|
case 7:
|
|
changed.st.Nlink++
|
|
}
|
|
k, ok := imageChecksumIdentity(changed, HashMD5, 0x2fc12fc1, now)
|
|
if ok && k == base {
|
|
t.Fatalf("identity field %d conflated", i%8)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestImageChecksumActualReadsAndFaults(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "image")
|
|
data := []byte("a complete image")
|
|
if err := os.WriteFile(path, data, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, mode := range []string{"short", "read-error", "stat-error", "mutation", "early-EOF"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
c := newImageChecksumCache(4, 2, 2, time.Second)
|
|
f := checksumOpenTestFile(t, path)
|
|
if _, ok := openedImageChecksumKey(f, HashMD5); !ok {
|
|
t.Skip("host filesystem intentionally not cacheable")
|
|
}
|
|
switch mode {
|
|
case "short":
|
|
f.maxRead = 1
|
|
case "read-error":
|
|
f.readError = syscall.EIO
|
|
case "stat-error":
|
|
f.statError = syscall.EIO
|
|
case "mutation":
|
|
f.mutate = true
|
|
case "early-EOF":
|
|
f.maxRead = 1
|
|
f.earlyEOF = true
|
|
}
|
|
got, err := checksumPrimaryImage(f, md5.New(), HashMD5, c)
|
|
if mode == "read-error" {
|
|
if !errors.Is(err, syscall.EIO) || len(c.entries) != 0 {
|
|
t.Fatal("read error cached")
|
|
}
|
|
return
|
|
}
|
|
if mode == "early-EOF" {
|
|
if len(c.entries) != 0 {
|
|
t.Fatal("partial EOF cached as full image")
|
|
}
|
|
return
|
|
}
|
|
want := fmt.Sprintf("%x", md5.Sum(data))
|
|
if err != nil || got != want {
|
|
t.Fatal("short/uncacheable read must return full correct digest")
|
|
}
|
|
if mode != "short" {
|
|
if len(c.entries) != 0 {
|
|
t.Fatal("unstable/unavailable metadata cached")
|
|
}
|
|
return
|
|
}
|
|
if f.reads <= 1 || len(c.entries) != 1 {
|
|
t.Fatal("short reads were not completed/cacheable")
|
|
}
|
|
again := checksumOpenTestFile(t, path)
|
|
got, err = checksumPrimaryImage(again, md5.New(), HashMD5, c)
|
|
if err != nil || got != want || again.reads != 0 {
|
|
t.Fatal("successful full image was re-read")
|
|
}
|
|
sha := checksumOpenTestFile(t, path)
|
|
got, err = checksumPrimaryImage(sha, sha1.New(), HashSHA1, c)
|
|
if err != nil || got != fmt.Sprintf("%x", sha1.Sum(data)) || sha.reads == 0 {
|
|
t.Fatal("MD5 reused as SHA1")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestImageChecksumPostLookupRecheck(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "image")
|
|
if err := os.WriteFile(path, []byte("image A"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c := newImageChecksumCache(4, 2, 2, time.Second)
|
|
first := checksumOpenTestFile(t, path)
|
|
if _, ok := openedImageChecksumKey(first, HashMD5); !ok {
|
|
t.Skip("host filesystem intentionally not cacheable")
|
|
}
|
|
if _, err := checksumPrimaryImage(first, md5.New(), HashMD5, c); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte("image B"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
next := checksumOpenTestFile(t, path)
|
|
next.info = first.info
|
|
next.mutateOnStat = 2
|
|
got, err := checksumPrimaryImage(next, md5.New(), HashMD5, c)
|
|
if err != nil || got != fmt.Sprintf("%x", md5.Sum([]byte("image B"))) || next.reads == 0 {
|
|
t.Fatal("version change after lookup reused stale digest")
|
|
}
|
|
}
|
|
|
|
func TestImageChecksumFallbackCannotSeedOrWait(t *testing.T) {
|
|
c := newImageChecksumCache(4, 2, 2, time.Second)
|
|
k := imageChecksumKey{ino: 1, algorithm: HashMD5}
|
|
if _, found := c.lookup(k); found || len(c.entries) != 0 || len(c.flights) != 0 {
|
|
t.Fatal("lookup seeded missing image")
|
|
}
|
|
started, release, done := make(chan struct{}), make(chan struct{}), make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
c.get(k, func() imageChecksumResult { close(started); <-release; return cacheOK("verified") })
|
|
}()
|
|
<-started
|
|
if _, found := c.lookup(k); found || c.waiters != 0 {
|
|
t.Fatal("fallback joined unverified flight")
|
|
}
|
|
close(release)
|
|
<-done
|
|
if r, found := c.lookup(k); !found || r.digest != "verified" {
|
|
t.Fatal("completed primary image unavailable to fallback")
|
|
}
|
|
}
|
|
|
|
func TestImageChecksumFallbackPostLookupRecheck(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "image")
|
|
if err := os.WriteFile(path, []byte("image A"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c := newImageChecksumCache(4, 2, 2, time.Second)
|
|
first := checksumOpenTestFile(t, path)
|
|
if _, ok := openedImageChecksumKey(first, HashMD5); !ok {
|
|
t.Skip("host filesystem intentionally not cacheable")
|
|
}
|
|
if _, err := checksumPrimaryImage(first, md5.New(), HashMD5, c); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte("image B"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
next := checksumOpenTestFile(t, path)
|
|
next.info = first.info
|
|
next.mutateOnStat = 2
|
|
got, err := checksumVerifiedFallback(next, md5.New(), HashMD5, c)
|
|
if err != nil || got != fmt.Sprintf("%x", md5.Sum([]byte("image B"))) || next.reads == 0 {
|
|
t.Fatal("fallback version change after lookup reused stale digest")
|
|
}
|
|
if len(c.entries) != 1 {
|
|
t.Fatal("fallback inserted a new image version")
|
|
}
|
|
}
|
|
|
|
func TestChecksumLiteralFastPaths(t *testing.T) {
|
|
t.Run("prefilled", func(t *testing.T) {
|
|
const digest = "0123456789abcdef0123456789abcdef"
|
|
p := &Process{Path: "unused-literal-fastpath", Checksums: map[string]string{HashMD5: digest}}
|
|
p.ComputeChecksum(HashMD5)
|
|
if p.Checksums[HashMD5] != digest {
|
|
t.Fatal("literal prefilled checksum changed")
|
|
}
|
|
})
|
|
t.Run("unsupported", func(t *testing.T) {
|
|
p := &Process{Path: "unused-literal-fastpath"}
|
|
p.ComputeChecksum("unsupported-checksum-algorithm")
|
|
if len(p.Checksums) != 0 {
|
|
t.Fatal("unsupported algorithm enabled")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestChecksumSameProcessConcurrentCalls(t *testing.T) {
|
|
path, _ := checksumLiveImage(t)
|
|
p := checksumFixtureProcess(400, path, ProcSelfExe)
|
|
var done sync.WaitGroup
|
|
done.Add(12)
|
|
for i := 0; i < 12; i++ {
|
|
go func() { defer done.Done(); p.ComputeChecksum(HashMD5) }()
|
|
}
|
|
done.Wait()
|
|
if p.Checksums[HashMD5] != checksumExpected(t, ProcSelfExe, HashMD5) {
|
|
t.Fatal("same-process checksum race")
|
|
}
|
|
}
|
|
|
|
// Interface conformance keeps fault wrappers on the same actual IO helper.
|
|
var _ imageChecksumFile = (*checksumTestFile)(nil)
|
|
var _ io.Reader = (*checksumTestFile)(nil)
|