Commit graph

5 commits

Author SHA1 Message Date
trvon
383c22cbd3
exec guard: answer a cache hit without holding the cache lock
Both hit paths (fanotify and Endpoint Security) called finish() while holding
cache_mutex_. finish() is the answer: it writes the kernel response, runs the
observation callback, bumps stats under stats_mutex_ and pushes to
report_mutex_. Holding the cache lock across that serialised every cache-hit
exec behind one mutex even though the daemon runs two workers, so on Linux a
slow answer was a slow exec - in the path whose whole job is to keep launches
fast.

detail::answer_cached_assessment copies the entry under the lock, releases it,
and only then answers. cache_mutex_ is now a leaf lock (also at the work-loop
insert), so the old edge cache_mutex_ -> {pending, stats, report} is gone and no
inversion can be introduced. Answer-exactly-once is unchanged: respond() still
checks pending->responded under the pending mutex, and the hit path still
answers once and returns.

The seam is declared in exec_guard.hpp so the tests no longer redeclare an
internal function in their own translation unit.
2026-11-19 17:29:38 -07:00
trvon
cae67981ab
detection: mark a timed-out scan incomplete without bypassing quarantine
Review of 893127c found that reporting `scanned = false` for a timed-out
guard scan was wrong in a way that mattered: decide_exec answers `allow` on
`!scanned` BEFORE it looks at the operator quarantine, so a quarantined binary
whose scan overran its deadline would have been allowed to run. It also dropped
the only `scanned = true` on the normal assessment path, so every ordinary
assessment became "unscanned".

The assessment now keeps `scanned = true` (quarantine, allowlist and IOC
decisions still apply, and the guard still fails open on a thin result) and
carries a separate `incomplete` flag, which alone prevents caching:
guard_assessment_cacheable() and the pipeline's cache_put both exclude it.

Also fixes the same verdict being lost one caller up: scan_on_demand set
ok = false with an explanatory error, then unconditionally reset ok = true after
the match loop, so control scan-fd, MCP and retro-hunt all reported a timed-out
scan as a completed one with no error.

Tests: a timed-out assessment keeps scanned true and top_rule evidence, is
marked incomplete, and a second exec re-scans instead of hitting the cache; a
quarantined incomplete assessment still blocks in both enforce and audit, while
an incomplete clean one is allowed and not cacheable.
2026-11-19 16:48:19 -07:00
trvon
666608c4c0
detection: ctime in the scan key, retry after a failed scan, never hold live execs
Three launch-path defects from the audit.

The scan cache key was dev/ino/size/mtime, and mtime is settable via utimensat,
so a rewrite that restored size and mtime (cp -p, install -p, package restore, or
a deliberate attacker) served the old verdict with no TTL. The key now includes
ctime, and the fanotify path calls the shared detail::image_cache_key instead of
re-deriving the fields, so the guard and the pipeline cannot drift apart again.

recent_images_ was inserted before the scan and never removed, so an image that
was unavailable or whose buffer grant was refused was never retried for that
process instance - contradicting scan_image's own comment. The key is now
dropped when the outcome is unavailable, read_error or yara_error, while
completed scans stay deduped.

Live exec scans were deferred for up to ~5 s per emergency episode. The live
queue is bounded at 4096 and full means dropped exec events, so a fork storm
during an emergency could lose telemetry the guard depends on. The live lane now
takes a single non-blocking probe (kLiveExecHold is 0, pinned by a
static_assert) and scans anyway, fail-open; background inventory keeps the
bounded hold.

Tests: a same-size, mtime-restored rewrite is re-scanned rather than served from
cache; a transient scan failure is retried on the next event while a completed
scan is not; the live-lane probe never blocks.
2026-11-19 16:21:34 -07:00
trvon
893127c691
detection: a timed-out or degraded scan is never a clean verdict
Two launch-path defects let a weaker-than-complete scan become the cached
answer for an executable until the next rules reload. In enforce mode that
means the exec guard silently stops consulting YARA for that binary.

1. A YARA deadline overrun was reported as success. timed_yara_scan/
   timed_yara_scan_ranges returned `ok || timed_out`, so assess_exec set
   have_matches and cached the image with an EMPTY match list; the guard cached
   that assessment too. The guard's YARA budget is exactly prevention_timeout_ms
   (1 s by default), so one slow scan was enough. They now return a
   BoundedYaraScan {ok, timed_out} and only `completed() == ok && !timed_out`
   may set have_matches or reach cache_put; scan_image returns yara_error and
   caches nothing; the assessment reports scanned == false so the guard fails
   open instead of inheriting a clean answer. Matches found before the deadline
   are still reported as evidence, and a timeout is counted in yara_timeouts.

2. The guard cached degraded and partial assessments, contradicting
   exec_guard.hpp ("not cached") and the pipeline, which already excluded them.
   A degraded result is produced by a transient condition (the scan-buffer lease
   is momentarily held), so one collision pinned the image to hash/IOC-only for
   the whole rules+policy generation. The rule now lives in one place,
   guard_assessment_cacheable(), used at both the Linux and macOS cache stores.

Tests: a timed-out assess_exec is not scanned, still reports the partial match,
and a second exec re-scans instead of hitting a cache; the cache predicate is
covered for clean, degraded, partial and unscanned assessments.
2026-11-19 11:49:41 -07:00
trvon
1d1ab9f57e
Add exec prevention, behavior telemetry and persistence watch
fanotify exec-permission guard (audit/enforce, fail-open deadline),
eBPF behavior engine (IOC connections, ptrace, module loads,
download-then-execute) and fanotify persistence watch, with unit tests
and privileged Docker live tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:17:16 -06:00