Skip to content

Conversation

@cmb69
Copy link
Member

@cmb69 cmb69 commented Jul 16, 2021

We need to check for potential integer overflow when allocating
zend_strings. To be able to bail out, we need to forward declare
zend_error_noreturn().


Note that there are more occurrences which would need to be patched accordingly, but I'm not sure whether this way of doing is good, and I'm not even sure if this will be fixed at all, since in practice it only affects 32bit systems, and even there is relevant only for pathological cases. I assume this is a known issue for ZE developers.

We need to check for potential integer overflow when allocating
`zend_string`s.  To be able to bail out, we need to forward declare
`zend_error_noreturn()`.
@cmb69 cmb69 changed the base branch from master to PHP-7.4 July 16, 2021 16:36
@cmb69 cmb69 added the Bug label Jul 16, 2021
zend_string *ret;

if (UNEXPECTED(size < len)) {
zend_error_noreturn(E_ERROR, "Integer overflow in string allocation");
Copy link
Member

Choose a reason for hiding this comment

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

It would be better to define a separate function for this (in zend_string.c).

@nikic
Copy link
Member

nikic commented Jul 19, 2021

For the particular bug report, isn't this covered by

if (UNEXPECTED(op1_len > SIZE_MAX - op2_len)) {
?

@nikic
Copy link
Member

nikic commented Jul 19, 2021

I believe @dstogov was of the opinion that we should use zend_string_alloc_safe in cases where this is an issue.

Personally I think it makes sense to do this check (and remove various workarounds we have introduced in other places, e.g. the one linked above). The check should be cheap and can be optimized away for constant strings.

@cmb69
Copy link
Member Author

cmb69 commented Jul 19, 2021

zend_string_safe_alloc() nor the ckeck above catches this issue, which is adding the struct size plus alignment without overflow check. E.g. typically strings which are a few bytes smaller than SIZE_MAX trigger this issue. See also https://bugs.php.net/75934#1524924818.

@cmb69
Copy link
Member Author

cmb69 commented Jul 21, 2021

@dstogov, any thoughts on this issue?

@dstogov
Copy link
Member

dstogov commented Jul 21, 2021

@dstogov, any thoughts on this issue?

I don't like to add check into zend_string_alloc()
zend_string_safe_alloc() should be used in potentially problematic cases.

Especially for this bug, I suppose, SIZE_MAX should be replaced by MAX_ZEND_STRING_LEN.

@cmb69
Copy link
Member Author

cmb69 commented Jul 21, 2021

Okay, that makes sense. Closing in favor of PR #7294.

@cmb69 cmb69 closed this Jul 21, 2021
@cmb69 cmb69 deleted the cmb/74960 branch July 21, 2021 13:38
@jedisct1
Copy link
Contributor

Expecting all users to check the size prior to calling zend_string_alloc() in order to handle an edge case is a bit overoptimistic.

ZSTR_MAX_LEN is useful, but adding the check to zend_string_alloc() has negligible overhead and immediately protects any string allocation against this bug. Addition of ZSTR_MAX_LEN is not incompatible with being better safe than sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants