From 71d47ef58bffdfcb4f7748502fe1a3433eb697a8 Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Fri, 27 Aug 2021 11:17:27 +0800 Subject: [PATCH] [thin/cache (rust)] Validate superblock checksum --- src/cache/superblock.rs | 6 +++++- src/thin/superblock.rs | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/cache/superblock.rs b/src/cache/superblock.rs index 23bc474..abe9b5d 100644 --- a/src/cache/superblock.rs +++ b/src/cache/superblock.rs @@ -134,7 +134,11 @@ fn unpack(data: &[u8]) -> IResult<&[u8], Superblock> { pub fn read_superblock(engine: &dyn IoEngine, loc: u64) -> Result { let b = engine.read(loc)?; - if let Ok((_, sb)) = unpack(&b.get_data()) { + if metadata_block_type(b.get_data()) != BT::CACHE_SUPERBLOCK { + return Err(anyhow!("bad checksum in superblock")); + } + + if let Ok((_, sb)) = unpack(b.get_data()) { Ok(sb) } else { Err(anyhow!("couldn't unpack superblock")) diff --git a/src/thin/superblock.rs b/src/thin/superblock.rs index e9c071c..41990cc 100644 --- a/src/thin/superblock.rs +++ b/src/thin/superblock.rs @@ -89,7 +89,11 @@ fn unpack(data: &[u8]) -> IResult<&[u8], Superblock> { pub fn read_superblock(engine: &dyn IoEngine, loc: u64) -> Result { let b = engine.read(loc)?; - if let Ok((_, sb)) = unpack(&b.get_data()) { + if metadata_block_type(b.get_data()) != BT::THIN_SUPERBLOCK { + return Err(anyhow!("bad checksum in superblock")); + } + + if let Ok((_, sb)) = unpack(b.get_data()) { Ok(sb) } else { Err(anyhow!("couldn't unpack superblock"))