Skip to content

ext/socket: socket_addrinfo_lookup check hints array. #17300

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
Show file tree
Hide file tree
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
29 changes: 25 additions & 4 deletions ext/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -2586,14 +2586,35 @@ PHP_FUNCTION(socket_addrinfo_lookup)
if (zhints && !HT_IS_PACKED(Z_ARRVAL_P(zhints))) {
ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(zhints), key, hint) {
if (key) {
bool failed = false;
if (zend_string_equals_literal(key, "ai_flags")) {
hints.ai_flags = zval_get_long(hint);
zend_long val = zval_try_get_long(hint, &failed);
if (failed) {
zend_argument_type_error(3, "\"ai_flags\" key must be of type int, %s given", zend_zval_type_name(hint));
RETURN_THROWS();
}
hints.ai_flags = (int)val;
Copy link
Member

Choose a reason for hiding this comment

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

Probably there should be a range check to see if the val can fit in a C int, but I'm happy to leave this for a follow-up PR

} else if (zend_string_equals_literal(key, "ai_socktype")) {
hints.ai_socktype = zval_get_long(hint);
zend_long val = zval_try_get_long(hint, &failed);
if (failed) {
zend_argument_type_error(3, "\"ai_socktype\" key must be of type int, %s given", zend_zval_type_name(hint));
RETURN_THROWS();
}
hints.ai_socktype = (int)val;
Copy link
Member

Choose a reason for hiding this comment

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

Ditto

} else if (zend_string_equals_literal(key, "ai_protocol")) {
hints.ai_protocol = zval_get_long(hint);
zend_long val = zval_try_get_long(hint, &failed);
if (failed) {
zend_argument_type_error(3, "\"ai_protocol\" key must be of type int, %s given", zend_zval_type_name(hint));
RETURN_THROWS();
}
hints.ai_protocol = (int)val;
Copy link
Member

Choose a reason for hiding this comment

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

ditto

} else if (zend_string_equals_literal(key, "ai_family")) {
hints.ai_family = zval_get_long(hint);
zend_long val = zval_try_get_long(hint, &failed);
if (failed) {
zend_argument_type_error(3, "\"ai_family\" key must be of type int, %s given", zend_zval_type_name(hint));
RETURN_THROWS();
}
hints.ai_family = (int)val;
Copy link
Member

Choose a reason for hiding this comment

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

ditto

} else {
zend_argument_value_error(3, "must only contain array keys \"ai_flags\", \"ai_socktype\", "
"\"ai_protocol\", or \"ai_family\"");
Expand Down
52 changes: 52 additions & 0 deletions ext/sockets/tests/socket_getaddrinfo_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
--TEST--
socket_addrinfo_lookup with invalid hints
--EXTENSIONS--
sockets
--FILE--
<?php
try {
socket_addrinfo_lookup('127.0.0.1', 2000, array(
'ai_family' => new stdClass(),
'ai_socktype' => SOCK_DGRAM,
'ai_flags' => 0,
'ai_protocol' => 0,
));
} catch (\TypeError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
socket_addrinfo_lookup('127.0.0.1', 2000, array(
'ai_family' => AF_INET,
'ai_socktype' => new stdClass(),
'ai_flags' => 0,
'ai_protocol' => 0,
));
} catch (\TypeError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
socket_addrinfo_lookup('127.0.0.1', 2000, array(
'ai_family' => AF_INET,
'ai_socktype' => SOCK_DGRAM,
'ai_flags' => new stdClass(),
'ai_protocol' => 0,
));
} catch (\TypeError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
socket_addrinfo_lookup('127.0.0.1', 2000, array(
'ai_family' => AF_INET,
'ai_socktype' => SOCK_DGRAM,
'ai_flags' => 0,
'ai_protocol' => new stdClass(),
));
} catch (\TypeError $e) {
echo $e->getMessage() . PHP_EOL;
}
?>
--EXPECT--
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_family" key must be of type int, stdClass given
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_socktype" key must be of type int, stdClass given
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_flags" key must be of type int, stdClass given
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_protocol" key must be of type int, stdClass given
Loading