buffer overflow problem when using mosquitto

537 views
Skip to first unread message

Shuai Xu

unread,
Nov 17, 2016, 2:54:06 PM11/17/16
to MQTT

I did a test about mosquitto. First, publish 100 messages.

 for(i = 0; i < 100; i++)
            mosquitto_publish
(mosq_p, NULL, "topic", strlen(buf), buf, 1, false);

Then stop the mosquitto by calling

    mosquitto_disconnect(mosq_p);
    sleep
(2);// without 2 seconds sleeping, buffer overflow will occur
    mosquitto_destroy
(mosq_p);
    mosquitto_lib_cleanup
();

Then core dump with the "buffer overflow detected" message.
    #0  0x00007f53aa022cc9 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
   
#1  0x00007f53aa0260d8 in __GI_abort () at abort.c:89
   
#2  0x00007f53aa05f394 in __libc_message (do_abort=do_abort@entry=2, fmt=fmt@entry=0x7f53aa16b52b "*** %s ***: %s terminated\n") at ../sysdeps/posix/libc_fatal.c:175
   
#3  0x00007f53aa0f6c9c in __GI___fortify_fail (msg=<optimized out>, msg@entry=0x7f53aa16b4c2 "buffer overflow detected") at fortify_fail.c:37
   
#4  0x00007f53aa0f5b60 in __GI___chk_fail () at chk_fail.c:28
   
#5  0x00007f53aa0f6be7 in __fdelt_chk (d=<optimized out>) at fdelt_chk.c:25
   
#6  0x00007f53aa3b6290 in mosquitto_loop () from /usr/lib/x86_64-linux-gnu/libmosquitto.so.1
   
#7  0x00007f53aa3b655e in mosquitto_loop_forever () from /usr/lib/x86_64-linux-gnu/libmosquitto.so.1

However, if I put sleep(2) between disconnect and destroy command (one second sleep is not enough to avoid this issue), it will run normally. Why this happened? And how to fix it?
I also have a stack overflow link for this question (https://stackoverflow.com/questions/40645874/mosquitto-lead-to-buffer-over-flow), you can also answer the question there.
Thank you!

Roger Light

unread,
Nov 17, 2016, 4:25:44 PM11/17/16
to MQTT
Hi Shuai,

It's a bit difficult to say without seeing the full code. Perhaps you
could update the code on stackoverflow and we can discuss it there.

Cheers,

Roger
> --
> To learn more about MQTT please visit http://mqtt.org
> ---
> You received this message because you are subscribed to the Google Groups
> "MQTT" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mqtt+uns...@googlegroups.com.
> To post to this group, send email to mq...@googlegroups.com.
> Visit this group at https://groups.google.com/group/mqtt.
> For more options, visit https://groups.google.com/d/optout.

Shuai Xu

unread,
Nov 21, 2016, 6:38:19 PM11/21/16
to MQTT
Finally figure out it is a multi-threading problem. The sleep(2) just make the threads run in correct order. I created a thread for the mosquitto_loop_forever, however the better practice should be mosquitto_loop_start plus mosquitto_loop_stop.
Reply all
Reply to author
Forward
0 new messages