Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/parser/seclang-scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions src/parser/seclang-scanner.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/utils/shared_files.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

#include "src/utils/shared_files.h"
#include "src/utils/system.h"

#include <fcntl.h>
#ifdef WIN32
Expand All @@ -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();
}
Expand Down
23 changes: 20 additions & 3 deletions src/utils/system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*
*/

#include <bits/types/FILE.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
Expand Down Expand Up @@ -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);
Expand All @@ -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
1 change: 1 addition & 0 deletions src/utils/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ std::string get_path(const std::string& file);
std::list<std::string> 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
Expand Down