Skip to content

Commit 5698426

Browse files
committed
Move log opening to appropriate execution phase
When piped logs are opened during parsing of configuration it results in unexpected situations in apache httpd and can cause hang of process which is trying to log into auditlog.
1 parent 8038a52 commit 5698426

File tree

4 files changed

+78
-58
lines changed

4 files changed

+78
-58
lines changed

Diff for: apache2/apache2_config.c

-58
Original file line numberDiff line numberDiff line change
@@ -1209,35 +1209,6 @@ static const char *cmd_audit_log(cmd_parms *cmd, void *_dcfg, const char *p1)
12091209
directory_config *dcfg = _dcfg;
12101210

12111211
dcfg->auditlog_name = (char *)p1;
1212-
1213-
if (dcfg->auditlog_name[0] == '|') {
1214-
const char *pipe_name = dcfg->auditlog_name + 1;
1215-
piped_log *pipe_log;
1216-
1217-
pipe_log = ap_open_piped_log(cmd->pool, pipe_name);
1218-
if (pipe_log == NULL) {
1219-
return apr_psprintf(cmd->pool, "ModSecurity: Failed to open the audit log pipe: %s",
1220-
pipe_name);
1221-
}
1222-
dcfg->auditlog_fd = ap_piped_log_write_fd(pipe_log);
1223-
}
1224-
else {
1225-
const char *file_name = ap_server_root_relative(cmd->pool, dcfg->auditlog_name);
1226-
apr_status_t rc;
1227-
1228-
if (dcfg->auditlog_fileperms == NOT_SET) {
1229-
dcfg->auditlog_fileperms = CREATEMODE;
1230-
}
1231-
rc = apr_file_open(&dcfg->auditlog_fd, file_name,
1232-
APR_WRITE | APR_APPEND | APR_CREATE | APR_BINARY,
1233-
dcfg->auditlog_fileperms, cmd->pool);
1234-
1235-
if (rc != APR_SUCCESS) {
1236-
return apr_psprintf(cmd->pool, "ModSecurity: Failed to open the audit log file: %s",
1237-
file_name);
1238-
}
1239-
}
1240-
12411212
return NULL;
12421213
}
12431214

@@ -1250,35 +1221,6 @@ static const char *cmd_audit_log2(cmd_parms *cmd, void *_dcfg, const char *p1)
12501221
}
12511222

12521223
dcfg->auditlog2_name = (char *)p1;
1253-
1254-
if (dcfg->auditlog2_name[0] == '|') {
1255-
const char *pipe_name = ap_server_root_relative(cmd->pool, dcfg->auditlog2_name + 1);
1256-
piped_log *pipe_log;
1257-
1258-
pipe_log = ap_open_piped_log(cmd->pool, pipe_name);
1259-
if (pipe_log == NULL) {
1260-
return apr_psprintf(cmd->pool, "ModSecurity: Failed to open the secondary audit log pipe: %s",
1261-
pipe_name);
1262-
}
1263-
dcfg->auditlog2_fd = ap_piped_log_write_fd(pipe_log);
1264-
}
1265-
else {
1266-
const char *file_name = ap_server_root_relative(cmd->pool, dcfg->auditlog2_name);
1267-
apr_status_t rc;
1268-
1269-
if (dcfg->auditlog_fileperms == NOT_SET) {
1270-
dcfg->auditlog_fileperms = CREATEMODE;
1271-
}
1272-
rc = apr_file_open(&dcfg->auditlog2_fd, file_name,
1273-
APR_WRITE | APR_APPEND | APR_CREATE | APR_BINARY,
1274-
dcfg->auditlog_fileperms, cmd->pool);
1275-
1276-
if (rc != APR_SUCCESS) {
1277-
return apr_psprintf(cmd->pool, "ModSecurity: Failed to open the secondary audit log file: %s",
1278-
file_name);
1279-
}
1280-
}
1281-
12821224
return NULL;
12831225
}
12841226

Diff for: apache2/mod_security2.c

+1
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,7 @@ static void register_hooks(apr_pool_t *mp) {
17321732

17331733
/* Logging */
17341734
ap_hook_error_log(hook_error_log, NULL, NULL, APR_HOOK_MIDDLE);
1735+
ap_hook_open_logs(modsec_open_logs, NULL, NULL, APR_HOOK_MIDDLE);
17351736
ap_hook_log_transaction(hook_log_transaction, NULL, transaction_afterme_list, APR_HOOK_MIDDLE);
17361737

17371738
/* Filter hooks */

Diff for: apache2/msc_logging.c

+74
Original file line numberDiff line numberDiff line change
@@ -2325,3 +2325,77 @@ void sec_audit_logger(modsec_rec *msr) {
23252325
}
23262326
#endif
23272327
}
2328+
2329+
int modsec_open_logs(apr_pool_t *pconf, apr_pool_t *p, apr_pool_t *ptemp, server_rec *s_main) {
2330+
directory_config *dcfg = ap_get_module_config(s_main->lookup_defaults, &security2_module);
2331+
2332+
if (dcfg->auditlog_name == NOT_SET_P) {
2333+
return OK;
2334+
}
2335+
if (dcfg->auditlog_name[0] == '|') {
2336+
const char *pipe_name = dcfg->auditlog_name + 1;
2337+
piped_log *pipe_log;
2338+
2339+
pipe_log = ap_open_piped_log(p, pipe_name);
2340+
if (pipe_log == NULL) {
2341+
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
2342+
"ModSecurity: Failed to open the audit log pipe: %s", pipe_name);
2343+
return DONE;
2344+
}
2345+
dcfg->auditlog_fd = ap_piped_log_write_fd(pipe_log);
2346+
}
2347+
else {
2348+
const char *file_name = ap_server_root_relative(p, dcfg->auditlog_name);
2349+
apr_status_t rc;
2350+
2351+
if (dcfg->auditlog_fileperms == NOT_SET) {
2352+
dcfg->auditlog_fileperms = CREATEMODE;
2353+
}
2354+
rc = apr_file_open(&dcfg->auditlog_fd, file_name,
2355+
APR_WRITE | APR_APPEND | APR_CREATE | APR_BINARY,
2356+
dcfg->auditlog_fileperms, p);
2357+
2358+
if (rc != APR_SUCCESS) {
2359+
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
2360+
"ModSecurity: Failed to open the audit log file: %s", file_name);
2361+
return DONE;
2362+
}
2363+
}
2364+
2365+
if (dcfg->auditlog2_name == NOT_SET_P) {
2366+
return OK;
2367+
}
2368+
if (dcfg->auditlog2_name[0] == '|') {
2369+
const char *pipe_name = ap_server_root_relative(p, dcfg->auditlog2_name + 1);
2370+
piped_log *pipe_log;
2371+
2372+
pipe_log = ap_open_piped_log(p, pipe_name);
2373+
if (pipe_log == NULL) {
2374+
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL,
2375+
"ModSecurity: Failed to open the secondary audit log pipe: %s",
2376+
pipe_name);
2377+
return OK;
2378+
}
2379+
dcfg->auditlog2_fd = ap_piped_log_write_fd(pipe_log);
2380+
}
2381+
else {
2382+
const char *file_name = ap_server_root_relative(p, dcfg->auditlog2_name);
2383+
apr_status_t rc;
2384+
2385+
if (dcfg->auditlog_fileperms == NOT_SET) {
2386+
dcfg->auditlog_fileperms = CREATEMODE;
2387+
}
2388+
rc = apr_file_open(&dcfg->auditlog2_fd, file_name,
2389+
APR_WRITE | APR_APPEND | APR_CREATE | APR_BINARY,
2390+
dcfg->auditlog_fileperms, p);
2391+
2392+
if (rc != APR_SUCCESS) {
2393+
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL,
2394+
"ModSecurity: Failed to open the secondary audit log file: %s",
2395+
file_name);
2396+
return OK;
2397+
}
2398+
}
2399+
2400+
return OK;
2401+
}

Diff for: apache2/msc_logging.h

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#define AUDITLOG_PART_ENDMARKER 'Z'
4444

4545
#include "modsecurity.h"
46+
#include "httpd.h"
4647
#include "apr_pools.h"
4748

4849
int DSOLOCAL is_valid_parts_specification(char *p);
@@ -51,4 +52,6 @@ char DSOLOCAL *construct_log_vcombinedus_limited(modsec_rec *msr, int _limit, in
5152

5253
void DSOLOCAL sec_audit_logger(modsec_rec *msr);
5354

55+
int modsec_open_logs(apr_pool_t *pconf, apr_pool_t *p, apr_pool_t *ptemp, server_rec *s_main);
56+
5457
#endif

0 commit comments

Comments
 (0)