Adjust the implementation of strncpy_s() for Linux

This change:
- prevents writing memory out of the range of the destination buffer
- prevents calling strlen() with non-null terminated c-string
- corrects the logic, which validates passed range to proceed
when real length fits the destination buffer

Related-To: NEO-7264
Signed-off-by: Wrobel, Patryk <patryk.wrobel@intel.com>
This commit is contained in:
Wrobel, Patryk
2022-08-17 15:46:39 +00:00
committed by Compute-Runtime-Automation
parent 5bdf758049
commit dda5b19859
8 changed files with 88 additions and 71 deletions

View File

@@ -51,7 +51,7 @@ bool LinuxFirmwareImp::isFirmwareSupported(void) {
void LinuxFirmwareImp::osGetFwProperties(zes_firmware_properties_t *pProperties) {
if (ZE_RESULT_SUCCESS != getFirmwareVersion(osFwType, pProperties)) {
strncpy_s(static_cast<char *>(pProperties->version), ZES_STRING_PROPERTY_SIZE, unknown.c_str(), ZES_STRING_PROPERTY_SIZE);
strncpy_s(static_cast<char *>(pProperties->version), ZES_STRING_PROPERTY_SIZE, unknown.c_str(), ZES_STRING_PROPERTY_SIZE - 1);
}
pProperties->canControl = true; //Assuming that user has permission to flash the firmware
}

View File

@@ -14,9 +14,9 @@ ze_result_t LinuxFirmwareImp::getFirmwareVersion(std::string fwType, zes_firmwar
std::string fwVersion;
ze_result_t result = pFwInterface->getFwVersion(fwType, fwVersion);
if (ZE_RESULT_SUCCESS == result) {
strncpy_s(static_cast<char *>(pProperties->version), ZES_STRING_PROPERTY_SIZE, fwVersion.c_str(), ZES_STRING_PROPERTY_SIZE);
strncpy_s(static_cast<char *>(pProperties->version), ZES_STRING_PROPERTY_SIZE, fwVersion.c_str(), ZES_STRING_PROPERTY_SIZE - 1);
}
return result;
}
} // namespace L0
} // namespace L0

View File

@@ -43,15 +43,15 @@ ze_result_t LinuxFirmwareImp::getFirmwareVersion(std::string fwType, zes_firmwar
// not able to read PSC version from iaf.x
return result;
}
strncpy_s(static_cast<char *>(pProperties->version), ZES_STRING_PROPERTY_SIZE, pscVersion.c_str(), ZES_STRING_PROPERTY_SIZE);
strncpy_s(static_cast<char *>(pProperties->version), ZES_STRING_PROPERTY_SIZE, pscVersion.c_str(), ZES_STRING_PROPERTY_SIZE - 1);
return result;
}
ze_result_t result = pFwInterface->getFwVersion(fwType, fwVersion);
if (result == ZE_RESULT_SUCCESS) {
strncpy_s(static_cast<char *>(pProperties->version), ZES_STRING_PROPERTY_SIZE, fwVersion.c_str(), ZES_STRING_PROPERTY_SIZE);
strncpy_s(static_cast<char *>(pProperties->version), ZES_STRING_PROPERTY_SIZE, fwVersion.c_str(), ZES_STRING_PROPERTY_SIZE - 1);
}
return result;
}
} // namespace L0
} // namespace L0