Skip to content
API Documentation
BastionGuard™
RootGuardEngine.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <cstddef>
5#include <cstdint>
6#include <filesystem>
7#include <memory>
8#include <string>
9#include <unordered_set>
10#include <vector>
11#include <sys/stat.h>
12
14#include "rootguard/Policy.hpp"
16
17struct ring_buffer;
18struct rootguard_bpf;
19
21
24 std::size_t trustedLoaded{};
25 std::size_t deniedLoaded{};
26 std::size_t protectedLoaded{};
27 std::vector<std::string> warnings;
28};
29
30class RootGuardEngine final {
31public:
32 explicit RootGuardEngine(IEventSink& sink) noexcept;
34
39
40 [[nodiscard]] PolicyApplyReport start(const Policy& policy);
41 [[nodiscard]] PolicyApplyReport reloadPolicy(const Policy& policy);
42 void enterAuditMode();
43 int pollOnce(int timeoutMilliseconds) noexcept;
44 void runIntegrityCheckIfDue() noexcept;
45
46 void authorizeProcess(std::uint32_t tgid,
47 std::uint32_t targetEuid,
48 std::chrono::milliseconds ttl,
49 std::uint32_t issuerPid);
50
51private:
53 void operator()(rootguard_bpf* value) const noexcept;
54 };
55
57 void operator()(ring_buffer* value) const noexcept;
58 };
59
61 std::filesystem::path path;
63 std::uint32_t ruleId{};
64 std::uint32_t mode{};
65 std::uint32_t uid{};
66 std::uint32_t gid{};
67 bool drifted{};
68 std::uint32_t lastReasons{};
69 };
70
71 using SkeletonPtr = std::unique_ptr<rootguard_bpf, SkeletonDeleter>;
72 using RingBufferPtr = std::unique_ptr<ring_buffer, RingBufferDeleter>;
73
74 static int handleEvent(void* context, void* data, std::size_t size) noexcept;
75 [[nodiscard]] bool shouldIgnoreEvent(const rg_event& event) const noexcept;
76 [[nodiscard]] bool enrichEventPath(rg_event& event) const noexcept;
77 [[nodiscard]] bool enrichActorExecutable(rg_event& event) const noexcept;
78 [[nodiscard]] bool isTrustedApplicationEvent(const rg_event& event) const noexcept;
79 [[nodiscard]] PolicyApplyReport applyPolicy(const Policy& policy);
80 void setKernelConfig(Mode mode,
81 Action unknownAction,
82 bool logTrusted,
83 Action metadataAction,
84 Action globalMetadataAction,
85 Action credentialAction,
86 bool monitorGlobalPrivilegeMetadata,
87 bool ignoreRunGlobalMetadata,
88 bool detectGlobalMetadataBursts,
89 std::uint32_t globalMetadataSampleLimit,
90 std::uint32_t metadataBurstThreshold,
91 std::uint32_t metadataBurstWindowMs,
92 std::uint32_t metadataBurstCooldownMs);
93 static void clearMap(int mapFd);
95 std::uint32_t eventType,
96 std::uint32_t verdict,
97 std::uint32_t reasons,
98 const struct stat* current) noexcept;
99
103 std::vector<ProtectedSnapshot> protectedSnapshots_;
104 std::vector<PolicyPath> ignoredPaths_;
105 std::unordered_set<std::string> trustedApplicationKeys_;
106 std::unordered_set<std::string> trustedApplicationPaths_;
107 std::unordered_set<std::string> ignoredExactNames_;
108 bool logTrusted_{true};
109 bool blockUserHome_{false};
111 std::chrono::seconds integrityInterval_{0};
112 std::chrono::steady_clock::time_point nextIntegrityCheck_{};
113};
114
115} // namespace bastionguard::rootguard
void emitIntegrityEvent(ProtectedSnapshot &snapshot, std::uint32_t eventType, std::uint32_t verdict, std::uint32_t reasons, const struct stat *current) noexcept
bool enrichEventPath(rg_event &event) const noexcept
std::unordered_set< std::string > ignoredExactNames_
static int handleEvent(void *context, void *data, std::size_t size) noexcept
RootGuardEngine & operator=(const RootGuardEngine &)=delete
PolicyApplyReport reloadPolicy(const Policy &policy)
std::unordered_set< std::string > trustedApplicationPaths_
void authorizeProcess(std::uint32_t tgid, std::uint32_t targetEuid, std::chrono::milliseconds ttl, std::uint32_t issuerPid)
RootGuardEngine(RootGuardEngine &&)=delete
int pollOnce(int timeoutMilliseconds) noexcept
bool shouldIgnoreEvent(const rg_event &event) const noexcept
void setKernelConfig(Mode mode, Action unknownAction, bool logTrusted, Action metadataAction, Action globalMetadataAction, Action credentialAction, bool monitorGlobalPrivilegeMetadata, bool ignoreRunGlobalMetadata, bool detectGlobalMetadataBursts, std::uint32_t globalMetadataSampleLimit, std::uint32_t metadataBurstThreshold, std::uint32_t metadataBurstWindowMs, std::uint32_t metadataBurstCooldownMs)
std::chrono::steady_clock::time_point nextIntegrityCheck_
std::vector< ProtectedSnapshot > protectedSnapshots_
std::unordered_set< std::string > trustedApplicationKeys_
std::unique_ptr< ring_buffer, RingBufferDeleter > RingBufferPtr
std::unique_ptr< rootguard_bpf, SkeletonDeleter > SkeletonPtr
RootGuardEngine(const RootGuardEngine &)=delete
PolicyApplyReport start(const Policy &policy)
PolicyApplyReport applyPolicy(const Policy &policy)
bool isTrustedApplicationEvent(const rg_event &event) const noexcept
RootGuardEngine & operator=(RootGuardEngine &&)=delete
bool enrichActorExecutable(rg_event &event) const noexcept
void operator()(rootguard_bpf *value) const noexcept