Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ PHP NEWS
. Fixed bug GH-23619 (cryptic error on servers that don't support TLS
session resumption on data connection). (ndossche)

- Hash:
. Fixed mhash_keygen_s2k() truncating a $length above INT_MAX instead of
throwing a ValueError. (Marc Bennewitz)

- Intl:
. Fixed cloning IntlDateFormatter and MessageFormatter losing PHP-side state
such as dateType, timeType, calendar and the message pattern.
Expand Down
10 changes: 8 additions & 2 deletions ext/hash/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -1294,12 +1294,18 @@ PHP_FUNCTION(mhash_keygen_s2k)
RETURN_THROWS();
}

bytes = (int)l_bytes;
if (bytes <= 0){
if (l_bytes <= 0) {
zend_argument_value_error(4, "must be a greater than 0");
RETURN_THROWS();
}

if (ZEND_LONG_INT_OVFL(l_bytes)) {
zend_argument_value_error(4, "must be less than or equal to %d", INT_MAX);
RETURN_THROWS();
}

bytes = (int)l_bytes;

salt_len = MIN(salt_len, SALT_SIZE);

memcpy(padded_salt, salt, salt_len);
Expand Down
29 changes: 29 additions & 0 deletions ext/hash/tests/gh23950.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
MHash: GH-23950 (mhash_keygen_s2k() $length must not be truncated to int)
--SKIPIF--
<?php
if (!function_exists('mhash')) die('skip mhash compatibility layer not available');
if (PHP_INT_SIZE == 4) die('skip only where a PHP integer is wider than an int');
?>
--FILE--
<?php
foreach ([2**31, 2**32, 2**32 + 16, PHP_INT_MAX] as $length) {
try {
var_dump(strlen(mhash_keygen_s2k(1, 'password', 'salt', $length)));
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
}
?>
--EXPECTF--
Deprecated: Function mhash_keygen_s2k() is deprecated since 8.1 in %s on line %d
ValueError: mhash_keygen_s2k(): Argument #4 ($length) must be less than or equal to 2147483647

Deprecated: Function mhash_keygen_s2k() is deprecated since 8.1 in %s on line %d
ValueError: mhash_keygen_s2k(): Argument #4 ($length) must be less than or equal to 2147483647

Deprecated: Function mhash_keygen_s2k() is deprecated since 8.1 in %s on line %d
ValueError: mhash_keygen_s2k(): Argument #4 ($length) must be less than or equal to 2147483647

Deprecated: Function mhash_keygen_s2k() is deprecated since 8.1 in %s on line %d
ValueError: mhash_keygen_s2k(): Argument #4 ($length) must be less than or equal to 2147483647
Loading