Skip to content

Commit 4ea8fd7

Browse files
TDemecobkchr
authored andcommitted
sp-trie: minor fix to avoid possible panic during node decoding (paritytech#6486)
# Description This PR is a simple fix consisting of adding a check to the process of decoding nodes of a storage proof to avoid panicking when receiving badly-constructed proofs, returning an error instead. This would close paritytech#6485 ## Integration No changes have to be done downstream, and as such the version bump should be minor. --------- Co-authored-by: Bastian Köcher <git@kchr.de>
1 parent 804c910 commit 4ea8fd7

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

prdoc/pr_6486.prdoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
title: "sp-trie: minor fix to avoid panic on badly-constructed proof"
2+
3+
doc:
4+
- audience: ["Runtime Dev", "Runtime User"]
5+
description: |
6+
"Added a check when decoding encoded proof nodes in `sp-trie` to avoid panicking when receiving a badly constructed proof, instead erroring out."
7+
8+
crates:
9+
- name: sp-trie
10+
bump: patch

substrate/primitives/trie/src/node_codec.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ where
110110
NodeHeader::Null => Ok(NodePlan::Empty),
111111
NodeHeader::HashedValueBranch(nibble_count) | NodeHeader::Branch(_, nibble_count) => {
112112
let padding = nibble_count % nibble_ops::NIBBLE_PER_BYTE != 0;
113+
// data should be at least the size of the offset
114+
if data.len() < input.offset {
115+
return Err(Error::BadFormat)
116+
}
113117
// check that the padding is valid (if any)
114118
if padding && nibble_ops::pad_left(data[input.offset]) != 0 {
115119
return Err(Error::BadFormat)
@@ -154,6 +158,10 @@ where
154158
},
155159
NodeHeader::HashedValueLeaf(nibble_count) | NodeHeader::Leaf(nibble_count) => {
156160
let padding = nibble_count % nibble_ops::NIBBLE_PER_BYTE != 0;
161+
// data should be at least the size of the offset
162+
if data.len() < input.offset {
163+
return Err(Error::BadFormat)
164+
}
157165
// check that the padding is valid (if any)
158166
if padding && nibble_ops::pad_left(data[input.offset]) != 0 {
159167
return Err(Error::BadFormat)

0 commit comments

Comments
 (0)