in_syslog: Fluent-bit takes 100% cpu stuck on regex parsing in "release" mode
Bug Report
Describe the bug The regex parser gets stuck with 100% cpu utilisation on a regex.
To Reproduce Compile in "release" (cmake w/ no options) Create a big regex, parse logs Will explain with details how to reproduce in a controlled environment in a followup comment.
Expected behavior Fluent does take 100% cpu with no output.
Your Environment
- Version used: head on master
- Configuration: syslog udp input with regex parser, any 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
- Compiler : gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
Additional context
I've been able to bypass this issue by compiling with debug enabled. The bug has not triggered yet. (cmake -DCMAKE_BUILD_TYPE=Debug)
I've recompiled on bionic just to be sure it wasn't an incompatibility between xenial and bionic. Able to reproduce 100% by replaying PCAP data. Also able to reproduce with Debug mode compilation.
GDB Trace
^C
Thread 1 "fluent-bit" received signal SIGINT, Interrupt.
0x0000000000544b5c in match_at (reg=reg@entry=0x96ce10,
str=str@entry=0x7fffffffdf60 "<134>Sep 21 13:23:04 [SNIP]"..., end=end@entry=0x7fffffffe35f "", right_range=right_range@entry=0x7fffffffe35f "",
sstart=sstart@entry=0x7fffffffdf60 "<134>Sep 21 13:23:04 [SNIP]"..., sprev=<optimized out>, msa=0x7fffffffdc30) at /root/fluent-bit/lib/onigmo/regexec.c:3022
3022 STACK_PUSH_POS_NOT(p + addr, s, sprev, pkeep);
(gdb) thread apply all bt
Thread 2 (Thread 0x7ffff6eb2700 (LWP 26778)):
#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 26777)):
#0 0x0000000000544b5c in match_at (reg=reg@entry=0x96ce10,
str=str@entry=0x7fffffffdf60 "<134>Sep 21 13:23:04 [SNIP]"..., end=end@entry=0x7fffffffe35f "", right_range=right_range@entry=0x7fffffffe35f "",
sstart=sstart@entry=0x7fffffffdf60 "<134>Sep 21 13:23:04 [SNIP]"..., sprev=<optimized out>, msa=0x7fffffffdc30) at /root/fluent-bit/lib/onigmo/regexec.c:3022
#1 0x000000000054c6ad in onig_search_gpos (reg=0x96ce10,
str=0x7fffffffdf60 "<134>Sep 21 13:23:04 [SNIP]"..., end=0x7fffffffe35f "", global_pos=<optimized out>, start=<optimized out>, range=0x7fffffffe35f "", region=0xc3d4c0, option=0)
at /root/fluent-bit/lib/onigmo/regexec.c:4385
#2 0x000000000054d036 in onig_search (reg=<optimized out>, str=<optimized out>, end=<optimized out>, start=<optimized out>, range=<optimized out>, region=<optimized out>, option=0)
at /root/fluent-bit/lib/onigmo/regexec.c:4145
#3 0x000000000044276f in flb_regex_do (r=0x969c50,
str=0x7fffffffdf60 "<134>Sep 21 13:23:04 [SNIP]"..., slen=1023, result=0x7fffffffddf0) at /root/fluent-bit/src/flb_regex.c:133
#4 0x000000000043cf23 in flb_parser_regex_do (parser=0x96dab0,
buf=0x7fffffffdf60 "<134>Sep 21 13:23:04 [SNIP]"..., length=1023, out_buf=0x7fffffffdee0, out_size=0x7fffffffdee8, out_time=0x7fffffffdf00) at /root/fluent-bit/src/flb_parser_regex.c:127
#5 0x000000000043bb87 in flb_parser_do (parser=0x96dab0,
buf=0x7fffffffdf60 "<134>Sep 21 13:23:04 [SNIP]"..., length=1023, out_buf=0x7fffffffdee0, out_size=0x7fffffffdee8, out_time=0x7fffffffdf00) at /root/fluent-bit/src/flb_parser.c:558
#6 0x000000000046b265 in syslog_prot_process_udp (
buf=0x7fffffffdf60 "<134>Sep 21 13:23:04 [SNIP]"..., size=1023, ctx=0x973330) at /root/fluent-bit/plugins/in_syslog/syslog_prot.c:136
#7 0x00000000004693af in in_syslog_collect_udp (i_ins=0x972760, config=0x950010, in_context=0x973330) at /root/fluent-bit/plugins/in_syslog/syslog.c:79
#8 0x000000000042d8a8 in flb_input_collector_fd (fd=16, config=0x950010) at /root/fluent-bit/src/flb_input.c:995
#9 0x0000000000434c6a in flb_engine_handle_event (config=0x950010, mask=1, fd=16) at /root/fluent-bit/src/flb_engine.c:296
#10 flb_engine_start (config=0x950010) at /root/fluent-bit/src/flb_engine.c:515
#11 0x00000000004208cf in main (argc=3, argv=0x7fffffffe618) at /root/fluent-bit/src/fluent-bit.c:824
[SNIP] is to hide the actual log line. What I've determined is that this line https://github.com/fluent/fluent-bit/blob/master/plugins/in_syslog/syslog.c#L72 causes the issue. If have MTU of 9000 on production servers, and the syslog spec says https://tools.ietf.org/html/rfc5426#section-3.2
All syslog receivers SHOULD be able to receive datagrams
with message sizes of up to and including 2048 octets. The ability
to receive larger messages is encouraged.
By bumping this value to 9000 instead of 1024 I cannot reproduce.
I have datagrams of data size up to 5000 bytes. So the regex probably gets confused.
The regex of the parser associated is the following :
[PARSER]
Name oioswift
Format regex
Regex ^\<(?<pri>[0-9]+)\>(?<datetime>([^ ]* {1,2}[^ ]* [^ ]*)) (?<host>[^ ]+) (?<ident>[^ ]+) *(((?<client_ip>([^ ]+)|-) (?<remote_address>(?:(?<![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]))|-) (?<time>((\d{2}\/\w+\/\d{4}\/\d{2}\/\d{2}\/\d{2})?|-)) (?<request_type>\b\w+\b) (?<request_path>(\/v.*?\/(?<user>.*?)([\/%].*?)*|.*)) (?<protocol>(.*?|-)) (?<return_code>(?:(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\.[0-9]+)?)|(?:\.[0-9]+))))) (?<referer>.*) (?<user_agent>.*) (.*?|-) ((?<bytes_recvd>(?:(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\.[0-9]+)?)|(?:\.[0-9]+)))))|-) ((?<bytes_sent>(?:(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\.[0-9]+)?)|(?:\.[0-9]+)))))|-) (?<client_etag>(.*?|-)) (?<transaction_id>.*?) (?<headers>(.*?|-)) (?<request_time>(?:(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\.[0-9]+)?)|(?:\.[0-9]+))))) (?<swift_source>(.*?|-)) (?<log_info>(.*?|-)) (?<request_start_time>(?:(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\.[0-9]+)?)|(?:\.[0-9]+))))) (?<request_end_time>(?:(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\.[0-9]+)?)|(?:\.[0-9]+))))) (?<policy_index>(.*?|-)))|(?<failure_message>.*))$
Time_Key datetime
Time_Format %b %d %H:%M:%S
Time_Keep On
@newtoncorp thanks for the research.
Do you have a test log file that can be used to trigger the problem ? I tried locally without success.
Hmmm. I'm unable to reproduce with a single log line. I use udpreplay ( https://github.com/rigtorp/udpreplay ) with a pcap file that's been saved from production. I can't really anonymise the pcap.
I'll try to trigger it with netcat.
@newtoncorp the buffer size in the latest version is now dynamic but I am worried about the Regex getting stuck, that should not happen, if you have a way to reproduce that specific behavior would be great.
would you please try to replicate with GIT Master ? we did a lib/onigmo upgrade that could be related.
I tried to compile it and it failed. (rm -rf build; mkdir build; cd build; cmake ..; make)
libtool: compile: gcc -DHAVE_CONFIG_H -I. -I/root/fluent-bit/lib/onigmo -I/root/fluent-bit/lib/onigmo -I/root/fluent-bit/build/include -I/root/fluent-bit/lib/onigmo/enc/unicode -Wall -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -MT euc_jp.lo -MD -MP -MF .deps/euc_jp.Tpo -c /root/fluent-bit/lib/onigmo/enc/euc_jp.c -fPIC -DPIC -o euc_jp.o
libtool: compile: gcc -DHAVE_CONFIG_H -I. -I/root/fluent-bit/lib/onigmo -I/root/fluent-bit/lib/onigmo -I/root/fluent-bit/build/include -I/root/fluent-bit/lib/onigmo/enc/unicode -Wall -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -MT shift_jis.lo -MD -MP -MF .deps/shift_jis.Tpo -c /root/fluent-bit/lib/onigmo/enc/shift_jis.c -fPIC -DPIC -o shift_jis.o
/root/fluent-bit/lib/onigmo/enc/shift_jis.c:31:23: fatal error: shift_jis.h: No such file or directory
compilation terminated.
Makefile:688: recipe for target 'shift_jis.lo' failed
make[5]: *** [shift_jis.lo] Error 1
Makefile:962: recipe for target 'all-recursive' failed
make[4]: *** [all-recursive] Error 1
Makefile:407: recipe for target 'all' failed
make[3]: *** [all] Error 2
CMakeFiles/onigmo.dir/build.make:110: recipe for target 'onigmo-prefix/src/onigmo-stamp/onigmo-build' failed
make[2]: *** [onigmo-prefix/src/onigmo-stamp/onigmo-build] Error 2
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/onigmo.dir/all' failed
make[1]: *** [CMakeFiles/onigmo.dir/all] Error 2
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2
Am I missing something ?
I broke it :/ ! thanks for reporting the problem, already fixed.
Unable to reproduce the infinite loop, even with reverting the udp syslog buffer patch.
Thanks !
I spoke too fast. Can reproduce with the patch reverted, cannot with the udp buffer patch.
So.. can you reproduce the problem with GIT master?
On Wed, Oct 3, 2018, 10:02 newtoncorp [email protected] wrote:
I spoke too fast. Can reproduce with the patch reverted, cannot with the udp buffer patch.
— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/fluent/fluent-bit/issues/775#issuecomment-426693679, or mute the thread https://github.com/notifications/unsubscribe-auth/AAWkNiOKe27fY0BdbUhFMjKsBqbSmto-ks5uhN-TgaJpZM4WwUag .
Nope.
Thanks
On Wed, Oct 3, 2018, 10:57 newtoncorp [email protected] wrote:
Nope.
— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/fluent/fluent-bit/issues/775#issuecomment-426714428, or mute the thread https://github.com/notifications/unsubscribe-auth/AAWkNqeuSHMuwK30iIQ6TrTiiT0XMbTdks5uhOyEgaJpZM4WwUag .
Sadly, it froze again several times (with master/head).
We've tweaked the regex a bit, it doesn't take 100% cpu anymore (those .* are really bad :( )
I'll report in a week or so if I reproduce.
Well, looks like the regex was in cause. Haven't. triggered it since then. I don't know exactly which buffer could have triggered it, but having a more restrictive regex fixed the issue.
Closing. Thanks for the help :)