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

Commit

Permalink
Merge pull request #6227 from paritytech/rlp-check
Browse files Browse the repository at this point in the history
Untrusted RLP length overflow check
  • Loading branch information
rphmeier authored Aug 3, 2017
2 parents 0c7c34e + d30e47a commit ae9f356
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions util/rlp/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub enum DecoderError {
RlpInvalidIndirection,
/// Declared length is inconsistent with data specified after.
RlpInconsistentLengthAndData,
/// Declared length is invalid and results in overflow
RlpInvalidLength,
/// Custom rlp decoding error.
Custom(&'static str),
}
Expand Down
13 changes: 11 additions & 2 deletions util/rlp/src/untrusted_rlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ impl<'a> BasicDecoder<'a> {
}
let len = decode_usize(&bytes[1..begin_of_value])?;

let last_index_of_value = begin_of_value + len;
let last_index_of_value = begin_of_value.checked_add(len)
.ok_or(DecoderError::RlpInvalidLength)?;
if bytes.len() < last_index_of_value {
return Err(DecoderError::RlpInconsistentLengthAndData);
}
Expand All @@ -385,7 +386,7 @@ impl<'a> BasicDecoder<'a> {

#[cfg(test)]
mod tests {
use UntrustedRlp;
use {UntrustedRlp, DecoderError};

#[test]
fn test_rlp_display() {
Expand All @@ -394,4 +395,12 @@ mod tests {
let rlp = UntrustedRlp::new(&data);
assert_eq!(format!("{}", rlp), "[\"0x05\", \"0x010efbef67941f79b2\", \"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421\", \"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470\"]");
}

#[test]
fn length_overflow() {
let bs = [0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe5];
let rlp = UntrustedRlp::new(&bs);
let res: Result<u8, DecoderError> = rlp.as_val();
assert_eq!(Err(DecoderError::RlpInvalidLength), res);
}
}

0 comments on commit ae9f356

Please sign in to comment.