Skip to content

Commit 044705a

Browse files
ZmnSCPxjrustyrussell
authored andcommitted
peer_control: Fix a use-after-free bug. (#1237)
This bug is a classic case of being lazy: 1. peer_accept_channel() allocated its return off the input message, rather than taking an explicit allocation context. This concealed the lifetime nature of the return. 2. The context for sanitize_error was the error itself, rather than the more obvious tmpctx (connect_failed does not take). The global tmpctx removes the "efficiency" excuse for grabbing a random object to use as context, and is also nice and explicit. All-the-hard-work-by: @ZmnSCPxj
1 parent e56eee5 commit 044705a

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

lightningd/opening_control.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,8 @@ static void channel_config(struct lightningd *ld,
617617
* NULL if we took over, otherwise hand back to gossipd with this
618618
* error.
619619
*/
620-
u8 *peer_accept_channel(struct lightningd *ld,
620+
u8 *peer_accept_channel(const tal_t *ctx,
621+
struct lightningd *ld,
621622
const struct pubkey *peer_id,
622623
const struct wireaddr *addr,
623624
const struct crypto_state *cs,
@@ -637,7 +638,7 @@ u8 *peer_accept_channel(struct lightningd *ld,
637638
/* Fails if there's already one */
638639
uc = new_uncommitted_channel(ld, NULL, peer_id, addr);
639640
if (!uc)
640-
return towire_errorfmt(open_msg, channel_id,
641+
return towire_errorfmt(ctx, channel_id,
641642
"Multiple channels unsupported");
642643

643644
uc->openingd = new_channel_subd(ld, "lightning_openingd", uc, uc->log,

lightningd/opening_control.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ void json_add_uncommitted_channel(struct json_result *response,
1616

1717
/* Peer has spontaneously exited from gossip due to open msg. Return
1818
* NULL if we took over, otherwise hand back to gossipd with this
19-
* error.
19+
* error (allocated off @ctx).
2020
*/
21-
u8 *peer_accept_channel(struct lightningd *ld,
21+
u8 *peer_accept_channel(const tal_t *ctx,
22+
struct lightningd *ld,
2223
const struct pubkey *peer_id,
2324
const struct wireaddr *addr,
2425
const struct crypto_state *cs,

lightningd/peer_control.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,8 @@ void peer_sent_nongossip(struct lightningd *ld,
438438

439439
/* Open request? */
440440
if (fromwire_peektype(in_msg) == WIRE_OPEN_CHANNEL) {
441-
error = peer_accept_channel(ld, id, addr, cs, gossip_index,
441+
error = peer_accept_channel(tmpctx,
442+
ld, id, addr, cs, gossip_index,
442443
gfeatures, lfeatures,
443444
peer_fd, gossip_fd, channel_id,
444445
in_msg);
@@ -459,18 +460,17 @@ void peer_sent_nongossip(struct lightningd *ld,
459460
}
460461

461462
/* Weird request. */
462-
error = towire_errorfmt(ld, channel_id,
463+
error = towire_errorfmt(tmpctx, channel_id,
463464
"Unexpected message %i for peer",
464465
fromwire_peektype(in_msg));
465466

466467
send_error:
467468
/* Hand back to gossipd, with an error packet. */
468-
connect_failed(ld, id, sanitize_error(error, error, NULL));
469+
connect_failed(ld, id, sanitize_error(tmpctx, error, NULL));
469470
msg = towire_gossipctl_hand_back_peer(ld, id, cs, gossip_index, error);
470471
subd_send_msg(ld->gossip, take(msg));
471472
subd_send_fd(ld->gossip, peer_fd);
472473
subd_send_fd(ld->gossip, gossip_fd);
473-
tal_free(error);
474474
}
475475

476476
static enum watch_result funding_announce_cb(struct channel *channel,

wallet/test/run-wallet.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ bool outpointfilter_matches(struct outpointfilter *of UNNEEDED,
275275
struct outpointfilter *outpointfilter_new(tal_t *ctx UNNEEDED)
276276
{ fprintf(stderr, "outpointfilter_new called!\n"); abort(); }
277277
/* Generated stub for peer_accept_channel */
278-
u8 *peer_accept_channel(struct lightningd *ld UNNEEDED,
278+
u8 *peer_accept_channel(const tal_t *ctx UNNEEDED,
279+
struct lightningd *ld UNNEEDED,
279280
const struct pubkey *peer_id UNNEEDED,
280281
const struct wireaddr *addr UNNEEDED,
281282
const struct crypto_state *cs UNNEEDED,
@@ -333,9 +334,6 @@ u8 *towire_channel_funding_locked(const tal_t *ctx UNNEEDED, const struct short_
333334
/* Generated stub for towire_channel_send_shutdown */
334335
u8 *towire_channel_send_shutdown(const tal_t *ctx UNNEEDED)
335336
{ fprintf(stderr, "towire_channel_send_shutdown called!\n"); abort(); }
336-
/* Generated stub for towire_error */
337-
u8 *towire_error(const tal_t *ctx UNNEEDED, const struct channel_id *channel_id UNNEEDED, const u8 *data UNNEEDED)
338-
{ fprintf(stderr, "towire_error called!\n"); abort(); }
339337
/* Generated stub for towire_errorfmt */
340338
u8 *towire_errorfmt(const tal_t *ctx UNNEEDED,
341339
const struct channel_id *channel UNNEEDED,

0 commit comments

Comments
 (0)