Skip to content

[BUGFIX] ByteBuffer(parent, offset, size): copy only what the parent has - #36

Merged
hasherezade merged 1 commit into
hasherezade:masterfrom
mkilijanek:fix/subbuffer-copy-size
Nov 7, 2026
Merged

hasherezade merged 1 commit into
hasherezade:masterfrom
mkilijanek:fix/subbuffer-copy-size

Conversation

@mkilijanek

Copy link
Copy Markdown
Contributor

The sub-buffer constructor ByteBuffer(AbstractByteBuffer *sourceBuf, offset, size) validates copySize = min(size, parent size) bytes of the parent, but then copies size bytes. When the requested size is bigger than the parent, it reads past the end of the parent's content. bearcommander does this for every file smaller than MINBUF (0x200): new ByteBuffer(fileView, 0, 0x200).

Change

Copy copySize bytes. The rest of the new buffer is already zero-filled by allocContent. 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, in memcpy called from ByteBuffer::ByteBuffer(AbstractByteBuffer*, ...).
  • this change: no report; the first 100 bytes are copied and the rest is zero.

test.sh against bearparser_tests: 26 cases print OK with and without the change. x86/kernel32 : explist prints "Failed" on unmodified master too. 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 FileView the over-read usually stays inside the mapped page; for a heap buffer it is an overflow read.

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.
@hasherezade
hasherezade merged commit bdbb1d9 into hasherezade:master Nov 7, 2026
2 checks passed
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.

2 participants