Validate LSMT and ZFile index sizes - #438
Conversation
7802649 to
0b01f46
Compare
| LOG_ERROR_RETURN(0, nullptr, "index offset wrong"); | ||
| index_bytes = stat.st_size - HeaderTrailer::SPACE; | ||
| pht->index_size = index_bytes / sizeof(SegmentMapping); | ||
| if (pht->index_size > MAX_LSMT_INDEX_SIZE) |
| } else { | ||
| if (pht->index_size > MAX_ZFILE_INDEX_SIZE) | ||
| LOG_ERROR_RETURN(0, false, "ZFile index size ` exceeds maximum `", | ||
| pht->index_size + 0, MAX_ZFILE_INDEX_SIZE); |
There was a problem hiding this comment.
Pull request overview
Adds defensive validation to reject oversized on-disk index metadata when loading LSMT and ZFile formats, with regression tests to ensure oversized indexes are rejected instead of being processed.
Changes:
- Introduces hard upper bounds for LSMT and ZFile index record counts and enforces them during load/merge paths.
- Adds tests for rejecting oversized LSMT/ZFile indexes and an oversized LSMT merge result.
- Exposes
MAX_LSMT_INDEX_SIZEin the LSMT index API header for shared use.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/overlaybd/zfile/zfile.cpp | Adds a max-record-count guard for ZFile jump-table (index) loading. |
| src/overlaybd/zfile/test/test.cpp | Adds a regression test that constructs a ZFile with an oversized index and expects open to fail. |
| src/overlaybd/lsmt/test/test.cpp | Adds regression tests for rejecting oversized LSMT on-disk indexes and oversized merges. |
| src/overlaybd/lsmt/index.h | Defines MAX_LSMT_INDEX_SIZE limit for use across LSMT code. |
| src/overlaybd/lsmt/index.cpp | Adds a post-merge size cap check for merged in-memory LSMT indexes. |
| src/overlaybd/lsmt/file.cpp | Adds a size cap check before allocating/reading the on-disk LSMT index. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (pht->index_size > MAX_LSMT_INDEX_SIZE) | ||
| LOG_ERROR_RETURN(0, nullptr, "LSMT index size ` exceeds maximum `", | ||
| pht->index_size + 0, MAX_LSMT_INDEX_SIZE); |
There was a problem hiding this comment.
yes, pht->index_size can be checked earlier.
| if (mapping.size() > MAX_LSMT_INDEX_SIZE) | ||
| LOG_ERROR_RETURN(0, nullptr, | ||
| "Merged LSMT index size ` exceeds maximum `", | ||
| mapping.size(), MAX_LSMT_INDEX_SIZE); |
There was a problem hiding this comment.
不超过“sum of input sizes”的话,这个标准过于严格了
| if (pht->index_size > MAX_ZFILE_INDEX_SIZE) | ||
| LOG_ERROR_RETURN(0, false, "ZFile index size ` exceeds maximum `", | ||
| pht->index_size + 0, MAX_ZFILE_INDEX_SIZE); |
There was a problem hiding this comment.
yes, pht->index_size can be checked earlier.
| LOG_ERROR_RETURN(0, false, | ||
| "trailer magic, trailer type, file type or sealedness doesn't match"); | ||
| } | ||
|
|
There was a problem hiding this comment.
do not delete this blank line
| if (pht->index_size > MAX_ZFILE_INDEX_SIZE) | ||
| LOG_ERROR_RETURN(0, false, "ZFile index size ` exceeds maximum `", | ||
| pht->index_size + 0, MAX_ZFILE_INDEX_SIZE); |
There was a problem hiding this comment.
yes, pht->index_size can be checked earlier.
| pht->index_size = index_bytes / sizeof(SegmentMapping); | ||
| } | ||
|
|
||
| if (pht->index_size > MAX_LSMT_INDEX_SIZE) |
| LOG_ERROR_RETURN(0, nullptr, "LSMT index size ` exceeds maximum `", | ||
| pht->index_size + 0, MAX_LSMT_INDEX_SIZE); | ||
|
|
||
| if (trailer) { |
There was a problem hiding this comment.
without moving index_bytes
| mapping.reserve(pi[0]->size()); | ||
| merge_indexes(0, mapping, pi, n, 0, UINT64_MAX); | ||
|
|
||
| if (mapping.size() > MAX_LSMT_INDEX_SIZE) |
There was a problem hiding this comment.
do it inside merge_indexes(), just before mapping.resize() or push_back().
| } | ||
| } | ||
|
|
||
| if (pht->index_size > MAX_ZFILE_INDEX_SIZE) |
There was a problem hiding this comment.
put it just after !pht->verify_magic()
| index_bytes = pht->index_size * sizeof(uint32_t); | ||
| index_bytes = pht->index_size * sizeof(uint32_t); | ||
|
|
||
| if (!header_overwrite) { |
What this PR does / why we need it:
Adds index-size validation when loading LSMT and ZFile files.
Which issue(s) this PR fixes:
N/A
Tests:
./build/output/lsmt_test --gtest_filter=FileTest.reject_oversized_index./build/output/zfile_test --gtest_filter=ZFileTest.reject_oversized_indexBoth tests passed.
Please check the following list: