Skip to content

Commit a450962

Browse files
committed
channeld/lightningd/hsmd: strengthen our checks against 0-output txs.
If we ever do this, we'd end up with an unspendable commitment tx anyway. It might be able to happen if we have htlcs added from the non-fee-paying party while the fees are increased, though. But better to close the channel and get a report about it if that happens. Signed-off-by: Rusty Russell <[email protected]>
1 parent 960bfb8 commit a450962

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

channeld/commit_tx.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
242242
n++;
243243
}
244244

245+
/* BOLT #2:
246+
*
247+
* - MUST set `channel_reserve_satoshis` greater than or equal to
248+
* `dust_limit_satoshis`.
249+
*/
250+
/* This means there must be at least one output. */
251+
assert(n > 0);
252+
245253
assert(n <= tx->wtx->outputs_allocation_len);
246254
tal_resize(htlcmap, n);
247255

hsmd/hsmd.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ static PRINTF_FMT(4,5)
152152
master_badmsg(fromwire_peektype(msg_in), msg_in);
153153
}
154154

155+
/*~ Nobody should give us bad requests; it's a sign something is broken */
156+
status_broken("%s: %s", type_to_string(tmpctx, struct node_id, &c->id), str);
157+
155158
/*~ Note the use of NULL as the ctx arg to towire_hsmstatus_: only
156159
* use NULL as the allocation when we're about to immediately free it
157160
* or hand it off with take(), as here. That makes it clear we don't
@@ -770,6 +773,12 @@ static struct io_plan *handle_sign_commitment_tx(struct io_conn *conn,
770773
&funding))
771774
return bad_req(conn, c, msg_in);
772775

776+
/* Basic sanity checks. */
777+
if (tx->wtx->num_inputs != 1)
778+
return bad_req_fmt(conn, c, msg_in, "tx must have 1 input");
779+
if (tx->wtx->num_outputs == 0)
780+
return bad_req_fmt(conn, c, msg_in, "tx must have > 0 outputs");
781+
773782
get_channel_seed(&peer_id, dbid, &channel_seed);
774783
derive_basepoints(&channel_seed,
775784
&local_funding_pubkey, NULL, &secrets, NULL);
@@ -823,6 +832,12 @@ static struct io_plan *handle_sign_remote_commitment_tx(struct io_conn *conn,
823832
&funding))
824833
bad_req(conn, c, msg_in);
825834

835+
/* Basic sanity checks. */
836+
if (tx->wtx->num_inputs != 1)
837+
return bad_req_fmt(conn, c, msg_in, "tx must have 1 input");
838+
if (tx->wtx->num_outputs == 0)
839+
return bad_req_fmt(conn, c, msg_in, "tx must have > 0 outputs");
840+
826841
get_channel_seed(&c->id, c->dbid, &channel_seed);
827842
derive_basepoints(&channel_seed,
828843
&local_funding_pubkey, NULL, &secrets, NULL);

lightningd/peer_htlcs.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,21 @@ static bool changed_htlc(struct channel *channel,
12241224
return update_in_htlc(channel, changed->id, changed->newstate);
12251225
}
12261226

1227+
/* FIXME: This should be a complete check, not just a sanity check.
1228+
* Perhaps that means we need a cookie from the HSM? */
1229+
static bool valid_commitment_tx(struct channel *channel,
1230+
const struct bitcoin_tx *tx)
1231+
{
1232+
/* We've had past issues where all outputs are trimmed. */
1233+
if (tx->wtx->num_outputs == 0) {
1234+
channel_internal_error(channel,
1235+
"channel_got_commitsig: zero output tx! %s",
1236+
type_to_string(tmpctx, struct bitcoin_tx, tx));
1237+
return false;
1238+
}
1239+
return true;
1240+
}
1241+
12271242
static bool peer_save_commitsig_received(struct channel *channel, u64 commitnum,
12281243
struct bitcoin_tx *tx,
12291244
const struct bitcoin_signature *commit_sig)
@@ -1236,6 +1251,10 @@ static bool peer_save_commitsig_received(struct channel *channel, u64 commitnum,
12361251
return false;
12371252
}
12381253

1254+
/* Basic sanity check */
1255+
if (!valid_commitment_tx(channel, tx))
1256+
return false;
1257+
12391258
channel->next_index[LOCAL]++;
12401259

12411260
/* Update channel->last_sig and channel->last_tx before saving to db */

0 commit comments

Comments
 (0)