Skip to content

Commit c00fc6f

Browse files
committed
ext/sockets: Throw ValueError when string has null bytes for options not passing string length
1 parent 64f819f commit c00fc6f

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

ext/sockets/sockets.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,10 @@ PHP_FUNCTION(socket_set_option)
19421942
zend_argument_type_error(4, "must be of type string when argument #3 ($option) is TCP_FUNCTION_BLK, %s given", zend_zval_value_name(arg4));
19431943
RETURN_THROWS();
19441944
}
1945+
if (zend_str_has_nul_byte(Z_STR_P(arg4))) {
1946+
zend_argument_value_error(4, "must not contain null bytes when argument #3 ($option) is TCP_FUNCTION_BLK");
1947+
RETURN_THROWS();
1948+
}
19451949
struct tcp_function_set tfs = {0};
19461950
strlcpy(tfs.function_set_name, Z_STRVAL_P(arg4), TCP_FUNCTION_NAME_LEN_MAX);
19471951

@@ -2069,6 +2073,10 @@ PHP_FUNCTION(socket_set_option)
20692073
zend_argument_type_error(4, "must be of type string when argument #3 ($option) is SO_ACCEPTFILTER, %s given", zend_zval_value_name(arg4));
20702074
RETURN_THROWS();
20712075
}
2076+
if (zend_str_has_nul_byte(Z_STR_P(arg4))) {
2077+
zend_argument_value_error(4, "must not contain null bytes when argument #3 ($option) is SO_ACCEPTFILTER");
2078+
RETURN_THROWS();
2079+
}
20722080
struct accept_filter_arg af = {0};
20732081
strlcpy(af.af_name, Z_STRVAL_P(arg4), sizeof(af.af_name));
20742082
opt_ptr = ⁡

ext/sockets/tests/socket_set_option_invalid_value_for_IPPROTO_TCP_TCP_FUNCTION_BLK.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,15 @@ try {
2626
} catch (Throwable $e) {
2727
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
2828
}
29+
try {
30+
$ret = socket_set_option($socket, IPPROTO_TCP, TCP_FUNCTION_BLK, "string\0with\0null\0bytes");
31+
var_dump($ret);
32+
} catch (Throwable $e) {
33+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
34+
}
2935

3036
socket_close($socket);
3137
?>
3238
--EXPECT--
3339
TypeError: socket_set_option(): Argument #4 ($value) must be of type string when argument #3 ($option) is TCP_FUNCTION_BLK, stdClass given
40+
ValueError: socket_set_option(): Argument #4 ($value) must not contain null bytes when argument #3 ($option) is TCP_FUNCTION_BLK

ext/sockets/tests/socket_set_option_invalid_value_for_SOL_SOCKET_SO_ACCEPTFILTER.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,15 @@ try {
2626
} catch (Throwable $e) {
2727
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
2828
}
29+
try {
30+
$ret = socket_set_option($socket, SOL_SOCKET, SO_ACCEPTFILTER, "string\0with\0null\0bytes");
31+
var_dump($ret);
32+
} catch (Throwable $e) {
33+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
34+
}
2935

3036
socket_close($socket);
3137
?>
3238
--EXPECT--
3339
TypeError: socket_set_option(): Argument #4 ($value) must be of type string when argument #3 ($option) is SO_ACCEPTFILTER, stdClass given
40+
ValueError: socket_set_option(): Argument #4 ($value) must not contain null bytes when argument #3 ($option) is SO_ACCEPTFILTER

0 commit comments

Comments
 (0)