Skip to content

Fix GH-16501: gmp_random_bits overflow. #16503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 9 additions & 3 deletions ext/gmp/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1803,15 +1803,21 @@ ZEND_FUNCTION(gmp_random_bits)
RETURN_THROWS();
}

if (bits <= 0) {
zend_argument_value_error(1, "must be greater than or equal to 1");
#if SIZEOF_SIZE_T == 4
const zend_long maxbits = ULONG_MAX / GMP_NUMB_BITS;
#else
const zend_long maxbits = INT_MAX;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still can overflow. libgmp does this check, but afterwards calculates ((new_alloc) * sizeof (mp_limb_t)), where sizeof(mp_limb_t) is certainly larger than 2 (I assume it's usually four or eight).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disregard. This is fine; I was confused by 32bit using ULONG_MAX, but 64bit using INT_MAX. The 32bit case looks fishy (for LP64), though.

#endif

if (bits <= 0 || bits > maxbits) {
zend_argument_value_error(1, "must be between 1 and " ZEND_LONG_FMT, maxbits);
RETURN_THROWS();
}

INIT_GMP_RETVAL(gmpnum_result);
gmp_init_random();

mpz_urandomb(gmpnum_result, GMPG(rand_state), bits);
mpz_urandomb(gmpnum_result, GMPG(rand_state), (mp_bitcnt_t)bits);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the mp_bitcnt_t cast correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It s just to avoid pedantic build warning I guess, bits being always > 0 and < max value for unsigned long int ; I think it is fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, just extraneous; I'd remove it (could still trigger some compiler/analyser diagnostic without the cast).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I overlooked your comment. Let's leave it as is.

}
/* }}} */

Expand Down
14 changes: 14 additions & 0 deletions ext/gmp/tests/gh16501.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
GH-16501 (gmp_random_bits overflow)
--EXTENSIONS--
gmp
--FILE--
<?php
try {
gmp_random_bits(PHP_INT_MAX);
} catch (\ValueError $e) {
echo $e->getMessage();
}
?>
--EXPECTF--
gmp_random_bits(): Argument #1 ($bits) must be between 1 and %d
6 changes: 3 additions & 3 deletions ext/gmp/tests/gmp_random_bits.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ while (1) {

echo "Done\n";
?>
--EXPECT--
gmp_random_bits(): Argument #1 ($bits) must be greater than or equal to 1
gmp_random_bits(): Argument #1 ($bits) must be greater than or equal to 1
--EXPECTF--
gmp_random_bits(): Argument #1 ($bits) must be between 1 and %d
gmp_random_bits(): Argument #1 ($bits) must be between 1 and %d
Done
Loading