Skip to content

Memory leak at handle_request #535

@zyingp

Description

@zyingp

The leak can be reproduced as follows:

  1. Build the project with ASan enabled and with assert() disabled:
    $ CXXFLAGS="-DNDEBUG=1" CFLAGS="-g -O0 -fsanitize=address -fno-omit-frame-pointer -DNDEBUG=1" LDFLAGS="-g -O0 -fsanitize=address -fno-omit-frame-pointer -DNDEBUG=1" ./configure --disable-tests --disable-documentation --enable-examples --disable-dtls --disable-shared

  2. Download a network sample input from https://github.com/zyingp/temp/raw/master/libcoap/src_net_abort Start the coap-server from a terminal:
    $ ./examples/coap-server
    In another terminal, switch to the folder has src_net_abort, and run:
    $ nc 127.0.0.1 5683 < src_net_abort
    Hit Ctrl+C in the first terminal, and you should see output like below

$ ./examples/coap-server
Aug 07 16:48:53:642799 DEBG created UDP endpoint [::]:5683
Aug 07 16:48:53:644229 DEBG created TCP endpoint [::]:5683
Aug 07 16:48:56:906199 DEBG ***[::ffff:127.0.0.1]:5683 <-> [::ffff:127.0.0.1]:4947 TCP : new incoming session
Aug 07 16:48:56:907329 DEBG ***EVENT: 0x1001
Aug 07 16:48:56:907795 DEBG ***[::ffff:127.0.0.1]:5683 <-> [::ffff:127.0.0.1]:4947 TCP : sending CSM
Aug 07 16:48:56:908294 DEBG * [::ffff:127.0.0.1]:5683 <-> [::ffff:127.0.0.1]:4947 TCP : sent 6 bytes
v:1 t:CON c:CSM i:0000 {} [ Max-Message-Size:8388864 ]
Aug 07 16:48:56:909039 DEBG * [::ffff:127.0.0.1]:5683 <-> [::ffff:127.0.0.1]:4947 TCP : received 1443 bytes
v:1 t:CON c:CSM i:0000 {} [ 1:, Max-Message-Size:8 ]
Aug 07 16:48:56:909685 DEBG ***[::ffff:127.0.0.1]:5683 <-> [::ffff:127.0.0.1]:4947 TCP : session connected
Aug 07 16:48:56:910067 DEBG ***EVENT: 0x2001
Aug 07 16:48:56:910455 DEBG coap_pdu_parse: invalid Token
v:1 t:CON c:2.15 i:0000 {} [ ]
Aug 07 16:48:56:910954 DEBG invalid option length
Aug 07 16:48:56:911255 DEBG coap_pdu_parse: missing payload start code
v:1 t:CON c:7.31 i:0000 {ff} [ ]
v:1 t:CON c:7.31 i:0000 {01} [ ]
v:1 t:CON c:GET i:0000 {} [ ]
Aug 07 16:48:56:911754 DEBG call custom handler for resource ''
Aug 07 16:48:56:912049 WARN coap_pdu_resize: pdu too big
Aug 07 16:48:56:912304 WARN coap_pdu_resize: pdu too big
Aug 07 16:48:56:912598 DEBG * [::ffff:127.0.0.1]:5683 <-> [::ffff:127.0.0.1]:4947 TCP : sent 8 bytes
v:1 t:ACK c:2.05 i:0000 {} [ ETag:\x1A\xA7\xE9\x26, Content-Format:text/plain ]
v:1 t:CON c:GET i:0000 {21} [ ]
Aug 07 16:48:56:912953 DEBG call custom handler for resource ''
Aug 07 16:48:56:913215 WARN coap_pdu_resize: pdu too big
Aug 07 16:48:56:913457 WARN coap_pdu_resize: pdu too big
Aug 07 16:48:56:913726 DEBG * [::ffff:127.0.0.1]:5683 <-> [::ffff:127.0.0.1]:4947 TCP : sent 8 bytes
v:1 t:ACK c:2.05 i:0000 {21} [ ETag:\x1A\xA7\xE9\x26 ]
v:1 t:CON c:GET i:0000 {ff} [ ]
Aug 07 16:48:56:914065 DEBG call custom handler for resource ''
Aug 07 16:48:56:914316 WARN coap_pdu_resize: pdu too big
Aug 07 16:48:56:914556 WARN coap_pdu_resize: pdu too big
Aug 07 16:48:56:914819 DEBG * [::ffff:127.0.0.1]:5683 <-> [::ffff:127.0.0.1]:4947 TCP : sent 8 bytes
v:1 t:ACK c:2.05 i:0000 {ff} [ ETag:\x1A\xA7\xE9\x26 ]
v:1 t:CON c:1.00 i:0000 {01} [ ]
Aug 07 16:48:56:915140 DEBG dropped message with invalid code (1.00)
Aug 07 16:48:56:915417 DEBG * [::ffff:127.0.0.1]:5683 <-> [::ffff:127.0.0.1]:4947 TCP : sent 2 bytes
v:1 t:RST c:0.00 i:0000 {} [ ]
v:1 t:CON c:GET i:0000 {0101100120011b80} [ ]
Aug 07 16:48:56:921329 DEBG call custom handler for resource ''
Aug 07 16:48:56:921827 WARN cannot generate response
Aug 07 16:48:56:922238 DEBG coap_pdu_parse: invalid Token
Aug 07 16:48:58:295117 DEBG ***[::ffff:127.0.0.1]:5683 <-> [::ffff:127.0.0.1]:4947 TCP : session disconnected (reason 1)
Aug 07 16:48:58:297167 DEBG ***EVENT: 0x1002
Aug 07 16:48:58:297577 DEBG ***EVENT: 0x2002
^CAug 07 16:49:00:587410 DEBG select: Interrupted system call (4)
Aug 07 16:49:00:588047 DEBG ***[::ffff:127.0.0.1]:5683 <-> [::ffff:127.0.0.1]:4947 TCP : session closed

=================================================================
==21034==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 56 byte(s) in 1 object(s) allocated from:
#0 0x7f862d11eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)
#1 0x7f862e42fa7d in coap_malloc_type src/mem.c:28
#2 0x7f862e44243f in coap_pdu_init src/pdu.c:82
#3 0x7f862e43c416 in handle_request src/net.c:2208
#4 0x7f862e43e61e in coap_dispatch src/net.c:2506
#5 0x7f862e4370c9 in coap_read_session src/net.c:1273
#6 0x7f862e438856 in coap_io_do_events src/net.c:1520
#7 0x7f862e41ffb9 in coap_run_once src/coap_io.c:1587
#8 0x7f862e411648 in main /mnt/d/zyp/fuzzer/fuzzed_projects/libcoap/libcoap-4.2.1/examples/coap-server.c:1023
#9 0x7f862cc61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)

Indirect leak of 12 byte(s) in 1 object(s) allocated from:
#0 0x7f862d11eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)
#1 0x7f862e42fa7d in coap_malloc_type src/mem.c:28
#2 0x7f862e4424f9 in coap_pdu_init src/pdu.c:104
#3 0x7f862e43c416 in handle_request src/net.c:2208
#4 0x7f862e43e61e in coap_dispatch src/net.c:2506
#5 0x7f862e4370c9 in coap_read_session src/net.c:1273
#6 0x7f862e438856 in coap_io_do_events src/net.c:1520
#7 0x7f862e41ffb9 in coap_run_once src/coap_io.c:1587
#8 0x7f862e411648 in main /mnt/d/zyp/fuzzer/fuzzed_projects/libcoap/libcoap-4.2.1/examples/coap-server.c:1023
#9 0x7f862cc61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)

SUMMARY: AddressSanitizer: 68 byte(s) leaked in 2 allocation(s).

(Sending the sample network input again, the leaked memory size will double.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions