Fix OOM DoS via unvalidated BlockHandle::size() in ReadBlock()#1314
Open
0daytrace wants to merge 1 commit intogoogle:mainfrom
Open
Fix OOM DoS via unvalidated BlockHandle::size() in ReadBlock()#13140daytrace wants to merge 1 commit intogoogle:mainfrom
0daytrace wants to merge 1 commit intogoogle:mainfrom
Conversation
A crafted SSTable with a block handle size of ~2 GiB causes an OOM allocation in ReadBlock() before Block::Block() validation runs. Add a 128 MiB sanity check before the allocation. Reported via Google OSS VRP issue 495694827
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
### Summary
Add a bounds check in ReadBlock() (table/format.cc) before the heap
allocation to reject maliciously large or corrupted block handles.
### Root Cause
BlockHandle::DecodeFrom() reads the size_ field directly from SSTable
file data as a raw varint with no upper-bound validation. ReadBlock() then
casts this value to size_t and uses it as an allocation size:
size_t n = static_cast<size_t>(handle.size());
char* buf = new char[n + kBlockTrailerSize]; // ← OOM if n is ~2 GiB
A crafted .ldb / .sst file with a block handle size of 0x7FFFFFFF
causes a ~2 GiB allocation attempt, crashing the process before any content
validation occurs.
The 2013 fix (commit 28dad91) added num_restarts validation inside
Block::Block(), but that check runs after the allocation — the
allocation itself was left unguarded.
### Fix
Add a pre-allocation sanity check in ReadBlock():
static const uint64_t kMaxBlockSize = 128ULL * 1024 * 1024; // 128 MiB
if (handle.size() > kMaxBlockSize) {
return Status::Corruption("block handle size exceeds sanity limit");
}
### The Patch (table/format.cc)
Apply immediately after the three result-> assignments at the start of
ReadBlock(), before the size_t n = ... line:
// Reject block handles with unreasonably large sizes to prevent OOM from
// malicious or corrupted SSTable files. BlockHandle::DecodeFrom() stores
// the size field with no range check; without this guard, a crafted file
// can trigger a multi-gigabyte allocation before Block::Block() validation.
static const uint64_t kMaxBlockSize = 128ULL * 1024 * 1024; // 128 MiB
if (handle.size() > kMaxBlockSize) {
return Status::Corruption("block handle size exceeds sanity limit");
}
### Impact
Any application calling DB::Open() or Table::Open() on a
user-supplied or attacker-influenced .ldb / .sst file is vulnerable.
Examples: backup tools, data browsers, sync clients, Chrome (IndexedDB).
### Test Case
The existing corruption_test.cc framework can verify this fix. A new test
should open a crafted table file with a block handle size of 0x7FFFFFFF
and assert that ReadBlock() returns Status::Corruption(...) rather than
crashing
TEST(CorruptionTest, OversizeBlockHandle) {
// Write a valid table, then corrupt the block handle size field to 0x7FFFFFFF
// in the raw .ldb file bytes. Opening the table should return Corruption,
// not OOM-crash.
//
// (Implementation uses existing corruption_test helpers to build and
// corrupt an SSTable, then calls Table::Open() and checks the status.)
}
### Affected Versions
All current versions. Tested against commit 7ee830d (HEAD as of report).
### References