Skip to content

Commit 378d73c

Browse files
rustyrussellcdecker
authored andcommitted
channeld: fix dev_disconnect doublefree crash.
We shouldn't unconditionally free msg in enqueue_peer_msg: DEBUG: lightning_channeld-0266e4598d1d3c415f572a8488830b60f7e744ed9235eb0b1ba93283b315c03518 chan #1: dev_disconnect: @WIRE_REVOKE_AND_ACK BROKEN: lightning_channeld-0266e4598d1d3c415f572a8488830b60f7e744ed9235eb0b1ba93283b315c03518 chan #1: FATAL SIGNAL 6 (version 8aae6a8) ... BROKEN: lightning_channeld-0266e4598d1d3c415f572a8488830b60f7e744ed9235eb0b1ba93283b315c03518 chan #1: backtrace: ccan/ccan/tal/tal.c:98 (call_error) 0x80855d1 BROKEN: lightning_channeld-0266e4598d1d3c415f572a8488830b60f7e744ed9235eb0b1ba93283b315c03518 chan #1: backtrace: ccan/ccan/tal/tal.c:170 (check_bounds) 0x8085730 BROKEN: lightning_channeld-0266e4598d1d3c415f572a8488830b60f7e744ed9235eb0b1ba93283b315c03518 chan #1: backtrace: ccan/ccan/tal/tal.c:181 (to_tal_hdr) 0x8085791 BROKEN: lightning_channeld-0266e4598d1d3c415f572a8488830b60f7e744ed9235eb0b1ba93283b315c03518 chan #1: backtrace: ccan/ccan/tal/tal.c:504 (tal_free) 0x8085fe6 BROKEN: lightning_channeld-0266e4598d1d3c415f572a8488830b60f7e744ed9235eb0b1ba93283b315c03518 chan #1: backtrace: channeld/channel.c:2651 (main) 0x8050639 For additional safety, handle each msg allocation separately, rather than freeing at bottom of large branch. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent e8edecf commit 378d73c

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

channeld/channel.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ static void enqueue_peer_msg(struct peer *peer, const u8 *msg TAKES)
277277
/* Should not return */
278278
abort();
279279
case DEV_DISCONNECT_DROPPKT:
280-
tal_free(msg);
280+
if (taken(msg))
281+
tal_free(msg);
281282
/* Fail next time we try to do something. */
282283
dev_sabotage_fd(PEER_FD);
283284
return;
@@ -1834,7 +1835,7 @@ static void resend_commitment(struct peer *peer, const struct changed_htlc *last
18341835

18351836
static bool channeld_send_reply(struct crypto_state *cs UNUSED,
18361837
int peer_fd UNUSED,
1837-
const u8 *msg UNUSED,
1838+
const u8 *msg,
18381839
struct peer *peer)
18391840
{
18401841
enqueue_peer_msg(peer, msg);
@@ -2625,15 +2626,15 @@ int main(int argc, char *argv[])
26252626
}
26262627

26272628
if (FD_ISSET(MASTER_FD, &rfds)) {
2628-
msg = wire_sync_read(peer, MASTER_FD);
2629+
msg = wire_sync_read(tmpctx, MASTER_FD);
26292630

26302631
if (!msg)
26312632
status_failed(STATUS_FAIL_MASTER_IO,
26322633
"Can't read command: %s",
26332634
strerror(errno));
26342635
req_in(peer, msg);
26352636
} else if (FD_ISSET(GOSSIP_FD, &rfds)) {
2636-
msg = wire_sync_read(peer, GOSSIP_FD);
2637+
msg = wire_sync_read(tmpctx, GOSSIP_FD);
26372638
/* Gossipd hangs up on us to kill us when a new
26382639
* connection comes in. */
26392640
if (!msg)
@@ -2644,11 +2645,11 @@ int main(int argc, char *argv[])
26442645
} else if (FD_ISSET(PEER_FD, &rfds)) {
26452646
/* This could take forever, but who cares? */
26462647
msg = channeld_read_peer_msg(peer);
2647-
if (msg)
2648+
if (msg) {
26482649
peer_in(peer, msg);
2649-
} else
2650-
msg = NULL;
2651-
tal_free(msg);
2650+
tal_free(msg);
2651+
}
2652+
}
26522653
}
26532654

26542655
/* We only exit when shutdown is complete. */

0 commit comments

Comments
 (0)