Skip to content

Commit 45de487

Browse files
committed
Fix use-after-free in OpenSSL empty cafile warning
1 parent 07d308a commit 45de487

2 files changed

Lines changed: 48 additions & 7 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
SSL cafile stream containing no valid certificates
3+
--EXTENSIONS--
4+
openssl
5+
--SKIPIF--
6+
<?php
7+
if (!function_exists('proc_open')) die('skip no proc_open');
8+
?>
9+
--FILE--
10+
<?php
11+
$serverCode = <<<'CODE'
12+
$server = stream_socket_server('tcp://127.0.0.1:0', $errno, $errstr);
13+
phpt_notify_server_start($server);
14+
15+
$client = stream_socket_accept($server, 2);
16+
if ($client) {
17+
fclose($client);
18+
}
19+
CODE;
20+
21+
$clientCode = <<<'CODE'
22+
$context = stream_context_create(['ssl' => [
23+
'cafile' => 'file://%s',
24+
]]);
25+
var_dump(stream_socket_client(
26+
'ssl://{{ ADDR }}',
27+
timeout: 2,
28+
context: $context,
29+
));
30+
CODE;
31+
$clientCode = sprintf($clientCode, __DIR__ . '/plain.txt');
32+
33+
include 'ServerClientTestCase.inc';
34+
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
35+
?>
36+
--EXPECTF--
37+
Warning: stream_socket_client(): no valid certs found cafile stream: '%s' in %sServerClientTestCase.inc(%d) : eval()'d code on line 4
38+
39+
Warning: stream_socket_client(): Failed to enable crypto in %sServerClientTestCase.inc(%d) : eval()'d code on line 4
40+
41+
Warning: stream_socket_client(): Unable to connect to ssl://127.0.0.1:%d (Unknown error) in %sServerClientTestCase.inc(%d) : eval()'d code on line 4
42+
bool(false)

ext/openssl/xp_ssl.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -921,15 +921,14 @@ static long php_openssl_load_stream_cafile(X509_STORE *cert_store, const char *c
921921
goto cert_start;
922922
}
923923

924-
stream_complete: {
925-
php_stream_close(stream);
926-
if (buffer_active == 1) {
927-
BIO_free(buffer);
928-
}
924+
stream_complete:
925+
if (certs_added == 0) {
926+
php_stream_warn(stream, DecodingFailed, "no valid certs found cafile stream: '%s'", cafile);
929927
}
930928

931-
if (certs_added == 0) {
932-
php_stream_warn(stream, DecodingFailed, "no valid certs found cafile stream: `%s'", cafile);
929+
php_stream_close(stream);
930+
if (buffer_active == 1) {
931+
BIO_free(buffer);
933932
}
934933

935934
return certs_added;

0 commit comments

Comments
 (0)