diff --git a/src/parser/seclang-scanner.cc b/src/parser/seclang-scanner.cc index 26385c206..bf2487d93 100644 --- a/src/parser/seclang-scanner.cc +++ b/src/parser/seclang-scanner.cc @@ -4953,6 +4953,7 @@ char *yytext; #include "src/parser/seclang-parser.hh" #include "src/utils/https_client.h" #include "src/utils/string.h" +#include "src/utils/system.h" using modsecurity::Parser::Driver; using modsecurity::Utils::HttpsClient; @@ -8400,8 +8401,7 @@ YY_RULE_SETUP driver.loc.push_back(new yy::location()); driver.m_filenames.push_back(f); driver.loc.back()->begin.filename = driver.loc.back()->end.filename = &(driver.m_filenames.back()); - yyin = fopen(f.c_str(), "r" ); - if (!yyin) { + if (!modsecurity::utils::fopen_modsec(&yyin, f.c_str(), "r") != 0) { // NOSONAR BEGIN(INITIAL); driver.loc.pop_back(); driver.error (*driver.loc.back(), "", s + std::string(": Not able to open file. ") + err); @@ -8432,9 +8432,7 @@ YY_RULE_SETUP driver.loc.push_back(new yy::location()); driver.m_filenames.push_back(f); driver.loc.back()->begin.filename = driver.loc.back()->end.filename = &(driver.m_filenames.back()); - - yyin = fopen(f.c_str(), "r" ); - if (!yyin) { + if (!modsecurity::utils::fopen_modsec(&yyin, f.c_str(), "r") != 0) { // NOSONAR BEGIN(INITIAL); driver.loc.pop_back(); driver.error (*driver.loc.back(), "", s + std::string(": Not able to open file. ") + err); diff --git a/src/parser/seclang-scanner.ll b/src/parser/seclang-scanner.ll index f954be892..2bf8b827e 100755 --- a/src/parser/seclang-scanner.ll +++ b/src/parser/seclang-scanner.ll @@ -8,6 +8,7 @@ #include "src/parser/seclang-parser.hh" #include "src/utils/https_client.h" #include "src/utils/string.h" +#include "src/utils/system.h" using modsecurity::Parser::Driver; using modsecurity::Utils::HttpsClient; @@ -1273,8 +1274,7 @@ EQUALS_MINUS (?i:=\-) driver.loc.push_back(new yy::location()); driver.m_filenames.push_back(f); driver.loc.back()->begin.filename = driver.loc.back()->end.filename = &(driver.m_filenames.back()); - yyin = fopen(f.c_str(), "r" ); - if (!yyin) { + if (!modsecurity::utils::fopen_modsec(&yyin, f.c_str(), "r")) { BEGIN(INITIAL); driver.loc.pop_back(); driver.error (*driver.loc.back(), "", s + std::string(": Not able to open file. ") + err); @@ -1303,8 +1303,7 @@ EQUALS_MINUS (?i:=\-) driver.m_filenames.push_back(f); driver.loc.back()->begin.filename = driver.loc.back()->end.filename = &(driver.m_filenames.back()); - yyin = fopen(f.c_str(), "r" ); - if (!yyin) { + if (!modsecurity::utils::fopen_modsec(&yyin, f.c_str(), "r") != 0) { BEGIN(INITIAL); driver.loc.pop_back(); driver.error (*driver.loc.back(), "", s + std::string(": Not able to open file. ") + err); diff --git a/src/utils/shared_files.cc b/src/utils/shared_files.cc index 6982d0d6e..4d313bdee 100644 --- a/src/utils/shared_files.cc +++ b/src/utils/shared_files.cc @@ -14,6 +14,7 @@ */ #include "src/utils/shared_files.h" +#include "src/utils/system.h" #include #ifdef WIN32 @@ -27,8 +28,8 @@ namespace utils { SharedFiles::handlers_map::iterator SharedFiles::add_new_handler( const std::string &fileName, std::string *error) { - FILE *fp = fopen(fileName.c_str(), "a"); - if (fp == 0) { + FILE *fp; + if (!fopen_modsec(&fp, fileName.c_str(), "a")) { error->assign("Failed to open file: " + fileName); return m_handlers.end(); } diff --git a/src/utils/system.cc b/src/utils/system.cc index 830b34243..6fd4ced5e 100644 --- a/src/utils/system.cc +++ b/src/utils/system.cc @@ -13,6 +13,7 @@ * */ +#include #include #include #include @@ -205,8 +206,8 @@ bool createDir(const std::string& dir, int mode, std::string *error) { bool isFile(const std::string& f) { struct stat fileInfo; - FILE *fp = fopen(f.c_str(), "r"); - if (fp == NULL) { + FILE *fp; + if (!fopen_modsec(&fp, f.c_str(), "r")) { return false; } fstat(fileno(fp), &fileInfo); @@ -219,6 +220,22 @@ bool isFile(const std::string& f) { return true; } - +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4996) +#endif +bool fopen_modsec(FILE **v_fp, const char *filename, const char *mode) { + if (v_fp == nullptr || filename == nullptr || mode == nullptr) { + return false; + } + *v_fp = fopen(filename, mode); + if (*v_fp == nullptr) { + return false; + } + return true; +} +#if defined(_MSC_VER) +#pragma warning(pop) +#endif } // namespace utils } // namespace modsecurity diff --git a/src/utils/system.h b/src/utils/system.h index d6b0adf63..9ffd3f270 100644 --- a/src/utils/system.h +++ b/src/utils/system.h @@ -33,6 +33,7 @@ std::string get_path(const std::string& file); std::list expandEnv(const std::string& var, int flags); bool createDir(const std::string& dir, int mode, std::string *error); bool isFile(const std::string& f); +bool fopen_modsec(FILE **v_fp, const char *filename, const char *mode); } // namespace utils } // namespace modsecurity