Skip to content

Fix GH-16501: gmp_random_bits overflow. - #16503

Closed
devnexen wants to merge 2 commits into
php:PHP-8.2from
devnexen:gh16501
Closed

devnexen wants to merge 2 commits into
php:PHP-8.2from
devnexen:gh16501

Conversation

@devnexen

Copy link
Copy Markdown
Member

we do the same calculation in advance as mpz_realloc overflow check to avoid abort.

we do the same calculation in advance as mpz_realloc overflow check to
avoid abort.
@devnexen
devnexen requested a review from Girgias as a code owner October 19, 2024 05:05
@devnexen devnexen linked an issue Oct 19, 2024 that may be closed by this pull request

@Girgias Girgias left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

MSTM, maybe @cmb69 wants to also have a quick look

@cmb69

cmb69 commented Oct 20, 2024

Copy link
Copy Markdown
Member

Well, what can I say … a library should never ever call abort(). I'm afraid we need a lot more such checks, basically all duplicating what's already done by libgmp. But for mpir, there are even no overflow checks in mpz_realloc(), so the situation may be worse.

Comment thread ext/gmp/gmp.c
gmp_init_random();

mpz_urandomb(gmpnum_result, GMPG(rand_state), bits);
mpz_urandomb(gmpnum_result, GMPG(rand_state), (mp_bitcnt_t)bits);

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread ext/gmp/gmp.c
#if SIZEOF_SIZE_T == 4
const zend_long maxbits = ULONG_MAX / GMP_NUMB_BITS;
#else
const zend_long maxbits = INT_MAX;

Copy link
Copy Markdown
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
Copy Markdown
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.

@devnexen

Copy link
Copy Markdown
Member Author

Well, what can I say … a library should never ever call abort().

Seems a particular use case generalised (or internal state going to be compromised if it keeps going ? no idea), I wish there were a runtime setting to at least being able to choose what to do

@devnexen devnexen closed this in 9ca68e0 Oct 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Overflow in gmp_random_bits

3 participants