Tommy 碎碎念

Tommy Wu's blog

« 上一篇 | 下一篇 »

奇怪的 nginx segfault 錯誤
post by tommy @ 12 二月, 2012 10:17

這幾個星期發現家裡的 nginx 在隔約 2 小時左右 (間隔不一定, 有時很久沒出現, 有時連續出現, 不過多數看起來是隔兩小時出現一次), 就會出現一次 segfault, 雖然只是其中一個 worker, 也會被重新執行, 並不會影響到實際的 web server 運作, 不過.. 在 log 裡常常看到這個錯誤也很奇怪.

試著在 error_log 中加上 debug 參數, 不過除了 log 變大之外, 出現 segfault 的那時間, 並沒有任何的 log 產生 (來不及寫出就當了? 還是還沒寫 log 就當了?) , 所以也看不出來是什麼原因.

加上些設定, 產生 coredump file 之後, 可以知道問題都是發生在 src/http/modules/ngx_http_index_module.c 裡頭的 ngx_http_index_handler() 一開始的地方所產生的. 試著加上一些 log, 不過... 透過 nginx 的 log function 來寫出, 似乎都沒有真的寫入到 log (是我用錯了嗎?), 最後乾脆自己用一般的 C stdio 來處理, 直接寫到 /tmp 的某個檔案去. 終於發現 segfault 產生時, r->uri.len 的值是 0, 導致一開始 -1後去讀取的位址不合法.

不過想不出為什麼會有長度是 0 的 uri 這種 request, 不知道怎麼由源頭處理, 所以就直接治標.... 檢查是 0 就直接回錯誤, 不再去處理. 這樣子做之後, 用了三天, 應該是解決這個問題了, 至少是沒看到 segfault 的錯誤了.

修正方式如下:

diff -Nur nginx-1.0.12.orig/src/http/modules/ngx_http_autoindex_module.c nginx-1.0.12/src/http/modules/ngx_http_autoindex_module.c
--- nginx-1.0.12.orig/src/http/modules/ngx_http_autoindex_module.c 2012-02-06 04:02:59.000000000 +0800
+++ nginx-1.0.12/src/http/modules/ngx_http_autoindex_module.c 2012-02-10 20:01:54.719060672 +0800
@@ -157,6 +157,10 @@
static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
 
+ // twu2 begin, try to avoid segfault
+ if (r == NULL || r->uri.len == 0)
+ return NGX_HTTP_BAD_REQUEST;
+ // twu2 end
if (r->uri.data[r->uri.len - 1] != '/') {
return NGX_DECLINED;
}
diff -Nur nginx-1.0.12.orig/src/http/modules/ngx_http_index_module.c nginx-1.0.12/src/http/modules/ngx_http_index_module.c
--- nginx-1.0.12.orig/src/http/modules/ngx_http_index_module.c 2012-02-06 04:02:59.000000000 +0800
+++ nginx-1.0.12/src/http/modules/ngx_http_index_module.c 2012-02-10 20:01:15.615399122 +0800
@@ -109,6 +109,10 @@
ngx_http_index_loc_conf_t *ilcf;
ngx_http_script_len_code_pt lcode;
 
+ // twu2 begin, try to avoid segfault
+ if (r == NULL || r->uri.len == 0)
+ return NGX_HTTP_BAD_REQUEST;
+ // twu2 end
if (r->uri.data[r->uri.len - 1] != '/') {
return NGX_DECLINED;
}
diff -Nur nginx-1.0.12.orig/src/http/modules/ngx_http_random_index_module.c nginx-1.0.12/src/http/modules/ngx_http_random_index_module.c
--- nginx-1.0.12.orig/src/http/modules/ngx_http_random_index_module.c 2012-02-06 04:02:59.000000000 +0800
+++ nginx-1.0.12/src/http/modules/ngx_http_random_index_module.c 2012-02-10 20:02:04.065805793 +0800
@@ -83,6 +83,10 @@
ngx_array_t names;
ngx_http_random_index_loc_conf_t *rlcf;
 
+ // twu2 begin, try to avoid segfault
+ if (r == NULL || r->uri.len == 0)
+ return NGX_HTTP_BAD_REQUEST;
+ // twu2 end
if (r->uri.data[r->uri.len - 1] != '/') {
return NGX_DECLINED;
}
diff -Nur nginx-1.0.12.orig/src/http/modules/ngx_http_static_module.c nginx-1.0.12/src/http/modules/ngx_http_static_module.c
--- nginx-1.0.12.orig/src/http/modules/ngx_http_static_module.c 2012-02-06 04:02:59.000000000 +0800
+++ nginx-1.0.12/src/http/modules/ngx_http_static_module.c 2012-02-10 20:02:16.225907865 +0800
@@ -59,6 +59,10 @@
ngx_open_file_info_t of;
ngx_http_core_loc_conf_t *clcf;
 
+ // twu2 begin, try to avoid segfault
+ if (r == NULL || r->uri.len == 0)
+ return NGX_HTTP_BAD_REQUEST;
+ // twu2 end
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
return NGX_HTTP_NOT_ALLOWED;
}

由 log 中猜測, 可能是下列的 request 造成的:

114.36.29.113 - - [11/Feb/2012:07:48:59 +0800] "cix\x92P%Z\xB8\x5CI'\xAF\x0B\xCD|}\xDB\x06\xCB\x14:\x05\x98\xEA\x86\xA1\x91 \xD0\xF88\x8A\xCC`\xC6" 400 173 "-" "-"
188.134.92.205 - - [11/Feb/2012:09:49:17 +0800] "\x03\xE3y\xB4\xDD\xEE\xB0\x97\xCC?\xDD,\xBA\xB2ux{\x97\xBD\xD0\xDA \xB7g\x86?1\xB5\xB5\x98K\x8B\xBB\x1D\x09`\xB0" 400 173 "-" "-"
99.125.248.167 - - [11/Feb/2012:11:49:36 +0800] "\xC1\xF0\x15\x0B\xF6b\x9D\x02n\xF7\xE4\xC7\xD7\xD4N!\xB3E\x22\xBB\xFE\x84\xCFrX\xC0\xEEp\x06\x1F \xC7" 400 173 "-" "-"
69.207.182.218 - - [11/Feb/2012:13:49:54 +0800] "\x0C\xC7/\xF57\xDF\x5C\x9D\xCC\xAB;M\xD8O\x08~\xBCL\x02\xCD\xD7 b#\x88\xB9\xAB\x93\xE1\xA1\xD7\x8C\x1A\xDA\x92\xFA#\xA6q\xC3S\xCF" 400 173 "-" "-"
94.159.209.183 - - [11/Feb/2012:15:50:13 +0800] "\x94\xC9t\xE3\x05E\xDA\xB3\xDC)\xFC\xCA\x09\xB4s~<,\x18M\xAE\xD1\x19\xFAO|\xC2\xECD\x06\x85\x0C\x8B\xF5\x8F\x8D\xAB\xC8\xEF\xED\x12;\xF8\x94\x06\x8FV;>\xF7\x08\xFB" 400 173 "-" "-"

原本以為那些是中文, 不過用 utf-8 或 big5 來看, 都不是中文字, 是某個 web server 的 bug 嗎? 若是入侵的動作, 每隔兩小時才出現一次 (每次來源都不同), 也很奇怪.

Del.icio.us Furl HEMiDEMi Technorati MyShare
commons icon [1] Re:奇怪的 nginx segfault 錯誤 [ 回覆 ]

我也有出現類似的字串
看起來應該是有機器人在掃漏洞

迴響
暱稱:
標題:
個人網頁:
電子郵件:
authimage

迴響

  

Bad Behavior 已經阻擋了 61 個過去 7 天試圖闖關的垃圾迴響與引用。
Power by LifeType. Template design by JamesHuang. Valid XHTML and CSS