From ed5f9f6a9b9f9fc84810cdc1c04ff0a1caa888ff Mon Sep 17 00:00:00 2001 From: Arik Sosman Date: Tue, 20 Jun 2023 13:17:28 -0700 Subject: [PATCH] Verify channel type features for decoding. --- lightning/src/chain/package.rs | 32 ++++++++++++++++++++++++++++++++ lightning/src/ln/chan_utils.rs | 8 ++++++++ lightning/src/ln/features.rs | 1 - 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lightning/src/chain/package.rs b/lightning/src/chain/package.rs index 0b7dbd462..891d7d1da 100644 --- a/lightning/src/chain/package.rs +++ b/lightning/src/chain/package.rs @@ -75,6 +75,30 @@ pub(crate) fn weight_received_htlc(channel_type_features: &ChannelTypeFeatures) if channel_type_features.supports_anchors_zero_fee_htlc_tx() { WEIGHT_RECEIVED_HTLC_ANCHORS } else { WEIGHT_RECEIVED_HTLC } } +/// Verifies deserializable channel type features +pub(crate) fn verify_channel_type_features(channel_type_features: &Option, additional_permitted_features: Option<&ChannelTypeFeatures>) -> Result<(), DecodeError> { + if let Some(features) = channel_type_features.as_ref() { + if features.requires_unknown_bits() { + return Err(DecodeError::UnknownRequiredFeature); + } + + let mut supported_feature_set = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(); + supported_feature_set.set_scid_privacy_required(); + supported_feature_set.set_zero_conf_required(); + + // allow the passing of an additional necessary permitted flag + if let Some(additional_permitted_features) = additional_permitted_features { + supported_feature_set |= additional_permitted_features; + } + + if !features.is_subset(&supported_feature_set) { + return Err(DecodeError::UnknownRequiredFeature); + } + } + + Ok(()) +} + // number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script pub(crate) const WEIGHT_REVOKED_OUTPUT: u64 = 1 + 1 + 73 + 1 + 1 + 1 + 77; @@ -239,6 +263,8 @@ impl Readable for CounterpartyOfferedHTLCOutput { (11, channel_type_features, option), }); + verify_channel_type_features(&channel_type_features, None)?; + Ok(Self { per_commitment_point: per_commitment_point.0.unwrap(), counterparty_delayed_payment_base_key: counterparty_delayed_payment_base_key.0.unwrap(), @@ -310,6 +336,8 @@ impl Readable for CounterpartyReceivedHTLCOutput { (9, channel_type_features, option), }); + verify_channel_type_features(&channel_type_features, None)?; + Ok(Self { per_commitment_point: per_commitment_point.0.unwrap(), counterparty_delayed_payment_base_key: counterparty_delayed_payment_base_key.0.unwrap(), @@ -385,6 +413,8 @@ impl Readable for HolderHTLCOutput { (7, channel_type_features, option), }); + verify_channel_type_features(&channel_type_features, None)?; + Ok(Self { amount_msat: amount_msat.0.unwrap(), cltv_expiry: cltv_expiry.0.unwrap(), @@ -444,6 +474,8 @@ impl Readable for HolderFundingOutput { (3, funding_amount, option) }); + verify_channel_type_features(&channel_type_features, None)?; + Ok(Self { funding_redeemscript: funding_redeemscript.0.unwrap(), channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key()), diff --git a/lightning/src/ln/chan_utils.rs b/lightning/src/ln/chan_utils.rs index d570347fe..7fa2308d0 100644 --- a/lightning/src/ln/chan_utils.rs +++ b/lightning/src/ln/chan_utils.rs @@ -952,6 +952,10 @@ impl Readable for ChannelTransactionParameters { (11, channel_type_features, option), }); + let mut additional_features = ChannelTypeFeatures::empty(); + additional_features.set_anchors_nonzero_fee_htlc_tx_required(); + chain::package::verify_channel_type_features(&channel_type_features, Some(&additional_features))?; + Ok(Self { holder_pubkeys: holder_pubkeys.0.unwrap(), holder_selected_contest_delay: holder_selected_contest_delay.0.unwrap(), @@ -1375,6 +1379,10 @@ impl Readable for CommitmentTransaction { (15, channel_type_features, option), }); + let mut additional_features = ChannelTypeFeatures::empty(); + additional_features.set_anchors_nonzero_fee_htlc_tx_required(); + chain::package::verify_channel_type_features(&channel_type_features, Some(&additional_features))?; + Ok(Self { commitment_number: commitment_number.0.unwrap(), to_broadcaster_value_sat: to_broadcaster_value_sat.0.unwrap(), diff --git a/lightning/src/ln/features.rs b/lightning/src/ln/features.rs index 714bfaa55..ca6ea70b6 100644 --- a/lightning/src/ln/features.rs +++ b/lightning/src/ln/features.rs @@ -595,7 +595,6 @@ impl ChannelTypeFeatures { ret } - #[cfg(any(anchors, test))] /// Constructs a ChannelTypeFeatures with anchors support pub(crate) fn anchors_zero_htlc_fee_and_dependencies() -> Self { let mut ret = Self::empty(); -- 2.39.5