Skip to content

Commit d2780bc

Browse files
authored
Fix use-after-free in OpenSSL empty cafile warning (GH-22880)
1 parent cc6a8c6 commit d2780bc

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
@@ -947,15 +947,14 @@ static long php_openssl_load_stream_cafile(X509_STORE *cert_store, const char *c
947947
goto cert_start;
948948
}
949949

950-
stream_complete: {
951-
php_stream_close(stream);
952-
if (buffer_active == 1) {
953-
BIO_free(buffer);
954-
}
950+
stream_complete:
951+
if (certs_added == 0) {
952+
php_stream_warn(stream, DecodingFailed, "no valid certs found cafile stream: '%s'", cafile);
955953
}
956954

957-
if (certs_added == 0) {
958-
php_stream_warn(stream, DecodingFailed, "no valid certs found cafile stream: `%s'", cafile);
955+
php_stream_close(stream);
956+
if (buffer_active == 1) {
957+
BIO_free(buffer);
959958
}
960959

961960
return certs_added;

0 commit comments

Comments
 (0)