Skip to content

Commit ec87dab

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 849cd7e commit ec87dab

File tree

4 files changed

+78
-58
lines changed

4 files changed

+78
-58
lines changed

apache2/apache2_config.c

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

12041204
dcfg->auditlog_name = (char *)p1;
1205-
1206-
if (dcfg->auditlog_name[0] == '|') {
1207-
const char *pipe_name = dcfg->auditlog_name + 1;
1208-
piped_log *pipe_log;
1209-
1210-
pipe_log = ap_open_piped_log(cmd->pool, pipe_name);
1211-
if (pipe_log == NULL) {
1212-
return apr_psprintf(cmd->pool, "ModSecurity: Failed to open the audit log pipe: %s",
1213-
pipe_name);
1214-
}
1215-
dcfg->auditlog_fd = ap_piped_log_write_fd(pipe_log);
1216-
}
1217-
else {
1218-
const char *file_name = ap_server_root_relative(cmd->pool, dcfg->auditlog_name);
1219-
apr_status_t rc;
1220-
1221-
if (dcfg->auditlog_fileperms == NOT_SET) {
1222-
dcfg->auditlog_fileperms = CREATEMODE;
1223-
}
1224-
rc = apr_file_open(&dcfg->auditlog_fd, file_name,
1225-
APR_WRITE | APR_APPEND | APR_CREATE | APR_BINARY,
1226-
dcfg->auditlog_fileperms, cmd->pool);
1227-
1228-
if (rc != APR_SUCCESS) {
1229-
return apr_psprintf(cmd->pool, "ModSecurity: Failed to open the audit log file: %s",
1230-
file_name);
1231-
}
1232-
}
1233-
12341205
return NULL;
12351206
}
12361207

@@ -1243,35 +1214,6 @@ static const char *cmd_audit_log2(cmd_parms *cmd, void *_dcfg, const char *p1)
12431214
}
12441215

12451216
dcfg->auditlog2_name = (char *)p1;
1246-
1247-
if (dcfg->auditlog2_name[0] == '|') {
1248-
const char *pipe_name = ap_server_root_relative(cmd->pool, dcfg->auditlog2_name + 1);
1249-
piped_log *pipe_log;
1250-
1251-
pipe_log = ap_open_piped_log(cmd->pool, pipe_name);
1252-
if (pipe_log == NULL) {
1253-
return apr_psprintf(cmd->pool, "ModSecurity: Failed to open the secondary audit log pipe: %s",
1254-
pipe_name);
1255-
}
1256-
dcfg->auditlog2_fd = ap_piped_log_write_fd(pipe_log);
1257-
}
1258-
else {
1259-
const char *file_name = ap_server_root_relative(cmd->pool, dcfg->auditlog2_name);
1260-
apr_status_t rc;
1261-
1262-
if (dcfg->auditlog_fileperms == NOT_SET) {
1263-
dcfg->auditlog_fileperms = CREATEMODE;
1264-
}
1265-
rc = apr_file_open(&dcfg->auditlog2_fd, file_name,
1266-
APR_WRITE | APR_APPEND | APR_CREATE | APR_BINARY,
1267-
dcfg->auditlog_fileperms, cmd->pool);
1268-
1269-
if (rc != APR_SUCCESS) {
1270-
return apr_psprintf(cmd->pool, "ModSecurity: Failed to open the secondary audit log file: %s",
1271-
file_name);
1272-
}
1273-
}
1274-
12751217
return NULL;
12761218
}
12771219

apache2/mod_security2.c

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

17221722
/* Logging */
17231723
ap_hook_error_log(hook_error_log, NULL, NULL, APR_HOOK_MIDDLE);
1724+
ap_hook_open_logs(modsec_open_logs, NULL, NULL, APR_HOOK_MIDDLE);
17241725
ap_hook_log_transaction(hook_log_transaction, NULL, transaction_afterme_list, APR_HOOK_MIDDLE);
17251726

17261727
/* Filter hooks */

apache2/msc_logging.c

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

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)