#include "strawberry/daemon_app.hpp" #include "strawberry/database.hpp" #include "strawberry/hash_utils.hpp" #include #include #include #include #include #include #include #include #if defined(__linux__) #include #include #include #include #endif #ifndef STRAWBERRY_HAVE_LIBBPF #define STRAWBERRY_HAVE_LIBBPF 0 #endif #ifndef STRAWBERRY_HAVE_YARA #define STRAWBERRY_HAVE_YARA 0 #endif namespace fs = std::filesystem; namespace { #if defined(__linux__) && STRAWBERRY_HAVE_LIBBPF && STRAWBERRY_HAVE_YARA constexpr const char* kMarker = "strawberry-live-daemon-marker"; bool ensure_tracefs_mounted() { if (::mount("tracefs", "/sys/kernel/tracing", "tracefs", 0, nullptr) == 0) { return true; } if (errno == EBUSY) { return true; } std::cerr << "warning: mount(tracefs) failed: " << std::strerror(errno) << std::endl; return false; } bool wait_for_running(strawberry::StrawberryDaemon& daemon, std::chrono::milliseconds timeout) { const auto deadline = std::chrono::steady_clock::now() + timeout; while (std::chrono::steady_clock::now() < deadline) { if (daemon.is_running()) { return true; } std::this_thread::sleep_for(std::chrono::milliseconds(25)); } return daemon.is_running(); } void append_file_bytes(std::ofstream& output, const fs::path& source) { std::ifstream input(source, std::ios::binary); if (!input) { throw std::runtime_error("failed to open source executable"); } output << input.rdbuf(); if (!output.good()) { throw std::runtime_error("failed to copy source executable"); } } void write_live_executable(const fs::path& destination) { if (destination.has_parent_path()) { std::error_code ec; fs::create_directories(destination.parent_path(), ec); } std::ofstream output(destination, std::ios::binary); if (!output) { throw std::runtime_error("failed to create staged executable"); } append_file_bytes(output, "/bin/echo"); output.write(kMarker, static_cast(std::strlen(kMarker))); output.close(); if (!output.good()) { throw std::runtime_error("failed to finalize staged executable"); } std::error_code ec; fs::permissions(destination, fs::perms::owner_exec | fs::perms::group_exec | fs::perms::others_exec, fs::perm_options::add, ec); if (ec) { throw std::runtime_error("failed to set staged executable permissions"); } } void write_rule_file(const fs::path& path) { if (path.has_parent_path()) { std::error_code ec; fs::create_directories(path.parent_path(), ec); } std::ofstream output(path); output << "rule LiveBpfDaemonRule {\n" " strings:\n" " $marker = \"" << kMarker << "\"\n" " condition:\n" " $marker\n" "}\n"; } void write_config_file(const fs::path& path, const std::string& db_path, const std::string& rules_dir) { if (path.has_parent_path()) { std::error_code ec; fs::create_directories(path.parent_path(), ec); } std::ofstream output(path); output << "db_path=" << db_path << "\n"; output << "yara_rules_path=" << rules_dir << "\n"; output << "daemonize=false\n"; output << "enable_bpf_monitoring=true\n"; } #endif } // namespace int main() { #if !defined(__linux__) || !STRAWBERRY_HAVE_LIBBPF || !STRAWBERRY_HAVE_YARA std::cout << "SKIP: live_bpf_daemon_smoke requires a Linux build with libbpf and libyara support" << std::endl; return 0; #else const auto root = fs::temp_directory_path() / "strawberry_live_bpf_daemon_smoke"; std::error_code ec; fs::remove_all(root, ec); fs::create_directories(root, ec); const auto rules_dir = root / "rules"; const auto executable_dir = root / "bin"; const auto live_executable = executable_dir / "live_daemon_echo"; const auto db_path = root / "live_daemon.db"; const auto config_path = root / "strawberry.conf"; try { write_rule_file(rules_dir / "live_bpf_daemon.yar"); write_live_executable(live_executable); write_config_file(config_path, db_path.string(), rules_dir.string()); } catch (const std::exception& e) { std::cerr << e.what() << std::endl; fs::remove_all(root, ec); return 1; } const std::string expected_hash = strawberry::HashUtils::sha256_file(live_executable.string()); if (expected_hash.empty()) { std::cerr << "Failed to hash staged live executable." << std::endl; fs::remove_all(root, ec); return 1; } ::setenv("STRAWBERRY_PROCESS_MONITOR_BACKEND", "bpf", 1); (void) ensure_tracefs_mounted(); strawberry::StrawberryDaemon daemon; if (!daemon.initialize(config_path.string())) { std::cerr << "Failed to initialize StrawberryDaemon." << std::endl; fs::remove_all(root, ec); return 1; } std::thread daemon_thread([&]() { daemon.run(); }); if (!wait_for_running(daemon, std::chrono::seconds(2))) { std::cerr << "Daemon did not report a running state." << std::endl; daemon.request_stop(); daemon_thread.join(); fs::remove_all(root, ec); return 1; } pid_t child = ::fork(); if (child < 0) { std::cerr << "fork() failed: " << std::strerror(errno) << std::endl; daemon.request_stop(); daemon_thread.join(); fs::remove_all(root, ec); return 1; } if (child == 0) { const std::string argv0 = live_executable.filename().string(); char* const argv[] = { const_cast(argv0.c_str()), const_cast("strawberry_live_bpf_daemon_smoke"), nullptr, }; ::execv(live_executable.c_str(), argv); std::perror("execv"); _exit(127); } int status = 0; (void) ::waitpid(child, &status, 0); std::this_thread::sleep_for(std::chrono::seconds(2)); daemon.request_stop(); daemon_thread.join(); strawberry::Database database; if (!database.open(db_path.string())) { std::cerr << "Failed to reopen daemon smoke database." << std::endl; fs::remove_all(root, ec); return 1; } auto hits = database.find_indicators_by_hash(expected_hash); if (hits.size() != 1) { std::cerr << "Expected exactly one persisted daemon live hit, got " << hits.size() << std::endl; database.close(); fs::remove_all(root, ec); return 1; } const auto& hit = hits.front(); if (hit.rule_name != "LiveBpfDaemonRule") { std::cerr << "Unexpected rule name: " << hit.rule_name << std::endl; database.close(); fs::remove_all(root, ec); return 1; } if (hit.metadata.at("process_path") != live_executable.string()) { std::cerr << "Persisted process path did not match staged live executable." << std::endl; database.close(); fs::remove_all(root, ec); return 1; } if (hit.metadata.at("cmdline").find("strawberry_live_bpf_daemon_smoke") == std::string::npos) { std::cerr << "Persisted cmdline did not include the expected live daemon smoke marker." << std::endl; std::cerr << "cmdline=" << hit.metadata.at("cmdline") << std::endl; database.close(); fs::remove_all(root, ec); return 1; } const std::string process_node_id = hit.metadata.count("process_node_id") != 0 ? hit.metadata.at("process_node_id") : "process_" + hit.metadata.at("pid"); const std::string file_node_id = "file_" + expected_hash; auto related = database.find_related_nodes(process_node_id, 1); bool saw_indicator = false; bool saw_file = false; for (const auto& node : related) { if (node.type == "indicator") { saw_indicator = true; } else if (node.id == file_node_id && node.type == "file") { saw_file = true; } } if (!saw_indicator || !saw_file) { std::cerr << "Expected persisted indicator + file nodes from daemon live smoke." << std::endl; database.close(); fs::remove_all(root, ec); return 1; } auto edges = database.get_node_edges(process_node_id); bool saw_matched = false; bool saw_executed = false; for (const auto& edge : edges) { if (edge.relationship_type == "matched") { saw_matched = true; } else if (edge.relationship_type == "executed" && edge.to_id == file_node_id) { saw_executed = true; } } if (!saw_matched || !saw_executed) { std::cerr << "Expected matched + executed edges for daemon live smoke process node." << std::endl; database.close(); fs::remove_all(root, ec); return 1; } auto file_edges = database.get_node_edges(file_node_id); bool saw_content_matched = false; for (const auto& edge : file_edges) { if (edge.relationship_type == "content_matched") { saw_content_matched = true; } } if (!saw_content_matched) { std::cerr << "Expected content_matched edge for daemon live smoke file node." << std::endl; database.close(); fs::remove_all(root, ec); return 1; } database.close(); std::cout << "Observed daemon-driven live BPF event persisted through StrawberryDaemon:" << std::endl; std::cout << " process_path=" << hit.metadata.at("process_path") << std::endl; std::cout << " cmdline=" << hit.metadata.at("cmdline") << std::endl; if (const auto it = hit.metadata.find("parent_pid"); it != hit.metadata.end()) { std::cout << " parent_pid=" << it->second << std::endl; } else { std::cout << " parent_pid=" << std::endl; } std::cout << " event_timestamp_ns=" << hit.metadata.at("event_timestamp_ns") << std::endl; std::cout << " sha256=" << expected_hash << std::endl; std::cout << "PASS: live libbpf -> StrawberryDaemon -> database smoke succeeded" << std::endl; fs::remove_all(root, ec); return 0; #endif }