strawberry/tests/live_bpf_pipeline_smoke.cpp
trvon ad1676a594
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
Integrate features into the daemon, build and docs
Wire the policy, feeds, containment, exec guard, behavior, governor,
crash safety and event store into the daemon, detection pipeline, CLI
and meson build (zstd now required); Dockerfile, conan, systemd unit
(LimitCORE=0, resource limits) and installer updates; existing tests
adapted (store paths are directories now). Detections are counted and
notified even when evidence cannot be persisted (persist_failures in
status). prevention_timeout_ms is clamped and behavior dedupe handles
out-of-order events. Docs: API contract, developer guide, plan, UX,
agent rules.

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

315 lines
10 KiB
C++

#include "strawberry/database.hpp"
#include "strawberry/detection_pipeline.hpp"
#include "strawberry/hash_utils.hpp"
#include "strawberry/process_monitor.hpp"
#include <chrono>
#include <condition_variable>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#if defined(__linux__)
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#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
bool ends_with(const std::string& value, const std::string& suffix) {
return value.size() >= suffix.size() &&
value.compare(value.size() - suffix.size(), suffix.size(), suffix) == 0;
}
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;
}
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 LiveBpfEchoElf {\n"
" strings:\n"
" $elf = { 7F 45 4C 46 }\n"
" condition:\n"
" $elf at 0\n"
"}\n";
}
#endif
} // namespace
int main() {
#if !defined(__linux__) || !STRAWBERRY_HAVE_LIBBPF || !STRAWBERRY_HAVE_YARA
std::cout << "SKIP: live_bpf_pipeline_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_pipeline_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_echo";
const auto db_path = root / "live_pipeline.db";
fs::create_directories(executable_dir, ec);
write_rule_file(rules_dir / "live_bpf_echo_elf.yar");
fs::copy_file("/bin/echo", live_executable, fs::copy_options::overwrite_existing, ec);
if (ec) {
std::cerr << "Failed to stage live executable: " << ec.message() << std::endl;
fs::remove_all(root, ec);
return 1;
}
fs::permissions(live_executable,
fs::perms::owner_exec | fs::perms::group_exec | fs::perms::others_exec,
fs::perm_options::add,
ec);
::setenv("STRAWBERRY_PROCESS_MONITOR_BACKEND", "bpf", 1);
(void) ensure_tracefs_mounted();
strawberry::Config config;
config.db_path = db_path.string();
config.yara_rules_path = rules_dir.string();
strawberry::DetectionPipeline pipeline;
if (!pipeline.initialize(config)) {
std::cerr << "Failed to initialize DetectionPipeline." << std::endl;
return 1;
}
strawberry::ProcessMonitor monitor;
if (!monitor.initialize()) {
std::cerr << "Failed to initialize ProcessMonitor with forced BPF backend." << std::endl;
pipeline.shutdown();
return 1;
}
std::mutex mutex;
std::condition_variable cv;
std::vector<strawberry::ProcessInfo> observed_events;
strawberry::ProcessInfo matched_event;
bool observed_match = false;
bool persisted_match = false;
if (!monitor.start([&](const strawberry::ProcessInfo& info) {
bool should_persist = false;
{
std::lock_guard<std::mutex> lock(mutex);
observed_events.push_back(info);
if (!observed_match &&
(info.executable_path == live_executable.string() ||
ends_with(info.executable_path, "/live_echo"))) {
matched_event = info;
observed_match = true;
should_persist = true;
}
}
cv.notify_all();
if (should_persist) {
pipeline.handle_process(info);
{
std::lock_guard<std::mutex> lock(mutex);
persisted_match = true;
}
cv.notify_all();
}
})) {
std::cerr << "Failed to start ProcessMonitor." << std::endl;
pipeline.shutdown();
return 1;
}
pid_t child = ::fork();
if (child < 0) {
std::cerr << "fork() failed: " << std::strerror(errno) << std::endl;
monitor.stop();
pipeline.shutdown();
return 1;
}
if (child == 0) {
const std::string argv0 = live_executable.filename().string();
char* const argv[] = {
const_cast<char*>(argv0.c_str()),
const_cast<char*>("strawberry_live_bpf_pipeline_smoke"),
nullptr,
};
::execv(live_executable.c_str(), argv);
std::perror("execv");
_exit(127);
}
int status = 0;
(void) ::waitpid(child, &status, 0);
bool observed = false;
{
std::unique_lock<std::mutex> lock(mutex);
observed = cv.wait_for(lock, std::chrono::seconds(5), [&]() {
return persisted_match;
});
}
monitor.stop();
pipeline.shutdown();
if (!observed) {
std::cerr << "Did not observe the expected live exec event persisted through the pipeline." << std::endl;
std::lock_guard<std::mutex> lock(mutex);
for (const auto& info : observed_events) {
std::cerr << "Observed event: pid=" << info.pid
<< " executable_path=" << info.executable_path
<< " cmdline=" << info.cmdline << std::endl;
}
return 1;
}
strawberry::Database database;
if (!database.open(db_path.string())) {
std::cerr << "Failed to reopen smoke-test database." << std::endl;
return 1;
}
const std::string expected_hash = strawberry::HashUtils::sha256_file(matched_event.executable_path);
auto hits = database.find_indicators_by_hash(expected_hash);
if (hits.size() != 1) {
std::cerr << "Expected exactly one persisted live BPF hit, got " << hits.size() << std::endl;
database.close();
return 1;
}
const auto& hit = hits.front();
if (hit.rule_name != "LiveBpfEchoElf") {
std::cerr << "Unexpected rule name: " << hit.rule_name << std::endl;
database.close();
return 1;
}
if (hit.metadata.at("process_path") != matched_event.executable_path) {
std::cerr << "Persisted process path did not match observed event path." << std::endl;
database.close();
return 1;
}
if (hit.metadata.at("cmdline") != matched_event.cmdline) {
std::cerr << "Persisted cmdline did not match observed event cmdline." << std::endl;
database.close();
return 1;
}
if (matched_event.cmdline.empty()) {
std::cerr << "Observed live event, but cmdline was empty." << std::endl;
database.close();
return 1;
}
const std::string process_node_id = strawberry::process_node_id(matched_event);
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 (related.size() != 2 || !saw_indicator || !saw_file) {
std::cerr << "Expected file + indicator related nodes for process node " << process_node_id << std::endl;
database.close();
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 (edges.size() != 2 || !saw_matched || !saw_executed) {
std::cerr << "Expected matched + executed edges for process node " << process_node_id << std::endl;
database.close();
return 1;
}
auto file_related = database.find_related_nodes(file_node_id, 1);
if (file_related.size() != 1 || file_related.front().type != "indicator") {
std::cerr << "Expected one related indicator node for file node " << file_node_id << std::endl;
database.close();
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 (file_edges.size() != 2 || !saw_content_matched) {
std::cerr << "Expected executed + content_matched edges for file node " << file_node_id << std::endl;
database.close();
return 1;
}
database.close();
if (matched_event.pid != child) {
std::cout << "NOTE: observed exec event path match, but callback pid " << matched_event.pid
<< " differed from child pid " << child
<< " (possible namespace/kernel pid difference)." << std::endl;
}
std::cout << "Observed live BPF event persisted through DetectionPipeline:" << std::endl;
std::cout << " pid=" << matched_event.pid << std::endl;
std::cout << " parent_pid=" << matched_event.parent_pid << std::endl;
std::cout << " executable_path=" << matched_event.executable_path << std::endl;
std::cout << " cmdline=" << matched_event.cmdline << std::endl;
std::cout << " event_timestamp_ns=" << matched_event.event_timestamp_ns << std::endl;
std::cout << " sha256=" << expected_hash << std::endl;
std::cout << "PASS: live libbpf -> DetectionPipeline -> database smoke succeeded" << std::endl;
fs::remove_all(root, ec);
return 0;
#endif
}