Unverified Commit a42236a1 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

turbo-persistence: cover block lengths in CRCs

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 77da597c
......@@ -86,7 +86,7 @@ The SST file contains only data without any header.
- foreach block
- 4 bytes block header (uncompressed length or sentinel)
- 4 bytes checksum (CRC32 of uncompressed block data)
- 4 bytes checksum (CRC32 of the 4 header bytes and the on-disk block data)
- block data (compressed or uncompressed)
- foreach block
- 4 bytes end of block offset relative to start of all blocks
......@@ -102,7 +102,7 @@ The 4-byte header distinguishes compressed from uncompressed storage:
#### Block Checksum
Each block stores a 4-byte CRC32 checksum (big-endian) computed on the **on-disk** block data (i.e. after compression). On read, the checksum is verified **before** decompression so that on-disk damage is caught before passing data to LZ4. A checksum mismatch returns an error indicating that the cached data is damaged.
Each block stores a 4-byte CRC32 checksum (big-endian) computed over the 4-byte header (uncompressed length or sentinel, big-endian) followed by the **on-disk** block data (i.e. after compression). On read, the checksum is verified **before** decompression, so that on-disk damage to either the data or the header is caught before the header's length is used to size a buffer or the data is passed to the decompressor. A checksum mismatch returns an error indicating that the cached data is damaged. Compaction copies medium value blocks without decompressing or verifying them, keeping their header and checksum, so a damaged block stays detectable when the copy is read.
#### Index Block
......@@ -250,10 +250,10 @@ reordering entries within a block is a format violation rather than a free choic
The plain value compressed with dynamic compression. Each blob file has an 8-byte header:
- 4 bytes: uncompressed length (u32 big-endian)
- 4 bytes: CRC32 checksum of the compressed data (u32 big-endian)
- 4 bytes: CRC32 checksum of the uncompressed length bytes followed by the compressed data (u32 big-endian)
- remaining bytes: value data compressed with the blob's key-family configuration
The checksum is verified on the compressed data **before** decompression when the blob is read.
The checksum is verified **before** decompression when the blob is read, so a damaged length is caught before it is used to size the decompression buffer.
## Reading
......
......@@ -337,7 +337,7 @@ fn read_block(
compressed_size
};
let actual_checksum = checksum_block(compressed_data);
let actual_checksum = checksum_block(uncompressed_length, compressed_data);
if actual_checksum != expected_checksum {
bail!(
"Cache corruption detected: checksum mismatch in block {} of {:08}.sst (expected \
......@@ -1017,7 +1017,7 @@ mod tests {
fn uncompressed_block(data: &[u8]) -> Vec<u8> {
let mut block = 0_u32.to_be_bytes().to_vec();
block.extend_from_slice(&checksum_block(data).to_be_bytes());
block.extend_from_slice(&checksum_block(0, data).to_be_bytes());
block.extend_from_slice(data);
block
}
......@@ -1111,10 +1111,27 @@ mod tests {
let data = b"compressible compressible compressible";
let compressed = lz4_flex::block::compress(data);
// Claim one more byte than the compressed data decodes to.
let mut block = (data.len() as u32 + 1).to_be_bytes().to_vec();
block.extend_from_slice(&checksum_block(&compressed).to_be_bytes());
let claimed_length = data.len() as u32 + 1;
let mut block = claimed_length.to_be_bytes().to_vec();
block.extend_from_slice(&checksum_block(claimed_length, &compressed).to_be_bytes());
block.extend_from_slice(&compressed);
let message = analyze_err(&sst_bytes(&[block]), 1);
assert!(message.contains("Decompressed length"), "{message}");
}
#[test]
fn analyze_rejects_corrupt_uncompressed_length() {
let data = b"compressible compressible compressible";
let compressed = lz4_flex::block::compress(data);
let mut block = (data.len() as u32).to_be_bytes().to_vec();
block.extend_from_slice(&checksum_block(data.len() as u32, &compressed).to_be_bytes());
block.extend_from_slice(&compressed);
for corrupt_length in [data.len() as u32 + 1, u32::MAX] {
// Corrupt only the length; the stored checksum no longer matches the header.
let mut block = block.clone();
block[..4].copy_from_slice(&corrupt_length.to_be_bytes());
let message = analyze_err(&sst_bytes(&[block]), 1);
assert!(message.contains("checksum mismatch"), "{message}");
}
}
}
......@@ -119,9 +119,17 @@ pub(crate) fn decompress_into_rc(
Ok(buffer)
}
/// Computes a CRC32 checksum of a byte slice.
pub fn checksum_block(data: &[u8]) -> u32 {
crc32fast::hash(data)
/// Computes the CRC32 checksum stored in a block or blob header.
///
/// The checksum covers the header's big-endian `uncompressed_length` followed by the on-disk
/// (possibly compressed) `data`. Readers verify it before decompressing, so a corrupt length is
/// rejected before it sizes a decompression buffer, and a corrupt compressed/uncompressed
/// sentinel is rejected before the data is used.
pub fn checksum_block(uncompressed_length: u32, data: &[u8]) -> u32 {
let mut hasher = crc32fast::Hasher::new();
hasher.update(&uncompressed_length.to_be_bytes());
hasher.update(data);
hasher.finalize()
}
/// Reusable compressor for a stream of blocks using the same family configuration.
......@@ -211,4 +219,20 @@ mod tests {
assert_eq!(&*output, input);
}
}
#[test]
fn checksum_covers_uncompressed_length() {
let data = b"block data";
// The checksum is the CRC32 of the big-endian length followed by the data.
for (length, data) in [(0, &data[..]), (10, data), (u32::MAX, data), (7, &[][..])] {
let mut prefixed = length.to_be_bytes().to_vec();
prefixed.extend_from_slice(data);
assert_eq!(checksum_block(length, data), crc32fast::hash(&prefixed));
}
// Changing only the length changes the checksum.
assert_ne!(checksum_block(0, data), checksum_block(1, data));
assert_ne!(checksum_block(10, data), checksum_block(11, data));
assert_ne!(checksum_block(10, data), checksum_block(u32::MAX, data));
assert_ne!(checksum_block(0, &[]), checksum_block(1, &[]));
}
}
......@@ -748,8 +748,9 @@ impl<S: ParallelScheduler, const FAMILIES: usize> TurboPersistence<S, FAMILIES>
.context("Failed to read uncompressed length from blob file")?;
let expected_checksum = reader.read_u32::<BE>()?;
// Verify checksum on the compressed on-disk data before decompression.
let actual_checksum = checksum_block(reader);
// Verify the checksum, which covers the length header and the compressed on-disk data,
// before the length is used to size the decompression buffer.
let actual_checksum = checksum_block(uncompressed_length, reader);
if actual_checksum != expected_checksum {
bail!(
"Cache corruption detected: checksum mismatch in blob file {:08}.blob (expected \
......
......@@ -482,8 +482,9 @@ impl IndexBlock {
/// Reads an SST's index block, which is always its last block, with one positional read.
///
/// Checks that the block is uncompressed (index blocks always are), that its CRC matches, and that
/// it starts with the index block type. Returns the block data after the block header.
/// Checks that its CRC (which covers the uncompressed length) matches, that the block is
/// uncompressed (index blocks always are), and that it starts with the index block type. Returns
/// the block data after the block header.
fn read_index_block<B: SharedBytes>(
file: &File,
block_offsets: &BlockOffsets,
......@@ -503,6 +504,9 @@ fn read_index_block<B: SharedBytes>(
block_index, meta.sequence_number
)
})?;
// Verified here rather than through `verified_blocks`: this is the one and only read of this
// block's bytes. Verified before the length check, since the checksum also covers the length.
verify_checksum(meta, uncompressed_length, &data, checksum, block_index)?;
ensure!(
uncompressed_length == 0,
"index block {} of {:08}.sst is compressed, but index blocks are always written \
......@@ -510,9 +514,6 @@ fn read_index_block<B: SharedBytes>(
block_index,
meta.sequence_number
);
// Verified here rather than through `verified_blocks`: this is the one and only read of this
// block's bytes.
verify_checksum(meta, &data, checksum, block_index)?;
ensure!(
data.first() == Some(&BLOCK_TYPE_INDEX),
"block {} of {:08}.sst is the last block but not an index block (type {:?})",
......@@ -895,7 +896,14 @@ fn get_or_read_block<'l>(
if uncompressed_length == 0 {
// Uncompressed: borrow directly from the mmap, taking no refcount.
// Verify CRC only once per file open.
verify_checksum_once(meta, block_data, checksum, block_index, verified_blocks)?;
verify_checksum_once(
meta,
uncompressed_length,
block_data,
checksum,
block_index,
verified_blocks,
)?;
return Ok(BlockRef::Mmap(block_data));
}
Some((uncompressed_length, checksum, block_data))
......@@ -927,14 +935,19 @@ fn get_or_read_block<'l>(
#[cfg(feature = "mmap")]
StaticSortedFileBacking::Mmap(_) => verify_checksum_once(
meta,
uncompressed_length,
&block_data,
checksum,
block_index,
verified_blocks,
)?,
StaticSortedFileBacking::File { .. } => {
verify_checksum(meta, &block_data, checksum, block_index)?
}
StaticSortedFileBacking::File { .. } => verify_checksum(
meta,
uncompressed_length,
&block_data,
checksum,
block_index,
)?,
}
let block = if uncompressed_length == 0 {
block_data.into_arc_bytes()
......@@ -1174,14 +1187,16 @@ fn pread_buf(file: &File, buf: io::BorrowedCursor<'_, u8>, offset: u64) -> io::R
}
}
/// Verifies the CRC32 checksum of on-disk block data. Returns an error on mismatch.
/// Verifies the CRC32 checksum of a block, which covers the header's `uncompressed_length` and the
/// on-disk block data. Returns an error on mismatch.
fn verify_checksum(
meta: &StaticSortedFileMetaData,
uncompressed_length: u32,
data: &[u8],
expected: u32,
block_index: u16,
) -> Result<()> {
let actual = checksum_block(data);
let actual = checksum_block(uncompressed_length, data);
if actual != expected {
bail!(
"Cache corruption detected: checksum mismatch in block {} of {:08}.sst (expected \
......@@ -1215,6 +1230,7 @@ fn is_block_verified(block_index: u16, verified_blocks: &[AtomicU64]) -> bool {
#[cfg_attr(not(feature = "mmap"), allow(dead_code))]
fn verify_checksum_once(
meta: &StaticSortedFileMetaData,
uncompressed_length: u32,
data: &[u8],
expected: u32,
block_index: u16,
......@@ -1225,7 +1241,7 @@ fn verify_checksum_once(
if verified_blocks[word_idx].load(AtomicOrdering::Relaxed) & bit != 0 {
return Ok(());
}
verify_checksum(meta, data, expected, block_index)?;
verify_checksum(meta, uncompressed_length, data, expected, block_index)?;
verified_blocks[word_idx].fetch_or(bit, AtomicOrdering::Relaxed);
Ok(())
}
......@@ -1240,7 +1256,7 @@ fn read_block_lookup(
) -> Result<ArcBytes> {
let (uncompressed_length, checksum, block) =
get_raw_block(backing, block_offsets, meta, block_index)?;
verify_checksum(meta, &block, checksum, block_index)?;
verify_checksum(meta, uncompressed_length, &block, checksum, block_index)?;
if uncompressed_length == 0 {
return match (backing, block) {
#[cfg(feature = "mmap")]
......@@ -1281,7 +1297,7 @@ fn read_block_iter(
) -> Result<RcBytes> {
let (uncompressed_length, checksum, block) =
get_raw_block_iter(file, block_offsets, meta, block_index)?;
verify_checksum(meta, &block, checksum, block_index)?;
verify_checksum(meta, uncompressed_length, &block, checksum, block_index)?;
if uncompressed_length == 0 {
return Ok(block);
}
......@@ -2163,7 +2179,7 @@ mod tests {
// A valid CRC of an empty uncompressed block must still be rejected by both readers before
// they read the index block type.
let mut data = 0_u32.to_be_bytes().to_vec();
data.extend_from_slice(&checksum_block(&[]).to_be_bytes());
data.extend_from_slice(&checksum_block(0, &[]).to_be_bytes());
assert_open_fails(1, &data, &[8], "not an index block");
}
}
......@@ -2012,6 +2012,53 @@ fn count_blob_files(dir: &Path) -> usize {
.count()
}
/// The blob CRC covers the header's uncompressed length, so corrupting only the length is reported
/// as a checksum mismatch before it is used to size the decompression buffer.
// Writes a production-size blob, which is too slow under Miri.
#[cfg(not(miri))]
#[rstest]
#[case(true)]
#[case(false)]
fn corrupt_blob_length_is_detected(#[case] mmap: bool) -> Result<()> {
let tempdir = tempfile::tempdir()?;
let path = tempdir.path();
let blob_value = vec![42u8; MAX_MEDIUM_VALUE_SIZE + 1];
{
let db = open_db::<1>(path, mmap)?;
let batch = db.write_batch()?;
batch.put(0, vec![1u8], blob_value.clone().into())?;
db.commit_write_batch(batch)?;
db.shutdown();
}
let blob_path = fs::read_dir(path)?
.filter_map(|e| e.ok())
.map(|e| e.path())
.find(|p| p.extension().is_some_and(|ext| ext == "blob"))
.expect("a blob file");
let original = fs::read(&blob_path)?;
let length = u32::from_be_bytes(original[..4].try_into()?);
assert_eq!(length as usize, blob_value.len());
for corrupt_length in [length + 1, length - 1, 0, u32::MAX] {
let mut bytes = original.clone();
bytes[..4].copy_from_slice(&corrupt_length.to_be_bytes());
fs::write(&blob_path, &bytes)?;
let db = open_db::<1>(path, mmap)?;
let message = match db.get(0, &vec![1u8]) {
Ok(value) => panic!(
"length {corrupt_length}: read succeeded ({:?} bytes)",
value.map(|v| v.len())
),
Err(err) => format!("{err:#}"),
};
assert!(
message.contains("checksum mismatch"),
"length {corrupt_length}: unexpected error: {message}"
);
}
Ok(())
}
/// Test that compaction deletes blob files when their entries are superseded
/// by newer values (SingleValue family).
// The first access-mode variant exceeded 20 minutes under Miri while processing the production-size
......
......@@ -511,7 +511,7 @@ impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize
let mut buffer = Vec::with_capacity(8 + compressed.len());
buffer.write_u32::<BE>(value.len() as u32)?;
buffer.write_u32::<BE>(checksum_block(&compressed))?;
buffer.write_u32::<BE>(checksum_block(value.len() as u32, &compressed))?;
buffer.extend_from_slice(&compressed);
let size = buffer.len() as u64;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment