Commit graph

12 commits

Author SHA1 Message Date
trvon
29e490dcab
Replay: add strict read-only training summaries
Reuse the existing resolver/sampler for opt-in aggregate training summaries. Reject incomplete or undecodable snapshots without usable partial reports, retain best-effort readers elsewhere, and document cooperative extent checks rather than atomic snapshot guarantees. Include command-table/help, JSON, fixture and corruption regressions; no model training or promotion.
2026-11-03 06:54:17 -07:00
trvon
52b02f1dc7
Learn: preserve label provenance and bound replay sampling
Resolve allowlist and quarantine retractions without erasing independent support. Refresh capped entity reservoirs and bound allocated slot bookkeeping; retain deterministic seeded mutation regressions.
2026-11-03 06:53:28 -07:00
trvon
4ade69ec7e
Learn: allowlisted binaries are negative labels, not teacher positives
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / macos (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
The teacher-label join made a window a positive if it was detected
(replay.cpp), but it never applied the allowlist, so a window running a cleared
false positive (e.g. /usr/bin/ss) was labelled a teacher positive — poisoning
Stage 4 training. Negative labels were written to the stream but replay dropped
them.

- replay_store now collects negative labels into ReplayOptions.negative_labels
  (it used to ignore any non-positive label).
- A window whose exec hash is negatively labelled is marked `suppressed`; the
  example/policy-eval positive is gated on `!suppressed`, so such a window is
  kept as a *negative* training example (useful signal) and never a positive,
  even if a cleared rule "detected" it.
- DetectionPipeline seeds a negative label for every allowlisted sha256 at init,
  so evidence stored before the allowlist (the historical ss storm) is covered
  without waiting for a re-exec.

Test: a detected window with a stored negative label for its hash is a positive
before the label and a kept-negative after. Suite 51/51; analyzer + clang-tidy
clean on replay.cpp and detection_pipeline.cpp.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-28 22:02:56 -06:00
trvon
bbfda149a5
Stage 4: execute a promoted model's inspections, under guardrails
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / macos (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
A promoted model (promoted.sbm, written only by `model promote` after the
gate) can now request real inspections. Only two, both additive:

- memory_scan: through the daemon's memory-scan queue;
- deep_rescan: the root's image re-scanned on demand; new matches are
  recorded as indicators of kind model_inspection (findings titled "YARA
  match found by a deeper rescan the inspection model requested") and
  positive labels.

watch_subtree has no mechanism and rescan_on_next_rules is the retro-hunt's
job, so both are masked out of acting decisions (kExecutableActions), and
replay's policy evaluation uses the same action set.

Guardrails:
- InspectionExecutor (bounded queue, one low-priority worker, submit()
  never blocks); WorkClass::model_inspect lets the governor defer it, and
  a deferred decision is dropped.
- Each action rechecks that the promoted model is still active at the
  decision's generation (demoting or replacing it revokes queued work),
  that the root's node id and raw image path still match (decisions carry
  root_image, not the feature template), and skips protected processes.
  A deep rescan discards its result if the process, image or rules changed
  during the scan.
- An acting decision is durably written before it is dispatched; every
  attempt becomes an inspection_outcome record (queued, scanned, skipped,
  deferred, revoked, queue_full, failed).
- Source invariant R10: no file may couple learning types to the
  exec-guard, OpenSnitch or policy APIs (daemon_app.cpp and main.cpp, the
  wiring points, are reviewed by hand). Models never block, contain, kill
  or create findings directly.
- Config model_act and model_lambda; status model block reports acting
  stats; timings.model_inspection.

docs/evaluation/stage4-execution-gate.md amends the gate's method (not its
thresholds): executable action set, greedy promoted decisions, budgets as
estimated inspection seconds by scan profile.

Also: TerminalSafeBuf's destructor no longer lets an exception escape;
the health message clamps huge durations; replay timestamp overflow; the
Docker test build runs with -j2.

Tests: test_inspection_executor (every skip and execute path, revocation,
queue limits); shadow and replay acting cases; R10 fixture; fault injection
and invariant updates. Validation reported by the session that wrote it:
48/48 local, 46/46 Docker plus privileged live checks, 20 fuzz targets
(9.5M runs), three paired KPI runs without regressions (no acting model
was loaded, so active-inspection cost is not measured yet).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 21:03:57 -06:00
trvon
b6d667b797
Stage 4: evaluate inspection policies in replay; gated promotion
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / macos (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
- replay --policy-eval: over ranked windows, simulates the model and
  rarity-greedy (the same chooser, rarity score / 48 as the value), each
  with its own hourly budget, and tallies the shadow decisions the daemon
  logged. Hits are inspected "extra" positives: late labels or injected
  evaluation traces on windows rules did not flag at exec time. Labels are
  known after the fact, so every action's payoff is computable without
  propensity weighting. Reports CPU-seconds, inspections, hits, recall,
  hits per CPU-second and budget refusals.
- docs/evaluation/stage4-promotion-gate.md (pre-registered): at least 10
  extra positives, model efficiency >= 1.5x rarity-greedy, model recall
  not lower. promotion_gate() checks it.
- strawberry model promote [--inject FILE] [--warmup H] [--yes]: replays
  with the shadow model, applies the gate and only then writes
  promoted.sbm; asks for confirmation.
- learn/inspection.hpp: options, the greedy chooser and the
  inspection_action record, shared by shadow mode and the evaluation.
- Fix: the hourly budget reset whenever a window closed with an earlier
  timestamp than the hour's start (windows close roughly, not strictly,
  in time order), letting spend exceed the budget; in both shadow mode
  and the evaluation.

Tests: test_replay R11 (gate criteria; extra positives exclude rule-flagged
windows; spend within the budget; logged shadow decisions joined to their
windows); CLI translations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 12:41:20 -06:00
trvon
6a3efbc22a
Stage 4: train the inspection model offline from replay
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / macos (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
- Replay builds each window's WindowSummary as it goes (exec depth and
  locations, connections, writes, ptrace, module loads, scan outcomes,
  surprisal) and can collect training examples: features, a label (a
  detection or late label touched the window), time, root program and the
  rarity score. Sampled to at most 50k: positives up to a fifth,
  negatives by reservoir with at most 1% per root program.
  --inject-train traces become positive examples and are not evaluated.
- learn/train: the value head (and hidden layers) learn P(teacher
  positive) with a class-weighted log loss and Adam; positive-unlabeled
  data, so unlabeled is "not known bad", documented. Split by time
  (newest 20% tested); reports test loss, top-k hits and ROC AUC for the
  model and for the rarity baseline. Refuses with fewer than 5 positives.
  Deterministic for a seed. The policy head waits for shadow-mode
  inspection outcomes.
- CLI: `strawberry model train [store_dir] [--inject-train FILE]
  [--epochs N] [--seed N] [--out FILE] [--json]` writes the shadow model
  (generation after the newest existing one, a note saying what it was
  trained on); `strawberry model show [file]`; `strawberry replay --model
  FILE` ranks windows (and evaluates injections) by the model's value.
  Config model_dir (default /var/lib/strawberry/model).
- doctor: the model check loads promoted.sbm and shadow.sbm; an invalid
  file is a problem (it is never used).

Tests: test_train (gradients against finite differences, skipping steps
that cross a ReLU kink; a planted pattern learned to high AUC; determinism;
refusal without labels; roc_auc); test_replay R10 (examples, labels,
per-program cap, training traces, model ranking); CLI and doctor cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 11:26:12 -06:00
trvon
db81372e19
Portability: 128-bit file_clock counts (libc++), split a hex-escape literal
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / macos (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
Clang rejects "\x80evil" (the escape takes the 'e'); GCC accepted it and
the test used a different string than written.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 10:08:21 -06:00
trvon
d54d987b3a
Retro-hunt: re-scan binaries seen before when the rules change
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
The main source of teacher labels over time: rules improve daily, while
most malware runs once.

- The pipeline remembers every binary that ran (image_seen evidence
  record: sha256, path, first/last seen, size; at most one write per hash
  a day). Deleted and memfd images are skipped: nothing to find again.
- Each rules load computes a digest of the rule files and IOC hash lists
  (paths after resolving symlinks, sizes, mtimes).
- RetroHunter (src/retro_hunt.cpp, own thread, idle priority, paced at
  retro_hunt_pace_ms, deferred by the governor as WorkClass::retro_hunt)
  re-scans when the digest differs from the last completed pass, most
  recently seen first. Only paths that still hold the same content are
  scanned (O_NOFOLLOW, regular files, hash compared); others count as
  changed or missing. A completed pass is recorded (retro_hunt record) so
  a restart does not repeat it; an interrupted one starts over.
- New matches (rules not recorded before for that hash, not allowlisted)
  become indicators with scan_kind retro_hunt, a file/indicator graph
  link, a positive label and a notification. Correlation turns them into
  findings titled "YARA match on a binary that ran here (found after a
  rules update)". Nothing is blocked or contained from here.
- Replay reads positive labels from the evidence stream and marks windows
  that ran a binary labelled bad later (LATE in the CLI).
- Status: a retro_hunt block (API_CONTRACT.md); config: retro_hunt,
  retro_hunt_pace_ms.

Tests: test_retro_hunt (remembered once; passes only on a digest change
and survive a restart; a clean-at-exec binary matching a new rule becomes
an indicator, label, finding and notification; a replaced binary is
counted as changed, never scanned as the old one; a stop abandons a pass
unrecorded); test_replay R9 (late labels, from a real store).
fuzz_evidence_records covers the new records.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 08:59:38 -06:00
trvon
4e25666a70
Inject scripted attack traces into replay and score them
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
- src/learn/trace.cpp: a small trace format (exec, connect, write,
  ptrace, module with +offsets and process aliases) compiled to
  telemetry records with pids above any pid_max and node ids that name
  the trace and copy.
- --replay --inject FILE [--inject-copies N]: copies are spread evenly
  over the ranked history (a first pass reads the time range), scored
  but never learned from, and checked against alert budgets of 1, 3 and
  10 per day: is the copy's best window above the (budget x days)-th best
  real window? Ties count against. Injection leaves real windows and
  their ranking unchanged.
- config/traces/standard.trace: the six plan traces (browser to curl|sh,
  systemd user persistence, memfd loader, ptrace injection, download
  then exec, python from a PDF viewer).
- docs/evaluation/stage3-rarity-preregistration.md: method and pass
  thresholds, written before any run on host data.

Tests: test_trace (standard traces, error messages with line numbers,
records); test_replay R8 (every copy found, real ranking unchanged, a
novel chain caught at 10/day and a bare shell not). fuzz_trace_dsl.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 22:37:44 -06:00
trvon
5fa2031c62
Replay: reorder over 2 minutes, not 30 s
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
On the host, 586 of 303k records arrived more than 30 s late: exec records
of large images are written after their paced, deferrable scan. Replay is
offline, so holding 2 minutes of records costs nothing that matters.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 22:27:00 -06:00
trvon
6fa5e68405
Make replay rankings fair over time and readable
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
From the first replay of real host data (3.5 h, 266k records):

- A never-seen event scored higher the more the model had seen, so the
  ranking favoured whatever happened last (the reinstall's systemctl
  and generator runs). Each event is now capped at 16 bits (never
  seen), and a window scores the sum of its three most surprising
  distinct events.
- Warm-up: on a fresh history nearly everything is new. Windows in the
  first 12 h are learned from but not ranked (--warmup H); detections
  are always listed. The CLI says so when nothing is ranked yet.
- Windows with the same root program and top pattern are one line with
  a count ("x40 similar").
- Counts: file writes split by persistence; events from processes
  started before recording are reported as such, not as an error;
  records more than 30 s out of order are counted as late.

tests/test_replay.cpp R7: warm-up, equal scores for a novel event early
and late, grouping, late-record counting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 22:10:51 -06:00
trvon
92cb4e9ba8
Add --replay, and keep non-UTF-8 host strings from breaking JSON
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
Replay (src/learn/replay.cpp, strawberry --replay [dir] [--top N] [--json]):
reads the telemetry stream read-only, orders events by time (a 30 s
reorder buffer absorbs the writer's batching), joins behavior events to
the latest exec of their pid at that time (keeps PID reuse apart), builds
60 s windows (a root exec, its descendants and their behavior, at most 64
events), scores each exec, connection and persistence write with the
rarity model before observing it, and marks windows whose processes had
detections. Prints the most unusual windows with the tuples that made
them unusual. Deterministic. tests/test_replay.cpp: chain windows, PID
reuse, order insensitivity within the horizon, determinism, ranking,
detection marking, a real store round trip. fuzz_replay feeds arbitrary
record streams.

UTF-8: file names are bytes, and the fuzzer showed nlohmann's dump()
throwing on a path that is not UTF-8. The same applied to the daemon's
control replies (kill/suspend/quarantine name the exe path) and to the
OpenSnitch containment rule, so a binary with such a name could not be
contained, and json_escape wrote the raw bytes into status JSON. Now:
- every .dump() in src/ uses error_handler_t::replace (U+FFFD), enforced
  by source invariant R9;
- json_escape writes invalid sequences as � (valid_utf8 and
  utf8_sequence_length in json_util.hpp);
- containment refuses such a path with a clear error (a rule could never
  match the real bytes); quarantine still blocks execution by hash.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 21:49:04 -06:00