Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix GH-10495: feof on OpenSSL stream hangs indefinitely
This fixes the issue with unbounded waiting on SSL_peek which can happen when only part of the record is fetched. It makes socket non blocking so it is possible to verify if OpenSSL is expecting some more data or if there is an error. This also fixes bug #79501 Closes GH-13487
- Loading branch information
Showing
3 changed files
with
208 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
--TEST-- | ||
GH-10495: feof hangs indefinitely | ||
--EXTENSIONS-- | ||
openssl | ||
--SKIPIF-- | ||
<?php | ||
if (!function_exists("proc_open")) die("skip no proc_open"); | ||
?> | ||
--FILE-- | ||
<?php | ||
$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'gh10495.pem.tmp'; | ||
$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'gh10495-ca.pem.tmp'; | ||
|
||
$peerName = 'gh10495'; | ||
$clientCode = <<<'CODE' | ||
$context = stream_context_create(['ssl' => ['verify_peer' => false, 'peer_name' => '%s']]); | ||
phpt_wait('server'); | ||
phpt_notify('proxy'); | ||
phpt_wait('proxy'); | ||
$fp = stream_socket_client("tlsv1.2://127.0.0.1:10012", $errornum, $errorstr, 1, STREAM_CLIENT_CONNECT, $context); | ||
phpt_wait('proxy'); | ||
$time = microtime(true); | ||
var_dump(feof($fp)); | ||
var_dump(microtime(true) - $time < 0.5); | ||
var_dump(stream_get_contents($fp, 6)); | ||
phpt_notify('server'); | ||
phpt_notify('proxy'); | ||
CODE; | ||
$clientCode = sprintf($clientCode, $peerName); | ||
|
||
$serverCode = <<<'CODE' | ||
$context = stream_context_create(['ssl' => ['local_cert' => '%s']]); | ||
$flags = STREAM_SERVER_BIND|STREAM_SERVER_LISTEN; | ||
$fp = stream_socket_server("tlsv1.2://127.0.0.1:10011", $errornum, $errorstr, $flags, $context); | ||
phpt_notify(); | ||
$conn = stream_socket_accept($fp); | ||
fwrite($conn, 'warmup'); | ||
phpt_wait(); | ||
fclose($conn); | ||
CODE; | ||
$serverCode = sprintf($serverCode, $certFile); | ||
|
||
$proxyCode = <<<'CODE' | ||
phpt_wait(); | ||
$upstream = stream_socket_client("tcp://127.0.0.1:10011", $errornum, $errorstr, 3000, STREAM_CLIENT_CONNECT); | ||
stream_set_blocking($upstream, false); | ||
$flags = STREAM_SERVER_BIND|STREAM_SERVER_LISTEN; | ||
$server = stream_socket_server("tcp://127.0.0.1:10012", $errornum, $errorstr, $flags); | ||
phpt_notify(); | ||
$conn = stream_socket_accept($server); | ||
stream_set_blocking($conn, false); | ||
$read = [$upstream, $conn]; | ||
$applicationData = false; | ||
$i = 1; | ||
while (stream_select($read, $write, $except, 1)) { | ||
foreach ($read as $fp) { | ||
$data = stream_get_contents($fp); | ||
if ($fp === $conn) { | ||
fwrite($upstream, $data); | ||
} else { | ||
if ($data !== '' && $data[0] === chr(23)) { | ||
if (!$applicationData) { | ||
$applicationData = true; | ||
fwrite($conn, $data[0]); | ||
phpt_notify(); | ||
sleep(1); | ||
fwrite($conn, substr($data, 1)); | ||
} else { | ||
fwrite($conn, $data); | ||
} | ||
} else { | ||
fwrite($conn, $data); | ||
} | ||
} | ||
} | ||
if (feof($upstream)) { | ||
break; | ||
} | ||
$read = [$upstream, $conn]; | ||
} | ||
phpt_wait(); | ||
CODE; | ||
|
||
include 'CertificateGenerator.inc'; | ||
$certificateGenerator = new CertificateGenerator(); | ||
$certificateGenerator->saveCaCert($cacertFile); | ||
$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile); | ||
|
||
include 'ServerClientTestCase.inc'; | ||
ServerClientTestCase::getInstance()->run($clientCode, [ | ||
'server' => $serverCode, | ||
'proxy' => $proxyCode, | ||
]); | ||
?> | ||
--CLEAN-- | ||
<?php | ||
@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'gh10495.pem.tmp'); | ||
@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'gh10495-ca.pem.tmp'); | ||
?> | ||
--EXPECT-- | ||
bool(false) | ||
bool(true) | ||
string(6) "warmup" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters