Skip to content
API Documentation
BastionGuard™
bastionguard::rootguard::AutomaticResponseSink Class Referencefinal

#include <AutomaticResponseSink.hpp>

Inheritance diagram for bastionguard::rootguard::AutomaticResponseSink:
bastionguard::rootguard::IEventSink

Classes

struct  PendingService

Public Member Functions

 AutomaticResponseSink (IEventSink &downstream, bool autoBlockSystemServices)
 ~AutomaticResponseSink () override
 AutomaticResponseSink (const AutomaticResponseSink &)=delete
AutomaticResponseSink & operator= (const AutomaticResponseSink &)=delete
void onEvent (const rg_event &event) noexcept override
void setAutoBlockSystemServices (bool enabled) noexcept
Public Member Functions inherited from bastionguard::rootguard::IEventSink
virtual ~IEventSink ()=default

Private Member Functions

void workerLoop () noexcept
void enqueueService (const std::filesystem::path &path, const rg_event &source) noexcept
void emitServiceResult (const std::filesystem::path &path, const rg_event &source, bool success) noexcept

Private Attributes

IEventSink & downstream_
std::atomic< bool > autoBlockSystemServices_ {true}
std::mutex mutex_
std::condition_variable condition_
std::deque< PendingService > queue_
std::set< std::string > queuedPaths_
bool stopping_ {}
std::thread worker_

Detailed Description

Definition at line 16 of file AutomaticResponseSink.hpp.

Constructor & Destructor Documentation

◆ AutomaticResponseSink() [1/2]

bastionguard::rootguard::AutomaticResponseSink::AutomaticResponseSink ( IEventSink & downstream,
bool autoBlockSystemServices )
explicit

◆ ~AutomaticResponseSink()

bastionguard::rootguard::AutomaticResponseSink::~AutomaticResponseSink ( )
override

Definition at line 56 of file AutomaticResponseSink.cpp.

57{
58 {
59 std::lock_guard lock(mutex_);
60 stopping_ = true;
61 }
62 condition_.notify_one();
63 if (worker_.joinable())
64 worker_.join();
65}

References condition_, mutex_, stopping_, and worker_.

◆ AutomaticResponseSink() [2/2]

bastionguard::rootguard::AutomaticResponseSink::AutomaticResponseSink ( const AutomaticResponseSink & )
delete

Member Function Documentation

◆ emitServiceResult()

void bastionguard::rootguard::AutomaticResponseSink::emitServiceResult ( const std::filesystem::path & path,
const rg_event & source,
bool success )
privatenoexcept

Definition at line 132 of file AutomaticResponseSink.cpp.

135{
136 try {
137 rg_event event = source;
139 event.verdict = success ? RG_VERDICT_BLOCKED : RG_VERDICT_ALERT;
140 copyString(event.comm, sizeof(event.comm), "rootguard-react");
141 copyString(event.filename, sizeof(event.filename), path.string());
142 downstream_.onEvent(event);
143 } catch (...) {
144 }
145}
virtual void onEvent(const rg_event &event) noexcept=0
void copyString(char *destination, const std::size_t size, const std::string &source)
@ RG_EVENT_SERVICE_BLOCKED
@ RG_VERDICT_ALERT
@ RG_VERDICT_BLOCKED

References downstream_, rg_event::event_type, RG_EVENT_SERVICE_BLOCKED, RG_VERDICT_ALERT, and RG_VERDICT_BLOCKED.

Referenced by operator=(), and workerLoop().

◆ enqueueService()

void bastionguard::rootguard::AutomaticResponseSink::enqueueService ( const std::filesystem::path & path,
const rg_event & source )
privatenoexcept

Definition at line 97 of file AutomaticResponseSink.cpp.

99{
100 try {
101 const std::string key = path.lexically_normal().string();
102 std::lock_guard lock(mutex_);
103 if (stopping_ || !queuedPaths_.insert(key).second)
104 return;
105 queue_.push_back(PendingService{path, source});
106 condition_.notify_one();
107 } catch (...) {
108 }
109}

References condition_, mutex_, queue_, queuedPaths_, and stopping_.

Referenced by onEvent(), and operator=().

◆ onEvent()

void bastionguard::rootguard::AutomaticResponseSink::onEvent ( const rg_event & event)
overridevirtualnoexcept

Implements bastionguard::rootguard::IEventSink.

Definition at line 72 of file AutomaticResponseSink.cpp.

73{
74 try {
75 downstream_.onEvent(event);
76
77 if (!autoBlockSystemServices_.load(std::memory_order_acquire) ||
79 return;
80
81 const std::filesystem::path path(event.filename);
82 const auto target = identifySystemService(path);
83 if (!target)
84 return;
85
86 /*
87 * The LSM denied the operation, therefore the service definition
88 * remains unchanged. Stopping it cannot execute attacker-controlled
89 * replacement content.
90 */
91 enqueueService(path, event);
92 } catch (...) {
93 // Automatic reaction must never terminate RootGuard.
94 }
95}
void enqueueService(const std::filesystem::path &path, const rg_event &source) noexcept
std::optional< ServiceTarget > identifySystemService(const std::filesystem::path &path) noexcept
char filename[RG_FILENAME_LEN]

References autoBlockSystemServices_, downstream_, enqueueService(), and bastionguard::rootguard::identifySystemService().

Referenced by operator=().

◆ operator=()

AutomaticResponseSink & bastionguard::rootguard::AutomaticResponseSink::operator= ( const AutomaticResponseSink & )
delete

◆ setAutoBlockSystemServices()

void bastionguard::rootguard::AutomaticResponseSink::setAutoBlockSystemServices ( bool enabled)
noexcept

Definition at line 67 of file AutomaticResponseSink.cpp.

68{
69 autoBlockSystemServices_.store(enabled, std::memory_order_release);
70}

References autoBlockSystemServices_.

Referenced by main(), and operator=().

◆ workerLoop()

void bastionguard::rootguard::AutomaticResponseSink::workerLoop ( )
privatenoexcept

Definition at line 111 of file AutomaticResponseSink.cpp.

112{
113 for (;;) {
114 PendingService pending;
115 {
116 std::unique_lock lock(mutex_);
117 condition_.wait(lock, [this] { return stopping_ || !queue_.empty(); });
118 if (stopping_ && queue_.empty())
119 break;
120 pending = std::move(queue_.front());
121 queue_.pop_front();
122 }
123
124 const auto result = blockSystemService(pending.path);
125 emitServiceResult(pending.path, pending.source, result.success);
126
127 std::lock_guard lock(mutex_);
128 queuedPaths_.erase(pending.path.lexically_normal().string());
129 }
130}
void emitServiceResult(const std::filesystem::path &path, const rg_event &source, bool success) noexcept
ServiceActionResult blockSystemService(const std::filesystem::path &path) noexcept

References bastionguard::rootguard::blockSystemService(), condition_, emitServiceResult(), mutex_, bastionguard::rootguard::AutomaticResponseSink::PendingService::path, queue_, queuedPaths_, bastionguard::rootguard::AutomaticResponseSink::PendingService::source, and stopping_.

Referenced by AutomaticResponseSink(), and operator=().

Member Data Documentation

◆ autoBlockSystemServices_

std::atomic<bool> bastionguard::rootguard::AutomaticResponseSink::autoBlockSystemServices_ {true}
private

◆ condition_

std::condition_variable bastionguard::rootguard::AutomaticResponseSink::condition_
private

Definition at line 44 of file AutomaticResponseSink.hpp.

Referenced by enqueueService(), workerLoop(), and ~AutomaticResponseSink().

◆ downstream_

IEventSink& bastionguard::rootguard::AutomaticResponseSink::downstream_
private

Definition at line 41 of file AutomaticResponseSink.hpp.

Referenced by AutomaticResponseSink(), emitServiceResult(), and onEvent().

◆ mutex_

std::mutex bastionguard::rootguard::AutomaticResponseSink::mutex_
private

Definition at line 43 of file AutomaticResponseSink.hpp.

Referenced by enqueueService(), workerLoop(), and ~AutomaticResponseSink().

◆ queue_

std::deque<PendingService> bastionguard::rootguard::AutomaticResponseSink::queue_
private

Definition at line 45 of file AutomaticResponseSink.hpp.

Referenced by enqueueService(), and workerLoop().

◆ queuedPaths_

std::set<std::string> bastionguard::rootguard::AutomaticResponseSink::queuedPaths_
private

Definition at line 46 of file AutomaticResponseSink.hpp.

Referenced by enqueueService(), and workerLoop().

◆ stopping_

bool bastionguard::rootguard::AutomaticResponseSink::stopping_ {}
private

Definition at line 47 of file AutomaticResponseSink.hpp.

Referenced by enqueueService(), workerLoop(), and ~AutomaticResponseSink().

◆ worker_

std::thread bastionguard::rootguard::AutomaticResponseSink::worker_
private

Definition at line 48 of file AutomaticResponseSink.hpp.

Referenced by AutomaticResponseSink(), and ~AutomaticResponseSink().


The documentation for this class was generated from the following files: