Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure derive_channel_keys doesn't panic if per-run seed is high #1935

Merged

Conversation

TheBlueMatt
Copy link
Collaborator

b04d1b8 changed the way we
calculate the channel_keys_id to include the 128-bit
user_channel_id as well, shifting the counter up four bytes and
the starting_time_nanos field up into the second four bytes.

In derive_channel_keys we hash the full channel_keys_id with an
HD-derived key from our master seed. Previously, that key was
derived with an index of the per-restart counter, re-calculated by
pulling the second four bytes out of the user_channel_id. Because
the channel_keys_id fields were shifted up four bytes, that is
now a reference to the starting_time_nanos value. This should be
fine, the derivation doesn't really add any value here, its all
being hashed anyway, except that derivation IDs must be below 2^31.
This implies that we panic if the user passes a
starting_time_nanos which has the high bit set. For those using
the nanosecond part of the current time this isn't an issue - the
value cannot exceed 1_000_000, which does not have the high bit
set, however, some users may use some other per-run seed.

Thus, here we simply drop the high bit from the seed, ensuring we
don't panic. Note that this is backwards compatible as it only
changes the key derivation in cases where we previously panicked.

Ideally we'd drop the derivation entirely, but that would break
backwards compatibility of key derivation.

I'm a little less concerned about this for rust users, but I do think we should backport this to 113 bindings, as bindings users more often use non-time for the starting_time_nanos field.

b04d1b8 changed the way we
calculate the `channel_keys_id` to include the 128-bit
`user_channel_id` as well, shifting the counter up four bytes and
the `starting_time_nanos` field up into the second four bytes.

In `derive_channel_keys` we hash the full `channel_keys_id` with an
HD-derived key from our master seed. Previously, that key was
derived with an index of the per-restart counter, re-calculated by
pulling the second four bytes out of the `user_channel_id`. Because
the `channel_keys_id` fields were shifted up four bytes, that is
now a reference to the `starting_time_nanos` value. This should be
fine, the derivation doesn't really add any value here, its all
being hashed anyway, except that derivation IDs must be below 2^31.
This implies that we panic if the user passes a
`starting_time_nanos` which has the high bit set. For those using
the nanosecond part of the current time this isn't an issue - the
value cannot exceed 1_000_000, which does not have the high bit
set, however, some users may use some other per-run seed.

Thus, here we simply drop the high bit from the seed, ensuring we
don't panic. Note that this is backwards compatible as it only
changes the key derivation in cases where we previously panicked.

Ideally we'd drop the derivation entirely, but that would break
backwards compatibility of key derivation.
@TheBlueMatt TheBlueMatt added this to the 0.0.114 milestone Dec 28, 2022
@codecov-commenter
Copy link

codecov-commenter commented Dec 28, 2022

Codecov Report

Base: 90.77% // Head: 91.72% // Increases project coverage by +0.94% 🎉

Coverage data is based on head (5dde803) compared to base (f6a9382).
Patch coverage: 83.33% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1935      +/-   ##
==========================================
+ Coverage   90.77%   91.72%   +0.94%     
==========================================
  Files          94       96       +2     
  Lines       49603    57157    +7554     
  Branches    49603    57157    +7554     
==========================================
+ Hits        45028    52427    +7399     
- Misses       4575     4730     +155     
Impacted Files Coverage Δ
lightning/src/chain/keysinterface.rs 86.02% <83.33%> (+2.87%) ⬆️
lightning/src/util/events.rs 29.35% <0.00%> (-0.23%) ⬇️
lightning/src/chain/onchaintx.rs 95.17% <0.00%> (-0.21%) ⬇️
lightning/src/ln/reorg_tests.rs 100.00% <0.00%> (ø)
lightning/src/offers/refund.rs 93.73% <0.00%> (ø)
lightning/src/ln/outbound_payment.rs 89.26% <0.00%> (ø)
lightning/src/ln/channel.rs 89.03% <0.00%> (+0.17%) ⬆️
lightning/src/ln/monitor_tests.rs 99.84% <0.00%> (+0.27%) ⬆️
lightning/src/ln/inbound_payment.rs 93.78% <0.00%> (+0.29%) ⬆️
lightning/src/offers/offer.rs 93.91% <0.00%> (+0.46%) ⬆️
... and 12 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, two nits.

lightning/src/chain/keysinterface.rs Show resolved Hide resolved
lightning/src/chain/keysinterface.rs Outdated Show resolved Hide resolved
@@ -1051,7 +1051,9 @@ impl KeysManager {
// We only seriously intend to rely on the channel_master_key for true secure
// entropy, everything else just ensures uniqueness. We rely on the unique_start (ie
// starting_time provided in the constructor) to be unique.
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");
let child_privkey = self.channel_master_key.ckd_priv(&self.secp_ctx,
ChildNumber::from_hardened_idx((chan_id as u32) % (1 << 31)).expect("key space exhausted")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great catch, but this is getting too complicated. Let's extract chan_id % (1 << 31) into a variable, and then let's extract ChildNumber::from_hardened_idx into a separate variable, and pass that to the method in one line.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how more variables makes things more readable, the indentation as-is makes clear what's being expected where, no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, most people find that more comfortable to read, but the PR's perfectly fine as is.

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.
@TheBlueMatt
Copy link
Collaborator Author

Pushed the fixup of the comments in the last commit -

diff --git a/lightning/src/chain/keysinterface.rs b/lightning/src/chain/keysinterface.rs
index 2a3fb2cc8..d260d0294 100644
--- a/lightning/src/chain/keysinterface.rs
+++ b/lightning/src/chain/keysinterface.rs
@@ -1264,8 +1264,9 @@ impl KeysInterface for KeysManager {
        fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, user_channel_id: u128) -> [u8; 32] {
                let child_idx = self.channel_child_index.fetch_add(1, Ordering::AcqRel);
-               // child_idx is the only thing guaranteed to make each channel unique without a restart
+               // `child_idx` is the only thing guaranteed to make each channel unique without a restart
                // (though `user_channel_id` should help, depending on user behavior). If it manages to
-               // role over, we're screwed. Because we only support 32-bit+ systems, assert that our
-               // AtomicUsize doesn't reach u32::MAX.
+               // roll over, we may generate duplicate keys for two different channels, which could result
+               // in loss of funds. Because we only support 32-bit+ systems, assert that our `AtomicUsize`
+               // doesn't reach `u32::MAX`.
                assert!(child_idx < core::u32::MAX as usize, "2^32 channels opened without restart");
                let mut id = [0; 32];

@TheBlueMatt TheBlueMatt merged commit e1208bf into lightningdevkit:main Jan 3, 2023
MaxFangX added a commit to lexe-app/rust-lightning that referenced this pull request Jan 12, 2023
phlip9 pushed a commit to lexe-app/lexe-public that referenced this pull request Jan 23, 2023
Like 0.0.112, 0.0.113 is broken; LDK panics with 50% probability if we
use random nanos:

lightningdevkit/rust-lightning#1935

This PR updates LDK past 0.0.113 to the commit that merges the fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants