Skip to content

Fix GH-16189: underflow on preg_match/preg_match_all start_offset. #16191

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 1 commit into from

Conversation

devnexen
Copy link
Member

@devnexen devnexen commented Oct 3, 2024

No description provided.

@devnexen devnexen linked an issue Oct 3, 2024 that may be closed by this pull request
@devnexen devnexen marked this pull request as draft October 3, 2024 04:48
@devnexen devnexen marked this pull request as ready for review October 3, 2024 06:15
Comment on lines 1138 to 1141
if (start_offset == LONG_MIN) {
zend_argument_value_error(5, "must be greater than " ZEND_LONG_FMT, LONG_MIN);
RETURN_THROWS();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (start_offset == LONG_MIN) {
zend_argument_value_error(5, "must be greater than " ZEND_LONG_FMT, LONG_MIN);
RETURN_THROWS();
}
if (start_offset == LONG_MIN) {
start_offset == LONG_MIN + 1;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

not sure we should hide the issue

Copy link
Contributor

Choose a reason for hiding this comment

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

I belive we should, user should be able to pass ZEND_LONG_MIN.

@@ -1135,6 +1135,11 @@ static void php_do_pcre_match(INTERNAL_FUNCTION_PARAMETERS, int global) /* {{{ *
RETURN_FALSE;
}

if (start_offset == LONG_MIN) {
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible that LONG_MIN is smaller than the minimum value of zend_off_t on some platforms?

For 8.4 and master we could update the start_offset parameter of php_pcre_match_impl() to ssize_t since it's casted to size_t after negation, and use SSIZE_MIN here.

For stable branches we could use ZEND_LONG_MIN as zend_long is always the same as zend_off_t.

Comment on lines +1138 to +1141
if (start_offset == ZEND_LONG_MIN) {
zend_argument_value_error(5, "must be greater than " ZEND_LONG_FMT, ZEND_LONG_MIN);
RETURN_THROWS();
}
Copy link
Member

Choose a reason for hiding this comment

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

Surely it should also be checked that it is not larger than ZEND_LONG_MAX ?

Copy link
Contributor

Choose a reason for hiding this comment

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

How long can be larger than ZEND_LONG_MAX?

Copy link
Member Author

Choose a reason for hiding this comment

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

@mvorisek is right on this, start_offset is a zend_long.

Copy link
Member

@arnaud-lb arnaud-lb left a comment

Choose a reason for hiding this comment

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

LGTM!

@devnexen devnexen closed this in f453d1a Oct 3, 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.

UBSan alert in ext/pcre/php_pcre.c:1208
4 participants