Skip to content

Commit 7a904bb

Browse files
committed
ext/sockets: Throw ValueError when string is too long
And replace calls to strlcpy to memcpy
1 parent c00fc6f commit 7a904bb

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

ext/sockets/sockets.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,8 +1946,13 @@ PHP_FUNCTION(socket_set_option)
19461946
zend_argument_value_error(4, "must not contain null bytes when argument #3 ($option) is TCP_FUNCTION_BLK");
19471947
RETURN_THROWS();
19481948
}
1949+
if (Z_STRLEN_P(arg4) > TCP_FUNCTION_NAME_LEN_MAX) {
1950+
zend_argument_value_error(4, "must be less than %zu bytes when argument #3 ($option) is TCP_FUNCTION_BLK", TCP_FUNCTION_NAME_LEN_MAX);
1951+
RETURN_THROWS();
1952+
}
19491953
struct tcp_function_set tfs = {0};
1950-
strlcpy(tfs.function_set_name, Z_STRVAL_P(arg4), TCP_FUNCTION_NAME_LEN_MAX);
1954+
/* Copy the trailing nul byte */
1955+
memcpy(tfs.function_set_name, Z_STRVAL_P(arg4), Z_STRLEN_P(arg4)+1);
19511956

19521957
optlen = sizeof(tfs);
19531958
opt_ptr = &tfs;
@@ -2077,8 +2082,13 @@ PHP_FUNCTION(socket_set_option)
20772082
zend_argument_value_error(4, "must not contain null bytes when argument #3 ($option) is SO_ACCEPTFILTER");
20782083
RETURN_THROWS();
20792084
}
2085+
if (Z_STRLEN_P(arg4) > sizeof(af.af_name)) {
2086+
zend_argument_value_error(4, "must be less than %zu bytes when argument #3 ($option) is SO_ACCEPTFILTER", sizeof(af.af_name));
2087+
RETURN_THROWS();
2088+
}
20802089
struct accept_filter_arg af = {0};
2081-
strlcpy(af.af_name, Z_STRVAL_P(arg4), sizeof(af.af_name));
2090+
/* Copy the trailing nul byte */
2091+
memcpy(af.af_name, Z_STRVAL_P(arg4), Z_STRLEN_P(arg4)+1);
20822092
opt_ptr = ⁡
20832093
optlen = sizeof(af);
20842094
break;

ext/sockets/tests/socket_set_option_invalid_value_for_IPPROTO_TCP_TCP_FUNCTION_BLK.phpt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@ try {
3232
} catch (Throwable $e) {
3333
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
3434
}
35+
try {
36+
$ret = socket_set_option($socket, IPPROTO_TCP, TCP_FUNCTION_BLK, str_repeat("a", 2048);
37+
var_dump($ret);
38+
} catch (Throwable $e) {
39+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
40+
}
3541

3642
socket_close($socket);
3743
?>
38-
--EXPECT--
44+
--EXPECTF--
3945
TypeError: socket_set_option(): Argument #4 ($value) must be of type string when argument #3 ($option) is TCP_FUNCTION_BLK, stdClass given
4046
ValueError: socket_set_option(): Argument #4 ($value) must not contain null bytes when argument #3 ($option) is TCP_FUNCTION_BLK
47+
ValueError: socket_set_option(): Argument #4 ($value) must be less than %d 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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@ try {
3232
} catch (Throwable $e) {
3333
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
3434
}
35+
try {
36+
$ret = socket_set_option($socket, SOL_SOCKET, SO_ACCEPTFILTER, str_repeat("a", 2048);
37+
var_dump($ret);
38+
} catch (Throwable $e) {
39+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
40+
}
3541

3642
socket_close($socket);
3743
?>
38-
--EXPECT--
44+
--EXPECTF--
3945
TypeError: socket_set_option(): Argument #4 ($value) must be of type string when argument #3 ($option) is SO_ACCEPTFILTER, stdClass given
4046
ValueError: socket_set_option(): Argument #4 ($value) must not contain null bytes when argument #3 ($option) is SO_ACCEPTFILTER
47+
ValueError: socket_set_option(): Argument #4 ($value) must be less than %d bytes when argument #3 ($option) is SO_ACCEPTFILTER

0 commit comments

Comments
 (0)