19
19
#include "ddebug.h"
20
20
21
21
#include "ngx_http_modsecurity_common.h"
22
+ #include "stdio.h"
22
23
23
24
static ngx_int_t ngx_http_modsecurity_init (ngx_conf_t * cf );
24
25
static void * ngx_http_modsecurity_create_main_conf (ngx_conf_t * cf );
@@ -28,32 +29,74 @@ static void ngx_http_modsecurity_main_config_cleanup(void *data);
28
29
static void ngx_http_modsecurity_config_cleanup (void * data );
29
30
30
31
/*
31
- * pcre malloc/free hack magic
32
+ * PCRE malloc/free workaround, based on
33
+ * https://github.com/openresty/lua-nginx-module/blob/master/src/ngx_http_lua_pcrefix.c
32
34
*/
35
+
33
36
static void * (* old_pcre_malloc )(size_t );
34
37
static void (* old_pcre_free )(void * ptr );
38
+ static ngx_pool_t * ngx_http_modsec_pcre_pool = NULL ;
35
39
36
- void
37
- ngx_http_modsecurity_pcre_malloc_init ( void )
40
+ static void *
41
+ ngx_http_modsec_pcre_malloc ( size_t size )
38
42
{
39
- old_pcre_malloc = pcre_malloc ;
40
- old_pcre_free = pcre_free ;
43
+ if (ngx_http_modsec_pcre_pool ) {
44
+ return ngx_palloc (ngx_http_modsec_pcre_pool , size );
45
+ }
41
46
42
- pcre_malloc = malloc ;
43
- pcre_free = free ;
47
+ fprintf (stderr , "error: modsec pcre malloc failed due to empty pcre pool" );
48
+
49
+ return NULL ;
44
50
}
45
51
46
- void
47
- ngx_http_modsecurity_pcre_malloc_done (void )
52
+ static void
53
+ ngx_http_modsec_pcre_free (void * ptr )
48
54
{
49
- if (old_pcre_malloc == NULL )
55
+ if (ngx_http_modsec_pcre_pool ) {
56
+ ngx_pfree (ngx_http_modsec_pcre_pool , ptr );
50
57
return ;
58
+ }
59
+
60
+ #if 0
61
+ /* this may happen when called from cleanup handlers */
62
+ fprintf (stderr , "error: modsec pcre free failed due to empty pcre pool" );
63
+ #endif
64
+
65
+ return ;
66
+ }
67
+
68
+ ngx_pool_t *
69
+ ngx_http_modsecurity_pcre_malloc_init (ngx_pool_t * pool )
70
+ {
71
+ ngx_pool_t * old_pool ;
72
+
73
+ if (pcre_malloc != ngx_http_modsec_pcre_malloc ) {
74
+ ngx_http_modsec_pcre_pool = pool ;
75
+
76
+ old_pcre_malloc = pcre_malloc ;
77
+ old_pcre_free = pcre_free ;
51
78
52
- pcre_malloc = old_pcre_malloc ;
53
- pcre_free = old_pcre_free ;
79
+ pcre_malloc = ngx_http_modsec_pcre_malloc ;
80
+ pcre_free = ngx_http_modsec_pcre_free ;
54
81
55
- old_pcre_malloc = NULL ;
56
- old_pcre_free = NULL ;
82
+ return NULL ;
83
+ }
84
+
85
+ old_pool = ngx_http_modsec_pcre_pool ;
86
+ ngx_http_modsec_pcre_pool = pool ;
87
+
88
+ return old_pool ;
89
+ }
90
+
91
+ void
92
+ ngx_http_modsecurity_pcre_malloc_done (ngx_pool_t * old_pool )
93
+ {
94
+ ngx_http_modsec_pcre_pool = old_pool ;
95
+
96
+ if (old_pool == NULL ) {
97
+ pcre_malloc = old_pcre_malloc ;
98
+ pcre_free = old_pcre_free ;
99
+ }
57
100
}
58
101
59
102
/*
@@ -494,6 +537,7 @@ ngx_http_modsecurity_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
494
537
{
495
538
ngx_http_modsecurity_loc_conf_t * p = NULL ;
496
539
ngx_http_modsecurity_loc_conf_t * c = NULL ;
540
+ ngx_pool_t * old_pool ;
497
541
498
542
p = parent ;
499
543
c = child ;
@@ -529,9 +573,9 @@ ngx_http_modsecurity_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
529
573
if (rules_remote_key == (char * )-1 ) {
530
574
return NGX_CONF_ERROR ;
531
575
}
532
- ngx_http_modsecurity_pcre_malloc_init ();
576
+ old_pool = ngx_http_modsecurity_pcre_malloc_init (cf -> pool );
533
577
res = msc_rules_add_remote (c -> rules_set , rules_remote_key , rules_remote_server , & error );
534
- ngx_http_modsecurity_pcre_malloc_done ();
578
+ ngx_http_modsecurity_pcre_malloc_done (old_pool );
535
579
dd ("Loading rules from: '%s'" , rules_remote_server );
536
580
if (res < 0 ) {
537
581
dd ("Failed to load the rules from: '%s' - reason: '%s'" , rules_remote_server , error );
@@ -547,9 +591,9 @@ ngx_http_modsecurity_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
547
591
if (rules_set == (char * )-1 ) {
548
592
return NGX_CONF_ERROR ;
549
593
}
550
- ngx_http_modsecurity_pcre_malloc_init ();
594
+ old_pool = ngx_http_modsecurity_pcre_malloc_init (cf -> pool );
551
595
res = msc_rules_add_file (c -> rules_set , rules_set , & error );
552
- ngx_http_modsecurity_pcre_malloc_done ();
596
+ ngx_http_modsecurity_pcre_malloc_done (old_pool );
553
597
dd ("Loading rules from: '%s'" , rules_set );
554
598
if (res < 0 ) {
555
599
dd ("Failed to load the rules from: '%s' - reason: '%s'" , rules_set , error );
@@ -565,9 +609,9 @@ ngx_http_modsecurity_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
565
609
if (rules == (char * )-1 ) {
566
610
return NGX_CONF_ERROR ;
567
611
}
568
- ngx_http_modsecurity_pcre_malloc_init ();
612
+ old_pool = ngx_http_modsecurity_pcre_malloc_init (cf -> pool );
569
613
res = msc_rules_add (c -> rules_set , rules , & error );
570
- ngx_http_modsecurity_pcre_malloc_done ();
614
+ ngx_http_modsecurity_pcre_malloc_done (old_pool );
571
615
dd ("Loading rules: '%s'" , rules );
572
616
if (res < 0 ) {
573
617
dd ("Failed to load the rules: '%s' - reason: '%s'" , rules , error );
@@ -584,20 +628,31 @@ ngx_http_modsecurity_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
584
628
static void
585
629
ngx_http_modsecurity_main_config_cleanup (void * data )
586
630
{
631
+ ngx_pool_t * old_pool ;
587
632
ngx_http_modsecurity_main_conf_t * cf = data ;
633
+
634
+ dd ("deleting a main conf %p" , data );
635
+
636
+ old_pool = ngx_http_modsecurity_pcre_malloc_init (NULL );
588
637
msc_cleanup (cf -> modsec );
638
+ ngx_http_modsecurity_pcre_malloc_done (old_pool );
639
+
589
640
cf -> modsec = NULL ;
590
641
}
591
642
592
643
593
644
static void
594
645
ngx_http_modsecurity_config_cleanup (void * data )
595
646
{
647
+ ngx_pool_t * old_pool ;
596
648
ngx_http_modsecurity_loc_conf_t * t = (ngx_http_modsecurity_loc_conf_t * ) data ;
649
+
597
650
dd ("deleting a loc conf -- RuleSet is: \"%p\"" , t -> rules_set );
598
- ngx_http_modsecurity_pcre_malloc_init ();
651
+
652
+ old_pool = ngx_http_modsecurity_pcre_malloc_init (NULL );
599
653
msc_rules_cleanup (t -> rules_set );
600
- ngx_http_modsecurity_pcre_malloc_done ();
654
+ ngx_http_modsecurity_pcre_malloc_done (old_pool );
655
+
601
656
t -> rules_set = NULL ;
602
657
}
603
658
0 commit comments