Skip to content

Commit 5dde803

Browse files
committed
Ensure the per-channel key derivation counter doesn't role over
Previously, the `derive_channel_keys` derivation ID asserted that the high bit of the per-channel key derivation counter doesn't role over as it checked the 31st bit was zero. As we no longer do that, we should ensure the assertion in `generate_channel_keys_id` asserts that we don't role over.
1 parent 34de734 commit 5dde803

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

lightning/src/chain/keysinterface.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,12 @@ impl KeysInterface for KeysManager {
12631263

12641264
fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, user_channel_id: u128) -> [u8; 32] {
12651265
let child_idx = self.channel_child_index.fetch_add(1, Ordering::AcqRel);
1266-
assert!(child_idx <= core::u32::MAX as usize);
1266+
// `child_idx` is the only thing guaranteed to make each channel unique without a restart
1267+
// (though `user_channel_id` should help, depending on user behavior). If it manages to
1268+
// roll over, we may generate duplicate keys for two different channels, which could result
1269+
// in loss of funds. Because we only support 32-bit+ systems, assert that our `AtomicUsize`
1270+
// doesn't reach `u32::MAX`.
1271+
assert!(child_idx < core::u32::MAX as usize, "2^32 channels opened without restart");
12671272
let mut id = [0; 32];
12681273
id[0..4].copy_from_slice(&(child_idx as u32).to_be_bytes());
12691274
id[4..8].copy_from_slice(&self.starting_time_nanos.to_be_bytes());

0 commit comments

Comments
 (0)