Fix buffer overflow in hash_pbkdf2() with a large output length - #23380
Open
lazerg wants to merge 1 commit into
Open
Fix buffer overflow in hash_pbkdf2() with a large output length#23380lazerg wants to merge 1 commit into
lazerg wants to merge 1 commit into
Conversation
lazerg
force-pushed
the
fix/issue-23377-pbkdf2-length-overflow
branch
from
August 19, 2026 13:00
514c90e to
95e52bd
Compare
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.
hash_pbkdf2()sizes the digest buffer withceil((float) length / 2.0)and the block count withceil((float) digest_length / (float) ops->digest_size). Afloatcarries 24 bits of mantissa, so past 2^24 the conversion rounds and both counts come out wrong: rounding up makeszend_bin2hex()write past the end of the return string, rounding down leaves the tail of the string uninitialized.hash_pbkdf2('md5', 'password', 'salt', 1, 268435473)writes 8 bytes out of bounds under ASAN.Both counts are exact in integer arithmetic, so this replaces them with round-up divisions.
<math.h>had no other user in the file.This came out of #23377. The odd
lengthcase reported there is fine on its own, as Sjord noted before closing it:zend_string_alloc(length)reserveslength + 1bytes and2 * ceil(length / 2) == length + 1, so the extra hexit lands on the terminator byte that gets overwritten a line later. The float conversion on those same two lines is a real overflow though.