Skip to content

Commit 4efb67e

Browse files
create fopen_modsec
1 parent f5a6fcd commit 4efb67e

9 files changed

Lines changed: 24 additions & 19 deletions

File tree

bindings/python

Submodule python deleted from bc625d5

others/libinjection

Submodule libinjection deleted from 2117822

others/mbedtls

Submodule mbedtls deleted from 0fe989b

src/parser/seclang-scanner.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#line 3 "seclang-scanner.cc"
32

43
#define YY_INT_ALIGNED short int
@@ -4953,6 +4952,7 @@ char *yytext;
49534952
#include "src/parser/seclang-parser.hh"
49544953
#include "src/utils/https_client.h"
49554954
#include "src/utils/string.h"
4955+
#include "src/utils/system.h"
49564956

49574957
using modsecurity::Parser::Driver;
49584958
using modsecurity::Utils::HttpsClient;
@@ -8400,8 +8400,7 @@ YY_RULE_SETUP
84008400
driver.loc.push_back(new yy::location());
84018401
driver.m_filenames.push_back(f);
84028402
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = &(driver.m_filenames.back());
8403-
yyin = fopen(f.c_str(), "r" );
8404-
if (!yyin) {
8403+
if (!modsecurity::utils::fopen_modsec(&yyin, f.c_str(), "r") != 0) {
84058404
BEGIN(INITIAL);
84068405
driver.loc.pop_back();
84078406
driver.error (*driver.loc.back(), "", s + std::string(": Not able to open file. ") + err);
@@ -8433,8 +8432,7 @@ YY_RULE_SETUP
84338432
driver.m_filenames.push_back(f);
84348433
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = &(driver.m_filenames.back());
84358434

8436-
yyin = fopen(f.c_str(), "r" );
8437-
if (!yyin) {
8435+
if (!modsecurity::utils::fopen_modsec(&yyin, f.c_str(), "r") != 0) {
84388436
BEGIN(INITIAL);
84398437
driver.loc.pop_back();
84408438
driver.error (*driver.loc.back(), "", s + std::string(": Not able to open file. ") + err);

src/parser/seclang-scanner.ll

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
%{ /* -*- C++ -*- */
1+
/* -*- C++ -*- */
22
#include <cerrno>
33
#include <climits>
44
#include <cstdlib>
@@ -8,6 +8,7 @@
88
#include "src/parser/seclang-parser.hh"
99
#include "src/utils/https_client.h"
1010
#include "src/utils/string.h"
11+
#include "src/utils/system.h"
1112

1213
using modsecurity::Parser::Driver;
1314
using modsecurity::Utils::HttpsClient;
@@ -1273,8 +1274,7 @@ EQUALS_MINUS (?i:=\-)
12731274
driver.loc.push_back(new yy::location());
12741275
driver.m_filenames.push_back(f);
12751276
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = &(driver.m_filenames.back());
1276-
yyin = fopen(f.c_str(), "r" );
1277-
if (!yyin) {
1277+
if (!modsecurity::utils::fopen_modsec(&yyin, f.c_str(), "r")) {
12781278
BEGIN(INITIAL);
12791279
driver.loc.pop_back();
12801280
driver.error (*driver.loc.back(), "", s + std::string(": Not able to open file. ") + err);
@@ -1303,8 +1303,7 @@ EQUALS_MINUS (?i:=\-)
13031303
driver.m_filenames.push_back(f);
13041304
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = &(driver.m_filenames.back());
13051305
1306-
yyin = fopen(f.c_str(), "r" );
1307-
if (!yyin) {
1306+
if (!modsecurity::utils::fopen_modsec(&yyin, f.c_str(), "r") != 0) {
13081307
BEGIN(INITIAL);
13091308
driver.loc.pop_back();
13101309
driver.error (*driver.loc.back(), "", s + std::string(": Not able to open file. ") + err);

src/utils/shared_files.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
#include "src/utils/shared_files.h"
17+
#include "src/utils/system.h"
1718

1819
#include <fcntl.h>
1920
#ifdef WIN32
@@ -27,8 +28,8 @@ namespace utils {
2728

2829
SharedFiles::handlers_map::iterator SharedFiles::add_new_handler(
2930
const std::string &fileName, std::string *error) {
30-
FILE *fp = fopen(fileName.c_str(), "a");
31-
if (fp == 0) {
31+
FILE *fp;
32+
if (!fopen_modsec(&fp, fileName.c_str(), "a")) {
3233
error->assign("Failed to open file: " + fileName);
3334
return m_handlers.end();
3435
}

src/utils/system.cc

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*
1414
*/
1515

16+
#include <bits/types/FILE.h>
1617
#include <stdio.h>
1718
#include <stdlib.h>
1819
#include <stddef.h>
@@ -205,8 +206,8 @@ bool createDir(const std::string& dir, int mode, std::string *error) {
205206

206207
bool isFile(const std::string& f) {
207208
struct stat fileInfo;
208-
FILE *fp = fopen(f.c_str(), "r");
209-
if (fp == NULL) {
209+
FILE *fp;
210+
if (!fopen_modsec(&fp, f.c_str(), "r")) {
210211
return false;
211212
}
212213
fstat(fileno(fp), &fileInfo);
@@ -219,6 +220,15 @@ bool isFile(const std::string& f) {
219220
return true;
220221
}
221222

222-
223+
bool fopen_modsec(FILE **v_fp, const char *filename, const char *mode) {
224+
if (v_fp == NULL || filename == NULL || mode == NULL) {
225+
return false;
226+
}
227+
*v_fp = fopen(filename, mode);
228+
if (*v_fp == NULL) {
229+
return false;
230+
}
231+
return true;
232+
}
223233
} // namespace utils
224234
} // namespace modsecurity

src/utils/system.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ std::string get_path(const std::string& file);
3333
std::list<std::string> expandEnv(const std::string& var, int flags);
3434
bool createDir(const std::string& dir, int mode, std::string *error);
3535
bool isFile(const std::string& f);
36+
bool fopen_modsec(FILE **v_fp, const char *filename, const char *mode);
3637

3738
} // namespace utils
3839
} // namespace modsecurity
Submodule secrules-language-tests deleted from a3d4405

0 commit comments

Comments
 (0)