Skip to content

Commit

Permalink
feat(box): check some needed kernel settings at runtime
Browse files Browse the repository at this point in the history
Signed-off-by: ComixHe <[email protected]>
  • Loading branch information
ComixHe committed Dec 26, 2024
1 parent f655205 commit 497b609
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions apps/ll-box/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,50 @@ Overload(Ts...) -> Overload<Ts...>;

int main(int argc, char **argv)
{
// detecting some kernel features at runtime to reporting errors friendly
std::error_code ec;

Check warning on line 680 in apps/ll-box/src/main.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Unused variable: ec
std::filesystem::path feature{ "/proc/sys/kernel/unprivileged_userns_clone" };

auto check = [](const std::filesystem::path &setting, int expected) {
// We assume that the fact that a file does not exist or that an error occurs during the
// detection process does not mean that the feature is disabled.
// Some distros may not enable some kernel features in default.
std::error_code ec;
if (!std::filesystem::exists(setting, ec)) {
return true;
}

std::ifstream stream{ setting };
if (!stream.is_open()) {
return true;
}

std::string content;
std::getline(stream, content);

try {
return std::stoi(content) == expected;
} catch (std::exception &e) {
logWan() << "ignore exception" << e.what() << "and continue"; // NOLINT
return true;
}
};

if (!check("/proc/sys/kernel/unprivileged_userns_clone", 1)) {
logErr() << "unprivileged_userns_clone is not enabled";
return EPERM;
}

if (!check("/proc/sys/kernel/apparmor_restrict_unprivileged_unconfined", 0)) {
logErr() << "apparmor_restrict_unprivileged_unconfined is not disabled";
return EPERM;
}

if (!check("/proc/sys/kernel/apparmor_restrict_unprivileged_userns", 0)) {
logErr() << "apparmor_restrict_unprivileged_userns is not disabled";
return EPERM;
}

if (argc == 1) {
logErr() << "please specify a command";
return -1;
Expand Down

0 comments on commit 497b609

Please sign in to comment.