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
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>
152 lines
4.7 KiB
C++
152 lines
4.7 KiB
C++
#include "strawberry/process_monitor.hpp"
|
|
|
|
#include <chrono>
|
|
#include <condition_variable>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#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
|
|
|
|
namespace {
|
|
|
|
#if defined(__linux__) && STRAWBERRY_HAVE_LIBBPF
|
|
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;
|
|
}
|
|
#endif
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
#if !defined(__linux__) || !STRAWBERRY_HAVE_LIBBPF
|
|
std::cout << "SKIP: process_monitor_live_smoke requires a Linux build with libbpf support" << std::endl;
|
|
return 0;
|
|
#else
|
|
::setenv("STRAWBERRY_PROCESS_MONITOR_BACKEND", "bpf", 1);
|
|
(void) ensure_tracefs_mounted();
|
|
|
|
strawberry::ProcessMonitor monitor;
|
|
if (!monitor.initialize()) {
|
|
std::cerr << "Failed to initialize ProcessMonitor with forced BPF backend." << std::endl;
|
|
std::cerr << "Tip: run this on Linux with sufficient privileges (for example, inside a privileged container)." << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
std::mutex mutex;
|
|
std::condition_variable cv;
|
|
std::vector<strawberry::ProcessInfo> events;
|
|
|
|
if (!monitor.start([&](const strawberry::ProcessInfo& info) {
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
events.push_back(info);
|
|
}
|
|
cv.notify_all();
|
|
})) {
|
|
std::cerr << "Failed to start ProcessMonitor." << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
pid_t child = ::fork();
|
|
if (child < 0) {
|
|
std::cerr << "fork() failed: " << std::strerror(errno) << std::endl;
|
|
monitor.stop();
|
|
return 1;
|
|
}
|
|
|
|
if (child == 0) {
|
|
char* const argv[] = {
|
|
const_cast<char*>("echo"),
|
|
const_cast<char*>("strawberry_process_monitor_live_smoke"),
|
|
nullptr,
|
|
};
|
|
::execv("/bin/echo", argv);
|
|
std::perror("execv");
|
|
_exit(127);
|
|
}
|
|
|
|
int status = 0;
|
|
(void) ::waitpid(child, &status, 0);
|
|
|
|
strawberry::ProcessInfo matched_event;
|
|
bool observed = false;
|
|
{
|
|
std::unique_lock<std::mutex> lock(mutex);
|
|
observed = cv.wait_for(lock, std::chrono::seconds(5), [&]() {
|
|
for (const auto& event : events) {
|
|
if (event.executable_path == "/bin/echo" || ends_with(event.executable_path, "/echo")) {
|
|
matched_event = event;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
});
|
|
}
|
|
|
|
monitor.stop();
|
|
|
|
if (!observed) {
|
|
std::cerr << "Did not observe the expected exec event for child PID " << child << std::endl;
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
for (const auto& event : events) {
|
|
std::cerr << "Observed event: pid=" << event.pid
|
|
<< " executable_path=" << event.executable_path
|
|
<< " cmdline=" << event.cmdline << std::endl;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
if (matched_event.image_identity.empty()) {
|
|
std::cerr << "Observed exec event, but the executable image was not resolved." << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
if (matched_event.cmdline.find("strawberry_process_monitor_live_smoke") == std::string::npos) {
|
|
std::cerr << "Observed exec event, but the cmdline did not contain the expected argument." << std::endl;
|
|
std::cerr << "cmdline=" << matched_event.cmdline << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
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 exec event:" << std::endl;
|
|
std::cout << " pid=" << matched_event.pid << std::endl;
|
|
std::cout << " executable_path=" << matched_event.executable_path << std::endl;
|
|
std::cout << " cmdline=" << matched_event.cmdline << std::endl;
|
|
std::cout << " image=" << matched_event.image_identity << std::endl;
|
|
std::cout << "PASS: live libbpf exec-event smoke succeeded" << std::endl;
|
|
return 0;
|
|
#endif
|
|
}
|