ExportXMLWordPrintable

    • Type: Problem report
    • Resolution: Unresolved
    • Priority: Trivial
    • None
    • Affects Version/s: 7.0.32
    • Component/s: None
    • None

      The same dereference is present in the source of release/7.0, release/7.4 and master as far as we could check by reading the code.

      Steps to reproduce

      1. Start zabbix_sender so that it blocks reading stdin, for example under GDB (a fresh process, not an attach):

         gdb -q -nx --args /usr/bin/zabbix_sender -z 127.0.0.1 -p 10052 -i -
         (gdb) handle SIGSEGV stop print nopass
         (gdb) handle SIGTERM stop print pass
         (gdb) starti
         (gdb) continue
      

         No data is written to stdin, so sender waits in fgets() (the stack shows read() <- fgets() <- zbx_fgets_alloc() <- main()).

      2. From another terminal send SIGTERM to this sender process:

         kill -TERM <pid of zabbix_sender>

      3. In GDB, run "continue" so that the signal is delivered to the program's handler.

      The same situation happens in production without a debugger: a shell pipeline such as
         some_slow_command | zabbix_sender -z <server> -p <port> -i -
      where the left side hangs and the whole process group is terminated with SIGTERM
      (for example when a Zabbix external check times out; src/libs/zbxexec/execute.c uses kill(-pid, SIGTERM)).
      The sender is already running and waiting for stdin when the signal arrives.

      Result

      zabbix_sender terminates with SIGSEGV (a core dump is produced if enabled):

         Program received signal SIGSEGV, Segmentation fault.

         0x0000555555567e24 in main_signal_handler ()
         #0  main_signal_handler ()
         #1  <signal handler called>
         #2  read () from /lib64/libc.so.6
         #3  __GI__IO_file_underflow ()
         #4  _IO_default_uflow ()
         #5  _IO_getline_info ()
         #6  fgets ()
         #7  zbx_fgets_alloc ()
         #8  main ()
      
         (gdb) x/i $pc
         => 0x555555567e24 <main_signal_handler+84>: mov (%rax),%edi
         (gdb) info registers rax rdi r12
         rax 0x0 0
         rdi 0xf 15
         r12 0xf 15
      

      Fault address is 0x0, the signal being handled is SIGTERM (15).
      We observed the identical stack, instruction and registers in dozens of core dumps from production proxies.

      Cause (src/zabbix_sender/zabbix_sender.c):

         static void main_signal_handler(int sig)
         {
             ...
             for (i = 0; i < destinations_count; i++)
             {
                 pid_t child = *(destinations[i].thread);      /* line 390 */
      
                 if (ZBX_THREAD_HANDLE_NULL != child)
                     kill(child, sig);
             }
         }
      
      • The handler is installed for SIGINT, SIGQUIT, SIGTERM, SIGHUP, SIGALRM and SIGPIPE in main() (lines ~1505-1510) before stdin is read.
      • destinations[] is filled by sender_add_serveractive_host_cb() while parsing parameters; destinations_count becomes > 0 there, but only the "addrs" member is initialized (the array comes from zbx_realloc(), which does not zero memory).
      • The "thread" member is assigned only in perform_data_sending() (destinations[i].thread = &threads[i], line ~743), i.e. at the first send.

      So between installing the handler and the first send, destinations_count > 0 while destinations[i].thread is not set (NULL in our cases), and the handler dereferences it.

      Related note: after perform_data_sending() returns, "threads" is freed (zbx_free(threads)) but destinations[i].thread still points to it, so a signal arriving between two sends would read freed memory. We did not observe this case.

      Expected

      zabbix_sender must handle SIGTERM (and the other handled signals) in this state without crashing: it should skip destinations that have no sender thread yet (thread == NULL), and terminate cleanly, instead of dying with SIGSEGV.
      It should neither crash nor hang when blocked waiting for input.
      Suggested direction: initialize destinations[i].thread to NULL when a destination is added, check it for NULL in main_signal_handler(), and reset it to NULL before "threads" is freed in perform_data_sending().

            Assignee:
            Maciej Czernielewski
            Reporter:
            Dmitry Matyushin
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: