fluent-bit icon indicating copy to clipboard operation
fluent-bit copied to clipboard

Fluent-bit crashes with a long input in ES output

Open newtoncorp opened this issue 8 years ago • 6 comments

Bug Report

Describe the bug Fluent-bit completely crashes on a realloc failure. This is probably due to a heap corruption in flb_msgpack_raw_to_json_str (https://github.com/fluent/fluent-bit/blob/45a85275adb69d92e57339671558be9f787fe0f8/src/flb_pack.c#L643)

To Reproduce

Create a regex with a bunch of keys and flush to ES. Will explain with details how to reproduce in a controlled environment in a followup comment.

Expected behavior

Not crashing.

Your Environment

  • Version used: head on master
  • Configuration: syslog udp input with haproxy logs, huge regex parser, es output
  • Environment name and version (e.g. Kubernetes? What version?): Compiled on Xenial, ran on Bionic
  • Server type and version: VM
  • Operating System and version: Bionic
  • Filters and plugins: syslog udp, regex parser, es output

Additional context

Program crashes.

I have a local bypass of the issue which is the following :

@@ -628,7 +629,7 @@ int flb_msgpack_raw_to_json_str(char *buf, size_t buf_size,
         return -1;
     }

-    json_size = (buf_size * 1.2);
+    json_size = (buf_size * 5);
     json_buf = flb_calloc(1, json_size);
     if (!json_buf) {
         flb_errno();

newtoncorp avatar Sep 19 '18 14:09 newtoncorp

I've recompiled on bionic just to be sure it wasn't an incompatibility between xenial and bionic.

Gdb trace :

corrupted size vs. prev_size

Thread 1 "fluent-bit-bion" received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) thread apply all bt

Thread 2 (Thread 0x7ffff6ded700 (LWP 30975)):
#0  0x00007ffff6f0fbb7 in epoll_wait (epfd=3, events=0x555555adeba0, maxevents=16, timeout=-1) at ../sysdeps/unix/sysv/linux/epoll_wait.c:30
#1  0x00005555557c7175 in _mk_event_wait ()
#2  0x00005555557c7465 in mk_event_wait ()
#3  0x0000555555590197 in log_worker_collector ()
#4  0x00005555555a057d in step_callback ()
#5  0x00007ffff7bbd6db in start_thread (arg=0x7ffff6ded700) at pthread_create.c:463
#6  0x00007ffff6f0f88f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Thread 1 (Thread 0x7ffff7fea980 (LWP 30968)):
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1  0x00007ffff6e2e801 in __GI_abort () at abort.c:79
#2  0x00007ffff6e77897 in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7ffff6fa4b9a "%s\n") at ../sysdeps/posix/libc_fatal.c:181
#3  0x00007ffff6e7e90a in malloc_printerr (str=str@entry=0x7ffff6fa2c9d "corrupted size vs. prev_size") at malloc.c:5350
#4  0x00007ffff6e83acf in _int_realloc (av=av@entry=0x7ffff71d9c40 <main_arena>, oldp=oldp@entry=0x555555c28640, oldsize=oldsize@entry=1072, nb=nb@entry=1200) at malloc.c:4564
#5  0x00007ffff6e86f9b in __GI___libc_realloc (oldmem=0x555555c28650, bytes=1192) at malloc.c:3230
#6  0x00005555555aa75c in flb_realloc ()
#7  0x00005555555ac742 in flb_msgpack_raw_to_json_str ()
#8  0x00005555555d5ffe in elasticsearch_format ()
#9  0x00005555555d661b in cb_es_flush ()
#10 0x000055555559c7da in output_pre_cb_flush ()
#11 0x0000555555682887 in co_init ()
#12 0x0000000000006210 in ?? ()
#13 0x0000000000000071 in ?? ()
#14 0x00007ffff7fea750 in ?? ()
#15 0x0000555555c3b670 in ?? ()
#16 0x0000000000000000 in ?? ()

Unable to reproduce in lab for now.

newtoncorp avatar Sep 21 '18 13:09 newtoncorp

Here's another gdb trace with debug activated, now able to reproduce in lab with a single haproxy log line.

*** Error in `/root/fluent-bit/build/bin/fluent-bit': double free or corruption (!prev): 0x0000000000db2c90 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7ffff6f2a7e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7ffff6f3337a]
/lib/x86_64-linux-gnu/libc.so.6(+0x83350)[0x7ffff6f36350]
/lib/x86_64-linux-gnu/libc.so.6(realloc+0x179)[0x7ffff6f37839]
/root/fluent-bit/build/bin/fluent-bit[0x442fa8]
/root/fluent-bit/build/bin/fluent-bit(flb_msgpack_raw_to_json_str+0x17f)[0x444f8e]
/root/fluent-bit/build/bin/fluent-bit[0x46e821]
/root/fluent-bit/build/bin/fluent-bit(cb_es_flush+0x90)[0x46ee3d]
/root/fluent-bit/build/bin/fluent-bit[0x4351c7]
/root/fluent-bit/build/bin/fluent-bit[0x51a367]
[0x96e4e2]
======= Memory map: ========
00400000-0073f000 r-xp 00000000 2b:01 529148                             /root/fluent-bit/build/bin/fluent-bit
0093e000-00948000 r--p 0033e000 2b:01 529148                             /root/fluent-bit/build/bin/fluent-bit
00948000-0094d000 rw-p 00348000 2b:01 529148                             /root/fluent-bit/build/bin/fluent-bit
0094d000-01248000 rw-p 00000000 00:00 0                                  [heap]
7ffff0000000-7ffff0021000 rw-p 00000000 00:00 0
7ffff0021000-7ffff4000000 ---p 00000000 00:00 0
7ffff66b2000-7ffff66b3000 ---p 00000000 00:00 0
7ffff66b3000-7ffff6eb3000 rw-p 00000000 00:00 0
7ffff6eb3000-7ffff7073000 r-xp 00000000 2b:01 918789                     /lib/x86_64-linux-gnu/libc-2.23.so
7ffff7073000-7ffff7273000 ---p 001c0000 2b:01 918789                     /lib/x86_64-linux-gnu/libc-2.23.so
7ffff7273000-7ffff7277000 r--p 001c0000 2b:01 918789                     /lib/x86_64-linux-gnu/libc-2.23.so
7ffff7277000-7ffff7279000 rw-p 001c4000 2b:01 918789                     /lib/x86_64-linux-gnu/libc-2.23.so
7ffff7279000-7ffff727d000 rw-p 00000000 00:00 0
7ffff727d000-7ffff7293000 r-xp 00000000 2b:01 918819                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7ffff7293000-7ffff7492000 ---p 00016000 2b:01 918819                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7ffff7492000-7ffff7493000 rw-p 00015000 2b:01 918819                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7ffff7493000-7ffff759b000 r-xp 00000000 2b:01 918844                     /lib/x86_64-linux-gnu/libm-2.23.so
7ffff759b000-7ffff779a000 ---p 00108000 2b:01 918844                     /lib/x86_64-linux-gnu/libm-2.23.so
7ffff779a000-7ffff779b000 r--p 00107000 2b:01 918844                     /lib/x86_64-linux-gnu/libm-2.23.so
7ffff779b000-7ffff779c000 rw-p 00108000 2b:01 918844                     /lib/x86_64-linux-gnu/libm-2.23.so
7ffff779c000-7ffff77b5000 r-xp 00000000 2b:01 918932                     /lib/x86_64-linux-gnu/libz.so.1.2.8
7ffff77b5000-7ffff79b4000 ---p 00019000 2b:01 918932                     /lib/x86_64-linux-gnu/libz.so.1.2.8
7ffff79b4000-7ffff79b5000 r--p 00018000 2b:01 918932                     /lib/x86_64-linux-gnu/libz.so.1.2.8
7ffff79b5000-7ffff79b6000 rw-p 00019000 2b:01 918932                     /lib/x86_64-linux-gnu/libz.so.1.2.8
7ffff79b6000-7ffff79b9000 r-xp 00000000 2b:01 918805                     /lib/x86_64-linux-gnu/libdl-2.23.so
7ffff79b9000-7ffff7bb8000 ---p 00003000 2b:01 918805                     /lib/x86_64-linux-gnu/libdl-2.23.so
7ffff7bb8000-7ffff7bb9000 r--p 00002000 2b:01 918805                     /lib/x86_64-linux-gnu/libdl-2.23.so
7ffff7bb9000-7ffff7bba000 rw-p 00003000 2b:01 918805                     /lib/x86_64-linux-gnu/libdl-2.23.so
7ffff7bba000-7ffff7bd2000 r-xp 00000000 2b:01 918892                     /lib/x86_64-linux-gnu/libpthread-2.23.so
7ffff7bd2000-7ffff7dd1000 ---p 00018000 2b:01 918892                     /lib/x86_64-linux-gnu/libpthread-2.23.so
7ffff7dd1000-7ffff7dd2000 r--p 00017000 2b:01 918892                     /lib/x86_64-linux-gnu/libpthread-2.23.so
7ffff7dd2000-7ffff7dd3000 rw-p 00018000 2b:01 918892                     /lib/x86_64-linux-gnu/libpthread-2.23.so
7ffff7dd3000-7ffff7dd7000 rw-p 00000000 00:00 0
7ffff7dd7000-7ffff7dfd000 r-xp 00000000 2b:01 918765                     /lib/x86_64-linux-gnu/ld-2.23.so
7ffff7fe9000-7ffff7fef000 rw-p 00000000 00:00 0
7ffff7ff5000-7ffff7ff7000 rw-p 00000000 00:00 0
7ffff7ff7000-7ffff7ffa000 r--p 00000000 00:00 0                          [vvar]
7ffff7ffa000-7ffff7ffc000 r-xp 00000000 00:00 0                          [vdso]
7ffff7ffc000-7ffff7ffd000 r--p 00025000 2b:01 918765                     /lib/x86_64-linux-gnu/ld-2.23.so
7ffff7ffd000-7ffff7ffe000 rw-p 00026000 2b:01 918765                     /lib/x86_64-linux-gnu/ld-2.23.so
7ffff7ffe000-7ffff7fff000 rw-p 00000000 00:00 0
7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0                          [stack]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

Thread 1 "fluent-bit" received signal SIGABRT, Aborted.
0x00007ffff6ee8428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
54      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) thread apply all bt

Thread 2 (Thread 0x7ffff6eb2700 (LWP 27572)):
#0  0x00007ffff6fbaa13 in epoll_wait () at ../sysdeps/unix/syscall-template.S:84
#1  0x000000000065ebfb in _mk_event_wait (loop=0x955210) at /root/fluent-bit/lib/monkey/mk_core/mk_event_epoll.c:343
#2  0x000000000065eeeb in mk_event_wait (loop=0x955210) at /root/fluent-bit/lib/monkey/mk_core/mk_event.c:163
#3  0x0000000000428b7a in log_worker_collector (data=0x96de50) at /root/fluent-bit/src/flb_log.c:122
#4  0x0000000000438f75 in step_callback (data=0x96d7c0) at /root/fluent-bit/src/flb_worker.c:50
#5  0x00007ffff7bc16ba in start_thread (arg=0x7ffff6eb2700) at pthread_create.c:333
#6  0x00007ffff6fba41d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109

Thread 1 (Thread 0x7ffff7fe9940 (LWP 27571)):
#0  0x00007ffff6ee8428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
#1  0x00007ffff6eea02a in __GI_abort () at abort.c:89
#2  0x00007ffff6f2a7ea in __libc_message (do_abort=do_abort@entry=2, fmt=fmt@entry=0x7ffff7043ed8 "*** Error in `%s': %s: 0x%s ***\n") at ../sysdeps/posix/libc_fatal.c:175
#3  0x00007ffff6f3337a in malloc_printerr (ar_ptr=<optimized out>, ptr=<optimized out>, str=0x7ffff7044008 "double free or corruption (!prev)", action=3) at malloc.c:5006
#4  _int_free (av=<optimized out>, p=<optimized out>, have_lock=1) at malloc.c:3867
#5  0x00007ffff6f36350 in _int_realloc (av=av@entry=0x7ffff7277b20 <main_arena>, oldp=oldp@entry=0xdb2c80, oldsize=oldsize@entry=1072, nb=nb@entry=1200) at malloc.c:4358
#6  0x00007ffff6f37839 in __GI___libc_realloc (oldmem=0xdb2c90, bytes=1192) at malloc.c:3045
#7  0x0000000000442fa8 in flb_realloc (ptr=0xdb2c90, size=1192) at /root/fluent-bit/include/fluent-bit/flb_mem.h:82
#8  0x0000000000444f8e in flb_msgpack_raw_to_json_str (buf=0xb96760 <incomplete sequence \336>, buf_size=887, out_buf=0x98f1e8, out_size=0x98f1f0)
    at /root/fluent-bit/src/flb_pack.c:643
#9  0x000000000046e821 in elasticsearch_format (data=0xe1d640, bytes=666920, tag=0xb22880 "syslog.0", tag_len=8, out_size=0x98f628, ctx=0xb22380)
    at /root/fluent-bit/plugins/out_es/es.c:298
#10 0x000000000046ee3d in cb_es_flush (data=0xe1d640, bytes=666920, tag=0xb22880 "syslog.0", tag_len=8, i_ins=0x9697f0, out_context=0xb22380, config=0x950010)
    at /root/fluent-bit/plugins/out_es/es.c:469
#11 0x00000000004351c7 in output_pre_cb_flush () at /root/fluent-bit/include/fluent-bit/flb_output.h:311
#12 0x000000000051a367 in co_init () at /root/fluent-bit/lib/flb_libco/amd64.c:117
#13 0x000000000096e4e2 in ?? ()
#14 0x0000000000000071 in ?? ()
#15 0x0000040000000018 in ?? ()
#16 0x00007f0100000000 in ?? ()
#17 0x00002ba600000001 in ?? ()
#18 0x000000000096e4e2 in ?? ()
#19 0x00007fffffffc2ed in ?? ()
#20 0x00007fffffffc2ec in ?? ()
#21 0x000000000098fbc0 in ?? ()
#22 0x0000000100000018 in ?? ()
#23 0x0000000000b22510 in ?? ()
#24 0x0000000000b22550 in ?? ()
#25 0x0000000000b22550 in ?? ()
#26 0x0000000000000000 in ?? ()

Log line :

<142>Sep 21 13:40:22 xxxxxxx-xxx-xxxxxxx haproxy[31148]: fe80:b8:287:200::01:36350 [21/Sep/2018:13:40:22.696] xxxxxx_xx xxxx-xx-xxxxxxx/xxxxxxx-xxx-xxxxxxxxxx 0/0/0/20/20 200 495 - - ---- 9/9/5/1/0 0/0 "HEAD /dsfdsf23.jpg HTTP/1.1"

Parser associated :

[PARSER]
    Name   bug-long-input
    Format regex
    Regex  ^\<(?<pri>[0-9]+)\>(?<datetime>[^ ]* {1,2}[^ ]* [^ ]*) (?<host>[^ ]+) (?<ident>[a-zA-Z0-9_\/\.\-]*)(?:\[(?<pid>[0-9]+)\]): (?<client_ip>(?:((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?|(?<![0-9])(?:(?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])[.](?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])[.](?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])[.](?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]))(?![0-9]))):(?<client_port>(?:[+-]?(?:[0-9]+))) \[(?<accept_date>(?<haproxy_monthday>(?:(?:0[1-9])|(?:[12][0-9])|(?:3[01])|[1-9]))/(?<haproxy_month>\b(?:[Jj]an(?:uary|uar)?|[Ff]eb(?:ruary|ruar)?|[Mm](?:a|<C3><A4>)?r(?:ch|z)?|[Aa]pr(?:il)?|[Mm]a(?:y|i)?|[Jj]un(?:e|i)?|[Jj]ul(?:y)?|[Aa]ug(?:ust)?|[Ss]ep(?:tember)?|[Oo](?:c|k)?t(?:ober)?|[Nn]ov(?:ember)?|[Dd]e(?:c|z)(?:ember)?)\b)/(?<haproxy_year>(?>\d\d){1,2}):(?<haproxy_time>(?!<[0-9])(?<haproxy_hour>(?:2[0123]|[01]?[0-9])):(?<haproxy_minute>(?:[0-5][0-9]))(?::(?<haproxy_second>(?:(?:[0-5]?[0-9]|60)(?:[:.,][0-9]+)?)))(?![0-9])).(?<haproxy_milliseconds>(?:[+-]?(?:[0-9]+))))\] (?<frontend_name>\S+) (?<backend_name>\S+)/(?<server_name>\S+) (?<time_request>(?:[+-]?(?:[0-9]+)))/(?<time_queue>(?:[+-]?(?:[0-9]+)))/(?<time_backend_connect>(?:[+-]?(?:[0-9]+)))/(?<time_backend_response>(?:[+-]?(?:[0-9]+)))/(?<time_duration>\S+) (?<return_code>(?:[+-]?(?:[0-9]+))) (?<bytes_read>\S+) (?<captured_request_cookie>.*?) (?<captured_response_cookie>.*?) (?<termination_state>\S+) (?<actconn>(?:[+-]?(?:[0-9]+)))/(?<feconn>(?:[+-]?(?:[0-9]+)))/(?<beconn>(?:[+-]?(?:[0-9]+)))/(?<srvconn>(?:[+-]?(?:[0-9]+)))/(?<retries>\S+) (?<srv_queue>(?:[+-]?(?:[0-9]+)))/(?<backend_queue>(?:[+-]?(?:[0-9]+))) (\{(?<captured_request_headers>.*?)\})?( )?(\{(?<captured_response_headers>.*?)\})?( )?"(<BADREQ>|((?<http_verb>\b\w+\b) ((?<http_proto>[A-Za-z]([A-Za-z0-9+\-.]+)+)://)?(?:(?<http_user>[a-zA-Z0-9._-]+)(?::[^@]*)?@)?(?:(?<http_host>(.*)(?::(?<port>\b(?:[1-9][0-9]*)\b))?))?(?:(?<http_request>(?:/[A-Za-z0-9$.+!*'(){},~:;=@#%&_\-]*)+(?:\?[A-Za-z0-9$.+!*'|(){},~@#%&/=:;_?\-\[\]<>]*)?))?( HTTP/(?<http_version>(?:(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\.[0-9]+)?)|(?:\.[0-9]+))))))?))?"?$

newtoncorp avatar Sep 21 '18 14:09 newtoncorp

thanks for reporting this problem.

I've not been able to reproduce in Bionic, I will give it a try to Xenial.

In the meanwhile, would you please check if with the following change you can reproduce the problem?, (https://github.com/fluent/fluent-bit/blob/master/src/flb_pack.c#L509), from:

ret = msgpack2json(json_str, &off, json_size, obj);

to:

ret = msgpack2json(json_str, &off, json_size - 1, obj);

edsiper avatar Sep 21 '18 17:09 edsiper

I've modified the code, and am currently replaying the PCAP that triggered the issue. No issue so far. Thanks for the fix. 👍

newtoncorp avatar Sep 21 '18 17:09 newtoncorp

thanks for confirming, I was not able to reproduce in Xenial without the code change.

If your reproducible case is fixed with 'json_size -1' I will go ahead and merge that change.

edsiper avatar Sep 21 '18 17:09 edsiper

Fix merged, thanks again!

edsiper avatar Sep 21 '18 17:09 edsiper