Skip to content
API Documentation
BastionGuard™
bastionguard::rootguard::anonymous_namespace{RootGuardEngine.cpp} Namespace Reference

Classes

struct  ResolvedRule
struct  PreparedProtected

Functions

std::runtime_error systemError (const char *operation)
std::uint64_t monotonicNanoseconds ()
std::string fileKeyString (const rg_file_key &key)
bool pathStartsWith (const std::filesystem::path &path, const std::filesystem::path &prefix) noexcept
bool isIgnoredPath (const Policy &policy, const std::filesystem::path &candidate) noexcept
bool isUserHomePath (const std::filesystem::path &candidate) noexcept
bool identityMatches (const std::filesystem::path &candidate, const rg_file_key &expected) noexcept
std::string readLink (const std::filesystem::path &path) noexcept
void copyString (char *destination, const std::size_t destinationSize, const std::string &source) noexcept

Variables

constexpr std::size_t kMaximumProtectedInodes = 8'000

Function Documentation

◆ copyString()

void bastionguard::rootguard::anonymous_namespace{RootGuardEngine.cpp}::copyString ( char * destination,
const std::size_t destinationSize,
const std::string & source )
noexcept

Definition at line 128 of file RootGuardEngine.cpp.

130{
131 if (!destination || destinationSize == 0)
132 return;
133 const auto count = std::min(destinationSize - 1, source.size());
134 std::memcpy(destination, source.data(), count);
135 destination[count] = '\0';
136}

References copyString().

Referenced by copyString().

◆ fileKeyString()

std::string bastionguard::rootguard::anonymous_namespace{RootGuardEngine.cpp}::fileKeyString ( const rg_file_key & key)
nodiscard

Definition at line 57 of file RootGuardEngine.cpp.

58{
59 return std::to_string(key.device) + ":" + std::to_string(key.inode);
60}

References rg_file_key::device, fileKeyString(), and rg_file_key::inode.

Referenced by fileKeyString().

◆ identityMatches()

bool bastionguard::rootguard::anonymous_namespace{RootGuardEngine.cpp}::identityMatches ( const std::filesystem::path & candidate,
const rg_file_key & expected )
noexcept

Definition at line 108 of file RootGuardEngine.cpp.

110{
111 struct stat metadata {};
112 if (::stat(candidate.c_str(), &metadata) != 0)
113 return false;
114 return FileIdentityResolver::kernelDeviceNumber(metadata.st_dev) == expected.device &&
115 static_cast<std::uint64_t>(metadata.st_ino) == expected.inode;
116}
static __u64 kernelDeviceNumber(dev_t encodedDevice)

References identityMatches(), and bastionguard::rootguard::FileIdentityResolver::kernelDeviceNumber().

Referenced by identityMatches().

◆ isIgnoredPath()

bool bastionguard::rootguard::anonymous_namespace{RootGuardEngine.cpp}::isIgnoredPath ( const Policy & policy,
const std::filesystem::path & candidate )
noexcept

Definition at line 79 of file RootGuardEngine.cpp.

81{
82 try {
83 const auto normalized = candidate.lexically_normal();
84 for (const auto& rule : policy.ignoredPaths) {
85 const auto ignored = rule.path.lexically_normal();
86 if (rule.recursive ? pathStartsWith(normalized, ignored)
87 : normalized == ignored)
88 return true;
89 }
90 } catch (...) {
91 }
92 return false;
93}
bool pathStartsWith(const std::filesystem::path &path, const std::filesystem::path &prefix) noexcept
std::vector< PolicyPath > ignoredPaths
Definition Policy.hpp:59

References isIgnoredPath(), and pathStartsWith().

Referenced by isIgnoredPath().

◆ isUserHomePath()

bool bastionguard::rootguard::anonymous_namespace{RootGuardEngine.cpp}::isUserHomePath ( const std::filesystem::path & candidate)
noexcept

Definition at line 96 of file RootGuardEngine.cpp.

97{
98 try {
99 const auto normalized = candidate.lexically_normal();
100 return pathStartsWith(normalized, "/home") ||
101 pathStartsWith(normalized, "/root") ||
102 pathStartsWith(normalized, "/var/home");
103 } catch (...) {
104 return false;
105 }
106}

References isUserHomePath(), and pathStartsWith().

Referenced by isUserHomePath().

◆ monotonicNanoseconds()

std::uint64_t bastionguard::rootguard::anonymous_namespace{RootGuardEngine.cpp}::monotonicNanoseconds ( )
nodiscard

Definition at line 48 of file RootGuardEngine.cpp.

49{
50 timespec time {};
51 if (::clock_gettime(CLOCK_MONOTONIC, &time) != 0)
52 throw systemError("clock_gettime");
53 return static_cast<std::uint64_t>(time.tv_sec) * 1'000'000'000ULL +
54 static_cast<std::uint64_t>(time.tv_nsec);
55}

References monotonicNanoseconds(), and systemError().

Referenced by monotonicNanoseconds().

◆ pathStartsWith()

bool bastionguard::rootguard::anonymous_namespace{RootGuardEngine.cpp}::pathStartsWith ( const std::filesystem::path & path,
const std::filesystem::path & prefix )
noexcept

Definition at line 62 of file RootGuardEngine.cpp.

64{
65 const auto normalizedPath = path.lexically_normal();
66 const auto normalizedPrefix = prefix.lexically_normal();
67
68 auto pathIterator = normalizedPath.begin();
69 for (auto prefixIterator = normalizedPrefix.begin();
70 prefixIterator != normalizedPrefix.end();
71 ++prefixIterator, ++pathIterator) {
72 if (pathIterator == normalizedPath.end() ||
73 *pathIterator != *prefixIterator)
74 return false;
75 }
76 return true;
77}

References pathStartsWith().

Referenced by isIgnoredPath(), isUserHomePath(), and pathStartsWith().

◆ readLink()

std::string bastionguard::rootguard::anonymous_namespace{RootGuardEngine.cpp}::readLink ( const std::filesystem::path & path)
noexcept

Definition at line 118 of file RootGuardEngine.cpp.

119{
120 char buffer[PATH_MAX + 1] {};
121 const ssize_t count = ::readlink(path.c_str(), buffer, PATH_MAX);
122 if (count <= 0)
123 return {};
124 buffer[count] = '\0';
125 return std::string(buffer, static_cast<std::size_t>(count));
126}

References readLink().

Referenced by readLink().

◆ systemError()

std::runtime_error bastionguard::rootguard::anonymous_namespace{RootGuardEngine.cpp}::systemError ( const char * operation)
nodiscard

Definition at line 43 of file RootGuardEngine.cpp.

44{
45 return std::runtime_error(std::string(operation) + ": " + std::strerror(errno));
46}

References systemError().

Referenced by monotonicNanoseconds(), and systemError().

Variable Documentation

◆ kMaximumProtectedInodes

std::size_t bastionguard::rootguard::anonymous_namespace{RootGuardEngine.cpp}::kMaximumProtectedInodes = 8'000
constexpr

Definition at line 41 of file RootGuardEngine.cpp.