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>
182 lines
6.9 KiB
C++
182 lines
6.9 KiB
C++
#include "strawberry/inspection_executor.hpp"
|
|
#include <vector>
|
|
|
|
#include <chrono>
|
|
#include <condition_variable>
|
|
#include <mutex>
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <random>
|
|
#include <stdexcept>
|
|
|
|
using namespace strawberry;
|
|
using namespace strawberry::learn;
|
|
|
|
namespace {
|
|
const uint64_t seed = [] {
|
|
const char* value = std::getenv("STRAWBERRY_PROPERTY_SEED");
|
|
return value ? std::strtoull(value, nullptr, 10) : uint64_t(std::random_device{}());
|
|
}();
|
|
#define CHECK(x) do { if (!(x)) { std::cerr << "failed: " #x << " line " << __LINE__ << " seed " << seed << '\n'; std::abort(); } } while (false)
|
|
|
|
void safety_properties() {
|
|
ProcessRef current{42, "process_42_abcd1234_123", "/tmp/test-image"};
|
|
CHECK(pid_from_node_id(current.node_id) == 42);
|
|
for (const auto* invalid : {"process_42", "process_42_boot_123", "process_1_abcd1234_123",
|
|
"process_42_abcd1234_0", "process_42_abcd1234_123_extra"}) {
|
|
CHECK(pid_from_node_id(invalid) == -1);
|
|
}
|
|
int scans = 0;
|
|
bool protected_process = false, admitted = true;
|
|
InspectionHooks hooks;
|
|
hooks.resolve = [&](int) { return std::optional(current); };
|
|
hooks.is_protected = [&](const ProcessRef&) { return protected_process; };
|
|
hooks.admit = [&] { return admitted; };
|
|
bool authorized = true;
|
|
hooks.authorized = [&](const InspectionDecision&) { return authorized; };
|
|
hooks.queue_memory_scan = [&](const ProcessRef& process, const InspectionDecision& decision) {
|
|
CHECK(process.node_id == decision.root_node);
|
|
++scans;
|
|
return true;
|
|
};
|
|
hooks.deep_rescan = [&](const ProcessRef&, const InspectionDecision&, std::string&) {
|
|
++scans;
|
|
return std::optional<uint32_t>(2);
|
|
};
|
|
InspectionDecision d;
|
|
d.shadow = false;
|
|
d.root_node = current.node_id;
|
|
d.root_exe = current.exe;
|
|
d.action = InspectAction::deep_rescan;
|
|
InspectionExecutor executor(hooks);
|
|
CHECK(executor.execute(d).status == "scanned" && scans == 1);
|
|
d.shadow = true;
|
|
CHECK(executor.execute(d).status == "unsupported" && scans == 1);
|
|
d.shadow = false;
|
|
authorized = false;
|
|
CHECK(executor.execute(d).status == "revoked" && scans == 1);
|
|
authorized = true;
|
|
protected_process = true;
|
|
CHECK(executor.execute(d).status == "skipped_protected" && scans == 1);
|
|
protected_process = false;
|
|
admitted = false;
|
|
CHECK(executor.execute(d).status == "deferred" && scans == 1);
|
|
admitted = true;
|
|
current.node_id = "process_42_abcd1234_124";
|
|
CHECK(executor.execute(d).status == "skipped_exited" && scans == 1);
|
|
current.node_id = d.root_node;
|
|
current.exe = "/tmp/replaced-image";
|
|
CHECK(executor.execute(d).status == "skipped_exited" && scans == 1);
|
|
current.exe = d.root_exe;
|
|
|
|
// Missing safety checks must fail closed, not silently scan.
|
|
auto incomplete = hooks;
|
|
incomplete.is_protected = {};
|
|
CHECK(InspectionExecutor(incomplete).execute(d).status == "failed" && scans == 1);
|
|
incomplete = hooks;
|
|
incomplete.admit = {};
|
|
CHECK(InspectionExecutor(incomplete).execute(d).status == "failed" && scans == 1);
|
|
|
|
std::mt19937_64 rng(seed);
|
|
for (int i = 0; i < 1000; ++i) {
|
|
d.action = static_cast<InspectAction>(5 + rng() % 251);
|
|
CHECK(executor.execute(d).status == "unsupported" && scans == 1);
|
|
}
|
|
d.action = InspectAction::memory_scan;
|
|
CHECK(executor.execute(d).status == "queued" && scans == 2);
|
|
hooks.resolve = [](int) -> std::optional<ProcessRef> { throw std::runtime_error("injected"); };
|
|
CHECK(InspectionExecutor(hooks).execute(d).status == "failed" && scans == 2);
|
|
hooks.resolve = [&](int) { return std::optional(current); };
|
|
hooks.deep_rescan = [](const ProcessRef&, const InspectionDecision&, std::string&) -> std::optional<uint32_t> {
|
|
throw 42;
|
|
};
|
|
d.action = InspectAction::deep_rescan;
|
|
CHECK(InspectionExecutor(hooks).execute(d).status == "failed" && scans == 2);
|
|
}
|
|
|
|
void queue_properties() {
|
|
std::mutex mutex;
|
|
std::condition_variable cv;
|
|
bool entered = false, release = false;
|
|
std::vector<std::string> outcomes;
|
|
InspectionHooks hooks;
|
|
hooks.resolve = [&](int) -> std::optional<ProcessRef> {
|
|
std::unique_lock lock(mutex);
|
|
entered = true;
|
|
cv.notify_all();
|
|
CHECK(cv.wait_for(lock, std::chrono::seconds(5), [&] { return release; }));
|
|
return ProcessRef{42, "process_42_abcd1234_123", "/tmp/image"};
|
|
};
|
|
hooks.is_protected = [](const ProcessRef&) { return false; };
|
|
hooks.admit = [] { return true; };
|
|
hooks.authorized = [](const InspectionDecision&) { return true; };
|
|
hooks.deep_rescan = [](const ProcessRef&, const InspectionDecision&, std::string&) {
|
|
return std::optional<uint32_t>(0);
|
|
};
|
|
hooks.emit = [&](const InspectionOutcome& o) {
|
|
std::lock_guard lock(mutex);
|
|
outcomes.push_back(o.status);
|
|
cv.notify_all();
|
|
};
|
|
InspectionDecision d;
|
|
d.shadow = false;
|
|
d.action = InspectAction::deep_rescan;
|
|
d.root_node = "process_42_abcd1234_123";
|
|
d.root_exe = "/tmp/image";
|
|
InspectionExecutor executor(hooks, 1);
|
|
executor.start();
|
|
executor.submit(d);
|
|
{
|
|
std::unique_lock lock(mutex);
|
|
CHECK(cv.wait_for(lock, std::chrono::seconds(5), [&] { return entered; }));
|
|
}
|
|
executor.submit(d);
|
|
executor.submit(d);
|
|
{
|
|
std::lock_guard lock(mutex);
|
|
CHECK(outcomes.size() == 1 && outcomes[0] == "queue_full");
|
|
release = true;
|
|
cv.notify_all();
|
|
}
|
|
{
|
|
std::unique_lock lock(mutex);
|
|
CHECK(cv.wait_for(lock, std::chrono::seconds(5), [&] { return outcomes.size() == 3; }));
|
|
}
|
|
executor.stop();
|
|
executor.submit(d);
|
|
CHECK(outcomes.size() == 4 && outcomes.back() == "stopped");
|
|
CHECK(executor.stats().deep_rescans == 2);
|
|
}
|
|
|
|
void codec_properties() {
|
|
std::mt19937_64 rng(seed);
|
|
for (int i = 0; i < 1000; ++i) {
|
|
InspectionOutcome in;
|
|
in.timestamp_ns = static_cast<int64_t>(rng() & 0x7fffffffffffffffULL);
|
|
in.root_node = "process_42_abcd1234_123";
|
|
in.action = static_cast<InspectAction>(rng() % kActions);
|
|
in.status = "scanned";
|
|
in.detail = std::string("host\0\xff", 6);
|
|
in.matches = static_cast<uint32_t>(rng());
|
|
in.duration_us = rng();
|
|
in.model_generation = rng();
|
|
InspectionOutcome out;
|
|
const auto encoded = encode_inspection_outcome(in);
|
|
CHECK(decode_inspection_outcome(encoded, out));
|
|
CHECK(encode_inspection_outcome(out) == encoded);
|
|
}
|
|
InspectionOutcome sentinel;
|
|
sentinel.status = "unchanged";
|
|
CHECK(!decode_inspection_outcome(std::string("\x18\xff\x01", 3), sentinel));
|
|
CHECK(sentinel.status == "unchanged");
|
|
CHECK(!decode_inspection_outcome(std::string("\x22\x05x", 3), sentinel));
|
|
CHECK(sentinel.status == "unchanged");
|
|
}
|
|
} // namespace
|
|
|
|
int main() {
|
|
safety_properties();
|
|
codec_properties();
|
|
queue_properties();
|
|
std::cout << "inspection executor properties passed, seed " << seed << '\n';
|
|
}
|