Skip to content

Commit e1208bf

Browse files
authored
Merge pull request #1935 from TheBlueMatt/2022-12-no-non-time-panic
Ensure derive_channel_keys doesn't panic if per-run seed is high
2 parents 7d84a45 + 5dde803 commit e1208bf

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lightning/src/chain/keysinterface.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,9 @@ impl KeysManager {
10721072
// We only seriously intend to rely on the channel_master_key for true secure
10731073
// entropy, everything else just ensures uniqueness. We rely on the unique_start (ie
10741074
// starting_time provided in the constructor) to be unique.
1075-
let child_privkey = self.channel_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(chan_id as u32).expect("key space exhausted")).expect("Your RNG is busted");
1075+
let child_privkey = self.channel_master_key.ckd_priv(&self.secp_ctx,
1076+
ChildNumber::from_hardened_idx((chan_id as u32) % (1 << 31)).expect("key space exhausted")
1077+
).expect("Your RNG is busted");
10761078
unique_start.input(&child_privkey.private_key[..]);
10771079

10781080
let seed = Sha256::from_engine(unique_start).into_inner();
@@ -1298,7 +1300,12 @@ impl SignerProvider for KeysManager {
12981300

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

0 commit comments

Comments
 (0)