Skip to content

Commit 92454a4

Browse files
author
Felipe Zimmerle
committed
Avoids segfault while running with proxy_pass
Duplicates the headers variables while coping data from/to ModSecurity. This seems to fix the segfault that was happening while using proxy_pass. The variable is later cleaned, which means that we don't have a leak because of that.
1 parent 13f16cc commit 92454a4

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

nginx/modsecurity/config

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@ CFLAGS="$CFLAGS \
66
-I/usr/include/apache2 \
77
-I/usr/include/libxml2 \
88
-DWITH_LUA -I/usr/include/lua5.1 \
9-
-DWITH_PCRE_STUDY -DMODSEC_PCRE_MATCH_LIMIT=1500 -DMODSEC_PCRE_MATCH_LIMIT_RECURSION=1500 -DREQUEST_EARLY \
9+
-DWITH_PCRE_STUDY -DMODSEC_PCRE_MATCH_LIMIT=1500 -DMODSEC_PCRE_MATCH_LIMIT_RECURSION=1500 -DREQUEST_EARLY -DWITH_APU_CRYPTO -DWITH_REMOTE_RULES \
1010
\
11-
-DWITH_YAJL -I/usr/include/yajl "
11+
-DWITH_YAJL -I/usr/include/yajl \
12+
-DWITH_SSDEEP -I/usr/"
1213

1314

1415
CORE_LIBS="$CORE_LIBS \
15-
-L/usr/lib -lapr-1 \
16-
-L/usr/lib -laprutil-1 \
16+
-L/usr/lib/x86_64-linux-gnu -lapr-1 \
17+
-L/usr/lib/x86_64-linux-gnu -laprutil-1 \
1718
-I/usr/include/apache2 \
1819
-L/usr/lib/x86_64-linux-gnu -lcurl \
1920
-lxml2 \
2021
-llua5.1 \
2122
-lpcre \
22-
-L/usr/lib -lcap \
23-
-lyajl "
23+
-L/usr/lib \
24+
-lyajl \
25+
-lfuzzy"
2426

2527
ngx_addon_name=ngx_http_modsecurity
2628

nginx/modsecurity/ngx_http_modsecurity.c

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -807,10 +807,10 @@ ngx_http_modsecurity_save_headers_out_visitor(void *data,
807807
{
808808
ngx_http_request_t *r = data;
809809
ngx_table_elt_t *h, he, *new_h;
810-
ngx_http_upstream_header_t *hh;
811-
ngx_http_upstream_main_conf_t *umcf;
810+
//ngx_http_upstream_header_t *hh;
811+
//ngx_http_upstream_main_conf_t *umcf;
812812

813-
umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
813+
//umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
814814

815815
h = &he;
816816

@@ -829,31 +829,37 @@ ngx_http_modsecurity_save_headers_out_visitor(void *data,
829829

830830
h->hash = ngx_hash_key(h->lowcase_key, h->key.len);
831831

832-
hh = ngx_hash_find(&umcf->headers_in_hash, h->hash,
833-
h->lowcase_key, h->key.len);
834-
835-
if (hh) {
836-
/* copy all */
837-
if (hh->copy_handler(r, h, hh->conf) != NGX_OK) {
838-
return 0;
839-
}
840-
} else {
841-
/* Add the response header directly to headers_out if not present in
842-
* the hash. This is done to passthrough such response headers.
843-
* Remember the response headers were cleared earlier using
844-
* ngx_http_clean_header(r) call in ngx_http_modsecurity_save_headers_out.
845-
*/
846-
847-
new_h = ngx_list_push(&r->headers_out.headers);
848-
if (new_h == NULL) {
849-
return NGX_ERROR;
850-
}
832+
//hh = ngx_hash_find(&umcf->headers_in_hash, h->hash,
833+
// h->lowcase_key, h->key.len);
834+
835+
// While using proxy_pass with a combination of other factores
836+
// there seems to be a memory corruption if we use hh->copy_handler.
837+
// Temporary using new_h. This demand a further investigation.
838+
//
839+
//if (hh) {
840+
// /* copy all */
841+
// if (hh->copy_handler(r, h, hh->conf) != NGX_OK) {
842+
// return 0;
843+
// }
844+
//} else {
845+
846+
/* Add the response header directly to headers_out if not present in
847+
* the hash. This is done to passthrough such response headers.
848+
* Remember the response headers were cleared earlier using
849+
* ngx_http_clean_header(r) call in ngx_http_modsecurity_save_headers_out.
850+
*/
851851

852-
new_h->hash = h->hash;
853-
new_h->key = h->key;
854-
new_h->value = h->value;
852+
new_h = ngx_list_push(&r->headers_out.headers);
853+
if (new_h == NULL) {
854+
return NGX_ERROR;
855855
}
856856

857+
new_h->hash = h->hash;
858+
new_h->key = h->key;
859+
new_h->value = h->value;
860+
861+
// }
862+
857863
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
858864
"ModSecurity: save headers out: \"%V: %V\"",
859865
&h->key, &h->value);

0 commit comments

Comments
 (0)