Skip to content
API Documentation
BastionGuard™
bastionguard::rootguard Namespace Reference

Namespaces

namespace  anonymous_namespace{AutomaticResponseSink.cpp}
namespace  anonymous_namespace{ConsoleEventSink.cpp}
namespace  anonymous_namespace{JsonEventSink.cpp}
namespace  anonymous_namespace{PolicyLoader.cpp}
namespace  anonymous_namespace{PolicySecurity.cpp}
namespace  anonymous_namespace{RootGuardEngine.cpp}
namespace  anonymous_namespace{SystemServiceControl.cpp}

Classes

class  AutomaticResponseSink
class  ConsoleEventSink
struct  FileIdentity
class  FileIdentityResolver
class  IEventSink
class  JsonEventSink
class  MultiEventSink
struct  Policy
struct  PolicyApplyReport
class  PolicyLoader
struct  PolicyPath
class  PolicySecurity
class  RootGuardEngine
struct  ServiceActionResult
struct  ServiceTarget

Enumerations

enum class  Mode : std::uint32_t { Audit = 0 , Enforce = 1 }
enum class  Action : std::uint32_t { Audit = 0 , Block = 1 }
enum class  InitSystem {
  None , Systemd , OpenRC , Dinit ,
  SysVInit
}

Functions

const char * toString (Mode mode) noexcept
const char * toString (Action action) noexcept
InitSystem detectInitSystem () noexcept
const char * toString (InitSystem init) noexcept
std::optional< ServiceTarget > identifySystemService (const std::filesystem::path &path) noexcept
ServiceActionResult blockSystemService (const std::filesystem::path &path) noexcept
ServiceActionResult unblockSystemService (const std::filesystem::path &path) noexcept

Enumeration Type Documentation

◆ Action

enum class bastionguard::rootguard::Action : std::uint32_t
strong
Enumerator
Audit 
Block 

Definition at line 15 of file Policy.hpp.

15 : std::uint32_t {
16 Audit = 0,
17 Block = 1,
18};

◆ InitSystem

◆ Mode

enum class bastionguard::rootguard::Mode : std::uint32_t
strong
Enumerator
Audit 
Enforce 

Definition at line 10 of file Policy.hpp.

10 : std::uint32_t {
11 Audit = 0,
12 Enforce = 1,
13};

Function Documentation

◆ blockSystemService()

ServiceActionResult bastionguard::rootguard::blockSystemService ( const std::filesystem::path & path)
nodiscardnoexcept

Definition at line 215 of file SystemServiceControl.cpp.

216{
217 const auto target = identifySystemService(path);
218 if (!target)
219 return {false, "The path is not a recognized system service definition."};
220 if (target->name == "bastionguard-rootguard.service" ||
221 target->name == "bastionguard-rootguard")
222 return {false, "RootGuard refuses to block its own service."};
223
224 std::vector<CommandResult> results;
225 switch (target->init) {
226 case InitSystem::Systemd: {
227 const auto systemctl = firstExecutable({"/usr/bin/systemctl", "/bin/systemctl"});
228 if (systemctl.empty())
229 return {false, "systemctl is unavailable."};
230 results.push_back(runCommand({systemctl, "stop", target->name}));
231 results.push_back(runCommand({systemctl, "mask", "--runtime", target->name}));
232 break;
233 }
234 case InitSystem::OpenRC: {
235 const auto rcService = firstExecutable({"/sbin/rc-service", "/usr/bin/rc-service"});
236 if (rcService.empty())
237 return {false, "rc-service is unavailable."};
238 results.push_back(runCommand({rcService, target->name, "stop"}));
239 break;
240 }
241 case InitSystem::Dinit: {
242 const auto dinitctl = firstExecutable({"/usr/bin/dinitctl", "/bin/dinitctl"});
243 if (dinitctl.empty())
244 return {false, "dinitctl is unavailable."};
245 results.push_back(runCommand({dinitctl, "stop", target->name}));
246 break;
247 }
249 results.push_back(runCommand({target->definition.string(), "stop"}));
250 break;
251 case InitSystem::None:
252 return {false, "No supported init system was detected."};
253 }
254 return resultFrom("Blocked", *target, results);
255}
CommandResult runCommand(const std::vector< std::string > &arguments) noexcept
std::string firstExecutable(std::initializer_list< const char * > candidates)
ServiceActionResult resultFrom(const std::string &operation, const ServiceTarget &target, const std::vector< CommandResult > &results)
std::optional< ServiceTarget > identifySystemService(const std::filesystem::path &path) noexcept

References blockSystemService(), Dinit, identifySystemService(), None, OpenRC, Systemd, and SysVInit.

Referenced by blockSystemService(), main(), anonymous_namespace{ActionHelper.cpp}::quarantineFile(), and bastionguard::rootguard::AutomaticResponseSink::workerLoop().

◆ detectInitSystem()

InitSystem bastionguard::rootguard::detectInitSystem ( )
nodiscardnoexcept

Definition at line 148 of file SystemServiceControl.cpp.

149{
150 try {
151 if (std::filesystem::exists("/run/systemd/system") &&
152 !firstExecutable({"/usr/bin/systemctl", "/bin/systemctl"}).empty())
153 return InitSystem::Systemd;
154 if (!firstExecutable({"/sbin/openrc-run", "/usr/bin/openrc-run"}).empty() &&
155 !firstExecutable({"/sbin/rc-service", "/usr/bin/rc-service"}).empty())
156 return InitSystem::OpenRC;
157 if (!firstExecutable({"/usr/bin/dinitctl", "/bin/dinitctl"}).empty())
158 return InitSystem::Dinit;
159 if (std::filesystem::exists("/etc/init.d"))
161 } catch (...) {
162 }
163 return InitSystem::None;
164}

References detectInitSystem(), Dinit, None, OpenRC, Systemd, and SysVInit.

Referenced by detectInitSystem(), and identifySystemService().

◆ identifySystemService()

std::optional< ServiceTarget > bastionguard::rootguard::identifySystemService ( const std::filesystem::path & path)
nodiscardnoexcept

Definition at line 179 of file SystemServiceControl.cpp.

180{
181 try {
182 if (input.empty() || !input.is_absolute())
183 return std::nullopt;
184 const auto path = input.lexically_normal();
185 const auto init = detectInitSystem();
186
187 const bool systemdPath =
188 startsWithPath(path, "/etc/systemd/system") ||
189 startsWithPath(path, "/usr/lib/systemd/system") ||
190 startsWithPath(path, "/lib/systemd/system");
191 if (systemdPath && init == InitSystem::Systemd) {
192 if (const auto unit = systemdUnitName(path))
193 return ServiceTarget{InitSystem::Systemd, *unit, path};
194 }
195
196 if (startsWithPath(path, "/etc/conf.d") &&
197 init == InitSystem::OpenRC && path.has_filename())
198 return ServiceTarget{InitSystem::OpenRC, path.filename().string(), path};
199
200 if (startsWithPath(path, "/etc/init.d") && path.has_filename()) {
201 const auto detected = init == InitSystem::OpenRC
203 return ServiceTarget{detected, path.filename().string(), path};
204 }
205
206 if ((startsWithPath(path, "/etc/dinit.d") ||
207 startsWithPath(path, "/usr/lib/dinit.d")) &&
208 init == InitSystem::Dinit && path.has_filename())
209 return ServiceTarget{InitSystem::Dinit, path.filename().string(), path};
210 } catch (...) {
211 }
212 return std::nullopt;
213}
bool startsWithPath(const std::filesystem::path &path, const std::filesystem::path &prefix) noexcept
InitSystem detectInitSystem() noexcept

References detectInitSystem(), Dinit, identifySystemService(), OpenRC, Systemd, and SysVInit.

Referenced by blockSystemService(), identifySystemService(), main(), bastionguard::rootguard::AutomaticResponseSink::onEvent(), anonymous_namespace{ActionHelper.cpp}::quarantineFile(), and unblockSystemService().

◆ toString() [1/3]

const char * bastionguard::rootguard::toString ( Action action)
nodiscardnoexcept

Definition at line 11 of file Policy.cpp.

12{
13 return action == Action::Block ? "block" : "audit";
14}

References Block, and toString().

◆ toString() [2/3]

const char * bastionguard::rootguard::toString ( InitSystem init)
nodiscardnoexcept

Definition at line 166 of file SystemServiceControl.cpp.

167{
168 switch (init) {
169 case InitSystem::Systemd: return "systemd";
170 case InitSystem::OpenRC: return "openrc";
171 case InitSystem::Dinit: return "dinit";
172 case InitSystem::SysVInit: return "sysvinit";
173 case InitSystem::None: return "none";
174 }
175 return "none";
176}

References Dinit, None, OpenRC, Systemd, SysVInit, and toString().

◆ toString() [3/3]

const char * bastionguard::rootguard::toString ( Mode mode)
nodiscardnoexcept

◆ unblockSystemService()

ServiceActionResult bastionguard::rootguard::unblockSystemService ( const std::filesystem::path & path)
nodiscardnoexcept

Definition at line 257 of file SystemServiceControl.cpp.

258{
259 const auto target = identifySystemService(path);
260 if (!target)
261 return {true, "The path is not a system service definition."};
262
263 std::vector<CommandResult> results;
264 switch (target->init) {
265 case InitSystem::Systemd: {
266 const auto systemctl = firstExecutable({"/usr/bin/systemctl", "/bin/systemctl"});
267 if (systemctl.empty())
268 return {false, "systemctl is unavailable."};
269 results.push_back(runCommand({systemctl, "unmask", "--runtime", target->name}));
270 break;
271 }
275 return {true, std::string("The service remains stopped; no runtime mask is used by ") +
276 toString(target->init) + "."};
277 case InitSystem::None:
278 return {false, "No supported init system was detected."};
279 }
280 return resultFrom("Unblocked", *target, results);
281}
const char * toString(Mode mode) noexcept
Definition Policy.cpp:6

References Dinit, identifySystemService(), None, OpenRC, Systemd, SysVInit, toString(), and unblockSystemService().

Referenced by main(), anonymous_namespace{ActionHelper.cpp}::restoreFile(), and unblockSystemService().