strawberry/tests/test_telemetry.cpp
trvon c452be93d4
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
Unify file ids, record uid/comm, redact command lines
File node ids: include/strawberry/node_ids.hpp is now the only place
they are built (file_<sha256> or file_path_<sha256 of path>). The three
modules disagreed: correlation used a 16-digit path hash and behavior
detections the raw path, so path-keyed evidence never met the rest. An
exec detection with a known hash also writes a same_path edge from the
path id to the content id. Source invariant R8 rejects hand-built file
ids in src/ and include/.

Processes carry the real uid and kernel comm: from the eBPF exec event
(which now includes the uid) or /proc. Stored on process nodes and in
process_exec telemetry (fields 14, 15).

Command lines are redacted before any storage (capture_redact_cmdline,
default on): sensitive flags and KEY=value pairs, Authorization/Bearer
credentials, URL passwords, tokens used as URL user names, well-known
token shapes (GitHub, GitLab, Slack, AWS key ids, sk-, JWTs) and mysql
-p<password>. The rules repeat to a fixed point, so redaction is
idempotent by construction. tests/test_redact.cpp covers the rules plus
idempotence, size and planted-secret properties; fuzz_cmdline_redactor
found two idempotence bugs and a leak (--password --token X) that are
fixed here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 17:58:48 -06:00

253 lines
10 KiB
C++

// Telemetry sink and records. A failure prints the seed; re-run with
// STRAWBERRY_PROPERTY_SEED=<seed>.
//
// C1 try_emit never blocks: producers finish with no consumer; emitted +
// dropped == attempted; a concurrent consumer receives every emitted
// event exactly once, in per-producer order
// records: every telemetry record round-trips (rule_match string hits capped); the command line is capped
#include "strawberry/telemetry.hpp"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <map>
#include <random>
#include <string>
#include <thread>
#include <vector>
using namespace strawberry;
namespace {
uint64_t seed_from_env() {
if (const char* text = std::getenv("STRAWBERRY_PROPERTY_SEED")) return std::strtoull(text, nullptr, 10);
return std::random_device{}();
}
const uint64_t kSeed = seed_from_env();
#define PROPERTY(cond) \
do { \
if (!(cond)) { \
std::cerr << "PROPERTY FAILED: " #cond " (seed " << kSeed << ", " << __FILE__ << ":" \
<< __LINE__ << ")" << std::endl; \
std::abort(); \
} \
} while (0)
std::string tag(int producer, int seq) {
return std::to_string(producer) + ":" + std::to_string(seq);
}
void test_never_blocks() {
TelemetrySink sink(1024);
constexpr int kProducers = 4, kEach = 50'000;
const auto start = std::chrono::steady_clock::now();
std::vector<std::thread> threads;
for (int p = 0; p < kProducers; ++p) {
threads.emplace_back([&sink, p] {
for (int i = 0; i < kEach; ++i) (void) sink.try_emit({store::RecordType::process_exec, tag(p, i)});
});
}
for (auto& t : threads) t.join();
PROPERTY(std::chrono::steady_clock::now() - start < std::chrono::seconds(10)); // nobody waited on a consumer
PROPERTY(sink.emitted() + sink.dropped() == static_cast<uint64_t>(kProducers) * kEach);
PROPERTY(sink.emitted() == sink.capacity()); // filled exactly, then dropped
std::vector<TelemetryEvent> out;
PROPERTY(sink.drain(out, 1'000'000) == sink.capacity());
PROPERTY(sink.approx_size() == 0);
std::cout << "C1 never blocks: PASSED (" << sink.dropped() << " dropped when full)" << std::endl;
}
void test_concurrent_exactly_once() {
std::mt19937_64 rng(kSeed);
TelemetrySink sink(256 << (rng() % 4));
constexpr int kProducers = 4, kEach = 40'000;
std::atomic<int> done{0};
std::vector<std::thread> threads;
for (int p = 0; p < kProducers; ++p) {
threads.emplace_back([&, p] {
for (int i = 0; i < kEach; ++i) {
while (!sink.try_emit({store::RecordType::scan_result, tag(p, i)})) {
std::this_thread::yield(); // test producer retries; the daemon drops instead
}
}
++done;
});
}
std::map<int, int> next; // per-producer expected sequence
uint64_t received = 0;
std::vector<TelemetryEvent> batch;
while (done.load() < kProducers || sink.approx_size() > 0) {
batch.clear();
sink.drain(batch, 512);
for (const auto& e : batch) {
PROPERTY(e.type == store::RecordType::scan_result);
const auto colon = e.payload.find(':');
const int p = std::atoi(e.payload.substr(0, colon).c_str());
const int seq = std::atoi(e.payload.substr(colon + 1).c_str());
PROPERTY(seq == next[p]); // in order, no gaps, no duplicates
++next[p];
++received;
}
}
for (auto& t : threads) t.join();
batch.clear();
sink.drain(batch, 1'000'000);
received += batch.size();
PROPERTY(received == static_cast<uint64_t>(kProducers) * kEach);
PROPERTY(sink.emitted() == received);
std::cout << "C1 concurrent exactly-once: PASSED" << std::endl;
}
void test_records() {
ProcessExecRecord e;
e.timestamp_ns = 1'800'000'000'000'000'000;
e.pid = 4242;
e.ppid = 1;
e.start_time_ticks = 99;
e.boot_id = "abc";
e.exe = "/usr/bin/x";
e.cmdline = std::string(10'000, 'a');
e.sha256 = std::string(64, 'f');
e.process_node_id = "process_4242_abc_99";
e.parent_node_id = "process_1_abc_1";
e.exe_deleted = true;
e.source = "live";
e.uid = 0;
e.comm = "bash";
ProcessExecRecord back;
PROPERTY(decode_process_exec(encode_process_exec(e), back));
PROPERTY(back.pid == 4242 && back.ppid == 1 && back.exe == e.exe && back.sha256 == e.sha256 && back.exe_deleted &&
!back.exe_memfd && back.source == "live" && back.timestamp_ns == e.timestamp_ns);
PROPERTY(back.cmdline.size() == kMaxTelemetryCmdline); // capped
PROPERTY(back.uid == 0 && back.comm == "bash");
int64_t ts = 0;
PROPERTY(store::record_timestamp(encode_process_exec(e), ts) && ts == e.timestamp_ns); // field-1 convention
ScanResultRecord s;
s.timestamp_ns = 5;
s.pid = 4242;
s.process_node_id = e.process_node_id;
s.outcome = ScanOutcome::too_large;
s.bytes = 0;
s.rules_generation = 3;
s.rules = {"A", "B"};
ScanResultRecord sb;
PROPERTY(decode_scan_result(encode_scan_result(s), sb));
PROPERTY(sb.outcome == ScanOutcome::too_large && sb.rules == s.rules && sb.rules_generation == 3);
PROPERTY(!decode_scan_result(std::string("\x28\x63", 2), sb)); // outcome 99: rejected
RuleMatchRecord rm;
rm.timestamp_ns = 6;
rm.pid = 4242;
rm.rule = "R";
rm.namespace_name = "ns";
rm.severity = "high";
rm.rules_generation = 3;
for (uint32_t i = 0; i < 100; ++i) rm.strings.push_back({"$a", i * 10ull, 7, 0xfedcba9876543210ull ^ i});
RuleMatchRecord rmb;
PROPERTY(decode_rule_match(encode_rule_match(rm), rmb));
PROPERTY(rmb.rule == "R" && rmb.namespace_name == "ns" && rmb.severity == "high" && rmb.rules_generation == 3);
PROPERTY(rmb.strings.size() == kMaxHitStringsPerRule * kMaxHitsPerString); // capped on encode
PROPERTY(std::equal(rmb.strings.begin(), rmb.strings.end(), rm.strings.begin()));
LabelRecord label;
label.timestamp_ns = 9;
label.polarity = LabelPolarity::retracted;
label.source = "operator";
label.action = "allowlist-remove";
label.uid = 0; // root is a uid, not "none"
label.allowlist_entry = "allow-1";
LabelRecord lb;
PROPERTY(decode_label(encode_label(label), lb));
PROPERTY(lb.polarity == LabelPolarity::retracted && lb.uid == 0 && lb.allowlist_entry == "allow-1" &&
lb.action == label.action && lb.sha256.empty());
label.uid = -1;
PROPERTY(decode_label(encode_label(label), lb) && lb.uid == -1);
PROPERTY(!decode_label(std::string("\x10\x04", 2), lb)); // polarity 4: rejected
RulesGenerationRecord rg;
rg.generation = 7;
rg.loaded = false;
rg.sources = {"=/etc/strawberry/rules", "f1=/var/lib/strawberry/feeds/f1/current"};
rg.error = "syntax error";
RulesGenerationRecord rgb;
PROPERTY(decode_rules_generation(encode_rules_generation(rg), rgb));
PROPERTY(rgb.generation == 7 && !rgb.loaded && rgb.sources == rg.sources && rgb.error == rg.error);
PolicyGenerationRecord pg{1, 3, std::string(64, 'a'), "allowlist-add", 1000};
PolicyGenerationRecord pgb;
PROPERTY(decode_policy_generation(encode_policy_generation(pg), pgb) && pgb.generation == 3 &&
pgb.digest == pg.digest && pgb.uid == 1000);
LifecycleRecord lc{2, LifecycleEvent::stop, "0.1.0", "boot", 123, "profile=balanced"};
LifecycleRecord lcb;
PROPERTY(decode_lifecycle(encode_lifecycle(lc), lcb) && lcb.event == LifecycleEvent::stop && lcb.pid == 123 &&
lcb.detail == lc.detail);
GovernorSampleRecord gs;
gs.level = 2;
gs.profile = "balanced";
gs.own_cpu_centi = 1234;
gs.io_some_centi = 0;
gs.admitted = {1, 2, 3};
gs.forced = {0, 0, 9};
GovernorSampleRecord gsb;
PROPERTY(decode_governor_sample(encode_governor_sample(gs), gsb));
PROPERTY(gsb.level == 2 && gsb.own_cpu_centi == 1234 && gsb.io_some_centi == 0 && gsb.cpu_some_centi == -1 &&
gsb.admitted == gs.admitted && gsb.forced == gs.forced && gsb.deferred.empty());
BehaviorEvent c;
c.type = BehaviorEvent::Type::connect;
c.pid = 7;
c.uid = 1000;
c.timestamp_ns = 11;
c.comm = "curl";
c.family = 2;
c.address = "203.0.113.9";
c.port = 443;
BehaviorEvent cb;
PROPERTY(decode_behavior(behavior_record_type(c.type), encode_behavior(c), cb));
PROPERTY(cb.type == c.type && cb.pid == 7 && cb.uid == 1000 && cb.address == c.address && cb.port == 443 &&
cb.comm == "curl" && cb.family == 2);
BehaviorEvent t;
t.type = BehaviorEvent::Type::ptrace;
t.pid = 8;
t.target_pid = 9;
t.request = 16;
PROPERTY(decode_behavior(store::RecordType::ptrace, encode_behavior(t), cb) && cb.target_pid == 9 && cb.request == 16);
PROPERTY(!decode_behavior(store::RecordType::scan_result, encode_behavior(t), cb)); // wrong type
FileWriteEvent w{12, "/etc/systemd/system/x.service", "persistence", "systemd unit", true, 13};
FileWriteEvent wb;
PROPERTY(decode_file_write(encode_file_write(w), wb) && wb.path == w.path && wb.system_scope && wb.pid == 12);
GuardObservation g;
g.pid = 14;
g.path = "/tmp/x";
g.verdict = ExecVerdict::would_block;
g.cache_hit = true;
g.top_rule = "R";
g.latency_us = 77;
GuardObservation gb;
uint64_t gts = 0;
PROPERTY(decode_guard_decision(encode_guard_decision(g, 15), gb, gts));
PROPERTY(gts == 15 && gb.verdict == ExecVerdict::would_block && gb.cache_hit && !gb.answered && gb.top_rule == "R" &&
gb.latency_us == 77);
std::cout << "records: PASSED" << std::endl;
}
} // namespace
int main() {
std::cout << "telemetry properties, seed " << kSeed << std::endl;
test_never_blocks();
test_concurrent_exactly_once();
test_records();
std::cout << "telemetry: all properties hold" << std::endl;
return 0;
}