Skip to content

Commit 8789348

Browse files
committed
Merge branch 'PHP-7.3'
* PHP-7.3: Initialize s_un (sockaddr_un) to zero before using it. Fixes #76839.
2 parents 27e9c05 + e38e44e commit 8789348

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

ext/sockets/sockets.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,9 @@ PHP_FUNCTION(socket_recvfrom)
18111811
switch (php_sock->type) {
18121812
case AF_UNIX:
18131813
slen = sizeof(s_un);
1814+
memset(&s_un, 0, slen);
18141815
s_un.sun_family = AF_UNIX;
1816+
18151817
retval = recvfrom(php_sock->bsd_socket, ZSTR_VAL(recv_buf), arg3, arg4, (struct sockaddr *)&s_un, (socklen_t *)&slen);
18161818

18171819
if (retval < 0) {

ext/sockets/tests/bug76839.phpt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
--TEST--
2+
Bug #76839: socket_recvfrom may return an invalid 'from' address on MacOS
3+
--SKIPIF--
4+
<?php
5+
if (strtolower(substr(PHP_OS, 0, 3)) === 'win') {
6+
die('skip not valid for Windows.');
7+
}
8+
if (!extension_loaded('sockets')) {
9+
die('skip sockets extension not available.');
10+
}
11+
--FILE--
12+
<?php
13+
14+
// This bug only occurs when a specific portion of memory is unclean.
15+
// Unforunately, looping around 10 times and using random paths is the
16+
// best way I could manage to reproduce this problem without modifying php itself :-/
17+
18+
for ($i = 0; $i < 10; $i++) {
19+
$senderSocketPath = '/tmp/' . substr(md5(rand()), 0, rand(8, 16)) . '.sock';
20+
$senderSocket = socket_create(AF_UNIX, SOCK_DGRAM, 0);
21+
socket_bind($senderSocket, $senderSocketPath);
22+
23+
$receiverSocketPath = '/tmp/' . substr(md5(rand()), 0, rand(8, 16)) . '.sock';
24+
$receiverSocket = socket_create(AF_UNIX, SOCK_DGRAM, 0);
25+
socket_bind($receiverSocket, $receiverSocketPath);
26+
27+
// Send message from sender socket to receiver socket
28+
socket_sendto($senderSocket, 'Ping!', 5, 0, $receiverSocketPath);
29+
30+
// Receive message on receiver socket
31+
$from = '';
32+
$message = '';
33+
socket_recvfrom($receiverSocket, $message, 65535, 0, $from);
34+
echo "Received '$message'\n";
35+
36+
// Respond to the sender using the 'from' address from socket_recvfrom
37+
socket_sendto($receiverSocket, 'Pong!', 5, 0, $from);
38+
echo "Responded to sender with 'Pong!'\n";
39+
40+
socket_close($receiverSocket);
41+
unlink($receiverSocketPath);
42+
socket_close($senderSocket);
43+
unlink($senderSocketPath);
44+
}
45+
--EXPECT--
46+
Received 'Ping!'
47+
Responded to sender with 'Pong!'
48+
Received 'Ping!'
49+
Responded to sender with 'Pong!'
50+
Received 'Ping!'
51+
Responded to sender with 'Pong!'
52+
Received 'Ping!'
53+
Responded to sender with 'Pong!'
54+
Received 'Ping!'
55+
Responded to sender with 'Pong!'
56+
Received 'Ping!'
57+
Responded to sender with 'Pong!'
58+
Received 'Ping!'
59+
Responded to sender with 'Pong!'
60+
Received 'Ping!'
61+
Responded to sender with 'Pong!'
62+
Received 'Ping!'
63+
Responded to sender with 'Pong!'
64+
Received 'Ping!'
65+
Responded to sender with 'Pong!'

0 commit comments

Comments
 (0)