Skip to content

Commit 8930bf8

Browse files
committed
Fix GH-8979: Possible Memory Leak with SSL-enabled MySQL connections
The stream context inside `mysqlnd_vio::enable_ssl()` is leaking. In particular: when `php_stream_context_set()` get called the refcount of `context` is increased by 1, which means that `context` will now have a refcount of 2. Later on we remove the context from the stream by calling `php_stream_context_set(stream, NULL)` but that leaves our `context` with a refcount of 1, and therefore it's never destroyed. In my test case this yielded a leak of 1456 bytes per connection (but could be more depending on your settings ofc). Annoyingly, Valgrind doesn't find it because the context is still in the `EG(regular_list)` and will thus be destroyed at the end of the request. However, I still think this bug needs to be fixed because as the users in the issue report already mentioned: there can be long-running PHP scripts. Fix it by decreasing the refcount to transfer the ownership. Closes GH-10909.
1 parent 90f5b2b commit 8930bf8

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ PHP NEWS
3131
. Fixed bug GH-10521 (ftp_get/ftp_nb_get resumepos offset is maximum 10GB).
3232
(nielsdos)
3333

34+
- MySQLnd:
35+
. Fixed bug GH-8979 (Possible Memory Leak with SSL-enabled MySQL
36+
connections). (nielsdos)
37+
3438
- Opcache:
3539
. Fixed build for macOS to cater with pkg-config settings. (David Carlier)
3640
. Fixed bug GH-8065 (opcache.consistency_checks > 0 causes segfaults in

ext/mysqlnd/mysqlnd_vio.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,10 @@ MYSQLND_METHOD(mysqlnd_vio, enable_ssl)(MYSQLND_VIO * const net)
561561
}
562562
}
563563
php_stream_context_set(net_stream, context);
564+
/* php_stream_context_set() increases the refcount of context, but we just want to transfer ownership
565+
* hence the need to decrease the refcount so the refcount will be equal to 1. */
566+
ZEND_ASSERT(GC_REFCOUNT(context->res) == 2);
567+
GC_DELREF(context->res);
564568
if (php_stream_xport_crypto_setup(net_stream, STREAM_CRYPTO_METHOD_TLS_CLIENT, NULL) < 0 ||
565569
php_stream_xport_crypto_enable(net_stream, 1) < 0)
566570
{

0 commit comments

Comments
 (0)