ctdb_io: fix use-after-free on invalid packets
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 1 Dec 2009 22:27:42 +0000 (08:57 +1030)
committerRonnie Sahlberg <ronniesahlberg@gmail.com>
Tue, 8 Dec 2009 00:53:05 +0000 (11:53 +1100)
commit1eec697a9ff10366b848989b920d6e339cee3976
tree5c62ffc5c320ba0b993c98591a714821b5de506e
parent0b6c947d7719d4bf782df0821a18eca4157d9ed5
ctdb_io: fix use-after-free on invalid packets

Wolfgang saw a talloc complaint about using freed memory in ctdb_tcp_read_cb.
His fix was to remove the talloc_free() in that function, which causes
loops when a socket is closed (as it does not get removed from the event
system), eg:
netcat 192.168.1.2 4379 < /dev/null

The real bug is that when we have more than one pending packet in the
queue, we loop calling the callback without any safeguards should that
callback free the queue (as it tends to do on invalid packets).  This
can be reproduced by sending more than one bogus packet at once:
# Length word at start: 4 == empty packet (assumed little endian)
/usr/bin/printf \\4\\0\\0\\0\\4\\0\\0\\0 > /tmp/pkt
netcat 192.168.1.2 4379 < /tmp/pkt

Using a destructor we can check if the callback frees us, and exit
immediately.  Elsewhere, we return after the callback anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
common/ctdb_io.c