From 2f30ba437f3224f1a56d6a97c85a1c4c83c893d8 Mon Sep 17 00:00:00 2001 From: Jasper van der Maarel Date: Wed, 20 Jan 2021 15:58:21 +0100 Subject: [PATCH] Safety: check length during deserialization --- core/src/libtx/secp_ser.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/src/libtx/secp_ser.rs b/core/src/libtx/secp_ser.rs index e0989fd60b..b527f3a0f8 100644 --- a/core/src/libtx/secp_ser.rs +++ b/core/src/libtx/secp_ser.rs @@ -83,6 +83,9 @@ pub mod option_sig_serde { Some(string) => from_hex(&string) .map_err(Error::custom) .and_then(|bytes: Vec| { + if bytes.len() < 64 { + return Err(Error::invalid_length(bytes.len(), &"64 bytes")); + } let mut b = [0u8; 64]; b.copy_from_slice(&bytes[0..64]); secp::Signature::from_compact(&static_secp, &b) @@ -125,6 +128,9 @@ pub mod option_seckey_serde { Some(string) => from_hex(&string) .map_err(Error::custom) .and_then(|bytes: Vec| { + if bytes.len() < 32 { + return Err(Error::invalid_length(bytes.len(), &"32 bytes")); + } let mut b = [0u8; 32]; b.copy_from_slice(&bytes[0..32]); secp::key::SecretKey::from_slice(&static_secp, &b) @@ -162,6 +168,9 @@ pub mod sig_serde { String::deserialize(deserializer) .and_then(|string| from_hex(&string).map_err(Error::custom)) .and_then(|bytes: Vec| { + if bytes.len() < 64 { + return Err(Error::invalid_length(bytes.len(), &"64 bytes")); + } let mut b = [0u8; 64]; b.copy_from_slice(&bytes[0..64]); secp::Signature::from_compact(&static_secp, &b).map_err(Error::custom)