Skip to content

Commit ddd6bd2

Browse files
committed
Nginx Improved: modsec terminate handling
1 parent c30bb6d commit ddd6bd2

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

nginx/modsecurity/ngx_http_modsecurity.c

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ static ngx_int_t ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_cha
5252
static ngx_int_t ngx_http_modsecurity_preconfiguration(ngx_conf_t *cf);
5353
static ngx_int_t ngx_http_modsecurity_init(ngx_conf_t *cf);
5454
static ngx_int_t ngx_http_modsecurity_init_process(ngx_cycle_t *cycle);
55-
static void ngx_http_modsecurity_exit_process(ngx_cycle_t *cycle);
5655
static void *ngx_http_modsecurity_create_loc_conf(ngx_conf_t *cf);
5756
static char *ngx_http_modsecurity_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
5857
static char *ngx_http_modsecurity_config(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
5958
static char *ngx_http_modsecurity_enable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
6059

6160
static ngx_http_modsecurity_ctx_t * ngx_http_modsecurity_create_ctx(ngx_http_request_t *r);
6261
static int ngx_http_modsecurity_drop_action(request_rec *r);
62+
static void ngx_http_modsecurity_finalize(void *data);
6363
static void ngx_http_modsecurity_cleanup(void *data);
6464

6565
static int ngx_http_modsecurity_save_headers_in_visitor(void *data, const char *key, const char *value);
@@ -113,16 +113,15 @@ ngx_module_t ngx_http_modsecurity = {
113113
ngx_http_modsecurity_init_process, /* init process */
114114
NULL, /* init thread */
115115
NULL, /* exit thread */
116-
ngx_http_modsecurity_exit_process, /* exit process */
117-
ngx_http_modsecurity_exit_process, /* exit master */
116+
NULL, /* exit process */
117+
NULL, /* exit master */
118118
NGX_MODULE_V1_PADDING
119119
};
120120

121121
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
122122
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
123123

124124
static ngx_http_upstream_t ngx_http_modsecurity_upstream;
125-
static ngx_uint_t ngx_http_modsecurity_term = 0;
126125

127126
static struct {
128127
char *name;
@@ -883,7 +882,8 @@ modsec_pcre_free(void *ptr)
883882
static ngx_int_t
884883
ngx_http_modsecurity_preconfiguration(ngx_conf_t *cf)
885884
{
886-
server_rec *s;
885+
server_rec *s;
886+
ngx_pool_cleanup_t *cln;
887887

888888
/* XXX: temporary hack, nginx uses pcre as well and hijacks these two */
889889
pcre_malloc = modsec_pcre_malloc;
@@ -898,6 +898,12 @@ ngx_http_modsecurity_preconfiguration(ngx_conf_t *cf)
898898
return NGX_ERROR;
899899
}
900900

901+
cln = ngx_pool_cleanup_add(cf->pool, 0);
902+
if (cln == NULL) {
903+
return NGX_ERROR;
904+
}
905+
cln->handler = ngx_http_modsecurity_finalize;
906+
901907
/* set host name */
902908
s->server_hostname = ngx_palloc(cf->pool, ngx_cycle->hostname.len + 1);
903909
if (s->server_hostname == NULL) {
@@ -911,6 +917,12 @@ ngx_http_modsecurity_preconfiguration(ngx_conf_t *cf)
911917
}
912918

913919

920+
static void
921+
ngx_http_modsecurity_finalize(void *data)
922+
{
923+
modsecTerminate();
924+
}
925+
914926

915927
static ngx_int_t
916928
ngx_http_modsecurity_init(ngx_conf_t *cf)
@@ -921,9 +933,6 @@ ngx_http_modsecurity_init(ngx_conf_t *cf)
921933
modsecFinalizeConfig();
922934

923935
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
924-
if (cmcf == NULL) {
925-
return NGX_ERROR;
926-
}
927936

928937
h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
929938
if (h == NULL) {
@@ -946,25 +955,13 @@ ngx_http_modsecurity_init(ngx_conf_t *cf)
946955
static ngx_int_t
947956
ngx_http_modsecurity_init_process(ngx_cycle_t *cycle)
948957
{
958+
/* must set log hook here cf->log maybe changed */
949959
modsecSetLogHook(cycle->log, modsecLog);
950960
modsecInitProcess();
951961
return NGX_OK;
952962
}
953963

954964

955-
static void
956-
ngx_http_modsecurity_exit_process(ngx_cycle_t *cycle)
957-
{
958-
/* ngx_single_process_cycle will call master and worker exit_process twice */
959-
if (ngx_http_modsecurity_term) {
960-
return;
961-
}
962-
963-
ngx_http_modsecurity_term = 1;
964-
modsecTerminate();
965-
}
966-
967-
968965
/*
969966
** [ENTRY POINT] does : this function called by nginx from the request handler
970967
*/

0 commit comments

Comments
 (0)