Browse Source

Fix unsafe codes in Debugger

tags/v1.0.0
maning202007 5 years ago
parent
commit
82bad1ab84
2 changed files with 39 additions and 4 deletions
  1. +36
    -4
      mindspore/ccsrc/debug/debugger/debugger.cc
  2. +3
    -0
      mindspore/ccsrc/debug/debugger/debugger.h

+ 36
- 4
mindspore/ccsrc/debug/debugger/debugger.cc View File

@@ -24,6 +24,7 @@
#include <cstring>
#include <utility>
#include <map>
#include <regex>
#include "debug/debugger/debugger.h"
#include "debug/data_dump/dump_json_parser.h"
#include "pipeline/jit/pipeline.h"
@@ -98,8 +99,21 @@ void Debugger::EnableDebugger() {
const char *env_host_str = std::getenv("MS_DEBUGGER_HOST");
std::string host;
if (env_host_str != nullptr) {
MS_LOG(INFO) << "Getenv MS_DEBUGGER_HOST: " << env_host_str;
host = std::string(env_host_str);
std::regex reg_ip(
"(25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])"
"[.](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])"
"[.](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])"
"[.](25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])");
std::smatch smat;
std::string host_str = std::string(env_host_str);
if (std::regex_match(host_str, smat, reg_ip)) {
MS_LOG(INFO) << "Getenv MS_DEBUGGER_HOST: " << env_host_str;
host = std::string(env_host_str);
} else {
MS_LOG(ERROR) << "Environment variable MS_DEBUGGER_HOST isn't a valid IP address. "
"Please set environment variable MS_DEBUGGER_HOST=x.x.x.x to a valid IP";
debugger_enabled_ = false;
}
} else {
MS_LOG(INFO) << "Environment variable MS_DEBUGGER_HOST doesn't exist. Using default debugger host: localhost";
host = "localhost";
@@ -108,8 +122,13 @@ void Debugger::EnableDebugger() {
const char *env_port_str = std::getenv("MS_DEBUGGER_PORT");
std::string port;
if (env_port_str != nullptr) {
MS_LOG(INFO) << "Getenv MS_DEBUGGER_PORT: " << env_port_str;
port = std::string(env_port_str);
if (CheckPort(env_port_str)) {
MS_LOG(INFO) << "Getenv MS_DEBUGGER_PORT: " << env_port_str;
port = std::string(env_port_str);
} else {
MS_LOG(ERROR) << "Environment variable MS_DEBUGGER_PORT is not valid. Custom port ranging from 0 to 65535";
debugger_enabled_ = false;
}
} else {
MS_LOG(INFO) << "Environment variable MS_DEBUGGER_PORT doesn't exist. Using default debugger port: 50051";
port = "50051";
@@ -800,4 +819,17 @@ std::vector<std::string> Debugger::CheckOpOverflow() {

void Debugger::SetTrainingDone(bool training_done) { training_done_ = training_done; }

bool Debugger::CheckPort(const char *port) {
char *p = const_cast<char *>(port);
int num = 0;
if (*p == '0' && *(p + 1) != '\0') return false;
while (*p != '\0') {
if (*p <= '0' && *p >= '9') return false;
num = num * 10 + (*p) - '0';
if (num < 0 || num > 65535) return false;
p++;
}
return true;
}

} // namespace mindspore

+ 3
- 0
mindspore/ccsrc/debug/debugger/debugger.h View File

@@ -157,6 +157,9 @@ class Debugger : public std::enable_shared_from_this<Debugger> {
// Find if any operation overflow happened and return their names
std::vector<std::string> CheckOpOverflow();

// Check if the port is valid
bool CheckPort(const char *port);

// class members
std::unique_ptr<GrpcClient> grpc_client_;
std::unique_ptr<DebugServices> debug_services_;


Loading…
Cancel
Save