// Live fanotify exec-prevention check. Needs root (CAP_SYS_ADMIN); run in a // privileged container: scripts/run_exec_guard_live_docker.sh #include "strawberry/exec_guard.hpp" #include "strawberry/crash_safety.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace fs = std::filesystem; using namespace strawberry; namespace { std::string read_fd(int fd) { std::string out; char buffer[65536]; off_t offset = 0; ssize_t n = 0; while ((n = ::pread(fd, buffer, sizeof(buffer), offset)) > 0) { out.append(buffer, static_cast(n)); offset += n; } return out; } // Exec `path`; returns 0 on success, or the execv errno (via exit code 100+errno). int try_exec(const std::string& path) { const pid_t child = ::fork(); if (child == 0) { char* argv[] = {const_cast(path.c_str()), nullptr}; ::execv(path.c_str(), argv); ::_exit(100 + errno); } int status = 0; ::waitpid(child, &status, 0); return WIFEXITED(status) ? WEXITSTATUS(status) : -1; } void make_binary(const fs::path& path, const std::string& marker) { fs::copy_file("/bin/true", path, fs::copy_options::overwrite_existing); std::ofstream(path, std::ios::app | std::ios::binary) << marker; fs::permissions(path, fs::perms::owner_all | fs::perms::group_exec | fs::perms::others_exec); } } // namespace int main() { if (::geteuid() != 0) { std::cout << "SKIP: exec_guard_live needs root" << std::endl; return 0; } const auto dir = fs::path("/var/tmp") / ("sb-exec-guard-" + std::to_string(::getpid())); fs::create_directories(dir); const auto evil = dir / "evil"; const auto clean = dir / "clean"; const auto slow = dir / "slow"; const auto quarantined = dir / "quarantined"; const auto system_evil = fs::path("/usr/local/bin/sb-exec-guard-evil"); make_binary(evil, "EVIL-MARKER"); make_binary(clean, "nothing"); make_binary(slow, "SLOW-MARKER"); make_binary(quarantined, "QUARANTINE-MARKER"); make_binary(system_evil, "EVIL-MARKER"); std::mutex mutex; std::string mode = "enforce"; std::vector decisions; ExecGuardOptions options; options.timeout = std::chrono::milliseconds(400); options.mount_points = {dir.string(), "/usr/local/bin"}; options.mode = [&]() { std::lock_guard l(mutex); return mode; }; options.min_severity = []() { return std::string("critical"); }; options.assess = [](int fd, const std::string&) { const auto content = read_fd(fd); ExecAssessment a; a.scanned = true; if (content.find("SLOW-MARKER") != std::string::npos) { std::this_thread::sleep_for(std::chrono::seconds(2)); } if (content.find("EVIL-MARKER") != std::string::npos) { a.top_rule = "Evil"; a.top_severity = "critical"; } a.quarantined = content.find("QUARANTINE-MARKER") != std::string::npos; return a; }; options.on_decision = [&](const ExecDecision& d) { std::lock_guard l(mutex); decisions.push_back(d); }; ExecGuard guard; if (!guard.start(options)) { std::cerr << "FAIL: " << guard.stats().error << std::endl; return 1; } std::cout << "Enforce: malicious file is denied with EPERM" << std::endl; assert(try_exec(evil.string()) == 100 + EPERM); std::cout << "Enforce: clean file runs" << std::endl; assert(try_exec(clean.string()) == 0); std::cout << "Enforce: second attempt is served from cache" << std::endl; assert(try_exec(evil.string()) == 100 + EPERM); std::cout << "Enforce: system location is never blocked (alert only)" << std::endl; assert(try_exec(system_evil.string()) == 0); std::cout << "Slow scan: allowed after the deadline (fail open)" << std::endl; const auto start = std::chrono::steady_clock::now(); assert(try_exec(slow.string()) == 0); assert(std::chrono::steady_clock::now() - start < std::chrono::milliseconds(1500)); { std::lock_guard l(mutex); mode = "audit"; } std::cout << "Audit: malicious file runs, decision recorded" << std::endl; assert(try_exec(evil.string()) == 0); { std::lock_guard l(mutex); mode = "off"; } std::cout << "Off: an operator quarantine is still enforced" << std::endl; assert(try_exec(quarantined.string()) == 100 + EPERM); assert(try_exec(clean.string()) == 0); std::this_thread::sleep_for(std::chrono::milliseconds(500)); const auto stats = guard.stats(); guard.stop(); std::cout << "stats: checked=" << stats.checked << " blocked=" << stats.blocked << " would_block=" << stats.would_block << " timeouts=" << stats.timeouts << " cache_hits=" << stats.cache_hits << std::endl; assert(stats.blocked == 3); assert(stats.would_block >= 2); // system path + audit assert(stats.timeouts == 1); assert(stats.cache_hits >= 1); { std::lock_guard l(mutex); bool saw_block = false; bool saw_audit = false; for (const auto& d : decisions) { saw_block |= d.verdict == ExecVerdict::block && d.path == evil.string(); saw_audit |= d.verdict == ExecVerdict::would_block && d.path == evil.string(); } assert(saw_block && saw_audit); } // 2026-10-25 freeze: a daemon holding exec permission events crashed and // its core dump deadlocked every exec on the host. A crashing guard must // release pending execs at once. std::cout << "Crash: a guard process that aborts releases execs immediately" << std::endl; { int ready[2]; assert(::pipe(ready) == 0); const pid_t holder = ::fork(); if (holder == 0) { ::close(ready[0]); strawberry::install_crash_safety(); ExecGuardOptions o = options; o.mode = []() { return std::string("enforce"); }; // Every scan hangs: execs stay pending until the process dies. o.assess = [](int, const std::string&) { std::this_thread::sleep_for(std::chrono::hours(1)); return ExecAssessment{}; }; o.timeout = std::chrono::hours(1); ExecGuard crashing; if (!crashing.start(o)) ::_exit(2); const char ok = 1; (void) !::write(ready[1], &ok, 1); std::this_thread::sleep_for(std::chrono::milliseconds(300)); std::abort(); } ::close(ready[1]); char ok = 0; assert(::read(ready[0], &ok, 1) == 1); ::close(ready[0]); const auto start = std::chrono::steady_clock::now(); assert(try_exec(clean.string()) == 0); // pending while the holder lives, then released const auto waited = std::chrono::steady_clock::now() - start; int status = 0; assert(::waitpid(holder, &status, 0) == holder && WIFSIGNALED(status) && !WCOREDUMP(status)); std::cout << " exec released after " << std::chrono::duration_cast(waited).count() << " ms" << std::endl; assert(waited < std::chrono::seconds(5)); } fs::remove_all(dir); fs::remove(system_evil); std::cout << "exec_guard_live: ALL PASSED" << std::endl; return 0; }