Skip to content

fix: validate context() restore inputs to prevent buffer overread#39

Draft
toddr-bot wants to merge 1 commit into
mainfrom
koan.toddr.bot/validate-context-input
Draft

fix: validate context() restore inputs to prevent buffer overread#39
toddr-bot wants to merge 1 commit into
mainfrom
koan.toddr.bot/validate-context-input

Conversation

@toddr-bot

Copy link
Copy Markdown

What

Add length validation in context() restore path before reading from user-supplied buffers.

Why

The context() 4-argument restore form reads 16 bytes from the state buffer (arg 2) without checking SvPV length. A short string causes an out-of-bounds read. Similarly, no validation on the unprocessed data argument (arg 4) — values over 63 bytes are invalid for MD5 block state.

This is a buffer overread, not a write — but it can leak adjacent memory contents through the reconstructed digest state, making it a potential information disclosure vector.

How

  • Check len < 16 for the state buffer; croak with a clear message
  • Check len > 63 for the unprocessed data buffer; croak similarly
  • Added tests in t/context.t verifying both validation paths

Testing

make test passes. New tests exercise both the too-short state and too-long data error paths.

Port of gisle/digest-md5#25.
Part of tracking issue #38.

The context() method, when called with arguments to restore internal
state, reads exactly 16 bytes from the state buffer without checking
its length. A short string causes a heap buffer overread. Similarly,
the optional unprocessed-data argument should be at most 63 bytes
(one partial block) but was passed through unchecked.

Add croak() guards for both conditions and corresponding tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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