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(network): validate addr length before reading #6320

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions zebra-network/src/protocol/external/addr/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use byteorder::{BigEndian, ReadBytesExt};
use thiserror::Error;

use zebra_chain::serialization::{
CompactSize64, DateTime32, SerializationError, TrustedPreallocate, ZcashDeserialize,
ZcashDeserializeInto,
zcash_deserialize_bytes_external_count, CompactSize64, DateTime32, SerializationError,
TrustedPreallocate, ZcashDeserialize, ZcashDeserializeInto,
};

use crate::{
Expand Down Expand Up @@ -282,19 +282,22 @@ impl ZcashDeserialize for AddrV2 {
// See the list of reserved network IDs in ZIP 155.
let network_id = reader.read_u8()?;

// > CompactSize The length in bytes of addr.
// > uint8[sizeAddr] Network address. The interpretation depends on networkID.
let addr: Vec<u8> = (&mut reader).zcash_deserialize_into()?;

// > uint16 Network port. If not relevant for the network this MUST be 0.
let port = reader.read_u16::<BigEndian>()?;

if addr.len() > MAX_ADDR_V2_ADDR_SIZE {
// > CompactSize The length in bytes of addr.
let max_size = MAX_ADDR_V2_ADDR_SIZE as u64; // `MAX_ADDR_V2_ADDR_SIZE` fits in `u64`.
let addr_len: CompactSize64 = (&mut reader).zcash_deserialize_into()?;
if addr_len > CompactSize64::from(max_size) {
return Err(SerializationError::Parse(
"addr field longer than MAX_ADDR_V2_ADDR_SIZE in addrv2 message",
));
}

// > uint8[sizeAddr] Network address. The interpretation depends on networkID.
let addr: Vec<u8> =
zcash_deserialize_bytes_external_count(u64::from(addr_len) as usize, &mut reader)?;

// > uint16 Network port. If not relevant for the network this MUST be 0.
let port = reader.read_u16::<BigEndian>()?;

let ip = if network_id == ADDR_V2_IPV4_NETWORK_ID {
AddrV2::ip_addr_from_bytes::<ADDR_V2_IPV4_ADDR_SIZE>(addr)?
} else if network_id == ADDR_V2_IPV6_NETWORK_ID {
Expand Down