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>
Wed, 2 Dec 2009 00:27:23 +0000 (11:27 +1100)
commit4d0523dd94fb07e860b3e8118691f93d1ef8d0fa
tree1f40350c9d2efa6a374ad1826f4fc6398d50336e
parentb5a21fd39269a6e2a9d1c8182dd42a1773ccbb3f
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