-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
Fix GH-16878: gmp_fact overflow on memory allocation attempt. #16880
base: PHP-8.2
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,12 @@ | |
#define GMP_BIG_ENDIAN (1 << 3) | ||
#define GMP_NATIVE_ENDIAN (1 << 4) | ||
|
||
#if SIZEOF_SIZE_T == 4 | ||
#define GMP_ALLOC_MAXBITS (ULONG_MAX / GMP_NUMB_BITS) | ||
#else | ||
#define GMP_ALLOC_MAXBITS INT_MAX | ||
#endif | ||
|
||
#include "gmp_arginfo.h" | ||
|
||
ZEND_DECLARE_MODULE_GLOBALS(gmp) | ||
|
@@ -1276,14 +1282,17 @@ ZEND_FUNCTION(gmp_fact) | |
{ | ||
zval *a_arg; | ||
mpz_ptr gmpnum_result; | ||
zend_long val; | ||
|
||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){ | ||
RETURN_THROWS(); | ||
} | ||
|
||
|
||
if (Z_TYPE_P(a_arg) == IS_LONG) { | ||
if (Z_LVAL_P(a_arg) < 0) { | ||
zend_argument_value_error(1, "must be greater than or equal to 0"); | ||
val = Z_LVAL_P(a_arg); | ||
if (val < 0 || val > GMP_ALLOC_MAXBITS) { | ||
zend_argument_value_error(1, "must be between 0 and " ZEND_LONG_FMT, GMP_ALLOC_MAXBITS); | ||
RETURN_THROWS(); | ||
} | ||
} else { | ||
|
@@ -1293,14 +1302,16 @@ ZEND_FUNCTION(gmp_fact) | |
FETCH_GMP_ZVAL(gmpnum, a_arg, temp_a, 1); | ||
FREE_GMP_TEMP(temp_a); | ||
|
||
if (mpz_sgn(gmpnum) < 0) { | ||
zend_argument_value_error(1, "must be greater than or equal to 0"); | ||
(void)gmpnum; | ||
val = zval_get_long(a_arg); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't quite understand how What happens if one calls There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Nope I get the exception in this case too. I ll see the rest later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, indeed! That is because the conversion happens in coercive mode. And that also shows that we can't easily drop the seemingly superfluous Still, there is an issue unrelated to this PRE.g. for
So apparently, there is an easy fix for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, I figured that much, but I wondered about values outside of |
||
if (val < 0 || val > GMP_ALLOC_MAXBITS) { | ||
zend_argument_value_error(1, "must be between 0 and " ZEND_LONG_FMT, GMP_ALLOC_MAXBITS); | ||
RETURN_THROWS(); | ||
} | ||
} | ||
|
||
INIT_GMP_RETVAL(gmpnum_result); | ||
mpz_fac_ui(gmpnum_result, zval_get_long(a_arg)); | ||
mpz_fac_ui(gmpnum_result, val); | ||
} | ||
/* }}} */ | ||
|
||
|
@@ -1859,14 +1870,8 @@ ZEND_FUNCTION(gmp_random_bits) | |
RETURN_THROWS(); | ||
} | ||
|
||
#if SIZEOF_SIZE_T == 4 | ||
const zend_long maxbits = ULONG_MAX / GMP_NUMB_BITS; | ||
#else | ||
const zend_long maxbits = INT_MAX; | ||
#endif | ||
|
||
if (bits <= 0 || bits > maxbits) { | ||
zend_argument_value_error(1, "must be between 1 and " ZEND_LONG_FMT, maxbits); | ||
if (bits <= 0 || bits > GMP_ALLOC_MAXBITS) { | ||
zend_argument_value_error(1, "must be between 1 and " ZEND_LONG_FMT, GMP_ALLOC_MAXBITS); | ||
RETURN_THROWS(); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
GH-16878 (gmp_fact overflow) | ||
--EXTENSIONS-- | ||
gmp | ||
--FILE-- | ||
<?php | ||
try { | ||
gmp_fact(PHP_INT_MAX); | ||
} catch (\ValueError $e) { | ||
echo $e->getMessage() . PHP_EOL; | ||
} | ||
|
||
try { | ||
gmp_fact(gmp_init(PHP_INT_MAX)); | ||
} catch (\ValueError $e) { | ||
echo $e->getMessage(); | ||
} | ||
?> | ||
--EXPECTF-- | ||
gmp_fact(): Argument #1 ($num) must be between 0 and %d | ||
gmp_fact(): Argument #1 ($num) must be between 0 and %d |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic to avoid overflow in
_mpz_realloc()
(libgmp 6.3.0) is:Assuming that
mp_size_t
is actuallysize_t
, the simplification seems to be correct (we're assumingsizeof(int) == 4
anyway). However, I do not understand howULONG_MAX
fits into the mix. That only works under the assumption thatULONG_MAX == UINT_MAX
for 32bit platforms (which is what php-src assumes anyway), but why don't they useUINT_MAX
in the first place? I think we should.