Skip to content

Commit

Permalink
transaction: range-check pegin data when parsing
Browse files Browse the repository at this point in the history
It is possible to cause a panic by calling `TxIn::pegin_data` on a
malformed pegin, which has 6 entries and the last one (which should be a
Merkle proof of block inclusion) is too small to even have a blockhash
in it.

On the actual chain this should not be possible because invalid pegin
data would be rejected.
  • Loading branch information
apoelstra committed Jun 20, 2024
1 parent a7c5013 commit 639c964
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ impl<'tx> PeginData<'tx> {
if pegin_witness.len() != 6 {
return Err("size not 6");
}
if pegin_witness[5].len() < 80 {
return Err("merkle proof too short");
}

Ok(PeginData {
outpoint: prevout,
Expand Down Expand Up @@ -2379,4 +2382,21 @@ mod tests {
Err(encode::Error::ParseFailed("superfluous asset issuance")),
));
}

#[test]
fn malformed_pegin() {
let mut input: TxIn = hex_deserialize!("\
0004000000000000ffffffff0000040000c0c0c0c0c0c0c0c0c0000000000000\
00805555555555555505c0c0c0c0c03fc0c0c0c0c0c0c0c0c0c0c0c00200ff01\
0000000000fd0000000000000000010000000000ffffffffffffffff00000000\
000000ff000000000000010000000000000000000001002d342d35313700\
");
input.witness = hex_deserialize!("\
0000000608202020202020202020202020202020202020202020202020202020\
2020202020202020202020202020202020202020202020202020202020202020\
2020202020202020202020202020202020202020202020202020202020200000\
00000000000000000000000000000002000400000000\
");
assert!(input.pegin_data().is_none());
}
}

0 comments on commit 639c964

Please sign in to comment.