ModSecurity icon indicating copy to clipboard operation
ModSecurity copied to clipboard

segfault - ModSecurity/2.9.1,nginx/1.12.0

Open JarRoDk opened this issue 9 years ago • 14 comments

I had problem with stability of modproxy and nginx, my developers want to have WAF to filter request. Even if i use only "SecRuleEngine DetectionOnly" in modsecure conf i can call "signal 17 (SIGCHLD) received" on nginx server. I understand that developers should fix client application but why it couse segmentfault.

Nginx Error log

2017/05/15 08:38:42 [notice] 31342#31342: exit
2017/05/15 08:38:42 [notice] 31497#31497: ModSecurity for nginx (STABLE)/2.9.1 (http://www.modsecurity.org/) configured.
2017/05/15 08:38:42 [notice] 31497#31497: ModSecurity: APR compiled version="1.5.1-dev"; loaded version="1.5.1-dev"
2017/05/15 08:38:42 [notice] 31497#31497: ModSecurity: PCRE compiled version="8.31 "; loaded version="8.31 2012-07-06"
2017/05/15 08:38:42 [notice] 31497#31497: ModSecurity: LUA compiled version="Lua 5.1"
2017/05/15 08:38:42 [notice] 31497#31497: ModSecurity: YAJL compiled version="2.0.4"
2017/05/15 08:38:42 [notice] 31497#31497: ModSecurity: LIBXML compiled version="2.9.1"
2017/05/15 08:38:42 [notice] 31497#31497: ModSecurity: Status engine is currently disabled, enable it by set SecStatusEngine to On.
2017/05/15 08:38:42 [warn] 31497#31497: conflicting server name "localhost" on 0.0.0.0:80, ignored
2017/05/15 08:38:42 [notice] 31497#31497: using the "epoll" event method
2017/05/15 08:38:42 [warn] 31497#31497: 4096 worker_connections exceed open file resource limit: 1024
2017/05/15 08:38:42 [notice] 31497#31497: nginx/1.12.0
2017/05/15 08:38:42 [notice] 31497#31497: built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
2017/05/15 08:38:42 [notice] 31497#31497: OS: Linux 3.16.0-77-generic
2017/05/15 08:38:42 [notice] 31497#31497: getrlimit(RLIMIT_NOFILE): 1024:4096
2017/05/15 08:38:42 [notice] 31498#31498: start worker processes
2017/05/15 08:38:42 [notice] 31498#31498: start worker process 31500
2017/05/15 08:38:50 [notice] 31498#31498: signal 17 (SIGCHLD) received
**2017/05/15 08:38:50 [alert] 31498#31498: worker process 31500 exited on signal 11 (core dumped)**
2017/05/15 08:38:50 [notice] 31498#31498: start worker process 31508

syslog

...
nginx[32493]: segfault at 0 ip 000000000042137d sp 00007ffd55d9d3b8 error 4 in nginx[400000+1a1000]
...

My Build

# nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
built with OpenSSL 1.0.1f 6 Jan 2014
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --without-http_access_module --without-http_auth_basic_module --without-http_autoindex_module --without-http_browser_module --without-http_charset_module --without-http_empty_gif_module --without-http_fastcgi_module --without-http_gzip_module --without-http_scgi_module --without-http_ssi_module --without-http_userid_module --without-http_uwsgi_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upstream_check_module-master --with-ld-opt='-L /usr/local/lib' --add-dynamic-module=/root/ModSecurity-nginx --add-module=/root/modsecurity-2.9.1/nginx/modsecurity

My dump https://gist.github.com/JarRoDk/e328aa8bc6e895aaee76ff22b0bd2761#file-segfault-modsecure2-9-1-nginx1-12-0

My config

server {
    listen       443 ssl;
    server_name  localhost;
    error_log /var/log/nginx/server.log debug;
    ssl_certificate      <hide>.pem;
    ssl_certificate_key  <hide>.pem;
        location = / {
                proxy_pass http://<hide>:443;
                ModSecurityEnabled on;
                ModSecurityConfig modsecurity.conf;
        }
}

Modsecurity.conf

# grep -vE "^$|^#" /etc/nginx/modsecurity.conf
SecRuleEngine DetectionOnly

JarRoDk avatar May 15 '17 09:05 JarRoDk

Hi @JarRoDk,

Please use ModSecurity-nginx instead of version 2.9.1. Further information about ModSecurity-nginx is available here: https://github.com/SpiderLabs/ModSecurity-nginx

zimmerle avatar May 15 '17 12:05 zimmerle

I am trying to understand how i should use modsecurity-nginx i look in .travis file https://github.com/SpiderLabs/ModSecurity-nginx/blob/master/.travis.yml#L36

- git clone https://github.com/SpiderLabs/ModSecurity.git
- cd ModSecurity
- git checkout v3/master
- git submodule init
- git submodule update
- ./build.sh
- ./configure --without-lmdb
- make
- sudo make install
- cd ..
- wget http://nginx.org/download/nginx-${VER_NGINX}.tar.gz && tar -xf nginx-${VER_NGINX}.tar.gz
- cd nginx-${VER_NGINX}
- ./configure --add-module=../ModSecurity-nginx

ModSecurity-nginx folder should exist, and in this procedure i don't see it ?

When i try normally get nginx source and git clone https://github.com/SpiderLabs/ModSecurity-nginx

./configure --add-module=../ModSecurity-nginx i had error:

adding module in ../ModSecurity-nginx
...
checking for ModSecurity library ... not found
checking for ModSecurity library in /usr/local/modsecurity ... not found
 ./configure: error: ngx_http_modsecurity_module requires the ModSecurity library.

JarRoDk avatar May 15 '17 13:05 JarRoDk

@zimmerle Thx for your help. My problem was that i don't install modsecure after configure and make. Because i build package with dpkg-buildpackage. @vikas027 maybe you will need this.

Before build:

export MODSECURITY_INC=/usr/local/modsecurity/lib
export MODSECURITY_LIB=/usr/local/modsecurity/include

Nginx: --add-module=../ModSecurity-nginx

Build log:

checking for ngx_http_upstream_check_module ... found
 + ngx_http_upstream_check_module was configured
adding module in /root/ModSecurity-nginx
checking for ModSecurity library in "/usr/local/modsecurity/lib" and "/usr/local/modsecurity/include" (specified by the MODSECURITY_LIB and MODSECURITY_INC env) ... found
 + ngx_http_modsecurity_module was configured
--- cut ---

i was also mislead by "ModSecurityEnabled on" in my config, it cause unknown directive "ModSecurityEnabled" i thought that modsecure was not installed at all i replace it to modsecurity on;

now i am surprised by this error: Invalid input: SecStreamInBodyInspection On in /etc/nginx/sites-enabled/test.conf:10 did i miss something again ?

JarRoDk avatar May 16 '17 15:05 JarRoDk

SecStreamInBodyInspection is no longer supported, it is safe to remove. By default v3 will try to use stream whenever it is possible.

zimmerle avatar May 17 '17 13:05 zimmerle

@JarRoDk when I make the Modsecurity-nginx is show some error "checking for ModSecurity library in "/usr/local/modsecurity/include" and "/usr/local/modsecurity/lib" (specified by the MODSECURITY_LIB and MODSECURITY_INC env) ... not found ./configure: error: ngx_http_modsecurity_module requires the ModSecurity library and MODSECURITY_LIB is defined as "/usr/local/modsecurity/include" and MODSECURITY_INC (path for modsecurity.h) "/usr/local/modsecurity/lib", but we cannot find ModSecurity there."

Hello-Linux avatar May 18 '17 04:05 Hello-Linux

@Hello-Linux yes this was hart to understand for me to. Please try:

- git clone https://github.com/SpiderLabs/ModSecurity.git
- cd ModSecurity
- git checkout v3/master
- git submodule init
- git submodule update
- ./build.sh
- ./configure 

and what i forgot is to install ModSecurity (this is writen in documentation!)

- make && make install

after that i had /usr/local/modsecurity/include and /usr/local/modsecurity/lib existing so when you will use

./configure --add-module=../ModSecurity-nginx

you should see:

adding module in /root/ModSecurity-nginx
checking for ModSecurity library in "/usr/local/modsecurity/lib" and "/usr/local/modsecurity/include" (specified by the MODSECURITY_LIB and MODSECURITY_INC env) ... found
 + ngx_http_modsecurity_module was configured

JarRoDk avatar May 18 '17 06:05 JarRoDk

@JarRoDk Yes,but There is something wrong with me according to your method you install libmodsecurity successful? error

Hello-Linux avatar May 18 '17 07:05 Hello-Linux

@zimmerle again thanks, i was to much focus on that directive because in my previous compilation content-type was not inspect without it:

SecRule &REQUEST_HEADERS_NAMES:Content-Type     "@eq 0" "log,deny,msg:'Content-Type not exist', status:406,phase:2,id:'20005'"
SecRule REQUEST_HEADERS:Content-Type            "!^text/xml; charset=utf-8$" "log,allow,msg:'Invalid Content-Type for XML', status:400,phase:2,id:'20006'"
SecRule STREAM_INPUT_BODY                       "^([\r\n]|^$|^ )" "log,deny,msg:'Block unwanted body request', status:406,phase:2,id:'20009'"

In new version i don't have any segfaults but my rules are not working:

- request must have "REQUEST_HEADER = Content-Type" exist
- accept only "Content-Type = text/xml; charset=utf-8"
- 3 Rule was to deny empty or request body with \r ( less important but i am curious )
- all other request deny

Sims in new version this working little different:

- rule 1 now not working 
- rule 2 how i should inverse rule in right way
- rule 3 syntax error, unexpected Operator RX (content only) 

My debug:

[4] JSON parser initialization
[9] yajl JSON parsing callback initialization
[4] Initializing transaction
[4] Transaction context created.
[4] Starting phase CONNECTION. (SecRules 0)
[9] This phase consists of 0 rule(s).
[4] Starting phase URI. (SecRules 0 + 1/2)
[4] Starting phase REQUEST_HEADERS.  (SecRules 1)
[9] This phase consists of 0 rule(s).
[9] Appending request body: 4 bytes. Limit set to: 13107200.000000
[4] Starting phase REQUEST_BODY. (SecRules 2)
[9] This phase consists of 5 rule(s).
[4] (Rule: 200001) Executing operator "Eq" with param "0" against REQBODY_ERROR.
[6] Resolving: reqbody_error_msg to: NULL
[9] Target value: "0" (Variable: REQBODY_ERROR)
[4] Rule returned 0.
[4] Matched vars cleaned.
[4] (Rule: 200003) Executing operator "Eq" with param "0" against MULTIPART_UNMATCHED_BOUNDARY.
[9] Target value: "" (Variable: MULTIPART_UNMATCHED_BOUNDARY)
[4] Rule returned 0.
[4] Matched vars cleaned.
[4] (Rule: 200004) Executing operator "StrEq" with param "0" against TX.
[6] Resolving: MATCHED_VAR_NAME to: NULL
[4] Rule returned 0.
[4] Matched vars cleaned.
[4] (Rule: 20005) Executing operator "Eq" with param "0" against REQUEST_HEADERS_NAMES.
[9] Target value: "1" (Variable: REQUEST_HEADERS_NAMES)
[4] Rule returned 0.
[4] Matched vars cleaned.
[4] (Rule: 20006) Executing operator "Rx" with param "!^text/xml; charset=utf-8$" against REQUEST_HEADERS:Content-Type.
[4] Rule returned 0.
[4] Matched vars cleaned.
[4] Starting phase RESPONSE_HEADERS. (SecRules 3)
[9] This phase consists of 0 rule(s).
[9] Appending response body: 0 bytes. Limit set to: 524288.000000
[4] Starting phase RESPONSE_BODY. (SecRules 4)
[9] This phase consists of 0 rule(s).
[4] Starting phase LOGGING. (SecRules 5)
[9] This phase consists of 0 rule(s).
[8] Checking if this request is suitable to be saved as an audit log.
[8] Checking if this request is relevant to be part of the audit logs.
[5] Saving this request as part of the audit logs.
[8] Request was relevant to be saved. Parts: 6006
[4] JSON: Cleaning up JSON results

JarRoDk avatar May 18 '17 07:05 JarRoDk

@zimmerle Yes,but There is something wrong with me according to your method you install libmodsecurity successful? error

Hello-Linux avatar May 18 '17 07:05 Hello-Linux

@Hello-Linux i think you miss some package, the funny think is that very help me docker manifestation please look at https://github.com/fooinha/docker-nginx-modsecurity/blob/master/build/Dockerfile#L21-L45

apt-get install -y --fix-missing \
    autoconf            \
    automake            \
    apt-utils           \
    bison               \
    build-essential     \
    curl                \
    devscripts          \
    dh-autoreconf       \
    doxygen             \
    flex                \
    g++                 \
    git-core            \
    libtool             \
    make                \
    pkg-config          \
    tree                \
    apache2-dev         \
    libcurl4-gnutls-dev \
    libgeoip-dev        \
    liblmdb-dev         \
    libpcre++-dev       \
    libxml2-dev         \
    libyajl-dev         \
zlib1g-dev

This is my example: image

JarRoDk avatar May 18 '17 07:05 JarRoDk

@JarRoDk Thank you ,Installation of most people is not modsecurity-nginx I use the original installation method can be used normally, what is the difference between the two versions?

Hello-Linux avatar May 18 '17 07:05 Hello-Linux

I find solution off course together with questions.

Old version:

SecRule &REQUEST_HEADERS_NAMES:Content-Type "@eq 0" "log,deny,msg:'Content-Type not exist', status:406,phase:2,id:'20005'"
SecRule REQUEST_HEADERS:Content-Type        "!^text/xml; charset=utf-8$" "log,allow,msg:'Invalid Content-Type for XML', status:400,phase:2,id:'20006'"
SecRule STREAM_INPUT_BODY                   "^([\r\n]|^$|^ )" "log,deny,msg:'Block unwanted body request', status:406,phase:2,id:'20009'"`

New version:

SecRule REQUEST_HEADERS_NAMES 		"!@contains Content-Type" "log,deny,msg:'Content-Type not exist', status:406,phase:2,id:'20004', setvar:'tx.msg=%{rule.msg}"
SecRule REQUEST_HEADERS:Content-Type    "!@rx ^(text/xml; charset=utf-8)$"  "log,deny,msg:'Invalid Content-Type for XML', status:400,phase:2,id:'20006'"
SecRule REQUEST_BODY 	                "@rx ^([\r\n]|^$|^ )" "log,deny,msg:'Block unwanted body request', status:406,phase:2,id:'20009'"
SecRule REQUEST_HEADERS:Content-Length  "^0$" "log,deny,msg:'Block empty body request', status:406,phase:2,id:'20010'" 

STREAM_INPUT_BODY seams to fail ( still not ported to v3 ? )
$^ is not discover empty request_body ( in older version this works )

for good understanding ModSecurity-2.9.1 cannot be build with modsecurity-nginx connector ? because after install i don't see /usr/local/modsecurity/lib and /usr/local/modsecurity/include

JarRoDk avatar May 18 '17 13:05 JarRoDk

Hi @JarRoDk,

At rule 2 you may want to check for text/xml not ``text/xml; charset...```. STREAM_INPUT_BODY is not supported.

zimmerle avatar May 18 '17 14:05 zimmerle

Ok thx

JarRoDk avatar May 19 '17 14:05 JarRoDk