Skip to content

Commit b6495c1

Browse files
committed
ext/sockets: socket_sendto() add max addr length control for AF_UNIX.
we just mirror what is done for socket_connect()/AF_UNIX type. close GH-21218
1 parent 539c5d9 commit b6495c1

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ PHP NEWS
4141
- Sockets:
4242
. Fixed bug GH-21161 (socket_set_option() crash with array 'addr'
4343
entry as null). (David Carlier)
44+
. Fixed possible addr length overflow with socket_connect() and AF_UNIX
45+
family sockets. (David Carlier)
4446

4547
- Windows:
4648
. Fixed compilation with clang (missing intrin.h include). (Kévin Dunglas)

ext/sockets/sockets.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,12 @@ PHP_FUNCTION(socket_sendto)
15841584
switch (php_sock->type) {
15851585
case AF_UNIX:
15861586
memset(&s_un, 0, sizeof(s_un));
1587+
1588+
if (addr_len >= sizeof(s_un.sun_path)) {
1589+
zend_argument_value_error(5, "must be less than %d", sizeof(s_un.sun_path));
1590+
RETURN_THROWS();
1591+
}
1592+
15871593
s_un.sun_family = AF_UNIX;
15881594
snprintf(s_un.sun_path, sizeof(s_un.sun_path), "%s", addr);
15891595

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
socket_sendto() with AF_UNIX rejects address exceeding sun_path limit
3+
--EXTENSIONS--
4+
sockets
5+
--SKIPIF--
6+
<?php
7+
if (substr(PHP_OS, 0, 3) == 'WIN') {
8+
die('skip not valid for Windows');
9+
}
10+
?>
11+
--FILE--
12+
<?php
13+
$socket = socket_create(AF_UNIX, SOCK_DGRAM, 0);
14+
if (!$socket) {
15+
die('Unable to create AF_UNIX socket');
16+
}
17+
18+
$long_addr = str_repeat('a', 512);
19+
20+
try {
21+
socket_sendto($socket, "data", 4, 0, $long_addr);
22+
} catch (\ValueError $e) {
23+
echo $e->getMessage() . PHP_EOL;
24+
}
25+
26+
socket_close($socket);
27+
?>
28+
--EXPECTF--
29+
socket_sendto(): Argument #5 ($address) must be less than %d

0 commit comments

Comments
 (0)