New meson option enable_endpoint_security (auto, macOS only) links EndpointSecurity and bsm and builds three ES backends behind the existing interfaces. Helpers are in src/platform/es_support.hpp. - ProcessMonitor: NOTIFY_EXEC events (pid, parent, path, dev:ino, argv, start time) plus NOTIFY_TRACE and NOTIFY_KEXTLOAD for the behavior rules. Events go to a bounded queue (4096, overflow counted in events_dropped) drained on the monitor thread, so callbacks stay on one thread. Without an ES client the monitor polls libproc (backend "libproc"). backend_name() reports "endpoint_security". - ExecGuard: AUTH_EXEC with the fanotify guard's rules. It fails open at min(timeout, ES deadline - 500 ms) and on a full queue, and on macOS 27+ ES_DEADLINE_MISS_MODE_FAIL_OPEN makes the kernel allow a missed deadline too. Platform binaries and SIP paths are never denied, and our own execs are muted. ES does not cache verdicts, so rule changes apply on the next exec. Our own cache keys on dev:ino:size:mtime:generation. The image is opened by path and its dev:ino checked against the event; a swapped file counts as unscanned. - FileWatcher: NOTIFY_CREATE/RENAME/CLOSE(modified) with inverted target-path muting: named entries selected literally, whole directories by prefix. macos_watch_specs() covers launchd agents/daemons, periodic jobs, cron tabs, sudo drop-ins, shell rc files, authorized_keys, Downloads and the temp directories. match_watch_spec() (direct children, as with fanotify directory marks) is portable and tested. Home dirs come from the directory service (local_home_dirs). Every ES handler is noexcept and records swallowed exceptions. Client errors name the missing requirement (root, entitlement, Full Disk Access), and the daemon keeps running detection-only. macos_es_live (root, ES entitlement) checks deny/allow/cache/ platform-binary/fail-open/audit for the guard, exec telemetry and the file watcher, using small signed payload binaries. It has not run yet: the development Mac has SIP enabled. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
248 lines
8.4 KiB
C++
248 lines
8.4 KiB
C++
// Live Endpoint Security check (macOS): exec prevention (AUTH_EXEC), exec
|
|
// telemetry (NOTIFY_EXEC) and the file watcher (NOTIFY_CREATE/CLOSE).
|
|
//
|
|
// Needs root, the ES entitlement and Full Disk Access:
|
|
// make macos-sign BINARY=builddir/tests/macos_es_live && sudo make macos-live-tests
|
|
// Without an Apple-granted entitlement this only runs with SIP disabled (a
|
|
// development VM). Prints SKIP and exits 0 when ES is not available.
|
|
|
|
#include "strawberry/exec_guard.hpp"
|
|
#include "strawberry/file_watch.hpp"
|
|
#include "strawberry/process_monitor.hpp"
|
|
|
|
#include <atomic>
|
|
#include <cassert>
|
|
#include <cerrno>
|
|
#include <chrono>
|
|
#include <condition_variable>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#include <mach-o/dyld.h>
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
|
|
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<std::size_t>(n));
|
|
offset += n;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
// Exec `path`; 0 on success, 100 + errno if execv failed.
|
|
int try_exec(const std::string& path) {
|
|
const pid_t child = ::fork();
|
|
if (child == 0) {
|
|
char* argv[] = {const_cast<char*>(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;
|
|
}
|
|
|
|
fs::path own_dir() {
|
|
char path[4096];
|
|
uint32_t size = sizeof(path);
|
|
if (::_NSGetExecutablePath(path, &size) != 0) {
|
|
return fs::current_path();
|
|
}
|
|
return fs::canonical(path).parent_path();
|
|
}
|
|
|
|
bool es_unavailable(const std::string& error) {
|
|
return error.find("Endpoint Security client failed") != std::string::npos;
|
|
}
|
|
|
|
template <typename Pred>
|
|
bool wait_for(Pred pred, std::chrono::milliseconds limit = std::chrono::milliseconds(5000)) {
|
|
const auto until = std::chrono::steady_clock::now() + limit;
|
|
while (std::chrono::steady_clock::now() < until) {
|
|
if (pred()) {
|
|
return true;
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(20));
|
|
}
|
|
return pred();
|
|
}
|
|
|
|
void test_exec_guard(const fs::path& bin) {
|
|
const auto evil = (bin / "es_live_evil").string();
|
|
const auto clean = (bin / "es_live_clean").string();
|
|
const auto slow = (bin / "es_live_slow").string();
|
|
|
|
std::mutex mutex;
|
|
std::string mode = "enforce";
|
|
std::vector<ExecDecision> decisions;
|
|
|
|
ExecGuardOptions options;
|
|
options.timeout = std::chrono::milliseconds(400);
|
|
options.mode = [&]() { std::lock_guard l(mutex); return mode; };
|
|
options.min_severity = []() { return std::string("critical"); };
|
|
options.assess = [](int fd, const std::string& path) {
|
|
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));
|
|
}
|
|
// /bin/echo stands in for a malicious platform binary.
|
|
if (content.find("EVIL-MARKER") != std::string::npos || path == "/bin/echo") {
|
|
a.top_rule = "Evil";
|
|
a.top_severity = "critical";
|
|
}
|
|
return a;
|
|
};
|
|
options.on_decision = [&](const ExecDecision& d) { std::lock_guard l(mutex); decisions.push_back(d); };
|
|
|
|
ExecGuard guard;
|
|
if (!guard.start(options)) {
|
|
const auto error = guard.stats().error;
|
|
if (es_unavailable(error)) {
|
|
std::cout << "SKIP: " << error << std::endl;
|
|
std::exit(0);
|
|
}
|
|
std::cerr << "FAIL: " << error << std::endl;
|
|
std::exit(1);
|
|
}
|
|
|
|
std::cout << "Enforce: malicious file is denied with EPERM" << std::endl;
|
|
assert(try_exec(evil) == 100 + EPERM);
|
|
std::cout << "Enforce: clean file runs" << std::endl;
|
|
assert(try_exec(clean) == 0);
|
|
std::cout << "Enforce: second attempt is served from cache" << std::endl;
|
|
assert(try_exec(evil) == 100 + EPERM);
|
|
assert(guard.stats().cache_hits >= 1);
|
|
std::cout << "Enforce: platform binary is never blocked (alert only)" << std::endl;
|
|
assert(try_exec("/bin/echo") == 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) == 0);
|
|
assert(std::chrono::steady_clock::now() - start < std::chrono::milliseconds(1500));
|
|
assert(guard.stats().timeouts >= 1);
|
|
|
|
{
|
|
std::lock_guard l(mutex);
|
|
mode = "audit";
|
|
}
|
|
std::cout << "Audit: malicious file runs, decision is would_block" << std::endl;
|
|
assert(try_exec(evil) == 0);
|
|
assert(wait_for([&]() {
|
|
std::lock_guard l(mutex);
|
|
for (const auto& d : decisions) {
|
|
if (d.path == evil && d.verdict == ExecVerdict::would_block) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}));
|
|
guard.stop();
|
|
std::cout << " exec guard: PASSED" << std::endl;
|
|
}
|
|
|
|
void test_process_monitor(const fs::path& bin) {
|
|
const auto clean = (bin / "es_live_clean").string();
|
|
ProcessMonitor monitor;
|
|
if (!monitor.initialize()) {
|
|
std::cerr << "FAIL: process monitor did not initialize" << std::endl;
|
|
std::exit(1);
|
|
}
|
|
if (monitor.backend_name() != "endpoint_security") {
|
|
std::cerr << "FAIL: expected the endpoint_security backend, got " << monitor.backend_name() << std::endl;
|
|
std::exit(1);
|
|
}
|
|
std::mutex mutex;
|
|
std::vector<ProcessInfo> seen;
|
|
assert(monitor.start([&](const ProcessInfo& info) {
|
|
std::lock_guard l(mutex);
|
|
seen.push_back(info);
|
|
}));
|
|
assert(try_exec(clean) == 0);
|
|
std::cout << "Exec telemetry: NOTIFY_EXEC reaches the callback with identity" << std::endl;
|
|
assert(wait_for([&]() {
|
|
std::lock_guard l(mutex);
|
|
for (const auto& info : seen) {
|
|
if (info.executable_path == clean && info.parent_pid == ::getpid() && info.start_time_ticks > 0
|
|
&& !info.image_identity.empty() && !info.boot_id.empty()) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}));
|
|
monitor.stop();
|
|
std::cout << " process monitor: PASSED" << std::endl;
|
|
}
|
|
|
|
void test_file_watch() {
|
|
const auto dir = fs::path("/private/var/tmp") / ("sb-es-watch-" + std::to_string(::getpid()));
|
|
fs::create_directories(dir);
|
|
std::mutex mutex;
|
|
std::vector<FileWriteEvent> events;
|
|
FileWatcher watcher;
|
|
std::vector<WatchSpec> specs{{dir.string(), "persistence", "test location", {}, true}};
|
|
if (!watcher.start(specs, [&](const FileWriteEvent& e) {
|
|
std::lock_guard l(mutex);
|
|
events.push_back(e);
|
|
})) {
|
|
std::cerr << "FAIL: " << watcher.error() << std::endl;
|
|
std::exit(1);
|
|
}
|
|
const auto target = dir / "com.example.agent.plist";
|
|
std::ofstream(target) << "<plist/>";
|
|
std::cout << "File watch: a write in a watched location is reported with the writer" << std::endl;
|
|
assert(wait_for([&]() {
|
|
std::lock_guard l(mutex);
|
|
for (const auto& e : events) {
|
|
if (e.path == target.string() && e.pid == ::getpid() && e.category == "persistence") {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}));
|
|
// Outside the watched directory: nothing.
|
|
const auto before = [&]() { std::lock_guard l(mutex); return events.size(); }();
|
|
std::ofstream(fs::path("/private/var/tmp") / ("sb-es-unwatched-" + std::to_string(::getpid()))) << "x";
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(300));
|
|
{
|
|
std::lock_guard l(mutex);
|
|
for (std::size_t i = before; i < events.size(); ++i) {
|
|
assert(events[i].path.starts_with(dir.string() + "/"));
|
|
}
|
|
}
|
|
watcher.stop();
|
|
std::error_code ec;
|
|
fs::remove_all(dir, ec);
|
|
fs::remove(fs::path("/private/var/tmp") / ("sb-es-unwatched-" + std::to_string(::getpid())), ec);
|
|
std::cout << " file watch: PASSED" << std::endl;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
if (::geteuid() != 0) {
|
|
std::cout << "SKIP: macos_es_live needs root" << std::endl;
|
|
return 0;
|
|
}
|
|
const auto bin = own_dir();
|
|
test_exec_guard(bin);
|
|
test_process_monitor(bin);
|
|
test_file_watch();
|
|
std::cout << "macos_es_live: ALL PASSED" << std::endl;
|
|
return 0;
|
|
}
|