strawberry/include/strawberry/daemon_app.hpp
trvon c77bc79dc7
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 / macos (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
Privacy: macOS tracker source via Little Snitch log-traffic (WS2 macOS)
Completes the tracker-attribution transport on macOS. There is no network
sandbox to work around there, so the daemon (root) reads Little Snitch's traffic
stream directly and feeds the same on_opensnitch_connection consumer — no relay
or ingest socket (those are the Linux sandbox answer).

- LittleSnitchParser (cross-platform, unit-tested): parses `littlesnitch
  log-traffic` CSV via a header-name->index map (tolerates column reordering),
  yielding the shared redacted OpenSnitchConnection for outbound rows only
  (program, uid, host/ip/port, protocol 6->tcp/17->udp). Little Snitch's CSV has
  no argv/env, so redaction is structural. split_csv_line handles quoted fields.
- LittleSnitchSource (macOS only): posix_spawns `littlesnitch log-traffic
  --stream`, reads the pipe on a guarded thread, parses and calls back; stop()
  SIGTERMs the CLI (EOF -> loop ends) then reaps it.
- Daemon: start_tracker_attribution gains an __APPLE__ branch that starts the
  source; shutdown/status handle it. New config littlesnitch_cli (default CLI
  path). status privacy.listener source "little-snitch".
- docs: macOS section rewritten (needs Little Snitch "Allow access via Terminal").

Test E10 covers the parser (header, outbound/inbound, deny, quoted exe, no-dest).
Linux suite 51/51; analyzer + clang-tidy clean (Linux view). The __APPLE__ code
compiles/validates on the Mac next (ssh); live spawn needs root + LS Terminal access.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-28 18:54:27 -06:00

258 lines
10 KiB
C++

#ifndef STRAWBERRY_DAEMON_APP_HPP
#define STRAWBERRY_DAEMON_APP_HPP
#include "strawberry/config.hpp"
#include "strawberry/learn/inspection.hpp"
#include "strawberry/opensnitch_events.hpp"
#include "strawberry/tracker_set.hpp"
#include <nlohmann/json_fwd.hpp>
#include "strawberry/process_monitor.hpp"
#include <atomic>
#include <condition_variable>
#include <cstdint>
#include <chrono>
#include <deque>
#include <mutex>
#include <optional>
#include <vector>
#include <thread>
#include <unordered_map>
#include <functional>
#include <memory>
#include <string>
namespace strawberry {
class ControlServer;
class ExecGuard;
class BehaviorEngine;
class InstalledFiles;
class TelemetrySink;
class RetroHunter;
class InspectionExecutor;
namespace learn {
class ShadowScorer;
}
enum class LifecycleEvent : uint8_t;
class FileWatcher;
class IocNetSet;
class DetectionPipeline;
class PolicyStore;
class ResourceGovernor;
struct Containment;
struct DetectionEvent;
class ProcessMonitor;
struct ProcessInfo;
/**
* @brief Long-running daemon wrapper around process monitoring + detection.
*
* This class owns the runtime pipeline and monitor wiring used by the
* production `strawberry` binary and daemon-level integration tests.
*/
class StrawberryDaemon {
public:
StrawberryDaemon();
~StrawberryDaemon();
StrawberryDaemon(const StrawberryDaemon&) = delete;
StrawberryDaemon& operator=(const StrawberryDaemon&) = delete;
/**
* @brief Load config and initialize daemon-owned subsystems.
*/
bool initialize(const std::string& config_path);
/**
* @brief Run until request_stop() is called or the predicate returns false.
*/
void run(const std::function<bool()>& should_continue = {});
/**
* @brief Request a graceful stop.
*/
void request_stop();
/**
* @brief Check whether initialize() succeeded.
*/
bool is_initialized() const;
/**
* @brief Check whether run() is currently active.
*/
bool is_running() const;
/**
* @brief Current daemon status as a JSON object (served on the control socket).
*/
std::string status_json() const;
std::string feeds_status_json() const;
private:
void shutdown();
void start_control_server();
void enqueue(const ProcessInfo& info);
// OpenSnitch response actions.
std::string opensnitch_lists_dir() const;
void sync_blocklists();
// Privacy: tracker attribution (advisory only, never blocks;
// docs/tracker-attribution.md). Lists reload with the blocklists.
void reload_tracker_lists();
void start_tracker_attribution();
void on_opensnitch_connection(const OpenSnitchConnection& connection) noexcept;
std::string privacy_status_json() const;
std::string privacy_trackers_json() const;
mutable std::mutex tracker_mutex_;
std::shared_ptr<const TrackerSet> tracker_lists_; // guarded by tracker_mutex_
std::vector<std::string> tracker_feeds_; // guarded by tracker_mutex_
std::string tracker_error_; // guarded by tracker_mutex_
TrackerContacts tracker_contacts_;
std::unique_ptr<TrackerIngestServer> tracker_ingest_; // Linux: fed by the host-ns relay
#if defined(__APPLE__)
std::unique_ptr<LittleSnitchSource> littlesnitch_source_; // macOS: reads littlesnitch log-traffic directly
#endif
void on_detection(const DetectionEvent& event);
std::optional<std::string> contain(const std::string& sha256, const std::string& md5, const std::string& path,
const std::string& reason, const std::string& severity, bool automatic,
unsigned uid, std::string& error);
void release_containments(const std::vector<Containment>& removed);
std::string opensnitch_status_json() const;
void apply_prevention_mode();
void start_behavior_monitoring();
bool queue_memory_scan(int pid, const std::string& reason, bool force = false,
std::optional<learn::InspectionDecision> inspection = std::nullopt);
void start_inspection_executor();
void record_inspection_outcome(const learn::InspectionOutcome& outcome) noexcept;
void memory_scan_loop();
std::optional<std::string> handle_response_verb(const std::string& verb, const nlohmann::json& payload,
unsigned uid, std::string& error);
std::string behavior_status_json() const;
std::string store_status_json() const;
std::string resources_status_json() const;
std::string timings_status_json() const;
std::string prevention_status_json() const;
void worker_loop(bool inventory);
void stop_worker();
Config config_;
std::string config_path_ = "/etc/strawberry/strawberry.conf";
std::unique_ptr<ResourceGovernor> governor_;
std::unique_ptr<DetectionPipeline> pipeline_;
std::unique_ptr<ProcessMonitor> monitor_;
std::unique_ptr<ControlServer> control_server_;
std::unique_ptr<ExecGuard> exec_guard_;
std::unique_ptr<BehaviorEngine> behavior_;
std::unique_ptr<InstalledFiles> installed_files_; // installer-written unit files
std::unique_ptr<FileWatcher> file_watcher_;
std::unique_ptr<IocNetSet> ioc_nets_;
mutable std::mutex ioc_nets_mutex_;
std::mutex flagged_mutex_;
std::unordered_map<int, std::string> flagged_; // pid -> process node id with high/critical findings
mutable std::mutex memory_queue_mutex_;
std::condition_variable memory_queue_cv_;
struct MemoryScanJob {
int pid;
std::string reason;
bool force;
std::optional<learn::InspectionDecision> inspection;
};
std::deque<MemoryScanJob> memory_queue_;
std::thread memory_worker_;
// Telemetry: producers emit into the sink; one writer thread stores it.
std::unique_ptr<TelemetrySink> telemetry_;
std::unique_ptr<RetroHunter> retro_hunter_;
std::string retro_hunt_status_json() const;
std::string doctor_report_json();
std::optional<std::string> run_maintenance(const nlohmann::json& payload, std::string& error);
std::thread telemetry_worker_;
// Shadow mode (telemetry writer thread only, except stats()).
std::unique_ptr<learn::ShadowScorer> shadow_;
std::unique_ptr<InspectionExecutor> inspection_executor_;
std::atomic<uint64_t> promoted_generation_{0};
std::atomic<bool> promoted_active_{false};
int64_t shadow_model_mtime_ns_ = -1;
int64_t promoted_model_mtime_ns_ = -1;
std::string promoted_model_error_; // guarded by shadow_error_mutex_
std::string shadow_model_error_; // guarded by shadow_error_mutex_
mutable std::mutex shadow_error_mutex_;
void refresh_shadow_model();
// The live rarity model, kept across restarts in model_dir/rarity.state
// (loaded at start, saved hourly and at stop by the telemetry writer).
void load_rarity_state();
void save_rarity_state() noexcept;
std::string rarity_load_error_; // guarded by shadow_error_mutex_
std::string rarity_save_error_; // guarded by shadow_error_mutex_
std::atomic<int64_t> rarity_saved_ns_{0};
std::string model_status_json() const;
std::atomic<bool> telemetry_stop_{false};
std::atomic<uint64_t> telemetry_written_{0};
std::atomic<uint64_t> telemetry_gaps_{0};
void telemetry_loop();
/// Encode-and-emit wrapper for producers on any thread; never throws.
template <typename Encode>
void emit_telemetry(Encode&& encode) noexcept;
void stop_telemetry();
void record_lifecycle(LifecycleEvent event) noexcept;
void emit_governor_sample() noexcept;
void record_policy_change(const std::string& verb, const std::string& payload, unsigned uid,
const std::string& result) noexcept;
std::string telemetry_status_json() const;
bool memory_stop_ = false;
std::string exec_guard_error_;
std::unique_ptr<PolicyStore> policy_;
std::string policy_error_;
// Exec events are queued so the eBPF ring buffer keeps draining while
// images are hashed and scanned on the worker thread.
static constexpr std::size_t kMaxQueuedEvents = 4096;
// Inventory (startup / rescan) waits behind live execs and is paced; it is
// re-queued, never dropped, while the host is busy.
static constexpr std::size_t kMaxInventoryEvents = 65536;
std::deque<ProcessInfo> inventory_queue_;
std::atomic<std::size_t> inventory_depth_{0};
std::atomic<uint64_t> memory_scans_skipped_{0};
std::atomic<uint64_t> worker_errors_{0}; // events that threw (logged, skipped)
// Failed rule reloads back off instead of retrying every loop tick.
std::chrono::steady_clock::time_point next_reload_attempt_{};
std::chrono::seconds reload_backoff_{0};
mutable std::mutex queue_mutex_;
std::condition_variable queue_cv_;
struct QueuedEvent {
ProcessInfo info;
std::chrono::steady_clock::time_point queued_at;
};
std::deque<QueuedEvent> queue_;
std::thread worker_;
std::thread inventory_worker_;
// Large uncached images are scanned off the live path: one scan per image,
// later execs of the same image wait on it instead of rescanning.
static constexpr uint64_t kLargeImageBytes = 8ull * 1024 * 1024;
static constexpr std::size_t kMaxCoalescedPerImage = 256;
std::thread large_worker_;
std::condition_variable large_cv_;
std::deque<std::string> large_queue_;
std::unordered_map<std::string, std::vector<ProcessInfo>> large_pending_;
std::atomic<uint64_t> large_coalesced_{0};
bool large_stop_ = false; // set after the live worker drained
void large_scan_loop();
std::condition_variable inventory_cv_;
bool worker_stop_ = false;
std::atomic<uint64_t> events_dropped_{0};
std::atomic<std::size_t> queue_depth_{0};
std::mutex blocklist_mutex_;
std::string blocklist_summary_ = "{}";
int64_t started_at_ns_ = 0;
std::atomic<bool> initialized_{false};
std::atomic<bool> running_{false};
std::atomic<bool> stop_requested_{false};
};
} // namespace strawberry
#endif // STRAWBERRY_DAEMON_APP_HPP