From ae94ce125280c7efc9cc4046dd2d8c2d459f0f74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20=C4=8Cerm=C3=A1k?= Date: Tue, 10 Dec 2024 14:37:42 +0000 Subject: [PATCH] Fix null pointer dereference in SPD --- ResourceManager.cpp | 42 +++++++++++++++++++++++++++++++++++++----- ResourceManager.h | 4 ++++ SPDAccessor.cpp | 10 ++++++++-- SPDAccessor.h | 2 +- 4 files changed, 50 insertions(+), 8 deletions(-) diff --git a/ResourceManager.cpp b/ResourceManager.cpp index cf9c8263..7fc1cfcc 100644 --- a/ResourceManager.cpp +++ b/ResourceManager.cpp @@ -1124,7 +1124,7 @@ void ResourceManager::DetectDevicesThreadFunction() LOG_INFO("------------------------------------------------------"); LOG_INFO("| Detecting I2C DIMM modules |"); LOG_INFO("------------------------------------------------------"); - for(unsigned int bus = 0; bus < busses.size(); bus++) + for(unsigned int bus = 0; bus < busses.size() && IsAnyDimmDetectorEnabled(detector_settings); bus++) { IF_DRAM_SMBUS(busses[bus]->pci_vendor, busses[bus]->pci_device) { @@ -1175,7 +1175,7 @@ void ResourceManager::DetectDevicesThreadFunction() /*-------------------------------------------------*\ | Update detection percent | \*-------------------------------------------------*/ - percent = ((float)i2c_detector_idx + 1.0f) / percent_denominator; + percent = (i2c_device_detectors.size() + i2c_detector_idx + 1.0f) / percent_denominator; detection_percent = (unsigned int)(percent * 100.0f); } @@ -1223,7 +1223,7 @@ void ResourceManager::DetectDevicesThreadFunction() /*-------------------------------------------------*\ | Update detection percent | \*-------------------------------------------------*/ - percent = (i2c_device_detectors.size() + i2c_detector_idx + 1.0f) / percent_denominator; + percent = (i2c_device_detectors.size() + i2c_dimm_device_detectors.size() + i2c_detector_idx + 1.0f) / percent_denominator; detection_percent = (unsigned int)(percent * 100.0f); } @@ -1378,7 +1378,7 @@ void ResourceManager::DetectDevicesThreadFunction() \*-------------------------------------------------*/ hid_device_count++; - percent = (i2c_device_detectors.size() + i2c_pci_device_detectors.size() + hid_device_count) / percent_denominator; + percent = (i2c_device_detectors.size() + i2c_dimm_device_detectors.size() + i2c_pci_device_detectors.size() + hid_device_count) / percent_denominator; detection_percent = (unsigned int)(percent * 100.0f); @@ -1491,7 +1491,7 @@ void ResourceManager::DetectDevicesThreadFunction() \*-------------------------------------------------*/ hid_device_count++; - percent = (i2c_device_detectors.size() + i2c_pci_device_detectors.size() + hid_device_count) / percent_denominator; + percent = (i2c_device_detectors.size() + i2c_dimm_device_detectors.size() + i2c_pci_device_detectors.size() + hid_device_count) / percent_denominator; detection_percent = percent * 100.0f; @@ -1729,6 +1729,21 @@ void ResourceManager::UpdateDetectorSettings() } } + /*-------------------------------------------------*\ + | Loop through all I2C DIMM detectors and see | + | if any need to be saved to the settings | + \*-------------------------------------------------*/ + for(unsigned int i2c_detector_idx = 0; i2c_detector_idx < (unsigned int)i2c_dimm_device_detectors.size(); i2c_detector_idx++) + { + detection_string = i2c_dimm_device_detectors[i2c_detector_idx].name.c_str(); + + if(!(detector_settings.contains("detectors") && detector_settings["detectors"].contains(detection_string))) + { + detector_settings["detectors"][detection_string] = true; + save_settings = true; + } + } + /*-------------------------------------------------*\ | Loop through all I2C PCI detectors and see if any | | need to be saved to the settings | @@ -1822,3 +1837,20 @@ void ResourceManager::WaitForDeviceDetection() DetectDeviceMutex.lock(); DetectDeviceMutex.unlock(); } + +bool ResourceManager::IsAnyDimmDetectorEnabled(json &detector_settings) +{ + for(unsigned int i2c_detector_idx = 0; i2c_detector_idx < i2c_dimm_device_detectors.size() && detection_is_required.load(); i2c_detector_idx++) + { + detection_string = i2c_dimm_device_detectors[i2c_detector_idx].name.c_str(); + /*-------------------------------------------------*\ + | Check if this detector is enabled | + \*-------------------------------------------------*/ + if(detector_settings.contains("detectors") && detector_settings["detectors"].contains(detection_string) && + detector_settings["detectors"][detection_string] == false) + { + return false; + } + } + return true; +} diff --git a/ResourceManager.h b/ResourceManager.h index feeb2677..3b2956c6 100644 --- a/ResourceManager.h +++ b/ResourceManager.h @@ -23,6 +23,9 @@ #include "hidapi_wrapper.h" #include "i2c_smbus.h" #include "filesystem.h" +#include "json.hpp" + +using json = nlohmann::json; #define HID_INTERFACE_ANY -1 #define HID_USAGE_ANY -1 @@ -231,6 +234,7 @@ private: void InitThreadFunction(); bool ProcessPreDetection(); void ProcessPostDetection(); + bool IsAnyDimmDetectorEnabled(json &detector_settings); /*-------------------------------------------------------------------------------------*\ | Static pointer to shared instance of ResourceManager | diff --git a/SPDAccessor.cpp b/SPDAccessor.cpp index 400a1f10..82f4aee4 100644 --- a/SPDAccessor.cpp +++ b/SPDAccessor.cpp @@ -95,7 +95,10 @@ void SPDDetector::detect_memory_type() else { mem_type = (SPDMemoryType) value; - valid = true; + // We are only interested in DDR4 and DDR5 systems + valid = (mem_type == SPD_DDR4_SDRAM || mem_type == SPD_DDR4E_SDRAM || + mem_type == SPD_LPDDR4_SDRAM || mem_type == SPD_LPDDR4X_SDRAM || + mem_type == SPD_DDR5_SDRAM || mem_type == SPD_LPDDR5_SDRAM); } return; } @@ -122,7 +125,10 @@ i2c_smbus_interface *SPDDetector::smbus() const SPDWrapper::SPDWrapper(const SPDWrapper &wrapper) { - this->accessor = wrapper.accessor->copy(); + if(wrapper.accessor != nullptr) + { + this->accessor = wrapper.accessor->copy(); + } this->address = wrapper.address; this->mem_type = wrapper.mem_type; } diff --git a/SPDAccessor.h b/SPDAccessor.h index dfb935d1..29399aca 100644 --- a/SPDAccessor.h +++ b/SPDAccessor.h @@ -103,7 +103,7 @@ class SPDWrapper uint16_t jedec_id(); private: - SPDAccessor *accessor; + SPDAccessor *accessor = nullptr; uint8_t address; SPDMemoryType mem_type; };