Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
contracts: Apply depth limit when decoding (#11991)
Browse files Browse the repository at this point in the history
  • Loading branch information
athei authored Aug 8, 2022
1 parent ef890f5 commit 74a6370
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions frame/contracts/src/wasm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
};

use bitflags::bitflags;
use codec::{Decode, DecodeAll, Encode, MaxEncodedLen};
use codec::{Decode, DecodeLimit, Encode, MaxEncodedLen};
use frame_support::{dispatch::DispatchError, ensure, traits::Get, weights::Weight};
use pallet_contracts_primitives::{ExecReturnValue, ReturnFlags};
use sp_core::{crypto::UncheckedFrom, Bytes};
Expand All @@ -36,6 +36,9 @@ use sp_sandbox::SandboxMemory;
use sp_std::prelude::*;
use wasm_instrument::parity_wasm::elements::ValueType;

/// The maximum nesting depth a contract can use when encoding types.
const MAX_DECODE_NESTING: u32 = 256;

/// Type of a storage key.
#[allow(dead_code)]
enum KeyType {
Expand Down Expand Up @@ -575,7 +578,7 @@ where
ptr: u32,
) -> Result<D, DispatchError> {
let buf = self.read_sandbox_memory(ptr, D::max_encoded_len() as u32)?;
let decoded = D::decode_all(&mut &buf[..])
let decoded = D::decode_all_with_depth_limit(MAX_DECODE_NESTING, &mut &buf[..])
.map_err(|_| DispatchError::from(Error::<E::T>::DecodingFailed))?;
Ok(decoded)
}
Expand All @@ -597,7 +600,7 @@ where
len: u32,
) -> Result<D, DispatchError> {
let buf = self.read_sandbox_memory(ptr, len)?;
let decoded = D::decode_all(&mut &buf[..])
let decoded = D::decode_all_with_depth_limit(MAX_DECODE_NESTING, &mut &buf[..])
.map_err(|_| DispatchError::from(Error::<E::T>::DecodingFailed))?;
Ok(decoded)
}
Expand Down

0 comments on commit 74a6370

Please sign in to comment.