From 6a91f30ce2e9ff7d19d222d5bdc7f858a922f78f Mon Sep 17 00:00:00 2001 From: Chris Punches Date: Sun, 23 Feb 2025 04:10:51 -0500 Subject: [PATCH] improved module validation --- include/ModuleLoader.hpp | 2 +- src/ModuleLoader.cpp | 25 ++++++++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/include/ModuleLoader.hpp b/include/ModuleLoader.hpp index fa140cd..15fef79 100644 --- a/include/ModuleLoader.hpp +++ b/include/ModuleLoader.hpp @@ -27,5 +27,5 @@ public: DPMError validate_module_interface(void* module_handle, std::vector& missing_symbols) const; private: - std::string module_path_; + std::string _module_path; }; \ No newline at end of file diff --git a/src/ModuleLoader.cpp b/src/ModuleLoader.cpp index 2dee215..4f0cec9 100644 --- a/src/ModuleLoader.cpp +++ b/src/ModuleLoader.cpp @@ -5,21 +5,21 @@ namespace fs = std::filesystem; ModuleLoader::ModuleLoader(std::string module_path) { try { - module_path_ = fs::absolute(module_path).string(); - if (!module_path_.empty() && module_path_.back() != '/') { - module_path_ += '/'; + _module_path = fs::absolute(module_path).string(); + if (!_module_path.empty() && _module_path.back() != '/') { + _module_path += '/'; } } catch (const fs::filesystem_error&) { - module_path_ = module_path; - if (!module_path_.empty() && module_path_.back() != '/') { - module_path_ += '/'; + _module_path = module_path; + if (!_module_path.empty() && _module_path.back() != '/') { + _module_path += '/'; } } } DPMError ModuleLoader::get_module_path(std::string& path) const { - path = module_path_; + path = _module_path; return DPMError::SUCCESS; } @@ -28,7 +28,7 @@ DPMError ModuleLoader::list_available_modules(std::vector& modules) modules.clear(); try { - for (const auto& entry : fs::directory_iterator(module_path_)) { + for (const auto& entry : fs::directory_iterator(_module_path)) { if (entry.is_regular_file()) { std::string filename = entry.path().filename().string(); if (filename.size() > 3 && filename.substr(filename.size() - 3) == ".so") { @@ -45,7 +45,7 @@ DPMError ModuleLoader::list_available_modules(std::vector& modules) DPMError ModuleLoader::load_module(const std::string& module_name, void*& module_handle) const { - std::string module_so_path = module_path_ + module_name + ".so"; + std::string module_so_path = _module_path + module_name + ".so"; module_handle = dlopen(module_so_path.c_str(), RTLD_LAZY); if (!module_handle) { @@ -65,6 +65,13 @@ DPMError ModuleLoader::execute_module(const std::string& module_name, const std: return load_error; } + std::vector missing_symbols; + DPMError validate_error = validate_module_interface(module_handle, missing_symbols); + if (validate_error != DPMError::SUCCESS) { + dlclose(module_handle); + return DPMError::INVALID_MODULE; + } + using ExecuteFn = int (*)(const char*, int, char**); ExecuteFn execute_fn = (ExecuteFn)dlsym(module_handle, "dpm_module_execute");