Skip to content

Commit 5099dce

Browse files
authored
Merge pull request #3632 from jkczyz/2025-02-reject-old-channel-versions
Reject channels serialized with version <= 2
2 parents 5786674 + 6f7e1c5 commit 5099dce

File tree

2 files changed

+28
-44
lines changed

2 files changed

+28
-44
lines changed

lightning/src/ln/channel.rs

+27-43
Original file line numberDiff line numberDiff line change
@@ -10296,28 +10296,25 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
1029610296
}
1029710297
}
1029810298

10299-
impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c ChannelTypeFeatures)> for FundedChannel<SP>
10299+
impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c ChannelTypeFeatures)> for FundedChannel<SP>
1030010300
where
1030110301
ES::Target: EntropySource,
1030210302
SP::Target: SignerProvider
1030310303
{
10304-
fn read<R : io::Read>(reader: &mut R, args: (&'a ES, &'b SP, u32, &'c ChannelTypeFeatures)) -> Result<Self, DecodeError> {
10305-
let (entropy_source, signer_provider, serialized_height, our_supported_features) = args;
10304+
fn read<R : io::Read>(reader: &mut R, args: (&'a ES, &'b SP, &'c ChannelTypeFeatures)) -> Result<Self, DecodeError> {
10305+
let (entropy_source, signer_provider, our_supported_features) = args;
1030610306
let ver = read_ver_prefix!(reader, SERIALIZATION_VERSION);
10307+
if ver <= 2 {
10308+
return Err(DecodeError::UnknownVersion);
10309+
}
1030710310

1030810311
// `user_id` used to be a single u64 value. In order to remain backwards compatible with
1030910312
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values. We read
1031010313
// the low bytes now and the high bytes later.
1031110314
let user_id_low: u64 = Readable::read(reader)?;
1031210315

10313-
let mut config = Some(LegacyChannelConfig::default());
10314-
if ver == 1 {
10315-
// Read the old serialization of the ChannelConfig from version 0.0.98.
10316-
config.as_mut().unwrap().options.forwarding_fee_proportional_millionths = Readable::read(reader)?;
10317-
config.as_mut().unwrap().options.cltv_expiry_delta = Readable::read(reader)?;
10318-
config.as_mut().unwrap().announce_for_forwarding = Readable::read(reader)?;
10319-
config.as_mut().unwrap().commit_upfront_shutdown_pubkey = Readable::read(reader)?;
10320-
} else {
10316+
let mut config = LegacyChannelConfig::default();
10317+
{
1032110318
// Read the 8 bytes of backwards-compatibility ChannelConfig data.
1032210319
let mut _val: u64 = Readable::read(reader)?;
1032310320
}
@@ -10481,23 +10478,17 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1048110478
let holder_dust_limit_satoshis = Readable::read(reader)?;
1048210479
let counterparty_max_htlc_value_in_flight_msat = Readable::read(reader)?;
1048310480
let mut counterparty_selected_channel_reserve_satoshis = None;
10484-
if ver == 1 {
10485-
// Read the old serialization from version 0.0.98.
10486-
counterparty_selected_channel_reserve_satoshis = Some(Readable::read(reader)?);
10487-
} else {
10488-
// Read the 8 bytes of backwards-compatibility data.
10481+
{
10482+
// Read the 8 bytes of backwards-compatibility counterparty_selected_channel_reserve_satoshis data.
1048910483
let _dummy: u64 = Readable::read(reader)?;
1049010484
}
1049110485
let counterparty_htlc_minimum_msat = Readable::read(reader)?;
1049210486
let holder_htlc_minimum_msat = Readable::read(reader)?;
1049310487
let counterparty_max_accepted_htlcs = Readable::read(reader)?;
1049410488

1049510489
let mut minimum_depth = None;
10496-
if ver == 1 {
10497-
// Read the old serialization from version 0.0.98.
10498-
minimum_depth = Some(Readable::read(reader)?);
10499-
} else {
10500-
// Read the 4 bytes of backwards-compatibility data.
10490+
{
10491+
// Read the 4 bytes of backwards-compatibility minimum_depth data.
1050110492
let _dummy: u32 = Readable::read(reader)?;
1050210493
}
1050310494

@@ -10542,20 +10533,20 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1054210533
// Prior to supporting channel type negotiation, all of our channels were static_remotekey
1054310534
// only, so we default to that if none was written.
1054410535
let mut channel_type = Some(ChannelTypeFeatures::only_static_remote_key());
10545-
let mut channel_creation_height = Some(serialized_height);
10536+
let mut channel_creation_height = 0u32;
1054610537
let mut preimages_opt: Option<Vec<Option<PaymentPreimage>>> = None;
1054710538

1054810539
// If we read an old Channel, for simplicity we just treat it as "we never sent an
1054910540
// AnnouncementSignatures" which implies we'll re-send it on reconnect, but that's fine.
10550-
let mut announcement_sigs_state = Some(AnnouncementSigsState::NotSent);
10541+
let mut announcement_sigs_state = AnnouncementSigsState::NotSent;
1055110542
let mut latest_inbound_scid_alias = None;
10552-
let mut outbound_scid_alias = None;
10543+
let mut outbound_scid_alias = 0u64;
1055310544
let mut channel_pending_event_emitted = None;
1055410545
let mut channel_ready_event_emitted = None;
1055510546
let mut funding_tx_broadcast_safe_event_emitted = None;
1055610547

1055710548
let mut user_id_high_opt: Option<u64> = None;
10558-
let mut channel_keys_id: Option<[u8; 32]> = None;
10549+
let mut channel_keys_id = [0u8; 32];
1055910550
let mut temporary_channel_id: Option<ChannelId> = None;
1056010551
let mut holder_max_accepted_htlcs: Option<u16> = None;
1056110552

@@ -10584,21 +10575,21 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1058410575
(2, channel_type, option),
1058510576
(3, counterparty_selected_channel_reserve_satoshis, option),
1058610577
(4, holder_selected_channel_reserve_satoshis, option),
10587-
(5, config, option), // Note that if none is provided we will *not* overwrite the existing one.
10578+
(5, config, required),
1058810579
(6, holder_max_htlc_value_in_flight_msat, option),
1058910580
(7, shutdown_scriptpubkey, option),
1059010581
(8, blocked_monitor_updates, optional_vec),
1059110582
(9, target_closing_feerate_sats_per_kw, option),
1059210583
(10, monitor_pending_update_adds, option), // Added in 0.0.122
1059310584
(11, monitor_pending_finalized_fulfills, optional_vec),
10594-
(13, channel_creation_height, option),
10585+
(13, channel_creation_height, required),
1059510586
(15, preimages_opt, optional_vec),
10596-
(17, announcement_sigs_state, option),
10587+
(17, announcement_sigs_state, required),
1059710588
(19, latest_inbound_scid_alias, option),
10598-
(21, outbound_scid_alias, option),
10589+
(21, outbound_scid_alias, required),
1059910590
(23, channel_ready_event_emitted, option),
1060010591
(25, user_id_high_opt, option),
10601-
(27, channel_keys_id, option),
10592+
(27, channel_keys_id, required),
1060210593
(28, holder_max_accepted_htlcs, option),
1060310594
(29, temporary_channel_id, option),
1060410595
(31, channel_pending_event_emitted, option),
@@ -10615,12 +10606,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1061510606
(53, funding_tx_broadcast_safe_event_emitted, option),
1061610607
});
1061710608

10618-
let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
10619-
let holder_signer = signer_provider.derive_channel_signer(channel_keys_id);
10620-
(channel_keys_id, holder_signer)
10621-
} else {
10622-
return Err(DecodeError::InvalidValue);
10623-
};
10609+
let holder_signer = signer_provider.derive_channel_signer(channel_keys_id);
1062410610

1062510611
if let Some(preimages) = preimages_opt {
1062610612
let mut iter = preimages.into_iter();
@@ -10651,8 +10637,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1065110637
// ChannelTransactionParameters may have had an empty features set upon deserialization.
1065210638
// To account for that, we're proactively setting/overriding the field here.
1065310639
channel_parameters.channel_type_features = chan_features.clone();
10654-
// ChannelTransactionParameters::channel_value_satoshis defaults to 0 prior to version 0.2.
10655-
channel_parameters.channel_value_satoshis = channel_value_satoshis;
1065610640

1065710641
let mut secp_ctx = Secp256k1::new();
1065810642
secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
@@ -10764,7 +10748,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1076410748
context: ChannelContext {
1076510749
user_id,
1076610750

10767-
config: config.unwrap(),
10751+
config,
1076810752

1076910753
prev_config: None,
1077010754

@@ -10775,7 +10759,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1077510759
channel_id,
1077610760
temporary_channel_id,
1077710761
channel_state,
10778-
announcement_sigs_state: announcement_sigs_state.unwrap(),
10762+
announcement_sigs_state,
1077910763
secp_ctx,
1078010764

1078110765
latest_monitor_update_id,
@@ -10825,7 +10809,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1082510809
funding_tx_confirmed_in,
1082610810
funding_tx_confirmation_height,
1082710811
short_channel_id,
10828-
channel_creation_height: channel_creation_height.unwrap(),
10812+
channel_creation_height,
1082910813

1083010814
counterparty_dust_limit_satoshis,
1083110815
holder_dust_limit_satoshis,
@@ -10858,7 +10842,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1085810842

1085910843
latest_inbound_scid_alias,
1086010844
// Later in the ChannelManager deserialization phase we scan for channels and assign scid aliases if its missing
10861-
outbound_scid_alias: outbound_scid_alias.unwrap_or(0),
10845+
outbound_scid_alias,
1086210846

1086310847
funding_tx_broadcast_safe_event_emitted: funding_tx_broadcast_safe_event_emitted.unwrap_or(false),
1086410848
channel_pending_event_emitted: channel_pending_event_emitted.unwrap_or(true),
@@ -11567,7 +11551,7 @@ mod tests {
1156711551
let mut s = crate::io::Cursor::new(&encoded_chan);
1156811552
let mut reader = crate::util::ser::FixedLengthReader::new(&mut s, encoded_chan.len() as u64);
1156911553
let features = channelmanager::provided_channel_type_features(&config);
11570-
let decoded_chan = FundedChannel::read(&mut reader, (&&keys_provider, &&keys_provider, 0, &features)).unwrap();
11554+
let decoded_chan = FundedChannel::read(&mut reader, (&&keys_provider, &&keys_provider, &features)).unwrap();
1157111555
assert_eq!(decoded_chan.context.pending_outbound_htlcs, pending_outbound_htlcs);
1157211556
assert_eq!(decoded_chan.context.holding_cell_htlc_updates, holding_cell_htlc_updates);
1157311557
}

lightning/src/ln/channelmanager.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13634,7 +13634,7 @@ where
1363413634
let mut close_background_events = Vec::new();
1363513635
for _ in 0..channel_count {
1363613636
let mut channel: FundedChannel<SP> = FundedChannel::read(reader, (
13637-
&args.entropy_source, &args.signer_provider, best_block_height, &provided_channel_type_features(&args.default_config)
13637+
&args.entropy_source, &args.signer_provider, &provided_channel_type_features(&args.default_config)
1363813638
))?;
1363913639
let logger = WithChannelContext::from(&args.logger, &channel.context, None);
1364013640
let channel_id = channel.context.channel_id();

0 commit comments

Comments
 (0)