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

turbo-persistence: validate sst_inspect block reads with BlockOffsets

Make `BlockOffsets` public so `sst_inspect` reads SST files with the same
block-directory validation as the database, instead of unchecked offset
arithmetic and slicing. Reject zero-block files and decompressed-length
mismatches with errors instead of panicking. Add end-to-end open tests
for corrupt files through both readers in every access mode.
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 2998a2e8
...@@ -437,8 +437,9 @@ Bottom merges are paced by the fresh (not yet compacted) files of the family: th ...@@ -437,8 +437,9 @@ Bottom merges are paced by the fresh (not yet compacted) files of the family: th
reads in file access mode. Either way, its block-offset table (4 bytes per block) is read with one reads in file access mode. Either way, its block-offset table (4 bytes per block) is read with one
positional read (`pread`) and decoded in place. It is validated once (every block holds at least positional read (`pread`) and decoded in place. It is validated once (every block holds at least
its header and ends before the table) and kept in memory, so locating a block neither reads the its header and ends before the table) and kept in memory, so locating a block neither reads the
file nor re-checks the offsets. The index block is read the same way, verified, and kept in memory file nor re-checks the offsets. The validation is exposed as `static_sorted_file::BlockOffsets`,
for every lookup. Blob files are read when a value references them. which the `sst_inspect` tool uses as well. The index block is read the same way, verified, and
kept in memory for every lookup. Blob files are read when a value references them.
## Closing ## Closing
......
...@@ -14,7 +14,7 @@ use std::{ ...@@ -14,7 +14,7 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result, bail, ensure};
use byteorder::{BE, ReadBytesExt}; use byteorder::{BE, ReadBytesExt};
use fs_err::{self as fs, File}; use fs_err::{self as fs, File};
use lz4_flex::block::decompress_into; use lz4_flex::block::decompress_into;
...@@ -26,11 +26,11 @@ use turbo_persistence::{ ...@@ -26,11 +26,11 @@ use turbo_persistence::{
read_current_version, read_current_version,
sst_filter::SstFilter, sst_filter::SstFilter,
static_sorted_file::{ static_sorted_file::{
FIXED_KEY_BLOCK_MIXED_VALUE_TYPE, FixedRegions, KEY_BLOCK_ENTRY_TYPE_BLOB, BlockOffsets, FIXED_KEY_BLOCK_MIXED_VALUE_TYPE, FixedRegions, KEY_BLOCK_ENTRY_TYPE_BLOB,
KEY_BLOCK_ENTRY_TYPE_INLINE_MIN, KEY_BLOCK_ENTRY_TYPE_KEY_DELETED, KEY_BLOCK_ENTRY_TYPE_INLINE_MIN, KEY_BLOCK_ENTRY_TYPE_KEY_DELETED,
KEY_BLOCK_ENTRY_TYPE_KEY_VALUE_DELETED_MIN, KEY_BLOCK_ENTRY_TYPE_MEDIUM, KEY_BLOCK_ENTRY_TYPE_KEY_VALUE_DELETED_MIN, KEY_BLOCK_ENTRY_TYPE_MEDIUM,
KEY_BLOCK_ENTRY_TYPE_SMALL, KEY_BLOCK_TABLE_ENTRY_SIZE_NO_HASH, KeyBlockLayout, KEY_BLOCK_ENTRY_TYPE_SMALL, KEY_BLOCK_TABLE_ENTRY_SIZE_NO_HASH, KeyBlockLayout,
key_block_table_stride, StaticSortedFileMetaData, key_block_table_stride,
}, },
}; };
...@@ -306,21 +306,21 @@ struct RawBlock { ...@@ -306,21 +306,21 @@ struct RawBlock {
} }
/// Reads, checksums, and decompresses a single block from the mmap. /// Reads, checksums, and decompresses a single block from the mmap.
///
/// The block's range comes from `block_offsets`, which was validated against the file length by
/// [`BlockOffsets::read`], so slicing `mmap` (the same file) cannot go out of bounds.
fn read_block( fn read_block(
mmap: &Mmap, mmap: &[u8],
block_offsets_start: usize, block_offsets: &BlockOffsets,
block_index: u16, block_index: u16,
sequence_number: u32, sequence_number: u32,
compression: Compression, compression: Compression,
) -> Result<RawBlock> { ) -> Result<RawBlock> {
let offset = block_offsets_start + block_index as usize * size_of::<u32>(); let range = block_offsets.block_range(block_index).with_context(|| {
format!("Failed to locate block {block_index} of {sequence_number:08}.sst")
let block_start = if block_index == 0 { })?;
0 let block_start = range.start;
} else { let block_end = range.end;
(&mmap[offset - size_of::<u32>()..offset]).read_u32::<BE>()? as usize
};
let block_end = (&mmap[offset..offset + size_of::<u32>()]).read_u32::<BE>()? as usize;
let uncompressed_length = let uncompressed_length =
(&mmap[block_start..block_start + size_of::<u32>()]).read_u32::<BE>()?; (&mmap[block_start..block_start + size_of::<u32>()]).read_u32::<BE>()?;
...@@ -358,9 +358,10 @@ fn read_block( ...@@ -358,9 +358,10 @@ fn read_block(
Compression::Zstd3 => zstd::bulk::decompress_to_buffer(compressed_data, &mut buffer) Compression::Zstd3 => zstd::bulk::decompress_to_buffer(compressed_data, &mut buffer)
.context("zstd decompression failed")?, .context("zstd decompression failed")?,
}; };
assert_eq!( ensure!(
bytes_written, uncompressed_length as usize, bytes_written == uncompressed_length as usize,
"Decompressed length does not match expected" "Decompressed length of block {block_index} in {sequence_number:08}.sst does not \
match its header ({bytes_written} bytes, expected {uncompressed_length})"
); );
buffer.into_boxed_slice() buffer.into_boxed_slice()
} else { } else {
...@@ -489,7 +490,25 @@ fn analyze_sst_file(db_path: &Path, info: &SstInfo) -> Result<SstStats> { ...@@ -489,7 +490,25 @@ fn analyze_sst_file(db_path: &Path, info: &SstInfo) -> Result<SstStats> {
let file = File::open(&path)?; let file = File::open(&path)?;
let file_size = file.metadata()?.len(); let file_size = file.metadata()?.len();
let file_len: usize = file_size.try_into()?;
let meta = StaticSortedFileMetaData {
sequence_number: info.sequence_number,
block_count: info.block_count,
};
// The same validation the database runs when it opens an SST: the offsets must form valid
// blocks that end before the block directory.
let block_offsets = BlockOffsets::read(&file, &meta, file_len)?;
// `BlockOffsets::read` accepts an empty directory; there must be at least the index block.
ensure!(
info.block_count > 0,
"{filename} has no blocks, so no index block"
);
let mmap = unsafe { Mmap::map(file.file())? }; let mmap = unsafe { Mmap::map(file.file())? };
ensure!(
mmap.len() == file_len,
"{filename} changed size while being opened ({file_len} -> {} bytes)",
mmap.len()
);
advise_mmap_for_persistence(&mmap)?; advise_mmap_for_persistence(&mmap)?;
let mut stats = SstStats { let mut stats = SstStats {
...@@ -498,15 +517,13 @@ fn analyze_sst_file(db_path: &Path, info: &SstInfo) -> Result<SstStats> { ...@@ -498,15 +517,13 @@ fn analyze_sst_file(db_path: &Path, info: &SstInfo) -> Result<SstStats> {
..Default::default() ..Default::default()
}; };
let block_offsets_start = mmap.len() - (info.block_count as usize * size_of::<u32>());
// Read the index block (always the last block) first to learn which blocks are key blocks. // Read the index block (always the last block) first to learn which blocks are key blocks.
// Without this, we'd have to guess block types from their first byte, which is wrong for // Without this, we'd have to guess block types from their first byte, which is wrong for
// value blocks (they have no type header and their data can start with any byte). // value blocks (they have no type header and their data can start with any byte).
let index_block_index = info.block_count - 1; let index_block_index = info.block_count - 1;
let index_raw = read_block( let index_raw = read_block(
&mmap, &mmap,
block_offsets_start, &block_offsets,
index_block_index, index_block_index,
info.sequence_number, info.sequence_number,
compression, compression,
...@@ -523,7 +540,7 @@ fn analyze_sst_file(db_path: &Path, info: &SstInfo) -> Result<SstStats> { ...@@ -523,7 +540,7 @@ fn analyze_sst_file(db_path: &Path, info: &SstInfo) -> Result<SstStats> {
for block_index in 0..index_block_index { for block_index in 0..index_block_index {
let raw = match read_block( let raw = match read_block(
&mmap, &mmap,
block_offsets_start, &block_offsets,
block_index, block_index,
info.sequence_number, info.sequence_number,
compression, compression,
...@@ -982,3 +999,122 @@ fn main() -> Result<()> { ...@@ -982,3 +999,122 @@ fn main() -> Result<()> {
Ok(()) Ok(())
} }
#[cfg(test)]
mod tests {
use super::*;
/// Builds an SST from raw block bytes (header included) followed by their end offsets.
fn sst_bytes(blocks: &[Vec<u8>]) -> Vec<u8> {
let mut bytes = blocks.concat();
let mut end = 0;
for block in blocks {
end += block.len() as u32;
bytes.extend_from_slice(&end.to_be_bytes());
}
bytes
}
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(data);
block
}
/// Writes `bytes` as `00000001.sst` and returns its directory, which must outlive any use.
fn write_sst(bytes: &[u8]) -> tempfile::TempDir {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("00000001.sst"), bytes).unwrap();
dir
}
/// Runs the full analysis on `bytes` and returns its error message.
fn analyze_err(bytes: &[u8], block_count: u16) -> String {
let dir = write_sst(bytes);
let info = SstInfo {
sequence_number: 1,
block_count,
compression: Compression::Lz4,
};
let Err(err) = analyze_sst_file(dir.path(), &info) else {
panic!("analyze_sst_file accepted a corrupt SST");
};
format!("{err:#}")
}
/// Opens `bytes` the way `analyze_sst_file` does and reads one block.
fn read(bytes: &[u8], block_count: u16, block_index: u16) -> Result<RawBlock> {
let dir = write_sst(bytes);
let file = File::open(dir.path().join("00000001.sst")).unwrap();
let meta = StaticSortedFileMetaData {
sequence_number: 1,
block_count,
};
let block_offsets = BlockOffsets::read(&file, &meta, bytes.len()).unwrap();
read_block(bytes, &block_offsets, block_index, 1, Compression::Lz4)
}
#[test]
fn read_block_reads_valid_blocks() {
let bytes = sst_bytes(&[uncompressed_block(b"value"), uncompressed_block(b"\0index")]);
let raw = read(&bytes, 2, 0).unwrap();
assert_eq!(&*raw.data, b"value");
}
#[test]
fn read_block_rejects_out_of_range_block_index() {
let bytes = sst_bytes(&[uncompressed_block(b"index")]);
let Err(err) = read(&bytes, 1, 1) else {
panic!("read_block accepted block 1 of 1");
};
let message = format!("{err:#}");
assert!(message.contains("out of bounds"), "{message}");
}
#[test]
fn analyze_rejects_file_shorter_than_block_directory() {
let message = analyze_err(&[0; 4], 2);
assert!(message.contains("too short"), "{message}");
}
#[test]
fn analyze_rejects_zero_blocks() {
let message = analyze_err(&[], 0);
assert!(message.contains("no blocks"), "{message}");
}
#[test]
fn analyze_rejects_corrupt_value_block_offset() {
let mut bytes = sst_bytes(&[uncompressed_block(b"value"), uncompressed_block(b"index")]);
let directory_start = bytes.len() - 2 * size_of::<u32>();
bytes[directory_start..directory_start + 4].copy_from_slice(&u32::MAX.to_be_bytes());
let message = analyze_err(&bytes, 2);
assert!(message.contains("shorter than its"), "{message}");
}
#[test]
fn analyze_rejects_corrupt_index_block_offset() {
let mut bytes = sst_bytes(&[uncompressed_block(b"value"), uncompressed_block(b"index")]);
let last_entry = bytes.len() - size_of::<u32>();
// Ends inside the block directory.
bytes[last_entry..].copy_from_slice(&(last_entry as u32 + 1).to_be_bytes());
let message = analyze_err(&bytes, 2);
assert!(
message.contains("past the start of its block-offset table"),
"{message}"
);
}
#[test]
fn analyze_rejects_decompressed_length_mismatch() {
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());
block.extend_from_slice(&compressed);
let message = analyze_err(&sst_bytes(&[block]), 1);
assert!(message.contains("Decompressed length"), "{message}");
}
}
...@@ -335,13 +335,21 @@ pub struct StaticSortedFileMetaData { ...@@ -335,13 +335,21 @@ pub struct StaticSortedFileMetaData {
/// [`BlockOffsets::read`] checks that every block is at least [`BLOCK_HEADER_SIZE`] bytes long and /// [`BlockOffsets::read`] checks that every block is at least [`BLOCK_HEADER_SIZE`] bytes long and
/// ends at or before the start of the table, so [`BlockOffsets::block_range`] needs no further /// ends at or before the start of the table, so [`BlockOffsets::block_range`] needs no further
/// checks. /// checks.
///
/// Public so tools that read SST files directly (such as `sst_inspect`) apply the same validation
/// as the database.
#[derive(Debug)] #[derive(Debug)]
struct BlockOffsets(Box<[u32]>); pub struct BlockOffsets(Box<[u32]>);
impl BlockOffsets { impl BlockOffsets {
/// Reads the block-offset table of `file` (`file_len` bytes long) with one positional read, /// Reads the block-offset table of `file` (`file_len` bytes long) with one positional read,
/// then decodes and validates it in place in a single pass. /// then decodes and validates it in place in a single pass.
fn read(file: &File, meta: &StaticSortedFileMetaData, file_len: usize) -> Result<Self> { ///
/// Returns an error if the file is too short for a table of `meta.block_count` entries, if a
/// block is shorter than its [`BLOCK_HEADER_SIZE`]-byte header (including offsets that go
/// backwards), or if the blocks extend into the table. An empty table (`block_count == 0`) is
/// accepted; callers that need an index block must reject it themselves.
pub fn read(file: &File, meta: &StaticSortedFileMetaData, file_len: usize) -> Result<Self> {
let block_count = meta.block_count as usize; let block_count = meta.block_count as usize;
let table_start = file_len let table_start = file_len
.checked_sub(block_count * size_of::<u32>()) .checked_sub(block_count * size_of::<u32>())
...@@ -384,8 +392,9 @@ impl BlockOffsets { ...@@ -384,8 +392,9 @@ impl BlockOffsets {
/// The byte range of block `block_index` in the file, header included. /// The byte range of block `block_index` in the file, header included.
/// ///
/// Only the index can be wrong here; everything else was checked by [`BlockOffsets::read`]. /// Only the index can be wrong here; everything else was checked by [`BlockOffsets::read`].
/// Returns an error if `block_index` is not less than the number of blocks.
#[inline] #[inline]
fn block_range(&self, block_index: u16) -> Result<Range<usize>> { pub fn block_range(&self, block_index: u16) -> Result<Range<usize>> {
let index = block_index as usize; let index = block_index as usize;
let Some(&end) = self.0.get(index) else { let Some(&end) = self.0.get(index) else {
bail!( bail!(
...@@ -2055,4 +2064,106 @@ mod tests { ...@@ -2055,4 +2064,106 @@ mod tests {
let err = read_offsets(&[12, 40, 60], 52).unwrap_err(); let err = read_offsets(&[12, 40, 60], 52).unwrap_err();
assert!(err.to_string().contains("past"), "{err:#}"); assert!(err.to_string().contains("past"), "{err:#}");
} }
fn access_modes() -> Vec<AccessMode> {
vec![
#[cfg(feature = "mmap")]
AccessMode::Mmap,
AccessMode::File,
]
}
/// Writes `data` followed by a block directory of `directory` offsets as `00000001.sst`, then
/// opens it for lookup in every access mode and for file-backed iteration. Each open must fail
/// with an error naming the file rather than panic.
///
/// The `BlockOffsets` tests above cover the validation itself; these check that both openers
/// run it (and the index block checks) before touching the file's contents.
fn assert_open_fails(block_count: u16, data: &[u8], directory: &[u32], expected: &str) {
let dir = tempfile::tempdir().unwrap();
let mut bytes = data.to_vec();
for offset in directory {
bytes.extend_from_slice(&offset.to_be_bytes());
}
std::fs::write(dir.path().join("00000001.sst"), &bytes).unwrap();
let meta = StaticSortedFileMetaData {
sequence_number: 1,
block_count,
};
for access_mode in access_modes() {
let err = StaticSortedFile::open(dir.path(), meta, Compression::Lz4, access_mode)
.err()
.unwrap_or_else(|| {
panic!("StaticSortedFile::open ({access_mode:?}) accepted a corrupt file")
});
let message = format!("{err:#}");
assert!(
message.contains("00000001.sst") && message.contains(expected),
"StaticSortedFile::open ({access_mode:?}) returned an unexpected error: {err:#}"
);
}
let err = StaticSortedFileIter::open(dir.path(), meta, Compression::Lz4)
.err()
.unwrap_or_else(|| panic!("StaticSortedFileIter::open accepted a corrupt file"));
let message = format!("{err:#}");
assert!(
message.contains("00000001.sst") && message.contains(expected),
"StaticSortedFileIter::open returned an unexpected error: {err:#}"
);
}
#[test]
fn open_rejects_empty_file() {
assert_open_fails(1, &[], &[], "too short");
}
#[test]
fn open_rejects_file_shorter_than_block_directory() {
assert_open_fails(4, &[0; 6], &[], "too short");
}
#[test]
fn open_rejects_zero_blocks() {
assert_open_fails(0, &[0; 16], &[], "has no blocks");
}
#[test]
fn open_rejects_index_block_past_end_of_file() {
assert_open_fails(
1,
&[0; 16],
&[1000],
"past the start of its block-offset table",
);
}
#[test]
fn open_rejects_index_block_overlapping_block_directory() {
// The directory starts at byte 16, but the index block claims to end at byte 20.
assert_open_fails(
2,
&[0; 16],
&[8, 20],
"past the start of its block-offset table",
);
}
#[test]
fn open_rejects_index_block_too_short_for_header() {
assert_open_fails(2, &[0; 16], &[10, 12], "shorter than its");
}
#[test]
fn open_rejects_index_block_ending_before_it_starts() {
assert_open_fails(2, &[0; 16], &[14, 4], "shorter than its");
}
#[test]
fn open_rejects_header_only_index_block() {
// 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());
assert_open_fails(1, &data, &[8], "not an index block");
}
} }
...@@ -2432,6 +2432,89 @@ mod tests { ...@@ -2432,6 +2432,89 @@ mod tests {
assert_corruption_detected(dir.path(), 1, &meta, &entries); assert_corruption_detected(dir.path(), 1, &meta, &entries);
} }
/// Overwriting the directory entry of any non-index block of a real SST with an offset
/// outside the data section must make lookup in every access mode and file-backed iteration
/// fail to open it, with the block-offset validation error rather than a panic or later error.
#[test]
fn corrupt_block_offsets_are_rejected_on_open() -> Result<()> {
let mut entries = vec![TestEntry::medium(b"medium", &vec![0xAB; 8192])];
for i in 0..64u32 {
entries.push(TestEntry::small(
format!("small-{i:04}").as_bytes(),
&[i as u8; 64],
));
}
sort_entries(&mut entries);
let dir = tempfile::tempdir()?;
let written = write_sst(dir.path(), 1, &entries, MetaEntryFlags::default())?;
let meta = StaticSortedFileMetaData {
sequence_number: 1,
block_count: written.block_count,
};
assert!(
meta.block_count >= 3,
"expected value, key and index blocks, got {} blocks",
meta.block_count
);
let path = dir.path().join("00000001.sst");
let original = std::fs::read(&path)?;
let directory_start = original.len() - usize::from(meta.block_count) * size_of::<u32>();
// Sanity check: the uncorrupted file reads back with the expected values.
let sst = open_sst(dir.path(), 1, &written)?;
let kc = make_cache();
let vc = make_cache();
for entry in &entries {
assert_lookup(&sst, entry, &kc, &vc)?;
}
// The caches can hold blocks that reference the mapping, so release them with the file
// before the file is rewritten below.
drop((sst, kc, vc));
let access_modes: &[crate::AccessMode] = &[
#[cfg(feature = "mmap")]
crate::AccessMode::Mmap,
crate::AccessMode::File,
];
for block_index in 0..usize::from(meta.block_count) - 1 {
for bad_end in [u32::MAX, directory_start as u32 + 4] {
let mut bytes = original.clone();
let entry = directory_start + block_index * size_of::<u32>();
bytes[entry..entry + size_of::<u32>()].copy_from_slice(&bad_end.to_be_bytes());
std::fs::write(&path, &bytes)?;
for &access_mode in access_modes {
let errors = [
StaticSortedFile::open(dir.path(), meta, Compression::Lz4, access_mode)
.err(),
crate::static_sorted_file::StaticSortedFileIter::open(
dir.path(),
meta,
Compression::Lz4,
)
.err(),
];
for err in errors {
let err = err.unwrap_or_else(|| {
panic!(
"block {block_index}, end {bad_end} ({access_mode:?}): a corrupt \
block offset was accepted"
)
});
let message = format!("{err:#}");
// Raising one block's end either makes the next block go backwards, or
// (for the last data block) pushes the blocks past the table.
assert!(
message.contains("shorter than its")
|| message.contains("past the start of its block-offset table"),
"block {block_index}, end {bad_end} ({access_mode:?}): {message}"
);
}
}
}
}
Ok(())
}
#[test] #[test]
fn be_key_order_matches_byte_order() { fn be_key_order_matches_byte_order() {
let keys4: Vec<[u8; 4]> = vec![ let keys4: Vec<[u8; 4]> = vec![
......
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