Skip to content

Improve socket cmsg space handling in sockets extension. #5387

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
Closed
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
16 changes: 11 additions & 5 deletions ext/sockets/sendrecvmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,18 @@ PHP_FUNCTION(socket_cmsg_space)
return;
}

if (entry->var_el_size > 0 && n > (zend_long)((ZEND_LONG_MAX - entry->size -
CMSG_SPACE(0) - 15L) / entry->var_el_size)) {
/* the -15 is to account for any padding CMSG_SPACE may add after the data */
php_error_docref(NULL, E_WARNING, "The value for the "
if (entry->var_el_size > 0) {
size_t rem_size = ZEND_LONG_MAX - entry->size;
size_t n_max = rem_size / entry->var_el_size;
size_t size = entry->size + n * entry->var_el_size;
size_t total_size = CMSG_SPACE(size);
if (n > n_max /* zend_long overflow */
|| total_size > ZEND_LONG_MAX
|| total_size < size /* align overflow */) {
php_error_docref(NULL, E_WARNING, "The value for the "
"third argument (" ZEND_LONG_FMT ") is too large", n);
return;
return;
}
}

RETURN_LONG((zend_long)CMSG_SPACE(entry->size + n * entry->var_el_size));
Expand Down