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

fix(derive_key_from_path): check length of current_key_material #2356

Merged
merged 3 commits into from
Feb 13, 2025

Conversation

DeckerSU
Copy link
Collaborator

@DeckerSU DeckerSU commented Feb 12, 2025

The derive_key_from_path function:

fn derive_key_from_path(master_node: &[u8], path: &str) -> MmResult<[u8; 32], KeyDerivationError> {
let mut current_key_material = master_node.to_vec();
for segment in path.split('/').filter(|s| !s.is_empty()) {
let mut mac = HmacSha512::new_from_slice(&current_key_material[..32])
.map_err(|_| KeyDerivationError::HmacInitialization)?;
mac.update(b"\x00");
mac.update(segment.as_bytes());
drop_mutability!(mac);
let hmac_result = mac.finalize().into_bytes();
current_key_material = hmac_result.to_vec();
}
drop_mutability!(current_key_material);
current_key_material[32..64]
.try_into()
.map_to_mm(|_| KeyDerivationError::InvalidKeyLength)
}

could potentially lead to a panic if the master_node length is not 64.

For example, calling it like this:

let master_node = [0u8; 32];
let result = derive_key_from_path(&master_node, "");

In this case, since the loop over path will not be executed, current_key_material remains unchanged, and attempting to slice current_key_material[32..64] will cause a panic.

Another potential panic occurs when attempting to slice current_key_material[..32] if master_node has a length below 32.

Possible Solution

A simple fix for both cases is to check the length before proceeding:

if current_key_material.len() < 64 {
    return MmError::err(KeyDerivationError::InvalidKeyLength);
}

This would prevent panics and ensure safer execution.

@shamardy shamardy merged commit 84f5c92 into dev Feb 13, 2025
18 of 24 checks passed
@shamardy shamardy deleted the fix-derive-key-path branch February 13, 2025 15:03
dimxy added a commit that referenced this pull request Feb 16, 2025
* dev:
  fix(derive_key_from_path): check length of current_key_material (#2356)
  chore(release): bump mm2 version to 2.4.0-beta (#2346)
  fix(tests): add additional testnet sepolia nodes to test code (#2358)
  fix(swaps): maintain legacy compatibility for negotiation messages (#2353)
  refactor(SwapOps): impl defaults for protocol specific swapops fns (#2354)
  feat(tpu-v2): provide swap protocol versioning (#2324)
  feat(wallet): add change mnemonic password rpc (#2317)
  fix(tpu-v2): fix tpu-v2 wait for payment spend and extract secret (#2261)
  feat(tendermint): unstaking/undelegation (#2330)
  fix(utxo-withdraw): get hw ctx only when `PrivKeyPolicy` is trezor (#2333)
  feat(event-streaming): API-driven subscription management (#2172)
  fix(hash-types): remove panic, enforce fixed-size arrays (#2279)
  fix(ARRR): store unconfirmed change output (#2276)
  feat(tendermint): staking/delegation (#2322)
  chore(deps): `timed-map` migration (#2247)
  fix(mem-leak): `running_swap` never shrinks (#2301)
  chore(dep-bump): libp2p (#2326)
  refactor(build script): rewrite the main build script (#2319)
dimxy added a commit that referenced this pull request Feb 16, 2025
* dev:
  fix(derive_key_from_path): check length of current_key_material (#2356)
  chore(release): bump mm2 version to 2.4.0-beta (#2346)
  fix(tests): add additional testnet sepolia nodes to test code (#2358)
  fix(swaps): maintain legacy compatibility for negotiation messages (#2353)
  refactor(SwapOps): impl defaults for protocol specific swapops fns (#2354)
  feat(tpu-v2): provide swap protocol versioning (#2324)
  feat(wallet): add change mnemonic password rpc (#2317)
  fix(tpu-v2): fix tpu-v2 wait for payment spend and extract secret (#2261)
  feat(tendermint): unstaking/undelegation (#2330)
  fix(utxo-withdraw): get hw ctx only when `PrivKeyPolicy` is trezor (#2333)
  feat(event-streaming): API-driven subscription management (#2172)
  fix(hash-types): remove panic, enforce fixed-size arrays (#2279)
  fix(ARRR): store unconfirmed change output (#2276)
  feat(tendermint): staking/delegation (#2322)
  chore(deps): `timed-map` migration (#2247)
  fix(mem-leak): `running_swap` never shrinks (#2301)
  chore(dep-bump): libp2p (#2326)
  refactor(build script): rewrite the main build script (#2319)
dimxy added a commit to dimxy/komodo-defi-framework that referenced this pull request Feb 24, 2025
* dev: (24 commits)
  fix(eth-tpu): remove state from funding validation (KomodoPlatform#2334)
  improvement(rpc-server): rpc server dynamic port allocation (KomodoPlatform#2342)
  fix(tests): fix or ignore unstable tests (KomodoPlatform#2365)
  fix(fs): make `filter_files_by_extension` return only files (KomodoPlatform#2364)
  fix(derive_key_from_path): check length of current_key_material (KomodoPlatform#2356)
  chore(release): bump mm2 version to 2.4.0-beta (KomodoPlatform#2346)
  fix(tests): add additional testnet sepolia nodes to test code (KomodoPlatform#2358)
  fix(swaps): maintain legacy compatibility for negotiation messages (KomodoPlatform#2353)
  refactor(SwapOps): impl defaults for protocol specific swapops fns (KomodoPlatform#2354)
  feat(tpu-v2): provide swap protocol versioning (KomodoPlatform#2324)
  feat(wallet): add change mnemonic password rpc (KomodoPlatform#2317)
  fix(tpu-v2): fix tpu-v2 wait for payment spend and extract secret (KomodoPlatform#2261)
  feat(tendermint): unstaking/undelegation (KomodoPlatform#2330)
  fix(utxo-withdraw): get hw ctx only when `PrivKeyPolicy` is trezor (KomodoPlatform#2333)
  feat(event-streaming): API-driven subscription management (KomodoPlatform#2172)
  fix(hash-types): remove panic, enforce fixed-size arrays (KomodoPlatform#2279)
  fix(ARRR): store unconfirmed change output (KomodoPlatform#2276)
  feat(tendermint): staking/delegation (KomodoPlatform#2322)
  chore(deps): `timed-map` migration (KomodoPlatform#2247)
  fix(mem-leak): `running_swap` never shrinks (KomodoPlatform#2301)
  ...
dimxy added a commit that referenced this pull request Mar 5, 2025
* dev:
  feat(rpc): add is_success field to legacy MySwapStatusResponse (#2371)
  fix(key-derivation): use stored Argon2 parameters instead of default values (#2360)
  fix(tests): stabilize `tendermint_coin::test_claim_staking_rewards` (#2373)
  improvement(RPCs): group staking rpcs under a namespace (#2372)
  feat(tendermint): claim delegation rewards (#2351)
  fix(eth-tpu): remove state from funding validation (#2334)
  improvement(rpc-server): rpc server dynamic port allocation (#2342)
  fix(tests): fix or ignore unstable tests (#2365)
  fix(fs): make `filter_files_by_extension` return only files (#2364)
  fix(derive_key_from_path): check length of current_key_material (#2356)
  chore(release): bump mm2 version to 2.4.0-beta (#2346)
  fix(tests): add additional testnet sepolia nodes to test code (#2358)
  fix(swaps): maintain legacy compatibility for negotiation messages (#2353)
  refactor(SwapOps): impl defaults for protocol specific swapops fns (#2354)
  feat(tpu-v2): provide swap protocol versioning (#2324)
  feat(wallet): add change mnemonic password rpc (#2317)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants