From 5caddc01db6e8ae502ef8ff84da0c9531f3940c9 Mon Sep 17 00:00:00 2001 From: Ignotus Peverell Date: Tue, 8 Jan 2019 19:18:44 +0000 Subject: [PATCH 1/2] Enforce zeroing of serialized proof excess bits --- core/src/pow/types.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/core/src/pow/types.rs b/core/src/pow/types.rs index 13b59a09..51e059dd 100644 --- a/core/src/pow/types.rs +++ b/core/src/pow/types.rs @@ -395,10 +395,14 @@ impl Readable for Proof { return Err(ser::Error::CorruptedData); } + // prepare nonces and read the right number of bytes let mut nonces = Vec::with_capacity(global::proofsize()); let nonce_bits = edge_bits as usize; - let bytes_len = BitVec::bytes_len(nonce_bits * global::proofsize()); + let bits_len = nonce_bits * global::proofsize(); + let bytes_len = BitVec::bytes_len(bits_len); let bits = reader.read_fixed_bytes(bytes_len)?; + + // set our nonces from what we read in the bitvec let bitvec = BitVec { bits }; for n in 0..global::proofsize() { let mut nonce = 0; @@ -409,6 +413,15 @@ impl Readable for Proof { } nonces.push(nonce); } + + // check the last bits of the last byte are zeroed, we don't use them but + // still better to enforce to avoid any malleability + for n in (bits_len+1)..(bytes_len*8) { + if bitvec.bit_at(n) { + return Err(ser::Error::CorruptedData); + } + } + Ok(Proof { edge_bits, nonces }) } } From 1806ee34f5beba7ba9511e640fd7bc1e83075970 Mon Sep 17 00:00:00 2001 From: Ignotus Peverell Date: Tue, 8 Jan 2019 22:43:33 +0000 Subject: [PATCH 2/2] Off by one --- core/src/pow/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/pow/types.rs b/core/src/pow/types.rs index 51e059dd..6d71a321 100644 --- a/core/src/pow/types.rs +++ b/core/src/pow/types.rs @@ -416,7 +416,7 @@ impl Readable for Proof { // check the last bits of the last byte are zeroed, we don't use them but // still better to enforce to avoid any malleability - for n in (bits_len+1)..(bytes_len*8) { + for n in bits_len..(bytes_len * 8) { if bitvec.bit_at(n) { return Err(ser::Error::CorruptedData); }