Skip to content

Commit 7440c18

Browse files
committed
implement quick workaround to return libmodsecurity's pcre into workable state
1 parent 54da83f commit 7440c18

7 files changed

+65
-30
lines changed

src/ngx_http_modsecurity_body_filter.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
159159
}
160160
}
161161

162+
ngx_http_modsecurity_pcre_malloc_init();
162163
msc_process_response_body(ctx->modsec_transaction);
164+
ngx_http_modsecurity_pcre_malloc_done();
165+
163166
/* XXX: I don't get how body from modsec being transferred to nginx's buffer. If so - after adjusting of nginx's
164167
XXX: body we can proceed to adjust body size (content-length). see xslt_body_filter() for example */
165168
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r);

src/ngx_http_modsecurity_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ extern ngx_module_t ngx_http_modsecurity_module;
9090
int ngx_http_modsecurity_process_intervention (Transaction *transaction, ngx_http_request_t *r);
9191
ngx_http_modsecurity_ctx_t *ngx_http_modsecurity_create_ctx(ngx_http_request_t *r);
9292
char *ngx_str_to_char(ngx_str_t a, ngx_pool_t *p);
93+
void ngx_http_modsecurity_pcre_malloc_init(void);
94+
void ngx_http_modsecurity_pcre_malloc_done(void);
9395

9496
/* ngx_http_modsecurity_body_filter.c */
9597
ngx_int_t ngx_http_modsecurity_body_filter_init(void);

src/ngx_http_modsecurity_header_filter.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,9 @@ ngx_http_modsecurity_header_filter(ngx_http_request_t *r)
518518
}
519519
#endif
520520

521+
ngx_http_modsecurity_pcre_malloc_init();
521522
msc_process_response_headers(ctx->modsec_transaction, status, http_response_ver);
523+
ngx_http_modsecurity_pcre_malloc_done();
522524
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r);
523525
if (ret > 0) {
524526
return ret;

src/ngx_http_modsecurity_log.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ ngx_http_modsecurity_log_handler(ngx_http_request_t *r)
6363
}
6464

6565
dd("calling msc_process_logging for %p", ctx);
66+
ngx_http_modsecurity_pcre_malloc_init();
6667
msc_process_logging(ctx->modsec_transaction);
68+
ngx_http_modsecurity_pcre_malloc_done();
6769

6870
return NGX_OK;
6971
}

src/ngx_http_modsecurity_module.c

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,40 @@
2121
#include "ngx_http_modsecurity_common.h"
2222

2323
static ngx_int_t ngx_http_modsecurity_init(ngx_conf_t *cf);
24-
static ngx_int_t ngx_http_modsecurity_preconfiguration(ngx_conf_t *cf);
2524
static void *ngx_http_modsecurity_create_main_conf(ngx_conf_t *cf);
2625
static void *ngx_http_modsecurity_create_loc_conf(ngx_conf_t *cf);
2726
static char *ngx_http_modsecurity_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
2827
static void ngx_http_modsecurity_main_config_cleanup(void *data);
2928
static void ngx_http_modsecurity_config_cleanup(void *data);
3029

30+
/*
31+
* pcre malloc/free hack magic
32+
*/
33+
static void *(*old_pcre_malloc)(size_t);
34+
static void (*old_pcre_free)(void *ptr);
35+
36+
void
37+
ngx_http_modsecurity_pcre_malloc_init(void)
38+
{
39+
old_pcre_malloc = pcre_malloc;
40+
old_pcre_free = pcre_free;
41+
42+
pcre_malloc = malloc;
43+
pcre_free = free;
44+
}
45+
46+
void
47+
ngx_http_modsecurity_pcre_malloc_done(void)
48+
{
49+
if (old_pcre_malloc == NULL)
50+
return;
51+
52+
pcre_malloc = old_pcre_malloc;
53+
pcre_free = old_pcre_free;
54+
55+
old_pcre_malloc = NULL;
56+
old_pcre_free = NULL;
57+
}
3158

3259
/*
3360
* ngx_string's are not null-terminated in common case, so we need to convert
@@ -262,25 +289,25 @@ static ngx_command_t ngx_http_modsecurity_commands[] = {
262289

263290

264291
static ngx_http_module_t ngx_http_modsecurity_ctx = {
265-
ngx_http_modsecurity_preconfiguration, /* preconfiguration */
266-
ngx_http_modsecurity_init, /* postconfiguration */
292+
NULL, /* preconfiguration */
293+
ngx_http_modsecurity_init, /* postconfiguration */
267294

268-
ngx_http_modsecurity_create_main_conf, /* create main configuration */
269-
NULL, /* init main configuration */
295+
ngx_http_modsecurity_create_main_conf, /* create main configuration */
296+
NULL, /* init main configuration */
270297

271-
NULL, /* create server configuration */
272-
NULL, /* merge server configuration */
298+
NULL, /* create server configuration */
299+
NULL, /* merge server configuration */
273300

274-
ngx_http_modsecurity_create_loc_conf, /* create location configuration */
275-
ngx_http_modsecurity_merge_loc_conf /* merge location configuration */
301+
ngx_http_modsecurity_create_loc_conf, /* create location configuration */
302+
ngx_http_modsecurity_merge_loc_conf /* merge location configuration */
276303
};
277304

278305

279306
ngx_module_t ngx_http_modsecurity_module = {
280307
NGX_MODULE_V1,
281-
&ngx_http_modsecurity_ctx, /* module context */
282-
ngx_http_modsecurity_commands, /* module directives */
283-
NGX_HTTP_MODULE, /* module type */
308+
&ngx_http_modsecurity_ctx, /* module context */
309+
ngx_http_modsecurity_commands, /* module directives */
310+
NGX_HTTP_MODULE, /* module type */
284311
NULL, /* init master */
285312
NULL, /* init module */
286313
NULL, /* init process */
@@ -292,24 +319,6 @@ ngx_module_t ngx_http_modsecurity_module = {
292319
};
293320

294321

295-
static ngx_int_t
296-
ngx_http_modsecurity_preconfiguration(ngx_conf_t *cf)
297-
{
298-
/*
299-
*
300-
* FIXME: Ops. Nginx hooks those two guys, we have to figure out a better
301-
* way to deal with it.
302-
*
303-
*/
304-
#if 0 /* XXX: attempt to find out a reason and solution */
305-
pcre_malloc = malloc;
306-
pcre_free = free;
307-
#endif
308-
309-
return NGX_OK;
310-
}
311-
312-
313322
static ngx_int_t
314323
ngx_http_modsecurity_init(ngx_conf_t *cf)
315324
{
@@ -516,7 +525,9 @@ ngx_http_modsecurity_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
516525
if (rules_remote_key == (char *)-1) {
517526
return NGX_CONF_ERROR;
518527
}
528+
ngx_http_modsecurity_pcre_malloc_init();
519529
res = msc_rules_add_remote(c->rules_set, rules_remote_key, rules_remote_server, &error);
530+
ngx_http_modsecurity_pcre_malloc_done();
520531
dd("Loading rules from: '%s'", rules_remote_server);
521532
if (res < 0) {
522533
dd("Failed to load the rules from: '%s' - reason: '%s'", rules_remote_server, error);
@@ -532,7 +543,9 @@ ngx_http_modsecurity_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
532543
if (rules_set == (char *)-1) {
533544
return NGX_CONF_ERROR;
534545
}
546+
ngx_http_modsecurity_pcre_malloc_init();
535547
res = msc_rules_add_file(c->rules_set, rules_set, &error);
548+
ngx_http_modsecurity_pcre_malloc_done();
536549
dd("Loading rules from: '%s'", rules_set);
537550
if (res < 0) {
538551
dd("Failed to load the rules from: '%s' - reason: '%s'", rules_set, error);
@@ -548,7 +561,9 @@ ngx_http_modsecurity_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
548561
if (rules == (char *)-1) {
549562
return NGX_CONF_ERROR;
550563
}
564+
ngx_http_modsecurity_pcre_malloc_init();
551565
res = msc_rules_add(c->rules_set, rules, &error);
566+
ngx_http_modsecurity_pcre_malloc_done();
552567
dd("Loading rules: '%s'", rules);
553568
if (res < 0) {
554569
dd("Failed to load the rules: '%s' - reason: '%s'", rules, error);
@@ -576,7 +591,9 @@ ngx_http_modsecurity_config_cleanup(void *data)
576591
{
577592
ngx_http_modsecurity_loc_conf_t *t = (ngx_http_modsecurity_loc_conf_t *) data;
578593
dd("deleting a loc conf -- RuleSet is: \"%p\"", t->rules_set);
594+
ngx_http_modsecurity_pcre_malloc_init();
579595
msc_rules_cleanup(t->rules_set);
596+
ngx_http_modsecurity_pcre_malloc_done();
580597
t->rules_set = NULL;
581598
}
582599

src/ngx_http_modsecurity_pre_access.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,10 @@ ngx_http_modsecurity_pre_access_handler(ngx_http_request_t *r)
189189

190190
/* XXX: once more -- is body can be modified ? content-length need to be adjusted ? */
191191

192+
ngx_http_modsecurity_pcre_malloc_init();
192193
msc_process_request_body(ctx->modsec_transaction);
194+
ngx_http_modsecurity_pcre_malloc_done();
195+
193196
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r);
194197
if (ret > 0) {
195198
return ret;

src/ngx_http_modsecurity_rewrite.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
8080
return NGX_HTTP_INTERNAL_SERVER_ERROR;
8181
}
8282
const char *server_addr = inet_ntoa(((struct sockaddr_in *) connection->sockaddr)->sin_addr);
83+
ngx_http_modsecurity_pcre_malloc_init();
8384
ret = msc_process_connection(ctx->modsec_transaction,
8485
client_addr, client_port,
8586
server_addr, server_port);
87+
ngx_http_modsecurity_pcre_malloc_done();
8688
if (ret != 1){
8789
dd("Was not able to extract connection information.");
8890
}
@@ -125,7 +127,9 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
125127
if (n_uri == (char*)-1 || n_method == (char*)-1) {
126128
return NGX_HTTP_INTERNAL_SERVER_ERROR;
127129
}
130+
ngx_http_modsecurity_pcre_malloc_init();
128131
msc_process_uri(ctx->modsec_transaction, n_uri, n_method, http_version);
132+
ngx_http_modsecurity_pcre_malloc_done();
129133

130134
dd("Processing intervention with the transaction information filled in (uri, method and version)");
131135
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r);
@@ -171,7 +175,9 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
171175
* to process this information.
172176
*/
173177

178+
ngx_http_modsecurity_pcre_malloc_init();
174179
msc_process_request_headers(ctx->modsec_transaction);
180+
ngx_http_modsecurity_pcre_malloc_done();
175181
dd("Processing intervention with the request headers information filled in");
176182
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r);
177183
if (ret > 0) {

0 commit comments

Comments
 (0)