Re: Haproxy crash while initializing compression

2013-04-28 Thread Willy Tarreau
Hi Godbach,

On Sun, Apr 28, 2013 at 12:16:17PM +0800, Godbach wrote:
 Hi, all
 
 Haproxy of latest snapshot will crash while initializing compression
 under such configuration:
 
 global
 ...
 tune.zlib.memlevel 6
 tune.zlib.windowsize 8
 frontend
 ...
 compression algo gzip deflate
 ...
 
 The coredump information as below:
 
 Core was generated by `./haproxy -f h.cfg -d'.
 Program terminated with signal 11, Segmentation fault.
 #0  0x00360e4066d3 in deflateReset () from /lib64/libz.so.1
 Missing separate debuginfos, use: debuginfo-install
 glibc-2.12-1.47.el6.x86_64 keyutils-libs-1.4-4.el6.x86_64
 krb5-libs-1.9-33.el6_3.3.x86_64 libcom_err-1.41.12-12.el6.x86_64
 libselinux-2.0.94-5.3.el6.x86_64 nss-softokn-freebl-3.12.9-11.el6.x86_64
 openssl-1.0.0-25.el6_3.1.x86_64 zlib-1.2.3-27.el6.x86_64
 (gdb) bt
 #0  0x00360e4066d3 in deflateReset () from /lib64/libz.so.1
 #1  0x00360e406aa4 in deflateInit_ () from /lib64/libz.so.1
 #2  0x00460d63 in deflate_init (comp_ctx=0x7fffe3b7b728,
 level=9) at src/compression.c:524
 #3  0x0041c8d8 in cfg_parse_listen (file=0x7fffe3b7d35d h.cfg,
 linenum=27, args=0x7fffe3b7bfd0, kwm=value optimized out)
 at src/cfgparse.c:5560
 #4  0x0041f418 in readcfgfile (file=0x7fffe3b7d35d h.cfg) at
 src/cfgparse.c:5987
 #5  0x00405736 in init (argc=value optimized out, argv=value
 optimized out) at src/haproxy.c:646
 #6  0x00406c49 in main (argc=value optimized out,
 argv=0x7fffe3b7c4b8) at src/haproxy.c:1287
 (gdb) quit
 
 
 Linenum 27 of cofiguration file is the compression algo config:
compression algo gzip deflate
 
 There will be no crash if I use the following configuration:
compression algo deflate gzip
 The only difference is to change the order of gzip and deflate.
 
 Ther will be no crash either if set tune.zlib.windowsize to default
 value 15 just keeping the algo order 'gzip deflate'.
 
 Zlib version is zlib-1.2.3.

Thank you for reporting this bug. I just found that deflate() still
uses default memory settings, but with the same allocator as gzip
which used the smaller settings you specified. The net result is
that the first call to deflate() overflows the small memory area.
Fortunately, since the compression algo is tested while parsing the
config, the error is detected very early.

I've fixed the bug, tested it and pushed it. You can use the attached
patch.

Thanks,
Willy

From c5599e7c498ea51a11fa939026c9249012eaa19c Mon Sep 17 00:00:00 2001
From: Willy Tarreau w...@1wt.eu
Date: Sun, 28 Apr 2013 08:52:52 +0200
Subject: [PATCH] BUG/MEDIUM: compression: the deflate algorithm must use
 global settings as well

Global compression settings (windowsize and memlevel) were only considered
for the gzip algorithm but not the deflate algorithm. Since a single allocator
is used for both algos, if gzip was first initialized the memory with parameters
smaller than default, then initializing deflate after with default settings
would result in overusing the small allocated areas.

To fix this, we make use of deflateInit2() for deflate_init() as well.

Thanks to Godbach for reporting this bug, introduced by in 1.5-dev13 by commit
8b52bb38. No backport is needed.
---
 src/compression.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/compression.c b/src/compression.c
index 906e4fd..c8e9144 100644
--- a/src/compression.c
+++ b/src/compression.c
@@ -513,7 +513,7 @@ int deflate_init(struct comp_ctx **comp_ctx, int level)
 
strm = (*comp_ctx)-strm;
 
-   if (deflateInit(strm, level) != Z_OK) {
+   if (deflateInit2(strm, level, Z_DEFLATED, global.tune.zlibwindowsize, 
global.tune.zlibmemlevel, Z_DEFAULT_STRATEGY) != Z_OK) {
deinit_comp_ctx(comp_ctx);
return -1;
}
-- 
1.7.12.2.21.g234cd45.dirty



Re: Haproxy crash while initializing compression

2013-04-28 Thread Godbach



Hi Godbach,

On Sun, Apr 28, 2013 at 12:16:17PM +0800, Godbach wrote:

Hi, all

Haproxy of latest snapshot will crash while initializing compression
under such configuration:

global
 ...
 tune.zlib.memlevel 6
 tune.zlib.windowsize 8
frontend
 ...
 compression algo gzip deflate
 ...

The coredump information as below:

Core was generated by `./haproxy -f h.cfg -d'.
Program terminated with signal 11, Segmentation fault.
#0  0x00360e4066d3 in deflateReset () from /lib64/libz.so.1
Missing separate debuginfos, use: debuginfo-install
glibc-2.12-1.47.el6.x86_64 keyutils-libs-1.4-4.el6.x86_64
krb5-libs-1.9-33.el6_3.3.x86_64 libcom_err-1.41.12-12.el6.x86_64
libselinux-2.0.94-5.3.el6.x86_64 nss-softokn-freebl-3.12.9-11.el6.x86_64
openssl-1.0.0-25.el6_3.1.x86_64 zlib-1.2.3-27.el6.x86_64
(gdb) bt
#0  0x00360e4066d3 in deflateReset () from /lib64/libz.so.1
#1  0x00360e406aa4 in deflateInit_ () from /lib64/libz.so.1
#2  0x00460d63 in deflate_init (comp_ctx=0x7fffe3b7b728,
level=9) at src/compression.c:524
#3  0x0041c8d8 in cfg_parse_listen (file=0x7fffe3b7d35d h.cfg,
linenum=27, args=0x7fffe3b7bfd0, kwm=value optimized out)
 at src/cfgparse.c:5560
#4  0x0041f418 in readcfgfile (file=0x7fffe3b7d35d h.cfg) at
src/cfgparse.c:5987
#5  0x00405736 in init (argc=value optimized out, argv=value
optimized out) at src/haproxy.c:646
#6  0x00406c49 in main (argc=value optimized out,
argv=0x7fffe3b7c4b8) at src/haproxy.c:1287
(gdb) quit


Linenum 27 of cofiguration file is the compression algo config:
compression algo gzip deflate

There will be no crash if I use the following configuration:
compression algo deflate gzip
The only difference is to change the order of gzip and deflate.

Ther will be no crash either if set tune.zlib.windowsize to default
value 15 just keeping the algo order 'gzip deflate'.

Zlib version is zlib-1.2.3.


Thank you for reporting this bug. I just found that deflate() still
uses default memory settings, but with the same allocator as gzip
which used the smaller settings you specified. The net result is
that the first call to deflate() overflows the small memory area.
Fortunately, since the compression algo is tested while parsing the
config, the error is detected very early.

I've fixed the bug, tested it and pushed it. You can use the attached
patch.

Thanks,
Willy




Hi, Willy

Thank you for replying so quickly.

Yeah, I have tested the patch just now and it works well. I have viewded 
the codes that test comression algo during parsing config yesterday. It 
is indeed a wonderful design. Perfect work!


Best Regards,
Godbach



Haproxy crash while initializing compression

2013-04-27 Thread Godbach
Hi, all

Haproxy of latest snapshot will crash while initializing compression
under such configuration:

global
...
tune.zlib.memlevel 6
tune.zlib.windowsize 8
frontend
...
compression algo gzip deflate
...

The coredump information as below:

Core was generated by `./haproxy -f h.cfg -d'.
Program terminated with signal 11, Segmentation fault.
#0  0x00360e4066d3 in deflateReset () from /lib64/libz.so.1
Missing separate debuginfos, use: debuginfo-install
glibc-2.12-1.47.el6.x86_64 keyutils-libs-1.4-4.el6.x86_64
krb5-libs-1.9-33.el6_3.3.x86_64 libcom_err-1.41.12-12.el6.x86_64
libselinux-2.0.94-5.3.el6.x86_64 nss-softokn-freebl-3.12.9-11.el6.x86_64
openssl-1.0.0-25.el6_3.1.x86_64 zlib-1.2.3-27.el6.x86_64
(gdb) bt
#0  0x00360e4066d3 in deflateReset () from /lib64/libz.so.1
#1  0x00360e406aa4 in deflateInit_ () from /lib64/libz.so.1
#2  0x00460d63 in deflate_init (comp_ctx=0x7fffe3b7b728,
level=9) at src/compression.c:524
#3  0x0041c8d8 in cfg_parse_listen (file=0x7fffe3b7d35d h.cfg,
linenum=27, args=0x7fffe3b7bfd0, kwm=value optimized out)
at src/cfgparse.c:5560
#4  0x0041f418 in readcfgfile (file=0x7fffe3b7d35d h.cfg) at
src/cfgparse.c:5987
#5  0x00405736 in init (argc=value optimized out, argv=value
optimized out) at src/haproxy.c:646
#6  0x00406c49 in main (argc=value optimized out,
argv=0x7fffe3b7c4b8) at src/haproxy.c:1287
(gdb) quit


Linenum 27 of cofiguration file is the compression algo config:
   compression algo gzip deflate

There will be no crash if I use the following configuration:
   compression algo deflate gzip
The only difference is to change the order of gzip and deflate.

Ther will be no crash either if set tune.zlib.windowsize to default
value 15 just keeping the algo order 'gzip deflate'.

Zlib version is zlib-1.2.3.

Best Regards,
Godbach