From 3543e17967699b89edba7436c932853f9580dd9c Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 18 Oct 2018 07:38:03 +0200 Subject: [PATCH] Ensure OpenSslEngine will not try to call SSL_free multiple times even when constructor throws. (#8399) Motivation: When the constructor of OpenSslEngine threw we could end up to self call SSL_free by ourself and then have the finalizer do the same which may lead to double free-ing and so SIGSEV. Modifications: Just call shutdown() when the constructor throws and so ensure SSL_free is guarded correctly in the finalizer. Result: No more SIGSEV possible. --- .../io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java b/handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java index 4ddcc2aefa..d70c49ab90 100644 --- a/handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java +++ b/handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java @@ -363,7 +363,11 @@ public class ReferenceCountedOpenSslEngine extends SSLEngine implements Referenc // setMode may impact the overhead. calculateMaxWrapOverhead(); } catch (Throwable cause) { - SSL.freeSSL(ssl); + // Call shutdown so we are sure we correctly release all native memory and also guard against the + // case when shutdown() will be called by the finalizer again. If we would call SSL.free(...) directly + // the finalizer may end up calling it again as we would miss to update the DESTROYED_UPDATER. + shutdown(); + PlatformDependent.throwException(cause); } }