Skip to content

Fix integer overflow in ReadBlock block size calculation#1308

Open
nsknv wants to merge 2 commits intogoogle:mainfrom
nsknv:fix-readblock-integer-overflow
Open

Fix integer overflow in ReadBlock block size calculation#1308
nsknv wants to merge 2 commits intogoogle:mainfrom
nsknv:fix-readblock-integer-overflow

Conversation

@nsknv
Copy link
Copy Markdown

@nsknv nsknv commented Mar 21, 2026

Summary

  • Fix integer overflow in ReadBlock() where n + kBlockTrailerSize wraps when handle.size() is near SIZE_MAX, bypassing the truncation check and causing an out-of-bounds heap read at data[n]

Details

BlockHandle::size_ is decoded from varint64 with no upper-bound validation. When the size is close to SIZE_MAX, the addition n + kBlockTrailerSize (where kBlockTrailerSize is 5) wraps to a small value. This causes:

  1. An undersized allocation (new char[wrapped_value])
  2. The size check at line 85 passes (compares against the same wrapped value)
  3. data[n] reads out of bounds, crashing the process

The fix adds an overflow check before the allocation.

In ReadBlock(), the expression `n + kBlockTrailerSize` can overflow
when `handle.size()` is close to SIZE_MAX, wrapping to a small value.
This causes an undersized heap allocation followed by an out-of-bounds
read when the code accesses `data[n]`, crashing the process.

Add an overflow check before the allocation to detect and reject
block handles with sizes that would cause the addition to wrap.
@google-cla
Copy link
Copy Markdown

google-cla bot commented Mar 21, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@nsknv
Copy link
Copy Markdown
Author

nsknv commented Mar 21, 2026

Referencing google issue tracker ID: 493717816

@nsknv
Copy link
Copy Markdown
Author

nsknv commented Mar 25, 2026

@pwnall - please review.

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.

1 participant