diff --git a/daemon/channel.c b/daemon/channel.c index b2c14bd26..61fc4f6a3 100644 --- a/daemon/channel.c +++ b/daemon/channel.c @@ -120,6 +120,12 @@ static bool change_funding(uint64_t anchor_satoshis, return true; } +bool anchor_too_large(uint64_t anchor_satoshis) +{ + /* Anchor must fit in 32 bit. */ + return anchor_satoshis >= (1ULL << 32) / 1000; +} + struct channel_state *initial_cstate(const tal_t *ctx, uint64_t anchor_satoshis, uint64_t fee_rate, @@ -134,8 +140,7 @@ struct channel_state *initial_cstate(const tal_t *ctx, cstate->num_nondust = 0; /* Anchor must fit in 32 bit. */ - if (anchor_satoshis >= (1ULL << 32) / 1000) - return tal_free(cstate); + assert(!anchor_too_large(anchor_satoshis)); fee_msat = calculate_fee_msat(0, fee_rate); if (fee_msat > anchor_satoshis * 1000) diff --git a/daemon/channel.h b/daemon/channel.h index 48ae2e21b..22d049139 100644 --- a/daemon/channel.h +++ b/daemon/channel.h @@ -133,6 +133,12 @@ bool force_fee(struct channel_state *cstate, uint64_t fee); */ uint64_t fee_by_feerate(size_t txsize, uint64_t fee_rate); +/** + * anchor_too_large: does anchor amount fit in 32-bits of millisatoshi. + * @anchor_satoshis: amount in satoshis + */ +bool anchor_too_large(uint64_t anchor_satoshis); + /* Routines to db to force HTLC changes out-of-order which may wrap. */ void force_add_htlc(struct channel_state *cstate, const struct htlc *htlc); void force_fail_htlc(struct channel_state *cstate, const struct htlc *htlc); diff --git a/daemon/packets.c b/daemon/packets.c index 1b901076d..2a106fa9f 100644 --- a/daemon/packets.c +++ b/daemon/packets.c @@ -343,6 +343,9 @@ Pkt *accept_pkt_anchor(struct peer *peer, const Pkt *pkt) assert(peer->local.offer_anchor == CMD_OPEN_WITHOUT_ANCHOR); assert(peer->remote.offer_anchor == CMD_OPEN_WITH_ANCHOR); + if (anchor_too_large(a->amount)) + return pkt_err(peer, "Anchor millisatoshis exceeds 32 bits"); + proto_to_sha256(a->txid, &peer->anchor.txid.sha); peer->anchor.index = a->output_index; peer->anchor.satoshis = a->amount; diff --git a/daemon/peer.c b/daemon/peer.c index 0714c1e95..8bc63e619 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -2862,6 +2862,11 @@ static void json_connect(struct command *cmd, connect->input->index = output; connect->input->amount = tx->output[output].amount; + if (anchor_too_large(connect->input->amount)) { + command_fail(cmd, "Amount %"PRIu64" is too large", + connect->input->amount); + return; + } if (!dns_resolve_and_connect(cmd->dstate, connect->name, connect->port, peer_connected_out, peer_failed, connect)) { command_fail(cmd, "DNS failed");