Repository navigation
[BUGFIX] ByteBuffer(parent, offset, size): copy only what the parent has - #36
Merged
hasherezade merged 1 commit intoNov 7, 2026
Merged
Conversation
The sub-buffer constructor validates copySize = min(v_size, parent size) bytes of the parent, but then copied v_size bytes. When the requested size is bigger than the parent, this read past the end of the parent's content. bearcommander does this for every file smaller than MINBUF (0x200): new ByteBuffer(fileView, 0, 0x200). Copy copySize bytes; the rest of the new buffer is already zero-filled by allocContent. A 0x200-byte sub-buffer of a 100-byte buffer gives, before, a 512-byte heap-buffer-overflow read in memcpy (ASan), and now a buffer whose first 100 bytes are copied and the rest zero.
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.
The sub-buffer constructor
ByteBuffer(AbstractByteBuffer *sourceBuf, offset, size)validatescopySize = min(size, parent size)bytes of the parent, but then copiessizebytes. When the requested size is bigger than the parent, it reads past the end of the parent's content.bearcommanderdoes this for every file smaller thanMINBUF(0x200):new ByteBuffer(fileView, 0, 0x200).Change
Copy
copySizebytes. The rest of the new buffer is already zero-filled byallocContent. One line.Verification
A 0x200-byte sub-buffer of a 100-byte heap buffer filled with
0x41, built with-fsanitize=address,undefined:master:AddressSanitizer: heap-buffer-overflow, READ of size 512, inmemcpycalled fromByteBuffer::ByteBuffer(AbstractByteBuffer*, ...).test.shagainstbearparser_tests: 26 cases print OK with and without the change.x86/kernel32 : explistprints "Failed" on unmodifiedmastertoo. In the sanitizer build the test run shows the same 11 reports (6 distinct places) before and after; this change adds none.For a memory-mapped
FileViewthe over-read usually stays inside the mapped page; for a heap buffer it is an overflow read.