#include "strawberry/database.hpp" #include "strawberry/detection_pipeline.hpp" #include "strawberry/exec_guard.hpp" #include #include "strawberry/store/log_store.hpp" #include "strawberry/node_ids.hpp" #include "strawberry/telemetry.hpp" #include "strawberry/resource_governor.hpp" #include "strawberry/hash_utils.hpp" #include "strawberry/policy.hpp" #include "strawberry/process_monitor.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(_WIN32) #include #endif #ifndef STRAWBERRY_HAVE_YARA #define STRAWBERRY_HAVE_YARA 0 #endif namespace fs = std::filesystem; // One-shot, current-thread-only allocation failure for shutdown. No real memory // exhaustion and no production callback contract is changed. Matching replacement // allocation/deallocation forms keep this test binary compatible with ASan. namespace { thread_local bool fail_next_allocation = false; [[gnu::noinline]] void* test_allocate(std::size_t size) { if (fail_next_allocation) { fail_next_allocation = false; throw std::bad_alloc(); } if (void* ptr = std::malloc(size == 0 ? 1 : size)) return ptr; throw std::bad_alloc(); } } [[gnu::noinline]] void* operator new(std::size_t size) { return test_allocate(size); } [[gnu::noinline]] void* operator new[](std::size_t size) { return test_allocate(size); } [[gnu::noinline]] void* operator new(std::size_t size, const std::nothrow_t&) noexcept { try { return test_allocate(size); } catch (...) { return nullptr; } } [[gnu::noinline]] void* operator new[](std::size_t size, const std::nothrow_t&) noexcept { try { return test_allocate(size); } catch (...) { return nullptr; } } [[gnu::noinline]] void operator delete(void* ptr) noexcept { std::free(ptr); } [[gnu::noinline]] void operator delete[](void* ptr) noexcept { std::free(ptr); } [[gnu::noinline]] void operator delete(void* ptr, std::size_t) noexcept { std::free(ptr); } [[gnu::noinline]] void operator delete[](void* ptr, std::size_t) noexcept { std::free(ptr); } [[gnu::noinline]] void operator delete(void* ptr, const std::nothrow_t&) noexcept { std::free(ptr); } [[gnu::noinline]] void operator delete[](void* ptr, const std::nothrow_t&) noexcept { std::free(ptr); } namespace { #if STRAWBERRY_HAVE_YARA fs::path make_test_root() { const auto root = fs::temp_directory_path() / "strawberry_runtime_pipeline_tests"; std::error_code ec; fs::remove_all(root, ec); fs::create_directories(root, ec); assert(!ec); return root; } void write_binary_file(const fs::path& path, const std::string& content) { if (path.has_parent_path()) { std::error_code ec; fs::create_directories(path.parent_path(), ec); assert(!ec); } std::ofstream output(path, std::ios::binary); assert(output.good()); output.write(content.data(), static_cast(content.size())); output.close(); assert(output.good()); } void write_cmdline_file(const fs::path& path, const std::vector& args) { if (path.has_parent_path()) { std::error_code ec; fs::create_directories(path.parent_path(), ec); assert(!ec); } std::ofstream output(path, std::ios::binary); assert(output.good()); for (const auto& arg : args) { output.write(arg.data(), static_cast(arg.size())); output.put('\0'); } output.close(); assert(output.good()); } void write_status_file(const fs::path& path, int parent_pid) { if (path.has_parent_path()) { std::error_code ec; fs::create_directories(path.parent_path(), ec); assert(!ec); } std::ofstream output(path); assert(output.good()); output << "Name:\tsynthetic-runtime\n"; output << "State:\tR (running)\n"; output << "PPid:\t" << parent_pid << "\n"; output.close(); assert(output.good()); } void write_rule_file(const fs::path& path) { if (path.has_parent_path()) { std::error_code ec; fs::create_directories(path.parent_path(), ec); assert(!ec); } std::ofstream output(path); assert(output.good()); output << "rule SyntheticRuntimeRule {\n" " meta:\n" " severity = \"High\"\n" " strings:\n" " $marker = \"synthetic-payload-marker\"\n" " condition:\n" " $marker\n" "}\n"; output.close(); assert(output.good()); } void set_env_var(const std::string& key, const std::string& value) { #if defined(_WIN32) _putenv_s(key.c_str(), value.c_str()); #else ::setenv(key.c_str(), value.c_str(), 1); #endif } void unset_env_var(const std::string& key) { #if defined(_WIN32) _putenv_s(key.c_str(), ""); #else ::unsetenv(key.c_str()); #endif } void test_procfs_event_scans_and_persists_match() { const auto root = make_test_root(); const auto proc_root = root / "proc"; const auto rules_dir = root / "rules"; const auto executable = root / "bin" / "synthetic_exec"; const auto database_path = (root / "runtime_pipeline.db").string(); std::error_code ec; fs::create_directories(proc_root, ec); assert(!ec); fs::create_directories(rules_dir, ec); assert(!ec); write_binary_file(executable, std::string("\x7f\x45\x4c\x46") + "synthetic-payload-marker"); write_rule_file(rules_dir / "synthetic_runtime.yar"); const auto expected_hash = strawberry::HashUtils::sha256_file(executable.string()); assert(!expected_hash.empty()); set_env_var("STRAWBERRY_PROCFS_ROOT", proc_root.string()); set_env_var("STRAWBERRY_PROCESS_MONITOR_POLL_MS", "25"); strawberry::Config config; config.db_path = database_path; config.yara_rules_path = rules_dir.string(); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); strawberry::ProcessMonitor monitor; assert(monitor.initialize()); std::mutex mutex; std::condition_variable cv; std::vector observed_events; std::cout << "Test procfs_event_scans_and_persists_match:" << std::endl; assert(monitor.start([&](const strawberry::ProcessInfo& info) { pipeline.handle_process(info); { std::lock_guard lock(mutex); observed_events.push_back(info); } cv.notify_all(); })); const auto staged_proc_dir = proc_root / "pending-321"; fs::create_directories(staged_proc_dir, ec); assert(!ec); fs::create_symlink(executable, staged_proc_dir / "exe", ec); assert(!ec); write_cmdline_file(staged_proc_dir / "cmdline", {"synthetic_exec", "--runtime-test"}); write_status_file(staged_proc_dir / "status", 77); const auto live_proc_dir = proc_root / "321"; fs::rename(staged_proc_dir, live_proc_dir, ec); assert(!ec); { std::unique_lock lock(mutex); const bool observed = cv.wait_for(lock, std::chrono::seconds(2), [&]() { return !observed_events.empty(); }); assert(observed); } monitor.stop(); pipeline.shutdown(); strawberry::Database database; assert(database.open(database_path)); auto hits = database.find_indicators_by_hash(expected_hash); assert(hits.size() == 1); assert(hits[0].rule_name == "SyntheticRuntimeRule"); assert(hits[0].severity == "high"); assert(hits[0].metadata.at("source") == "yara"); assert(hits[0].metadata.at("scan_kind") == "process_executable"); assert(hits[0].metadata.at("pid") == "321"); assert(hits[0].metadata.at("parent_pid") == "77"); assert(hits[0].metadata.at("process_path") == executable.string()); assert(hits[0].metadata.at("cmdline") == "synthetic_exec --runtime-test"); assert(!hits[0].metadata.at("event_timestamp_ns").empty()); auto findings = database.list_findings(); assert(findings.size() == 1); assert(findings[0].title == "YARA match on executed process image"); assert(findings[0].severity == "high"); assert(findings[0].metadata.at("rule") == "process_executable_yara_match"); assert(findings[0].metadata.at("pid") == "321"); assert(findings[0].summary.find("SyntheticRuntimeRule") != std::string::npos); const std::string file_node_id = "file_" + expected_hash; auto related = database.find_related_nodes("process_321", 1); assert(related.size() == 2); bool saw_file = false; bool saw_indicator = false; for (const auto& node : related) { if (node.id == file_node_id) { saw_file = true; assert(node.type == "file"); assert(node.properties.at("path") == executable.string()); assert(node.properties.at("hash") == expected_hash); assert(node.properties.at("kind") == "executable_image"); } else if (node.type == "indicator") { saw_indicator = true; assert(node.properties.at("rule_name") == "SyntheticRuntimeRule"); assert(node.properties.at("cmdline") == "synthetic_exec --runtime-test"); assert(node.properties.at("parent_pid") == "77"); } } assert(saw_file); assert(saw_indicator); auto edges = database.get_node_edges("process_321"); assert(edges.size() == 3); bool saw_spawned = false; bool saw_executed = false; bool saw_matched = false; for (const auto& edge : edges) { if (edge.relationship_type == "spawned") { saw_spawned = true; } else if (edge.relationship_type == "executed") { saw_executed = true; assert(edge.to_id == file_node_id); } else if (edge.relationship_type == "matched") { saw_matched = true; } } assert(saw_spawned); assert(saw_executed); assert(saw_matched); auto parent_related = database.find_related_nodes("process_77", 1); assert(parent_related.size() == 1); assert(parent_related[0].id == "process_321"); auto file_related = database.find_related_nodes(file_node_id, 1); assert(file_related.size() == 1); assert(file_related[0].type == "indicator"); assert(file_related[0].properties.at("rule_name") == "SyntheticRuntimeRule"); auto file_edges = database.get_node_edges(file_node_id); assert(file_edges.size() == 3); // executed, content_matched, same_path bool saw_content_matched = false; bool saw_same_path = false; for (const auto& edge : file_edges) { if (edge.relationship_type == "content_matched") { saw_content_matched = true; } else if (edge.relationship_type == strawberry::kSamePathRelationship) { // The path id (used by path-keyed evidence) points at this content id. saw_same_path = edge.to_id == file_node_id && edge.from_id.rfind("file_path_", 0) == 0; } } assert(saw_content_matched); assert(saw_same_path); database.close(); std::cout << " PASSED" << std::endl; unset_env_var("STRAWBERRY_PROCFS_ROOT"); unset_env_var("STRAWBERRY_PROCESS_MONITOR_POLL_MS"); fs::remove_all(root, ec); } void write_text_file(const fs::path& path, const std::string& content) { write_binary_file(path, content); } void test_fails_closed_without_rules() { const auto root = make_test_root(); const auto empty_rules = root / "empty_rules"; std::error_code ec; fs::create_directories(empty_rules, ec); std::cout << "Test fails_closed_without_rules:" << std::endl; strawberry::Config config; config.db_path = (root / "no_rules.db").string(); config.yara_rules_path = empty_rules.string(); strawberry::DetectionPipeline strict; assert(!strict.initialize(config)); config.require_rules = false; strawberry::DetectionPipeline permissive; assert(permissive.initialize(config)); permissive.shutdown(); std::cout << " PASSED" << std::endl; fs::remove_all(root, ec); } void test_single_writer_lock_and_read_only_queries() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; write_rule_file(rules_dir / "synthetic_runtime.yar"); const auto db_path = (root / "locked.db").string(); std::cout << "Test single_writer_lock_and_read_only_queries:" << std::endl; strawberry::Config config; config.db_path = db_path; config.yara_rules_path = rules_dir.string(); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); // A second writer must be refused while the pipeline owns the store. strawberry::Database second_writer; assert(!second_writer.open(db_path)); strawberry::DetectionPipeline second_pipeline; assert(!second_pipeline.initialize(config)); // Readers are allowed and never write back. strawberry::Database reader; assert(reader.open_read_only(db_path)); assert(reader.is_read_only()); reader.close(); pipeline.shutdown(); assert(second_writer.open(db_path)); second_writer.close(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } void test_stable_ids_severity_default_and_heuristics() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto executable = root / "bin" / "reused_pid_exec"; const auto db_path = (root / "identity.db").string(); write_binary_file(executable, std::string("\x7f\x45\x4c\x46") + "no-severity-marker"); write_text_file(rules_dir / "no_severity.yar", "rule NoSeverityRule {\n" " strings:\n" " $m = \"no-severity-marker\"\n" " condition:\n" " $m\n" "}\n"); const auto hash = strawberry::HashUtils::sha256_file(executable.string()); std::cout << "Test stable_ids_severity_default_and_heuristics:" << std::endl; strawberry::Config config; config.db_path = db_path; config.yara_rules_path = rules_dir.string(); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); // Two different process instances that happen to share PID 500. strawberry::ProcessInfo first; first.pid = 500; first.parent_pid = 1; first.executable_path = executable.string(); first.image_path = executable.string(); first.hash = hash; first.cmdline = "reused_pid_exec --first"; first.start_time_ticks = 1000; first.parent_start_time_ticks = 1; first.boot_id = "feedfacecafebeef"; first.event_timestamp_ns = 1'000; auto second = first; second.cmdline = "reused_pid_exec --second"; second.start_time_ticks = 9000; second.event_timestamp_ns = 2'000; second.executable_deleted = true; pipeline.handle_process(first); pipeline.handle_process(second); assert(pipeline.run_pending_correlation()); assert(!pipeline.run_pending_correlation()); pipeline.shutdown(); strawberry::Database database; assert(database.open_read_only(db_path)); const auto first_id = strawberry::process_node_id(first); const auto second_id = strawberry::process_node_id(second); assert(first_id == "process_500_feedface_1000"); assert(second_id == "process_500_feedface_9000"); auto first_node = database.find_graph_node(first_id); auto second_node = database.find_graph_node(second_id); assert(first_node && second_node); assert(first_node->properties.at("cmdline") == "reused_pid_exec --first"); assert(second_node->properties.at("cmdline") == "reused_pid_exec --second"); assert(second_node->properties.at("exe_deleted") == "true"); assert(second_node->properties.at("parent_node_id") == "process_1_feedface_1"); bool saw_yara = false; bool saw_deleted = false; for (const auto& hit : database.find_indicators_by_hash(hash)) { if (hit.rule_name == "NoSeverityRule") { saw_yara = true; assert(hit.severity == "medium"); } else if (hit.rule_name == "DeletedExecutable") { saw_deleted = true; assert(hit.severity == "low"); assert(hit.metadata.at("source") == "heuristic"); assert(hit.metadata.at("process_node_id") == second_id); } } assert(saw_yara); assert(saw_deleted); // Same hash in two process instances (same PID) is still two processes. bool saw_same_hash = false; std::size_t per_process_findings = 0; for (const auto& finding : database.list_findings()) { if (finding.metadata.at("rule") == "same_hash_multiple_process_matches") { saw_same_hash = true; } if (finding.metadata.at("rule") == "process_executable_yara_match") { ++per_process_findings; } } assert(saw_same_hash); assert(per_process_findings == 2); database.close(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } std::string identity_of(const fs::path& path) { struct stat st{}; assert(::stat(path.c_str(), &st) == 0); return std::to_string(st.st_dev) + ":" + std::to_string(st.st_ino); } void test_scans_only_the_verified_image() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto malicious = root / "bin" / "malicious"; const auto clean = root / "bin" / "clean"; const auto db_path = (root / "verified.db").string(); write_rule_file(rules_dir / "synthetic_runtime.yar"); write_binary_file(malicious, std::string("\x7f\x45\x4c\x46") + "synthetic-payload-marker"); write_binary_file(clean, std::string("\x7f\x45\x4c\x46") + "nothing to see"); std::cout << "Test scans_only_the_verified_image:" << std::endl; strawberry::Config config; config.db_path = db_path; config.yara_rules_path = rules_dir.string(); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); // Event resolved to the clean image, but by scan time the pinned path // points at a different inode (process re-exec'd): must not be scanned. strawberry::ProcessInfo swapped; swapped.pid = 700; swapped.executable_path = clean.string(); swapped.image_path = malicious.string(); swapped.image_identity = identity_of(clean); pipeline.handle_process(swapped); // Process already exited (pinned link gone): fall back to the on-disk // path because it is still the same inode. strawberry::ProcessInfo exited; exited.pid = 701; exited.executable_path = malicious.string(); exited.image_path = (root / "proc" / "701" / "exe").string(); exited.image_identity = identity_of(malicious); pipeline.handle_process(exited); // Duplicate event for the same process instance and image is ignored. pipeline.handle_process(exited); pipeline.shutdown(); strawberry::Database database; assert(database.open_read_only(db_path)); const auto indicators = database.list_indicators(); assert(indicators.size() == 1); assert(indicators[0].metadata.at("pid") == "701"); assert(indicators[0].rule_name == "SyntheticRuntimeRule"); assert(indicators[0].hash == strawberry::HashUtils::sha256_file(malicious.string())); database.close(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } void test_stats_and_rescan_after_rule_reload() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto executable = root / "bin" / "late_detected"; const auto db_path = (root / "stats.db").string(); // Start with a rule that does not match the binary. write_text_file(rules_dir / "active.yar", "rule Unrelated { strings: $a = \"nothing-matches-this\" condition: $a }\n"); write_binary_file(executable, std::string("\x7f\x45\x4c\x46") + "synthetic-payload-marker"); std::cout << "Test stats_and_rescan_after_rule_reload:" << std::endl; strawberry::Config config; config.db_path = db_path; config.yara_rules_path = rules_dir.string(); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); auto stats = pipeline.stats(); assert(stats.rules_loaded && stats.rules_generation == 1 && stats.rule_files == 1); assert(pipeline.database_path() == db_path); strawberry::ProcessInfo info; info.pid = 800; info.executable_path = executable.string(); info.image_path = executable.string(); info.image_identity = identity_of(executable); info.start_time_ticks = 42; pipeline.handle_process(info); pipeline.handle_process(info); // rescan with same rules: deduped stats = pipeline.stats(); assert(stats.events_handled == 2); assert(stats.images_scanned == 1); assert(stats.detections == 0); // A broken rule file: reload fails, previous rules stay active, error is reported. write_text_file(rules_dir / "active.yar", "rule Broken { condition: }\n"); assert(!pipeline.reload_rules()); stats = pipeline.stats(); assert(stats.rules_loaded && stats.rules_generation == 1); assert(!stats.last_rules_error.empty()); // Fixed rule that matches: rescan of the same process now records it once. write_rule_file(rules_dir / "active.yar"); assert(pipeline.reload_rules()); stats = pipeline.stats(); assert(stats.rules_generation == 2 && stats.last_rules_error.empty()); pipeline.handle_process(info); pipeline.handle_process(info); stats = pipeline.stats(); assert(stats.detections == 1); assert(stats.last_detection_ns > 0); pipeline.shutdown(); strawberry::Database database; assert(database.open_read_only(db_path)); assert(database.list_indicators().size() == 1); database.close(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } void test_scan_on_demand_is_read_only_and_bounded() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto sample = root / "sample.bin"; const auto db_path = (root / "ondemand.db").string(); write_rule_file(rules_dir / "synthetic_runtime.yar"); write_binary_file(sample, std::string("prefix synthetic-payload-marker suffix")); std::cout << "Test scan_on_demand_is_read_only_and_bounded:" << std::endl; strawberry::Config config; config.db_path = db_path; config.yara_rules_path = rules_dir.string(); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); int fd = ::open(sample.c_str(), O_RDONLY | O_CLOEXEC); assert(fd >= 0); auto result = pipeline.scan_on_demand(fd, 1024); assert(result.ok); assert(result.size == fs::file_size(sample)); assert(result.sha256 == strawberry::HashUtils::sha256_file(sample.string())); assert(result.matches.size() == 1); assert(result.matches[0].rule == "SyntheticRuntimeRule"); assert(result.matches[0].severity == "high"); // Size cap. result = pipeline.scan_on_demand(fd, 8); assert(!result.ok && result.error.find("too large") != std::string::npos); ::close(fd); // Non-regular file. fd = ::open(root.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC); assert(fd >= 0); result = pipeline.scan_on_demand(fd, 1024); assert(!result.ok && result.error == "not a regular file"); ::close(fd); assert(pipeline.stats().detections == 0); pipeline.shutdown(); strawberry::Database database; assert(database.open_read_only(db_path)); assert(database.list_indicators().empty()); database.close(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } void test_feed_sources_allowlist_and_size_cap() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto feeds_dir = root / "feeds"; const auto executable = root / "bin" / "dual_match"; const auto db_path = (root / "policy_pipeline.db").string(); write_rule_file(rules_dir / "synthetic_runtime.yar"); write_binary_file(executable, std::string("\x7f\x45\x4c\x46") + "synthetic-payload-marker feed-marker"); // One installed feed (feeds/f1/current -> v1) with its own rule. fs::create_directories(feeds_dir / "f1" / "v1"); write_text_file(feeds_dir / "f1" / "v1" / "feed.yar", "rule FeedRule { meta: severity = \"medium\" strings: $f = \"feed-marker\" condition: $f }\n"); fs::create_directory_symlink("v1", feeds_dir / "f1" / "current"); std::cout << "Test feed_sources_allowlist_and_size_cap:" << std::endl; strawberry::Config config; config.db_path = db_path; config.yara_rules_path = rules_dir.string(); config.policy_path = (root / "policy.json").string(); config.feeds_dir = feeds_dir.string(); strawberry::PolicyStore policy(config.policy_path); assert(policy.load()); std::string error; assert(policy.apply("feed-remove", R"({"id":"yara-forge-core"})", 0, &error)); assert(policy.apply("feed-add", R"({"id":"f1","type":"url","url":"https://example.com/feed.yar"})", 0, &error)); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config, &policy)); assert(pipeline.stats().rule_count == 2); // local + feed, separate namespaces const auto hash = strawberry::HashUtils::sha256_file(executable.string()); uint64_t start = 100; const auto event = [&]() { strawberry::ProcessInfo info; info.pid = 900; info.executable_path = executable.string(); info.image_path = executable.string(); info.image_identity = identity_of(executable); info.start_time_ticks = start++; return info; }; pipeline.handle_process(event()); assert(pipeline.stats().detections == 2); // Suppress the feed rule everywhere: only the local rule is stored. assert(policy.apply("allowlist-add", R"({"kind":"rule","value":"FeedRule"})", 1000, &error)); pipeline.handle_process(event()); assert(pipeline.stats().detections == 3); assert(pipeline.stats().suppressed == 1); // Allowlist the exact binary: nothing stored. assert(policy.apply("allowlist-add", R"({"kind":"sha256","value":")" + hash + R"("})", 1000, &error)); pipeline.handle_process(event()); assert(pipeline.stats().detections == 3); assert(pipeline.stats().suppressed == 3); pipeline.handle_process(event()); // suppressed again: counted, but no second label assert(pipeline.stats().suppressed == 5); // Behavior detections honor path and rule entries too: below high // severity they carry no image hash (they used to bypass the allowlist). strawberry::BehaviorDetection behavior; behavior.process = strawberry::ProcessRef{4242, "process_4242_x", "/usr/bin/install"}; behavior.rule = "PersistenceWrite"; behavior.severity = "medium"; behavior.description = "Wrote autostart/persistence location (systemd unit): /etc/systemd/system/x.service"; const auto suppressed_before = pipeline.stats().suppressed; pipeline.record_behavior(behavior); assert(pipeline.stats().suppressed == suppressed_before); // not allowlisted yet assert(policy.apply("allowlist-add", R"({"kind":"path","value":"/usr/bin/install","rule":"PersistenceWrite"})", 1000, &error)); pipeline.record_behavior(behavior); assert(pipeline.stats().suppressed == suppressed_before + 1); behavior.rule = "PtraceAttach"; // the entry is scoped to its rule pipeline.record_behavior(behavior); assert(pipeline.stats().suppressed == suppressed_before + 1); // Disabling the feed drops its rules on reload. assert(policy.apply("feed-set-enabled", R"({"id":"f1","enabled":false})", 1000, &error)); assert(pipeline.reload_rules()); assert(pipeline.stats().rule_count == 1); pipeline.shutdown(); // Suppressions are negative labels (one per entry, subject and rule), and // every rules load is a rules_generation record, in the evidence stream. std::vector labels; std::vector generations; strawberry::store::scan_store_read_only(db_path, strawberry::store::Stream::evidence, [&](uint32_t type, std::string_view payload) { if (type == static_cast(strawberry::store::RecordType::label)) { strawberry::LabelRecord label; assert(strawberry::decode_label(payload, label)); labels.push_back(label); } else if (type == static_cast(strawberry::store::RecordType::rules_generation)) { strawberry::RulesGenerationRecord generation; assert(strawberry::decode_rules_generation(payload, generation)); generations.push_back(generation); } }); // FeedRule by the rule entry (the sha256 entry's FeedRule hit matches the // rule entry first: same label, deduped); the local rule by the sha256 // entry; the behavior detection by the path entry. assert(labels.size() == 3); for (const auto& label : labels) { assert(label.polarity == strawberry::LabelPolarity::negative && label.source == "allowlist"); assert(label.action == "suppressed" && !label.allowlist_entry.empty() && !label.rule.empty()); } assert(labels[0].rule == "FeedRule" && labels[1].sha256 == hash && labels[1].rule != "FeedRule"); assert(labels[2].path == "/usr/bin/install" && labels[2].rule == "PersistenceWrite"); assert(generations.size() == 2); assert(generations[0].loaded && generations[0].generation == 1 && generations[0].rules == 2); assert(generations[0].sources.size() == 2 && generations[0].sources[1].rfind("f1=", 0) == 0); assert(generations[1].generation == 2 && generations[1].rules == 1); strawberry::Database database; assert(database.open_read_only(db_path)); std::size_t feed_hits = 0; for (const auto& indicator : database.list_indicators()) { feed_hits += indicator.rule_name == "FeedRule" ? 1 : 0; if (indicator.rule_name == "FeedRule") { assert(indicator.metadata.at("namespace") == "f1"); } } assert(feed_hits == 1); database.close(); // Size cap: images over process_scan_max_bytes are counted, not scanned. config.process_scan_max_bytes = 8; config.db_path = (root / "size_cap.db").string(); strawberry::DetectionPipeline capped; assert(capped.initialize(config)); capped.handle_process(event()); assert(capped.stats().images_too_large == 1); assert(capped.stats().images_scanned == 0); assert(capped.stats().detections == 0); capped.shutdown(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } void test_ioc_hash_detection_and_score_severity() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto feeds_dir = root / "feeds"; const auto known_bad = root / "bin" / "known_bad"; const auto scored = root / "bin" / "scored"; write_text_file(rules_dir / "scored.yar", "rule Scored_Rule { meta: score = 75 strings: $s = \"scored-marker\" condition: $s }\n"); write_binary_file(known_bad, std::string("\x7f\x45\x4c\x46") + "no rule matches this"); write_binary_file(scored, std::string("\x7f\x45\x4c\x46") + "scored-marker"); const auto bad_hash = strawberry::HashUtils::sha256_file(known_bad.string()); fs::create_directories(feeds_dir / "hash-feed" / "v1"); write_text_file(feeds_dir / "hash-feed" / "v1" / "list.txt", std::string(64, '0') + "\n" + bad_hash + "\n"); fs::create_directory_symlink("v1", feeds_dir / "hash-feed" / "current"); std::cout << "Test ioc_hash_detection_and_score_severity:" << std::endl; strawberry::Config config; config.db_path = (root / "ioc.db").string(); config.yara_rules_path = rules_dir.string(); config.policy_path = (root / "policy.json").string(); config.feeds_dir = feeds_dir.string(); strawberry::PolicyStore policy(config.policy_path); assert(policy.load()); std::string error; // A curated feed: max_severity "critical" makes its hits force prevention // and report as critical (the trusted path; the advisory default is covered // by test_ioc_community_feed_is_advisory). assert(policy.apply("feed-add", R"({"id":"hash-feed","kind":"hashes","type":"url","url":"https://example.com/h.txt","max_severity":"critical"})", 0, &error)); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config, &policy)); assert(pipeline.stats().ioc_hashes == 2); const auto event = [&](const fs::path& exe, uint64_t start) { strawberry::ProcessInfo info; info.pid = 950; info.executable_path = exe.string(); info.image_path = exe.string(); info.image_identity = identity_of(exe); info.start_time_ticks = start; return info; }; pipeline.handle_process(event(known_bad, 1)); pipeline.handle_process(event(scored, 2)); // On-demand scans report IOC hits too. const int fd = ::open(known_bad.c_str(), O_RDONLY | O_CLOEXEC); const auto on_demand = pipeline.scan_on_demand(fd, 1 << 20); ::close(fd); assert(on_demand.ok && on_demand.matches.size() == 1); assert(on_demand.matches[0].rule == "IOC_KnownMalwareHash" && on_demand.matches[0].severity == "critical"); assert(on_demand.matches[0].namespace_name == "hash-feed"); // Guard path: a critical (curated) feed forces a prevention hit. const int afd = ::open(known_bad.c_str(), O_RDONLY | O_CLOEXEC); const auto trusted = pipeline.assess_exec(afd, known_bad.string()); ::close(afd); assert(trusted.ioc_match && trusted.top_severity == "critical"); pipeline.shutdown(); strawberry::Database database; assert(database.open_read_only(config.db_path)); bool saw_ioc = false; bool saw_scored = false; for (const auto& indicator : database.list_indicators()) { if (indicator.rule_name == "IOC_KnownMalwareHash") { saw_ioc = true; assert(indicator.severity == "critical"); assert(indicator.metadata.at("source") == "ioc"); assert(indicator.hash == bad_hash); } else if (indicator.rule_name == "Scored_Rule") { saw_scored = true; assert(indicator.severity == "high"); // score 75 -> high } } assert(saw_ioc && saw_scored); bool saw_title = false; for (const auto& finding : database.list_findings()) { saw_title |= finding.title.find("Known malware executed") != std::string::npos && finding.severity == "critical"; } assert(saw_title); database.close(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } void test_governed_shared_cache_degrade_and_caps() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto executable = root / "bin" / "guarded"; const auto other = root / "bin" / "guarded_later"; write_rule_file(rules_dir / "synthetic_runtime.yar"); write_binary_file(executable, std::string("\x7f\x45\x4c\x46") + "synthetic-payload-marker"); write_binary_file(other, std::string("\x7f\x45\x4c\x46") + "synthetic-payload-marker-2"); std::cout << "Test governed_shared_cache_degrade_and_caps:" << std::endl; strawberry::ResourceSample sample; sample.host_source = "psi"; sample.cpu_some_pct = 1; sample.own_cpu_pct = 1; sample.rss_bytes = 10ull << 20; strawberry::GovernorOptions options; options.profile = *strawberry::scan_profile_named("balanced"); options.sampler = [&sample]() { return sample; }; options.sleeper = [](std::chrono::milliseconds) {}; strawberry::ResourceGovernor governor(options); governor.tick(); strawberry::Config config; config.db_path = (root / "governed.db").string(); config.yara_rules_path = rules_dir.string(); strawberry::DetectionPipeline pipeline; pipeline.set_governor(&governor); assert(pipeline.initialize(config)); // The exec guard scans first; its result serves the pipeline too. int fd = ::open(executable.c_str(), O_RDONLY | O_CLOEXEC); assert(fd >= 0); auto assessment = pipeline.assess_exec(fd, executable.string()); ::close(fd); assert(assessment.scanned && !assessment.degraded); assert(assessment.top_rule == "SyntheticRuntimeRule"); assert(pipeline.perf().yara_scan_us.summary().count == 1); strawberry::ProcessInfo info; info.pid = 900; info.executable_path = executable.string(); info.image_path = executable.string(); info.image_identity = identity_of(executable); info.start_time_ticks = 7; // Small, already cached image: cheap, handled on the live path. assert(pipeline.slow_scan_key(info, 0).empty()); pipeline.handle_process(info); auto stats = pipeline.stats(); assert(stats.detections == 1); assert(stats.scan_cache_hits == 1 && stats.images_scanned == 0); assert(pipeline.perf().yara_scan_us.summary().count == 1); // no second YARA pass // Severe host pressure: the guard decides on hash/IOC/quarantine only, and // a degraded result is not cached (the pipeline still scans it). sample.cpu_some_pct = 95; governor.tick(); fd = ::open(other.c_str(), O_RDONLY | O_CLOEXEC); assert(fd >= 0); assessment = pipeline.assess_exec(fd, other.string()); ::close(fd); assert(assessment.scanned && assessment.degraded && assessment.top_rule.empty()); assert(!assessment.sha256.empty() && !assessment.md5.empty()); assert(pipeline.perf().yara_scan_us.summary().count == 1); info.pid = 901; info.executable_path = other.string(); info.image_path = other.string(); info.image_identity = identity_of(other); // Uncached: needs a full scan (key returned) unless below the size floor. assert(!pipeline.slow_scan_key(info, 0).empty()); assert(pipeline.slow_scan_key(info, 1 << 20).empty()); pipeline.handle_process(info); assert(pipeline.slow_scan_key(info, 0).empty()); // cached now stats = pipeline.stats(); assert(stats.images_scanned == 1 && stats.detections == 2); pipeline.shutdown(); // The profile's image cap applies even when the config allows more. auto small = *strawberry::scan_profile_named("balanced"); small.pipeline_max_image_bytes = 8; options.profile = small; strawberry::ResourceGovernor capped(options); capped.tick(); strawberry::DetectionPipeline limited; limited.set_governor(&capped); config.db_path = (root / "capped.db").string(); assert(limited.initialize(config)); info.pid = 902; info.executable_path = executable.string(); info.image_path = executable.string(); info.image_identity = identity_of(executable); limited.handle_process(info); assert(limited.stats().images_too_large == 1 && limited.stats().images_scanned == 0); // Repeated execs of a too-large image are cache hits, not re-reads. info.pid = 903; limited.handle_process(info); assert(limited.stats().images_too_large == 1 && limited.stats().scan_cache_hits == 1); limited.shutdown(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } void test_oversized_cache_never_rehashes() { const auto root = make_test_root(); const auto executable = root / "bin" / "oversized"; write_rule_file(root / "rules" / "synthetic_runtime.yar"); write_binary_file(executable, std::string("\x7f\x45\x4c\x46") + "synthetic-payload-marker"); strawberry::Config config; config.db_path = (root / "oversized.db").string(); config.yara_rules_path = (root / "rules").string(); config.process_scan_max_bytes = 8; strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); strawberry::TelemetrySink sink(32); pipeline.set_telemetry_sink(&sink); strawberry::ProcessInfo info; info.pid = 6100; info.executable_path = executable.string(); info.image_path = executable.string(); info.image_identity = identity_of(executable); pipeline.handle_process(info); // rejected before hashing, negative cache has no digest assert(pipeline.stats().images_too_large == 1); for (int i = 0; i < 3; ++i) { info.pid = 6101 + i; if (i == 1) pipeline.handle_inventory_process(info); else pipeline.handle_process(info); } // A trusted caller may already know the hash: retain it for IOC checks, // but it still must not turn a skipped YARA scan into a completed one. info.pid = 6104; info.hash = strawberry::HashUtils::sha256_file(executable.string()); assert(!info.hash.empty()); pipeline.handle_process(info); std::vector events; sink.drain(events, 32); int scans = 0, hashed_refusals = 0, misreported = 0; for (const auto& event : events) { if (event.type != strawberry::store::RecordType::scan_result) continue; strawberry::ScanResultRecord result; assert(strawberry::decode_scan_result(event.payload, result)); ++scans; if (result.pid == 6104) assert(result.sha256 == info.hash); else hashed_refusals += !result.sha256.empty(); misreported += result.outcome != strawberry::ScanOutcome::too_large; } std::cout << "oversized-cache: repeats=3 hashed_refusals=" << hashed_refusals << " misreported=" << misreported << std::endl; assert(scans == 5 && hashed_refusals == 0 && misreported == 0); assert(pipeline.stats().images_scanned == 0 && pipeline.stats().detections == 0); assert(pipeline.stats().scan_cache_hits == 4); pipeline.set_telemetry_sink(nullptr); pipeline.shutdown(); std::error_code ec; fs::remove_all(root, ec); std::cout << "Test oversized_cache_never_rehashes: PASSED" << std::endl; } void test_negative_cache_is_not_a_guard_verdict() { const auto root = make_test_root(); const auto executable = root / "bin" / "guard_candidate"; write_rule_file(root / "rules" / "synthetic_runtime.yar"); write_binary_file(executable, std::string("\x7f\x45\x4c\x46") + "synthetic-payload-marker"); strawberry::GovernorOptions options; options.profile = *strawberry::scan_profile_named("balanced"); options.profile.pipeline_max_image_bytes = 8; options.profile.guard_full_scan_max_bytes = 4096; options.profile.guard_degrades = false; options.sampler = [] { return strawberry::ResourceSample{}; }; strawberry::ResourceGovernor governor(options); governor.tick(); strawberry::Config config; config.db_path = (root / "guard.db").string(); config.yara_rules_path = (root / "rules").string(); config.process_scan_max_bytes = 4096; strawberry::DetectionPipeline pipeline; pipeline.set_governor(&governor); assert(pipeline.initialize(config)); strawberry::ProcessInfo info; info.pid = 6200; info.executable_path = executable.string(); info.image_path = executable.string(); info.image_identity = identity_of(executable); pipeline.handle_process(info); // pipeline cap rejects it; guard's own cap permits it assert(pipeline.stats().images_too_large == 1); const int fd = ::open(executable.c_str(), O_RDONLY | O_CLOEXEC); assert(fd >= 0); const auto assessment = pipeline.assess_exec(fd, executable.string()); ::close(fd); std::cout << "negative-cache-guard: rule=" << assessment.top_rule << " degraded=" << assessment.degraded << std::endl; assert(assessment.scanned && !assessment.degraded); assert(assessment.top_rule == "SyntheticRuntimeRule"); assert(pipeline.perf().yara_scan_us.summary().count == 1); const int again_fd = ::open(executable.c_str(), O_RDONLY | O_CLOEXEC); assert(again_fd >= 0); const auto again = pipeline.assess_exec(again_fd, executable.string()); ::close(again_fd); assert(again.top_rule == assessment.top_rule && again.sha256 == assessment.sha256); assert(pipeline.perf().yara_scan_us.summary().count == 1); // actual scan result remains reusable pipeline.shutdown(); std::error_code ec; fs::remove_all(root, ec); std::cout << "Test negative_cache_is_not_a_guard_verdict: PASSED" << std::endl; } #endif } // namespace void test_replaced_binary_is_evidence_only() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto executable = root / "bin" / "updated_app"; write_text_file(rules_dir / "active.yar", "rule Unrelated { strings: $a = \"nothing-matches-this\" condition: $a }\n"); write_binary_file(executable, std::string("\x7f\x45\x4c\x46") + "an ordinary program"); std::cout << "Test replaced_binary_is_evidence_only:" << std::endl; strawberry::Config config; config.db_path = (root / "replaced.db").string(); config.yara_rules_path = rules_dir.string(); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); // Running image deleted, a new file at the same path: an update. strawberry::ProcessInfo info; info.pid = 4100; info.executable_path = executable.string(); info.image_path = executable.string(); info.image_identity = identity_of(executable); info.start_time_ticks = 7; info.executable_deleted = true; int notified = 0; pipeline.set_detection_callback([&](const strawberry::DetectionEvent&) { ++notified; }); pipeline.handle_process(info); auto stats = pipeline.stats(); assert(stats.evidence_only == 1 && stats.detections == 0 && notified == 0); // Nothing at the path any more (self-deleting binary): a detection. info.pid = 4101; info.start_time_ticks = 8; info.executable_path = (root / "bin" / "vanished").string(); pipeline.handle_process(info); stats = pipeline.stats(); assert(stats.evidence_only == 1 && stats.detections == 1); pipeline.shutdown(); strawberry::Database database; assert(database.open_read_only(config.db_path)); std::size_t evidence = 0; for (const auto& e : database.list_indicators()) { if (e.rule_name == "DeletedExecutable" && e.metadata.count("evidence_only")) ++evidence; } assert(evidence == 1); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } void test_repeated_detections_are_summarized() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto executable = root / "bin" / "repeat_bad"; write_text_file(rules_dir / "active.yar", "rule RepeatRule { meta: severity = \"medium\" strings: $m = \"repeat-marker\" condition: $m }\n"); write_binary_file(executable, std::string("\x7f\x45\x4c\x46") + "repeat-marker"); std::cout << "Test repeated_detections_are_summarized:" << std::endl; strawberry::Config config; config.db_path = (root / "repeat.db").string(); config.yara_rules_path = rules_dir.string(); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); int notified = 0; pipeline.set_detection_callback([&](const strawberry::DetectionEvent&) { ++notified; }); for (int i = 0; i < 30; ++i) { strawberry::ProcessInfo info; info.pid = 5000 + i; info.executable_path = executable.string(); info.image_path = executable.string(); info.image_identity = identity_of(executable); info.start_time_ticks = 100 + static_cast(i); pipeline.handle_process(info); } const auto stats = pipeline.stats(); assert(stats.detections == 30); // every execution counts assert(stats.repeats_aggregated == 10); // beyond the first 20 assert(notified == 21); // 20 + one rate-limited alert pipeline.shutdown(); strawberry::Database database; assert(database.open_read_only(config.db_path)); std::size_t full = 0, summaries = 0; for (const auto& e : database.list_indicators()) { if (e.id.starts_with("process-hit:")) ++full; if (e.id.starts_with("repeat:")) { ++summaries; assert(e.metadata.at("count") == "1" || std::stoi(e.metadata.at("count")) >= 1); // invariant-allow(sto): test assert(e.metadata.at("first_pid") == "5020"); } } assert(full == 20 && summaries == 1); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } void test_every_exec_is_telemetry() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto clean = root / "bin" / "clean_tool"; const auto bad = root / "bin" / "bad_tool"; write_text_file(rules_dir / "active.yar", "rule TelemetryRule { meta: severity = \"medium\" strings: $m = \"telemetry-marker\" condition: $m }\n"); write_binary_file(clean, std::string("\x7f\x45\x4c\x46") + "nothing here"); write_binary_file(bad, std::string("\x7f\x45\x4c\x46") + "telemetry-marker"); std::cout << "Test every_exec_is_telemetry:" << std::endl; strawberry::Config config; config.db_path = (root / "telemetry.db").string(); config.yara_rules_path = rules_dir.string(); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); strawberry::TelemetrySink sink(1024); pipeline.set_telemetry_sink(&sink); auto exec = [&](int pid, const fs::path& image) { strawberry::ProcessInfo info; info.pid = pid; info.executable_path = image.string(); info.image_path = image.string(); info.image_identity = identity_of(image); info.start_time_ticks = static_cast(pid); return info; }; for (int pid : {700, 701, 702, 703}) pipeline.handle_process(exec(pid, clean)); // clean: no evidence at all pipeline.handle_process(exec(704, bad)); pipeline.handle_process(exec(704, bad)); // same process + image again: deduped, no second record std::vector events; sink.drain(events, 100); std::map execs, scans; std::map outcome; int rule_matches = 0; for (const auto& e : events) { if (e.type == strawberry::store::RecordType::process_exec) { strawberry::ProcessExecRecord r; assert(strawberry::decode_process_exec(e.payload, r)); ++execs[r.pid]; assert(r.source == "live" && !r.sha256.empty()); } else if (e.type == strawberry::store::RecordType::scan_result) { strawberry::ScanResultRecord r; assert(strawberry::decode_scan_result(e.payload, r)); ++scans[r.pid]; outcome[r.pid] = r.outcome; if (r.pid == 704) assert(r.rules == std::vector{"TelemetryRule"}); } else if (e.type == strawberry::store::RecordType::rule_match) { strawberry::RuleMatchRecord r; assert(strawberry::decode_rule_match(e.payload, r)); ++rule_matches; assert(r.pid == 704 && r.rule == "TelemetryRule" && r.severity == "medium"); #if STRAWBERRY_HAVE_YARA assert(r.strings.size() == 1 && r.strings[0].identifier == "$m" && r.strings[0].offset == 4 && r.strings[0].length == 16); #endif } } assert(execs.size() == 5 && scans.size() == 5); for (const auto& [pid, n] : execs) assert(n == 1 && scans[pid] == 1); // exactly one each (C2) assert(outcome[700] == strawberry::ScanOutcome::scanned); assert(outcome[701] == strawberry::ScanOutcome::cache_hit); // same image as 700 assert(rule_matches == 1); // Stored in the telemetry stream, not in the evidence view. const auto stored_events = events.size(); assert(pipeline.write_telemetry(events, true)); std::size_t stored = 0; strawberry::store::scan_store_read_only(config.db_path, strawberry::store::Stream::telemetry, [&](uint32_t, std::string_view) { ++stored; }); assert(stored == stored_events); pipeline.set_telemetry_sink(nullptr); pipeline.shutdown(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } // A community hash feed (no max_severity) is advisory: its hits report at // "high" and do NOT force a prevention hit, so with the default critical // threshold a flagged binary is not would-blocked (the /usr/bin/ss storm). void test_ioc_community_feed_is_advisory() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto feeds_dir = root / "feeds"; const auto flagged = root / "bin" / "flagged"; write_text_file(rules_dir / "noop.yar", "rule Noop { strings: $s = \"never-matches-xyzzy\" condition: $s }\n"); write_binary_file(flagged, std::string("\x7f\x45\x4c\x46") + "a benign tool a noisy feed flags"); const auto hash = strawberry::HashUtils::sha256_file(flagged.string()); fs::create_directories(feeds_dir / "community" / "v1"); write_text_file(feeds_dir / "community" / "v1" / "list.txt", hash + "\n"); fs::create_directory_symlink("v1", feeds_dir / "community" / "current"); std::cout << "Test ioc_community_feed_is_advisory:" << std::endl; strawberry::Config config; config.db_path = (root / "adv.db").string(); config.yara_rules_path = rules_dir.string(); config.policy_path = (root / "policy.json").string(); config.feeds_dir = feeds_dir.string(); strawberry::PolicyStore policy(config.policy_path); assert(policy.load()); std::string error; // No max_severity -> advisory default "high". assert(policy.apply("feed-add", R"({"id":"community","kind":"hashes","type":"url","url":"https://example.com/c.txt"})", 0, &error)); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config, &policy)); const int fd = ::open(flagged.c_str(), O_RDONLY | O_CLOEXEC); const auto a = pipeline.assess_exec(fd, flagged.string()); ::close(fd); // Advisory: the hit is "high", ioc_match is NOT forced. assert(a.scanned && !a.ioc_match && a.top_severity == "high"); // With the default audit/critical policy the exec is allowed (no would-block // record, so no per-exec finding storm), on a normal path and a system path. assert(strawberry::decide_exec("audit", "critical", a, /*protected_path=*/false) == strawberry::ExecVerdict::allow); assert(strawberry::decide_exec("enforce", "critical", a, /*protected_path=*/false) == strawberry::ExecVerdict::allow); pipeline.shutdown(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } // Doctor cleanup: once a hash is allowlisted, the findings already stored for it // are stale and retire_allowlisted_findings removes them (findings.allowlisted -> // retire-allowlisted-findings). Allowlisting suppresses new detections but does // not delete old findings; this is the one-shot cleanup, surfaced by doctor. void test_retire_allowlisted_findings() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto feeds_dir = root / "feeds"; const auto flagged = root / "bin" / "flagged"; write_text_file(rules_dir / "noop.yar", "rule Noop { strings: $s = \"never-xyzzy\" condition: $s }\n"); write_binary_file(flagged, std::string("\x7f\x45\x4c\x46") + "flagged-by-feed"); const auto hash = strawberry::HashUtils::sha256_file(flagged.string()); fs::create_directories(feeds_dir / "hashes" / "v1"); write_text_file(feeds_dir / "hashes" / "v1" / "list.txt", hash + "\n"); fs::create_directory_symlink("v1", feeds_dir / "hashes" / "current"); std::cout << "Test retire_allowlisted_findings:" << std::endl; strawberry::Config config; config.db_path = (root / "ra.db").string(); config.yara_rules_path = rules_dir.string(); config.policy_path = (root / "policy.json").string(); config.feeds_dir = feeds_dir.string(); strawberry::PolicyStore policy(config.policy_path); assert(policy.load()); std::string error; assert(policy.apply( "feed-add", R"({"id":"hashes","kind":"hashes","type":"url","url":"https://example.com/h.txt","max_severity":"critical"})", 0, &error)); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config, &policy)); const auto exec = [&](int pid, uint64_t start) { strawberry::ProcessInfo info; info.pid = pid; info.executable_path = flagged.string(); info.image_path = flagged.string(); info.image_identity = identity_of(flagged); info.start_time_ticks = start; pipeline.handle_process(info); }; exec(4001, 1); exec(4002, 2); // two processes, same hash -> a hash-group finding referencing it pipeline.request_correlation(); assert(pipeline.run_pending_correlation()); assert(pipeline.finding_count() >= 1); // Not allowlisted yet: nothing is stale. assert(pipeline.retire_allowlisted_findings(true).empty()); // Allowlist the hash: the findings already stored for it are now stale. assert(policy.apply("allowlist-add", R"({"kind":"sha256","value":")" + hash + R"("})", 0, &error)); const auto stale = pipeline.retire_allowlisted_findings(true); assert(!stale.empty()); const auto before = pipeline.finding_count(); const auto retired = pipeline.retire_allowlisted_findings(false); assert(retired.size() == stale.size()); assert(pipeline.finding_count() == before - retired.size()); assert(pipeline.retire_allowlisted_findings(true).empty()); // idempotent pipeline.shutdown(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } // Repeated identical guard decisions collapse: the first 20 per window are full // exec-audit indicators, the rest become one rolling exec-repeat summary (so a // flagged binary run thousands of times does not create a finding per exec). void test_repeated_would_block_is_summarized() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; write_text_file(rules_dir / "noop.yar", "rule Noop { strings: $s = \"never-matches-xyzzy\" condition: $s }\n"); std::cout << "Test repeated_would_block_is_summarized:" << std::endl; strawberry::Config config; config.db_path = (root / "wb.db").string(); config.yara_rules_path = rules_dir.string(); config.policy_path = (root / "policy.json").string(); strawberry::PolicyStore policy(config.policy_path); assert(policy.load()); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config, &policy)); strawberry::ExecAssessment a; a.scanned = true; a.sha256 = std::string(64, 'a'); a.top_rule = "IOC_KnownMalwareHash"; a.top_severity = "critical"; a.ioc_match = true; for (int i = 0; i < 30; ++i) { strawberry::ExecDecision d; d.pid = 1000 + i; // distinct actors, same image/rule/verdict d.path = "/opt/app/flagged"; d.assessment = a; d.verdict = strawberry::ExecVerdict::would_block; pipeline.record_exec_decision(d); } assert(pipeline.stats().repeats_aggregated == 10); // 30 - 20 full pipeline.shutdown(); strawberry::Database database; assert(database.open_read_only(config.db_path)); int full = 0; int summaries = 0; for (const auto& ind : database.list_indicators()) { if (ind.id.rfind("exec-audit:", 0) == 0) ++full; else if (ind.id.rfind("exec-repeat:", 0) == 0) ++summaries; } database.close(); assert(full == 20 && summaries == 1); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } #if STRAWBERRY_HAVE_YARA void test_scoped_allowlist_preserves_independent_findings() { for (const std::string scope : {"hash-rule", "global-rule", "path-rule"}) { const auto root = make_test_root(); const std::string hash(64, 'a'); strawberry::Config config; config.db_path = (root / "scope.db").string(); config.policy_path = (root / "policy.json").string(); config.feeds_dir = (root / "feeds").string(); config.yara_rules_path = (root / "no-rules").string(); config.require_rules = false; strawberry::IndicatorEntry yara; yara.id = "real-yara"; yara.hash = hash; yara.rule_name = "RealMalware"; yara.severity = "high"; yara.timestamp = 100; yara.metadata = {{"source", "yara"}, {"pid", "101"}, {"process_node_id", "process_101_boot_1"}, {"process_path", "/synthetic/a"}}; auto ioc = yara; ioc.id = "false-ioc"; ioc.rule_name = "IOC_KnownMalwareHash"; ioc.metadata["source"] = "ioc"; ioc.metadata["pid"] = "102"; ioc.metadata["process_node_id"] = "process_102_boot_1"; { strawberry::Database seed; assert(seed.open(config.db_path)); assert(seed.store_indicator(yara) && seed.store_indicator(ioc)); } strawberry::PolicyStore policy(config.policy_path); assert(policy.load()); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config, &policy)); assert(pipeline.run_pending_correlation()); assert(pipeline.finding_count() == 3); // two process findings plus shared hash std::string error; const auto payload = scope == "global-rule" ? std::string(R"({"kind":"rule","value":"IOC_KnownMalwareHash"})") : scope == "path-rule" ? std::string(R"({"kind":"path","value":"/synthetic/a","rule":"IOC_KnownMalwareHash"})") : R"({"kind":"sha256","value":")" + hash + R"(","rule":"IOC_KnownMalwareHash"})"; assert(policy.apply("allowlist-add", payload, 0, &error)); const auto stale = pipeline.retire_allowlisted_findings(true); assert(stale.size() == 1); // only IOC-only support; NEVER the independent YARA or mixed group assert(pipeline.retire_allowlisted_findings(false).size() == 1); pipeline.request_correlation(); assert(pipeline.run_pending_correlation()); assert(pipeline.finding_count() == 1); // mixed hash finding withdrawn, YARA survives const auto entries = policy.snapshot().allowlist; assert(!entries.empty()); assert(policy.apply("allowlist-remove", R"({"id":")" + entries.back().id + R"("})", 0, &error)); pipeline.request_correlation(); assert(pipeline.run_pending_correlation()); assert(pipeline.finding_count() == 3); // raw evidence retained and restored pipeline.shutdown(); std::error_code ec; fs::remove_all(root, ec); } std::cout << "Test scoped_allowlist_preserves_independent_findings: PASSED" << std::endl; } void test_policy_rebuild_withdraws_stale_groups() { const auto root = make_test_root(); strawberry::Config config; config.db_path = (root / "rebuild.db").string(); config.policy_path = (root / "policy.json").string(); config.feeds_dir = (root / "feeds").string(); config.yara_rules_path = (root / "no-rules").string(); config.require_rules = false; strawberry::IndicatorEntry a; a.id = "a"; a.hash = std::string(64, 'a'); a.rule_name = "SharedRule"; a.severity = "high"; a.timestamp = 100; a.metadata = {{"source", "yara"}, {"pid", "101"}, {"process_node_id", "process_101_boot_1"}, {"process_path", "/synthetic/a"}}; auto b = a; b.id = "b"; b.hash = std::string(64, 'b'); b.metadata["pid"] = "102"; b.metadata["process_node_id"] = "process_102_boot_1"; { strawberry::Database seed; assert(seed.open(config.db_path)); assert(seed.store_indicator(a) && seed.store_indicator(b)); } strawberry::PolicyStore policy(config.policy_path); assert(policy.load()); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config, &policy)); assert(pipeline.run_pending_correlation()); assert(pipeline.finding_count() == 3); std::string error; bool invalidated = false; pipeline.set_correlation_recheck_hook([&] { assert(policy.apply("allowlist-add", R"({"kind":"sha256","value":")" + a.hash + R"("})", 0, &error)); assert(pipeline.retire_allowlisted_findings(false).size() == 1); pipeline.request_correlation(); invalidated = true; }); pipeline.request_correlation(); assert(!pipeline.run_pending_correlation()); // computed before the policy change: discard, do not resurrect A assert(invalidated && pipeline.finding_count() == 2); pipeline.set_correlation_recheck_hook({}); assert(pipeline.run_pending_correlation()); assert(pipeline.finding_count() == 1); // only B, not the now-unqualified shared-rule aggregate pipeline.set_correlation_recheck_hook([] { throw std::runtime_error("test observer"); }); pipeline.request_correlation(); bool threw = false; try { (void) pipeline.run_pending_correlation(); } catch (const std::runtime_error&) { threw = true; } assert(threw && pipeline.has_pending_correlation() && pipeline.finding_count() == 1); pipeline.set_correlation_recheck_hook({}); assert(pipeline.run_pending_correlation()); pipeline.shutdown(); std::error_code ec; fs::remove_all(root, ec); std::cout << "Test policy_rebuild_withdraws_stale_groups: PASSED" << std::endl; } void test_capped_support_and_foreign_findings() { const auto root = make_test_root(); strawberry::Config config; config.db_path = (root / "capped.db").string(); config.policy_path = (root / "policy.json").string(); config.feeds_dir = (root / "feeds").string(); config.yara_rules_path = (root / "absent").string(); config.require_rules = false; const std::string hash(64, 'a'); { strawberry::Database seed; assert(seed.open(config.db_path)); assert(seed.begin_transaction()); for (int pid = 1000; pid < 1201; ++pid) { strawberry::IndicatorEntry e; e.id = std::to_string(pid); e.hash = hash; e.rule_name = "Wide"; e.timestamp = pid; e.severity = "high"; e.metadata = {{"source", "yara"}, {"pid", std::to_string(pid)}, {"process_path", "/synthetic/a"}, {"process_node_id", "process_" + std::to_string(pid) + "_boot_1"}}; assert(seed.store_indicator(e)); } strawberry::FindingRecord foreign; foreign.id = "foreign"; foreign.title = "Other engine"; foreign.metadata = {{"engine", "other-engine"}, {"indicator_count", "1"}}; foreign.related_indicator_ids = {"1000"}; foreign.related_node_ids = {"file_" + hash}; assert(seed.store_finding(foreign)); assert(seed.commit_transaction()); } strawberry::PolicyStore policy(config.policy_path); assert(policy.load()); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config, &policy)); assert(pipeline.run_pending_correlation()); assert(pipeline.finding_count() == 204); // 201 process, hash, rule, foreign std::string error; assert(policy.apply("allowlist-add", R"({"kind":"sha256","value":")" + hash + R"("})", 0, &error)); assert(pipeline.retire_allowlisted_findings(true).size() == 201); // never infer all 201 from capped 200 assert(pipeline.retire_allowlisted_findings(false).size() == 201); assert(pipeline.finding_count() == 3); // capped aggregates plus foreign assert(pipeline.run_pending_correlation()); assert(pipeline.finding_count() == 1); // full support cleared, other engine untouched pipeline.shutdown(); strawberry::Database reader; assert(reader.open_read_only(config.db_path)); assert(reader.find_finding("foreign").has_value()); assert(reader.list_indicators().size() == 201); // no raw evidence deletion reader.close(); std::error_code ec; fs::remove_all(root, ec); std::cout << "Test capped_support_and_foreign_findings: PASSED" << std::endl; } void test_shutdown_survives_correlation_allocation_failure() { const auto root = make_test_root(); strawberry::Config config; config.db_path = (root / "shutdown.db").string(); config.yara_rules_path = (root / "absent").string(); config.require_rules = false; { strawberry::Database seed; assert(seed.open(config.db_path)); for (int pid : {101, 102}) { strawberry::IndicatorEntry e; e.id = std::to_string(pid); e.hash = std::string(64, pid == 101 ? 'a' : 'b'); e.rule_name = "Shared"; e.severity = "high"; e.timestamp = pid; e.metadata = {{"source", "yara"}, {"namespace", "feed-A"}, {"pid", std::to_string(pid)}, {"process_node_id", "process_" + std::to_string(pid) + "_boot_1"}, {"process_path", "/synthetic/x"}}; assert(seed.store_indicator(e)); } } strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); const auto failures = pipeline.persist_failures(); bool escaped = false; fail_next_allocation = true; try { pipeline.shutdown(); } catch (const std::bad_alloc&) { escaped = true; } const bool consumed = !fail_next_allocation; fail_next_allocation = false; // never affect assertions/recovery or later test cases std::cout << "shutdown allocation consumed=" << consumed << " escaped=" << escaped << std::endl; assert(consumed); assert(!escaped); assert(!pipeline.is_initialized()); assert(pipeline.persist_failures() > failures); { strawberry::DetectionPipeline recovered; assert(recovered.initialize(config)); // proves failed final flush released the writer assert(recovered.run_pending_correlation()); assert(recovered.finding_count() == 3); // raw evidence supports a fresh full rebuild recovered.shutdown(); } { strawberry::DetectionPipeline implicit_shutdown; assert(implicit_shutdown.initialize(config)); fail_next_allocation = true; // an exception must not cross the implicit-noexcept destructor } const bool destructor_consumed = !fail_next_allocation; fail_next_allocation = false; assert(destructor_consumed); strawberry::Database reader; assert(reader.open(config.db_path)); // implicit shutdown also released writer/resources assert(reader.list_indicators().size() == 2 && reader.list_findings().size() == 3); reader.close(); std::error_code ec; fs::remove_all(root, ec); std::cout << "Test shutdown_survives_correlation_allocation_failure: PASSED" << std::endl; } void test_typed_rule_id_rebuild_retires_legacy_id() { const auto root = make_test_root(); strawberry::Config config; config.db_path = (root / "migration.db").string(); config.yara_rules_path = (root / "absent").string(); config.require_rules = false; const std::string raw = "rule\nShared"; const auto legacy = "finding_rule_yara_" + strawberry::HashUtils::sha256_data( reinterpret_cast(raw.data()), raw.size()).substr(0, 16); { strawberry::Database seed; assert(seed.open(config.db_path)); for (int pid : {101, 102}) { strawberry::IndicatorEntry e; e.id = std::to_string(pid); e.hash = std::string(64, pid == 101 ? 'a' : 'b'); e.rule_name = "Shared"; e.severity = "high"; e.timestamp = pid; e.metadata = {{"source", "yara"}, {"namespace", "feed-A"}, {"pid", std::to_string(pid)}, {"process_node_id", "process_" + std::to_string(pid) + "_boot_1"}, {"process_path", "/synthetic/x"}}; assert(seed.store_indicator(e)); } strawberry::FindingRecord old; old.id = legacy; old.title = "Old unqualified rule"; old.metadata = {{"engine", "strawberry-rule-correlation-v1"}, {"rule", "same_yara_rule_multiple_processes"}, {"matched_rule", "Shared"}}; assert(seed.store_finding(old)); } strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); assert(pipeline.run_pending_correlation()); assert(pipeline.finding_count() == 3); pipeline.shutdown(); strawberry::Database reader; assert(reader.open_read_only(config.db_path)); assert(!reader.find_finding(legacy).has_value()); bool typed = false; for (const auto& f : reader.list_findings()) { if (f.metadata.at("rule") != "same_yara_rule_multiple_processes") continue; assert(f.id != legacy && f.metadata.at("rule_namespace") == "feed-A"); typed = true; } assert(typed && reader.list_indicators().size() == 2); reader.close(); std::error_code ec; fs::remove_all(root, ec); std::cout << "Test typed_rule_id_rebuild_retires_legacy_id: PASSED" << std::endl; } #endif int main(int argc, char** argv) { std::cout << "Running DetectionPipeline tests..." << std::endl << std::endl; #if !STRAWBERRY_HAVE_YARA std::cout << "SKIP: runtime pipeline end-to-end test requires libyara" << std::endl; return 0; #else if (argc == 2) { if (std::string(argv[1]) == "oversized-cache") { test_oversized_cache_never_rehashes(); return 0; } if (std::string(argv[1]) == "negative-cache-guard") { test_negative_cache_is_not_a_guard_verdict(); return 0; } if (std::string(argv[1]) == "scoped-allowlist") { test_scoped_allowlist_preserves_independent_findings(); return 0; } if (std::string(argv[1]) == "policy-rebuild") { test_policy_rebuild_withdraws_stale_groups(); return 0; } if (std::string(argv[1]) == "capped-support") { test_capped_support_and_foreign_findings(); return 0; } if (std::string(argv[1]) == "rule-migration") { test_typed_rule_id_rebuild_retires_legacy_id(); return 0; } if (std::string(argv[1]) == "shutdown-failure") { test_shutdown_survives_correlation_allocation_failure(); return 0; } return 2; } test_shutdown_survives_correlation_allocation_failure(); test_scoped_allowlist_preserves_independent_findings(); test_policy_rebuild_withdraws_stale_groups(); test_capped_support_and_foreign_findings(); test_typed_rule_id_rebuild_retires_legacy_id(); test_procfs_event_scans_and_persists_match(); test_fails_closed_without_rules(); test_single_writer_lock_and_read_only_queries(); test_stable_ids_severity_default_and_heuristics(); test_scans_only_the_verified_image(); test_stats_and_rescan_after_rule_reload(); test_scan_on_demand_is_read_only_and_bounded(); test_feed_sources_allowlist_and_size_cap(); test_ioc_hash_detection_and_score_severity(); test_ioc_community_feed_is_advisory(); test_retire_allowlisted_findings(); test_repeated_would_block_is_summarized(); test_governed_shared_cache_degrade_and_caps(); test_oversized_cache_never_rehashes(); test_negative_cache_is_not_a_guard_verdict(); test_replaced_binary_is_evidence_only(); test_repeated_detections_are_summarized(); test_every_exec_is_telemetry(); std::cout << std::endl << "All tests passed!" << std::endl; return 0; #endif }