Skip to content

Validate LSMT and ZFile index sizes - #438

Open
williswus wants to merge 5 commits into
containerd:mainfrom
williswus:fix/index-size-validation
Open

Validate LSMT and ZFile index sizes#438
williswus wants to merge 5 commits into
containerd:mainfrom
williswus:fix/index-size-validation

Conversation

@williswus

@williswus williswus commented Aug 4, 2026

Copy link
Copy Markdown

What this PR does / why we need it:

Adds index-size validation when loading LSMT and ZFile files.

  • Rejects LSMT indexes containing more than 1,000,000 records.
  • Rejects ZFile indexes containing more than 1,000,000,000 records.
  • Performs validation before calculating index memory requirements.
  • Adds regression tests confirming oversized indexes are rejected.

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_index

Both tests passed.

Please check the following list:

  • The affected code has corresponding unit tests.
  • This change does not require a documentation update.
  • This change does not introduce breaking changes.
  • No new files were added.

@williswus
williswus force-pushed the fix/index-size-validation branch from 7802649 to 0b01f46 Compare August 4, 2026 15:14
Comment thread src/overlaybd/lsmt/file.cpp Outdated
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

统一在分配之前判断?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外在索引merge的时候也要判断

Comment thread src/overlaybd/zfile/zfile.cpp Outdated
} 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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

统一在分配之前判断?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

收到,计划在明天修改完成并更新 PR,谢谢您!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_SIZE in 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.

Comment on lines +1392 to +1394
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);

@lihuiba lihuiba Aug 7, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, pht->index_size can be checked earlier.

Comment on lines +957 to +960
if (mapping.size() > MAX_LSMT_INDEX_SIZE)
LOG_ERROR_RETURN(0, nullptr,
"Merged LSMT index size ` exceeds maximum `",
mapping.size(), MAX_LSMT_INDEX_SIZE);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不超过“sum of input sizes”的话,这个标准过于严格了

Comment thread src/overlaybd/zfile/zfile.cpp Outdated
Comment on lines +1111 to +1113
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, pht->index_size can be checked earlier.

LOG_ERROR_RETURN(0, false,
"trailer magic, trailer type, file type or sealedness doesn't match");
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not delete this blank line

Comment thread src/overlaybd/zfile/zfile.cpp Outdated
Comment on lines +1111 to +1113
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, pht->index_size can be checked earlier.

pht->index_size = index_bytes / sizeof(SegmentMapping);
}

if (pht->index_size > MAX_LSMT_INDEX_SIZE)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put it inside verify_ht()

LOG_ERROR_RETURN(0, nullptr, "LSMT index size ` exceeds maximum `",
pht->index_size + 0, MAX_LSMT_INDEX_SIZE);

if (trailer) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do it inside merge_indexes(), just before mapping.resize() or push_back().

}
}

if (pht->index_size > MAX_ZFILE_INDEX_SIZE)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not change it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants