Merge pull request #594 from RCasatta/capped

Count bytes read in encoding
This commit is contained in:
Dr. Maxim Orlovsky 2021-05-01 16:28:57 +02:00 committed by GitHub
commit 68096242d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 7 deletions

View File

@ -34,6 +34,7 @@ use blockdata::constants::WITNESS_SCALE_FACTOR;
#[cfg(feature="bitcoinconsensus")] use blockdata::script;
use blockdata::script::Script;
use consensus::{encode, Decodable, Encodable};
use consensus::encode::MAX_VEC_SIZE;
use hash_types::{SigHash, Txid, Wtxid};
use VarInt;
@ -566,7 +567,8 @@ impl Encodable for Transaction {
}
impl Decodable for Transaction {
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, encode::Error> {
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, encode::Error> {
let mut d = d.take(MAX_VEC_SIZE as u64);
let version = i32::consensus_decode(&mut d)?;
let input = Vec::<TxIn>::consensus_decode(&mut d)?;
// segwit

View File

@ -573,6 +573,7 @@ macro_rules! impl_vec {
return Err(self::Error::OversizedVectorAllocation { requested: byte_size, max: MAX_VEC_SIZE })
}
let mut ret = Vec::with_capacity(len as usize);
let mut d = d.take(MAX_VEC_SIZE as u64);
for _ in 0..len {
ret.push(Decodable::consensus_decode(&mut d)?);
}
@ -995,6 +996,15 @@ mod tests {
assert_eq!(cd.ok(), Some(CheckedData(vec![1u8, 2, 3, 4, 5])));
}
#[test]
fn limit_read_test() {
let witness = vec![vec![0u8; 3_999_999]; 2];
let ser = serialize(&witness);
let mut reader = io::Cursor::new(ser);
let err = Vec::<Vec<u8>>::consensus_decode(&mut reader);
assert!(err.is_err());
}
#[test]
fn serialization_round_trips() {
macro_rules! round_trip {

View File

@ -33,8 +33,9 @@ macro_rules! impl_consensus_encoding {
impl $crate::consensus::Decodable for $thing {
#[inline]
fn consensus_decode<D: ::std::io::Read>(
mut d: D,
d: D,
) -> Result<$thing, $crate::consensus::encode::Error> {
let mut d = d.take($crate::consensus::encode::MAX_VEC_SIZE as u64);
Ok($thing {
$($field: $crate::consensus::Decodable::consensus_decode(&mut d)?),+
})

View File

@ -29,9 +29,8 @@ use network::address::{Address, AddrV2Message};
use network::message_network;
use network::message_blockdata;
use network::message_filter;
use consensus::encode::{CheckedData, Decodable, Encodable, VarInt};
use consensus::encode::{CheckedData, Decodable, Encodable, VarInt, MAX_VEC_SIZE};
use consensus::{encode, serialize};
use consensus::encode::MAX_VEC_SIZE;
/// The maximum number of [Inventory] items in an `inv` message.
///

View File

@ -19,6 +19,7 @@ use std::cmp;
use blockdata::transaction::Transaction;
use consensus::{encode, Encodable, Decodable};
use consensus::encode::MAX_VEC_SIZE;
use util::psbt::map::Map;
use util::psbt::raw;
use util::psbt;
@ -228,8 +229,8 @@ impl Map for Global {
impl_psbtmap_consensus_encoding!(Global);
impl Decodable for Global {
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, encode::Error> {
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, encode::Error> {
let mut d = d.take(MAX_VEC_SIZE as u64);
let mut tx: Option<Transaction> = None;
let mut version: Option<u32> = None;
let mut unknowns: BTreeMap<raw::Key, Vec<u8>> = Default::default();

View File

@ -21,6 +21,7 @@
use blockdata::script::Script;
use blockdata::transaction::Transaction;
use consensus::{encode, Encodable, Decodable};
use consensus::encode::MAX_VEC_SIZE;
use std::io;
@ -162,7 +163,8 @@ impl Encodable for PartiallySignedTransaction {
}
impl Decodable for PartiallySignedTransaction {
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, encode::Error> {
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, encode::Error> {
let mut d = d.take(MAX_VEC_SIZE as u64);
let magic: [u8; 4] = Decodable::consensus_decode(&mut d)?;
if *b"psbt" != magic {