21
21
#include "ngx_http_modsecurity_common.h"
22
22
23
23
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 );
25
24
static void * ngx_http_modsecurity_create_main_conf (ngx_conf_t * cf );
26
25
static void * ngx_http_modsecurity_create_loc_conf (ngx_conf_t * cf );
27
26
static char * ngx_http_modsecurity_merge_loc_conf (ngx_conf_t * cf , void * parent , void * child );
28
27
static void ngx_http_modsecurity_main_config_cleanup (void * data );
29
28
static void ngx_http_modsecurity_config_cleanup (void * data );
30
29
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
+ }
31
58
32
59
/*
33
60
* 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[] = {
262
289
263
290
264
291
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 */
267
294
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 */
270
297
271
- NULL , /* create server configuration */
272
- NULL , /* merge server configuration */
298
+ NULL , /* create server configuration */
299
+ NULL , /* merge server configuration */
273
300
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 */
276
303
};
277
304
278
305
279
306
ngx_module_t ngx_http_modsecurity_module = {
280
307
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 */
284
311
NULL , /* init master */
285
312
NULL , /* init module */
286
313
NULL , /* init process */
@@ -292,24 +319,6 @@ ngx_module_t ngx_http_modsecurity_module = {
292
319
};
293
320
294
321
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
-
313
322
static ngx_int_t
314
323
ngx_http_modsecurity_init (ngx_conf_t * cf )
315
324
{
@@ -516,7 +525,9 @@ ngx_http_modsecurity_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
516
525
if (rules_remote_key == (char * )-1 ) {
517
526
return NGX_CONF_ERROR ;
518
527
}
528
+ ngx_http_modsecurity_pcre_malloc_init ();
519
529
res = msc_rules_add_remote (c -> rules_set , rules_remote_key , rules_remote_server , & error );
530
+ ngx_http_modsecurity_pcre_malloc_done ();
520
531
dd ("Loading rules from: '%s'" , rules_remote_server );
521
532
if (res < 0 ) {
522
533
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)
532
543
if (rules_set == (char * )-1 ) {
533
544
return NGX_CONF_ERROR ;
534
545
}
546
+ ngx_http_modsecurity_pcre_malloc_init ();
535
547
res = msc_rules_add_file (c -> rules_set , rules_set , & error );
548
+ ngx_http_modsecurity_pcre_malloc_done ();
536
549
dd ("Loading rules from: '%s'" , rules_set );
537
550
if (res < 0 ) {
538
551
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)
548
561
if (rules == (char * )-1 ) {
549
562
return NGX_CONF_ERROR ;
550
563
}
564
+ ngx_http_modsecurity_pcre_malloc_init ();
551
565
res = msc_rules_add (c -> rules_set , rules , & error );
566
+ ngx_http_modsecurity_pcre_malloc_done ();
552
567
dd ("Loading rules: '%s'" , rules );
553
568
if (res < 0 ) {
554
569
dd ("Failed to load the rules: '%s' - reason: '%s'" , rules , error );
@@ -576,7 +591,9 @@ ngx_http_modsecurity_config_cleanup(void *data)
576
591
{
577
592
ngx_http_modsecurity_loc_conf_t * t = (ngx_http_modsecurity_loc_conf_t * ) data ;
578
593
dd ("deleting a loc conf -- RuleSet is: \"%p\"" , t -> rules_set );
594
+ ngx_http_modsecurity_pcre_malloc_init ();
579
595
msc_rules_cleanup (t -> rules_set );
596
+ ngx_http_modsecurity_pcre_malloc_done ();
580
597
t -> rules_set = NULL ;
581
598
}
582
599
0 commit comments