From e0448535a3bc0b6632c75bf7bdd380e683b7fa31 Mon Sep 17 00:00:00 2001 From: Tony Tye Date: Sun, 10 Jan 2021 12:05:36 +0000 Subject: [PATCH] Use strncpy Use strncpy instead of strcpy to ensure the arrays will not be overflowered. Only copy one less than size of char array to leave a NUL character at the end even if the copy is truncated provided the original object is zeroed memory. Change-Id: I00f7679630cf28dcb9a51cb0aba2810a4f4c72b9 --- rocclr/device/gpu/gpudevice.cpp | 10 +++++----- rocclr/device/gpu/gslbe/src/rt/GSLDevice.cpp | 4 ++-- .../device/gpu/gslbe/src/rt/inifile/ini_export.cpp | 2 +- rocclr/device/pal/paldevice.cpp | 10 +++++----- rocclr/device/rocm/rocdevice.cpp | 14 +++++++------- rocclr/thread/monitor.cpp | 9 ++++----- 6 files changed, 24 insertions(+), 25 deletions(-) diff --git a/rocclr/device/gpu/gpudevice.cpp b/rocclr/device/gpu/gpudevice.cpp index 96c5a56e3c..2aed1afc2e 100644 --- a/rocclr/device/gpu/gpudevice.cpp +++ b/rocclr/device/gpu/gpudevice.cpp @@ -496,11 +496,11 @@ void NullDevice::fillDeviceInfo(const CALdeviceattribs& calAttr, const gslMemInf if ((calTarget() == CAL_TARGET_CARRIZO) && ASICREV_IS_CARRIZO_BRISTOL(calAttr.asicRevision)) { const static char* bristol = "Bristol Ridge"; - ::strcpy(info_.name_, bristol); + ::strncpy(info_.name_, bristol, sizeof(info_.name_) - 1); } else { - ::strcpy(info_.name_, hwInfo()->targetName_); + ::strncpy(info_.name_, hwInfo()->targetName_, sizeof(info_.name_) - 1); } - ::strcpy(info_.vendor_, "Advanced Micro Devices, Inc."); + ::strncpy(info_.vendor_, "Advanced Micro Devices, Inc.", sizeof(info_.vendor_) - 1); ::snprintf(info_.driverVersion_, sizeof(info_.driverVersion_) - 1, AMD_BUILD_STRING); info_.profile_ = "FULL_PROFILE"; @@ -536,7 +536,7 @@ void NullDevice::fillDeviceInfo(const CALdeviceattribs& calAttr, const gslMemInf info_.extensions_ = getExtensionString(); - ::strncpy(info_.driverStore_, calAttr.driverStore, sizeof(info_.driverStore_)); + ::strncpy(info_.driverStore_, calAttr.driverStore, sizeof(info_.driverStore_) - 1); // OpenCL1.2 device info fields info_.builtInKernels_ = ""; @@ -577,7 +577,7 @@ void NullDevice::fillDeviceInfo(const CALdeviceattribs& calAttr, const gslMemInf } if (settings().checkExtension(ClAmdDeviceAttributeQuery)) { - ::strncpy(info_.boardName_, calAttr.boardName, sizeof(info_.boardName_)); + ::strncpy(info_.boardName_, calAttr.boardName, sizeof(info_.boardName_) - 1); info_.deviceTopology_.pcie.type = CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD; info_.deviceTopology_.pcie.bus = (calAttr.pciTopologyInformation & (0xFF << 8)) >> 8; diff --git a/rocclr/device/gpu/gslbe/src/rt/GSLDevice.cpp b/rocclr/device/gpu/gslbe/src/rt/GSLDevice.cpp index 6b4d4dca46..4b6784f89d 100644 --- a/rocclr/device/gpu/gslbe/src/rt/GSLDevice.cpp +++ b/rocclr/device/gpu/gslbe/src/rt/GSLDevice.cpp @@ -143,10 +143,10 @@ CALGSLDevice::getAttribs_int(gsl::gsCtx* cs) m_attribs.pciTopologyInformation = m_adp->getLocationId(); const uint8* boardName = cs->getString(GSL_GS_RENDERER); - ::strncpy(m_attribs.boardName, (char*)boardName, CAL_ASIC_INFO_MAX_LEN * sizeof(char)); + ::strncpy(m_attribs.boardName, (char*)boardName, CAL_ASIC_INFO_MAX_LEN * sizeof(char) - 1); const uint8* driverStore = cs->getString(GSL_GS_DRIVER_STORE_PATH); - ::strncpy(m_attribs.driverStore, (char*)driverStore, CAL_DRIVER_STORE_MAX_LEN * sizeof(char)); + ::strncpy(m_attribs.driverStore, (char*)driverStore, CAL_DRIVER_STORE_MAX_LEN * sizeof(char) - 1); m_attribs.counterFreq = cs->getCounterFreq(); m_attribs.nanoSecondsPerTick = 1000000000.0 / cs->getCounterFreq(); diff --git a/rocclr/device/gpu/gslbe/src/rt/inifile/ini_export.cpp b/rocclr/device/gpu/gslbe/src/rt/inifile/ini_export.cpp index 2cfacd0713..9e8c24fab3 100644 --- a/rocclr/device/gpu/gslbe/src/rt/inifile/ini_export.cpp +++ b/rocclr/device/gpu/gslbe/src/rt/inifile/ini_export.cpp @@ -62,7 +62,7 @@ getConfigFromFile(gslStaticRuntimeConfig& scfg, // Check if location string is longer than 128 then assign, if not default location will be C:\packet.txt in gsl_ctx.cpp: gsCtxManager::PacketDump() uintp length = commandbufferDumpFilename.length(); if (length > 0 && length < sizeof(dcfg.CommandbufferDumpFilename)) - strcpy(dcfg.CommandbufferDumpFilename, commandbufferDumpFilename.c_str()); + ::strncpy(dcfg.CommandbufferDumpFilename, commandbufferDumpFilename.c_str(), sizeof(dcfg.CommandbufferDumpFilename) - 1); iniFile.getValue(section, CAL_ENABLEPATCHDUMP, (CALint*) &dcfg.nPatchDumpLevel.value); iniFile.getValue(section, CAL_ENABLEMACROTILE, (CALboolean*) ¯o); diff --git a/rocclr/device/pal/paldevice.cpp b/rocclr/device/pal/paldevice.cpp index 3758e080b9..6399f64bb8 100644 --- a/rocclr/device/pal/paldevice.cpp +++ b/rocclr/device/pal/paldevice.cpp @@ -512,7 +512,7 @@ void NullDevice::fillDeviceInfo(const Pal::DeviceProperties& palProp, info_.platform_ = AMD_PLATFORM; if (settings().useLightning_) { - ::strcpy(info_.name_, hwInfo()->machineTargetLC_); + ::strncpy(info_.name_, hwInfo()->machineTargetLC_, sizeof(info_.name_) - 1); if (hwInfo()->srameccSumpported_) { if (palProp.gfxipProperties.shaderCore.flags.eccProtectedGprs) { @@ -530,13 +530,13 @@ void NullDevice::fillDeviceInfo(const Pal::DeviceProperties& palProp, } } - ::strcpy(info_.targetId_, "amdgcn-amd-amdhsa--"); + ::strncpy(info_.targetId_, "amdgcn-amd-amdhsa--", sizeof(info_.targetId_) - 1); ::strcat(info_.targetId_, info_.name_); } else { - ::strcpy(info_.name_, hwInfo()->machineTarget_); + ::strncpy(info_.name_, hwInfo()->machineTarget_, sizeof(info_.name_) - 1); } - ::strcpy(info_.vendor_, "Advanced Micro Devices, Inc."); + ::strncpy(info_.vendor_, "Advanced Micro Devices, Inc.", sizeof(info_.vendor_) - 1); ::snprintf(info_.driverVersion_, sizeof(info_.driverVersion_) - 1, AMD_BUILD_STRING " (PAL%s)", settings().useLightning_ ? ",LC" : ",HSAIL"); @@ -614,7 +614,7 @@ void NullDevice::fillDeviceInfo(const Pal::DeviceProperties& palProp, if (settings().checkExtension(ClAmdDeviceAttributeQuery)) { ::strncpy(info_.boardName_, palProp.gpuName, - ::strnlen(palProp.gpuName, sizeof(info_.boardName_))); + ::strnlen(palProp.gpuName, sizeof(info_.boardName_) - 1)); info_.deviceTopology_.pcie.type = CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD; info_.deviceTopology_.pcie.bus = palProp.pciProperties.busNumber; diff --git a/rocclr/device/rocm/rocdevice.cpp b/rocclr/device/rocm/rocdevice.cpp index ebf99a8c8a..b5ea2da46b 100644 --- a/rocclr/device/rocm/rocdevice.cpp +++ b/rocclr/device/rocm/rocdevice.cpp @@ -145,13 +145,13 @@ bool NullDevice::create(const AMDDeviceInfo& deviceInfo) { } // Report the device name - ::strcpy(info_.name_, "AMD HSA Device"); + ::strncpy(info_.name_, "AMD HSA Device", sizeof(info_.name_) - 1); info_.extensions_ = getExtensionString(); info_.maxWorkGroupSize_ = hsaSettings->maxWorkGroupSize_; - ::strcpy(info_.vendor_, "Advanced Micro Devices, Inc."); + ::strncpy(info_.vendor_, "Advanced Micro Devices, Inc.", sizeof(info_.vendor_) - 1); info_.oclcVersion_ = "OpenCL C " OPENCL_C_VERSION_STR " "; info_.spirVersions_ = ""; - strcpy(info_.driverVersion_, "1.0 Provisional (hsa)"); + ::strncpy(info_.driverVersion_, "1.0 Provisional (hsa)", sizeof(info_.driverVersion_) - 1); info_.version_ = "OpenCL " OPENCL_VERSION_STR " "; return true; } @@ -1018,7 +1018,7 @@ bool Device::populateOCLDeviceConstants() { if (nullptr == gfxSubString) { return false; } - ::strcpy(info_.name_, gfxSubString); + ::strncpy(info_.name_, gfxSubString, sizeof(info_.name_) - 1); info_.gfxipMajor_ = deviceInfo_.gfxipMajor_; info_.gfxipMinor_ = deviceInfo_.gfxipMinor_; @@ -1028,7 +1028,7 @@ bool Device::populateOCLDeviceConstants() { if (HSA_STATUS_SUCCESS == hsa_agent_get_info(_bkendDevice, (hsa_agent_info_t)HSA_AMD_AGENT_INFO_PRODUCT_NAME, device_name)) { - ::strcpy(info_.boardName_, device_name); + ::strncpy(info_.boardName_, device_name, sizeof(info_.boardName_) - 1); } if (HSA_STATUS_SUCCESS != @@ -1236,7 +1236,7 @@ bool Device::populateOCLDeviceConstants() { info_.queueProperties_ = CL_QUEUE_PROFILING_ENABLE; info_.platform_ = AMD_PLATFORM; info_.profile_ = "FULL_PROFILE"; - strcpy(info_.vendor_, "Advanced Micro Devices, Inc."); + ::strncpy(info_.vendor_, "Advanced Micro Devices, Inc.", sizeof(info_.vendor_) - 1); info_.addressBits_ = LP64_SWITCH(32, 64); info_.maxSamplers_ = 16; @@ -1255,7 +1255,7 @@ bool Device::populateOCLDeviceConstants() { ss << AMD_BUILD_STRING " (HSA" << major << "." << minor << "," << (settings().useLightning_ ? "LC" : "HSAIL"); ss << ")"; - strcpy(info_.driverVersion_, ss.str().c_str()); + ::strncpy(info_.driverVersion_, ss.str().c_str(), sizeof(info_.driverVersion_) - 1); // Enable OpenCL 2.0 for Vega10+ if (deviceInfo_.gfxipMajor_ >= 9) { diff --git a/rocclr/thread/monitor.cpp b/rocclr/thread/monitor.cpp index b6ce28f76f..198873241b 100644 --- a/rocclr/thread/monitor.cpp +++ b/rocclr/thread/monitor.cpp @@ -32,15 +32,14 @@ namespace amd { Monitor::Monitor(const char* name, bool recursive) : contendersList_(0), onDeck_(0), waitersList_(NULL), owner_(NULL), recursive_(recursive) { - const size_t maxNameLen = sizeof(name_); if (name == NULL) { const char* unknownName = "@unknown@"; - assert(sizeof(unknownName) < maxNameLen && "just checking"); - strcpy(name_, unknownName); + assert(sizeof(unknownName) < sizeof(name_) && "just checking"); + ::strncpy(name_, unknownName, sizeof(name_) - 1); } else { - strncpy(name_, name, maxNameLen - 1); - name_[maxNameLen - 1] = '\0'; + ::strncpy(name_, name, sizeof(name_) - 1); } + name_[sizeof(name_) - 1] = '\0'; } bool Monitor::trySpinLock() {