From 51a3d2fbbb9d82baa913a54c68cd212c29727e48 Mon Sep 17 00:00:00 2001 From: Nav Date: Wed, 5 Oct 2022 20:58:25 +0100 Subject: [PATCH] Tidied Process class and moved Application::isRunningAsRoot() function to Process class --- src/Application.cpp | 4 --- src/Application.hpp | 7 ----- src/Helpers/Process.cpp | 31 ++++++++++++++----- src/Helpers/Process.hpp | 22 ++++++++++++- .../TargetControllerComponent.cpp | 6 ++-- 5 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index c11b7771..b2e4c3f1 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -87,10 +87,6 @@ namespace Bloom return EXIT_SUCCESS; } - bool Application::isRunningAsRoot() { - return geteuid() == 0; - } - std::map> Application::getCommandHandlersByCommandName() { return std::map> { { diff --git a/src/Application.hpp b/src/Application.hpp index 5f2b8e4d..d0476bbb 100644 --- a/src/Application.hpp +++ b/src/Application.hpp @@ -44,13 +44,6 @@ namespace Bloom */ int run(); - /** - * Checks if the current effective user running Bloom has root privileges. - * - * @return - */ - static bool isRunningAsRoot(); - private: std::vector arguments; diff --git a/src/Helpers/Process.cpp b/src/Helpers/Process.cpp index 66fc29e2..8c342c3d 100644 --- a/src/Helpers/Process.cpp +++ b/src/Helpers/Process.cpp @@ -4,6 +4,8 @@ #include #include +#include "src/Exceptions/Exception.hpp" + namespace Bloom { ::pid_t Process::getProcessId() { @@ -14,6 +16,26 @@ namespace Bloom return getppid(); } + ::uid_t Process::getEffectiveUserId(std::optional<::pid_t> processId) { + if (!processId.has_value()) { + processId = Process::getProcessId(); + } + + const auto processInfo = Process::getProcessInfo(processId.value()); + + if (!processInfo) { + throw Exceptions::Exception( + "Failed to fetch process info for process ID " + std::to_string(processId.value()) + ); + } + + return static_cast<::uid_t>(processInfo->euid); + } + + bool Process::isRunningAsRoot(std::optional<::pid_t> processId) { + return Process::getEffectiveUserId(processId) == 0; + } + bool Process::isManagedByClion(std::optional<::pid_t> parentProcessId) { if (!parentProcessId.has_value()) { parentProcessId = Process::getParentProcessId(); @@ -48,16 +70,11 @@ namespace Bloom } Process::Proc Process::getProcessInfo(::pid_t processId) { - auto proc = std::unique_ptr<::PROCTAB, decltype(&::closeproc)>( + const auto proc = std::unique_ptr<::PROCTAB, decltype(&::closeproc)>( ::openproc(PROC_FILLSTAT | PROC_FILLARG | PROC_PID, &processId), ::closeproc ); - auto processInfo = Proc(::readproc(proc.get(), NULL), ::freeproc); - if (processInfo == NULL) { - return Proc(nullptr, ::freeproc); - } - - return processInfo; + return Proc(::readproc(proc.get(), NULL), ::freeproc); } } diff --git a/src/Helpers/Process.hpp b/src/Helpers/Process.hpp index 48a0b13b..e432ea55 100644 --- a/src/Helpers/Process.hpp +++ b/src/Helpers/Process.hpp @@ -2,6 +2,7 @@ #include #include +#include #include namespace Bloom @@ -23,6 +24,26 @@ namespace Bloom */ static ::pid_t getParentProcessId(); + /** + * Returns the effective user ID of the given process. + * + * @param processId + * If not provided, this function will use the current process ID. + * + * @return + */ + static ::uid_t getEffectiveUserId(std::optional<::pid_t> processId = std::nullopt); + + /** + * Returns true if the given process is running as root. + * + * @param processId + * If not provided, this function will perform the check against the current process. + * + * @return + */ + static bool isRunningAsRoot(std::optional<::pid_t> processId = std::nullopt); + /** * Returns true if the given process is managed by CLion. * @@ -33,7 +54,6 @@ namespace Bloom */ static bool isManagedByClion(std::optional<::pid_t> parentProcessId = std::nullopt); - private: using Proc = std::unique_ptr<::proc_t, decltype(&::freeproc)>; static Proc getProcessInfo(::pid_t processId); diff --git a/src/TargetController/TargetControllerComponent.cpp b/src/TargetController/TargetControllerComponent.cpp index 84a7c22b..89e6bc20 100644 --- a/src/TargetController/TargetControllerComponent.cpp +++ b/src/TargetController/TargetControllerComponent.cpp @@ -5,8 +5,10 @@ #include #include -#include "src/Application.hpp" +#include "Responses/Error.hpp" + #include "src/Helpers/Paths.hpp" +#include "src/Helpers/Process.hpp" #include "src/Logger/Logger.hpp" #include "src/TargetController/Exceptions/DeviceFailure.hpp" @@ -446,7 +448,7 @@ namespace Bloom::TargetController Logger::warning("Bloom udev rules missing - attempting installation"); // We can only install them if we're running as root - if (!Application::isRunningAsRoot()) { + if (!Process::isRunningAsRoot()) { Logger::error("Bloom udev rules missing - cannot install udev rules without root privileges.\n" "Running Bloom once with root privileges will allow it to automatically install the udev rules. " "Alternatively, instructions on manually installing the udev rules can be found "