Security improvements

Improvements include
* adding additional build flags that warn about stack-smashing
and type conversion errors
* run-time checks for valid function input values and adquate
space for the result of arithmetic operations.
* make sure default case for switch statements do something
besides just assert
* disable using env. var. debugging in release mode

Change-Id: I5f048310c5c56e05d9ec31bcc273404d6a0dd646
This commit is contained in:
Chris Freehill
2020-01-14 15:37:52 -06:00
parent fe4f7ed4a1
commit d00b9ac07d
11 changed files with 278 additions and 89 deletions
+13 -10
View File
@@ -461,6 +461,10 @@ static const std::map<const char *, dev_depends_t> kDevFuncDependsMap = {
Device::Device(std::string p, RocmSMI_env_vars const *e) : path_(p), env_(e) {
monitor_ = nullptr;
#ifdef NDEBUG
env_ = nullptr;
#endif
// Get the device name
size_t i = path_.rfind('/', path_.length());
std::string dev = path_.substr(i + 1, path_.length() - i);
@@ -486,6 +490,7 @@ template <typename T>
int Device::openSysfsFileStream(DevInfoTypes type, T *fs, const char *str) {
auto sysfs_path = path_;
#ifdef DEBUG
if (env_->path_DRM_root_override && type == env_->enum_override) {
sysfs_path = env_->path_DRM_root_override;
@@ -493,6 +498,7 @@ int Device::openSysfsFileStream(DevInfoTypes type, T *fs, const char *str) {
sysfs_path += ".write";
}
}
#endif
sysfs_path += "/device/";
sysfs_path += kDevAttribNameMap.at(type);
@@ -545,11 +551,8 @@ int Device::writeDevInfoStr(DevInfoTypes type, std::string valStr) {
return ret;
}
try {
fs << valStr;
} catch (...) {
std::cout << "Write to file threw exception" << std::endl;
}
// We'll catch any exceptions in rocm_smi.cc code.
fs << valStr;
fs.close();
return 0;
@@ -581,7 +584,7 @@ int Device::writeDevInfo(DevInfoTypes type, uint64_t val) {
break;
default:
break;
return EINVAL;
}
return -1;
@@ -599,7 +602,7 @@ int Device::writeDevInfo(DevInfoTypes type, std::string val) {
return writeDevInfoStr(type, val);
default:
break;
return EINVAL;
}
return -1;
@@ -709,7 +712,7 @@ int Device::readDevInfo(DevInfoTypes type, uint64_t *val) {
break;
default:
return -1;
return EINVAL;
}
return 0;
}
@@ -734,7 +737,7 @@ int Device::readDevInfo(DevInfoTypes type, std::vector<std::string> *val) {
break;
default:
return -1;
return EINVAL;
}
return 0;
@@ -759,7 +762,7 @@ int Device::readDevInfo(DevInfoTypes type, std::string *val) {
break;
default:
return -1;
return EINVAL;
}
return 0;
}