Fix psbt fuzz crash

Fixes: https://github.com/rust-bitcoin/rust-bitcoin/issues/3628

This occurs when combining two PSBTs with different xpub key sources. Added a length check before indexing into slices to prevent out-of-bounds access.
This commit is contained in:
Sanket Kanjalkar 2024-11-26 21:35:14 -08:00
parent 0bff8d05fc
commit 9aebb96fb9
3 changed files with 15 additions and 2 deletions

View File

@ -255,7 +255,8 @@ impl Psbt {
== derivation2[derivation2.len() - derivation1.len()..]) == derivation2[derivation2.len() - derivation1.len()..])
{ {
continue; continue;
} else if derivation2[..] } else if derivation2.len() <= derivation1.len()
&& derivation2[..]
== derivation1[derivation1.len() - derivation2.len()..] == derivation1[derivation1.len() - derivation2.len()..]
{ {
entry.insert((fingerprint1, derivation1)); entry.insert((fingerprint1, derivation1));
@ -2113,6 +2114,16 @@ mod tests {
assert_eq!(psbt1, psbt2); assert_eq!(psbt1, psbt2);
} }
// https://github.com/rust-bitcoin/rust-bitcoin/issues/3628
#[test]
fn test_combine_psbt_fuzz_3628() {
let mut psbt1 = hex_psbt(include_str!("../../tests/data/psbt_fuzz1.hex")).unwrap();
let psbt2 = hex_psbt(include_str!("../../tests/data/psbt_fuzz2.hex")).unwrap();
assert!(matches!(psbt1.combine(psbt2).unwrap_err(), Error::CombineInconsistentKeySources(_)));
}
#[cfg(feature = "rand-std")] #[cfg(feature = "rand-std")]
fn gen_keys() -> (PrivateKey, PublicKey, Secp256k1<All>) { fn gen_keys() -> (PrivateKey, PublicKey, Secp256k1<All>) {
use secp256k1::rand::thread_rng; use secp256k1::rand::thread_rng;

View File

@ -0,0 +1 @@
70736274ff01000a000000ff0000000074ff4f010488b21eff02000001004a92244992244902030203030303030303030303030303030303030303030303030303030303030303f4000000000000000a000208ffffffff08080804000000000000000c080808000b0000000000010000

View File

@ -0,0 +1 @@
70736274ff01000a000000ff0000000074ff4f010488b21eff02000001004a92244992244902030203030303030303030303030303030303030303030303030303030303030303f4000000000000000a000208ffffffff080808040000000000000008000000000000001000