Skip to content

Commit 6627420

Browse files
committed
Fix cause of crashes with Native + NIO2 + OpenSSL
Prevent concurrent release of <code>OpenSSLEngine</code> resources and the termination of the Tomcat Native library as it can cause crashes during Tomcat shutdown. NIO2 is no longer present in 12.0.x but there may be rarer crashes with NIO.
1 parent 1fd4b37 commit 6627420

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

java/org/apache/tomcat/util/net/openssl/OpenSSLEngine.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.List;
3131
import java.util.Map;
3232
import java.util.Set;
33+
import java.util.concurrent.locks.Lock;
3334

3435
import javax.net.ssl.SSLEngine;
3536
import javax.net.ssl.SSLEngineResult;
@@ -42,6 +43,7 @@
4243

4344
import org.apache.juli.logging.Log;
4445
import org.apache.juli.logging.LogFactory;
46+
import org.apache.tomcat.jni.AprStatus;
4547
import org.apache.tomcat.jni.Buffer;
4648
import org.apache.tomcat.jni.Pool;
4749
import org.apache.tomcat.jni.SSL;
@@ -222,9 +224,9 @@ public String getNegotiatedProtocol() {
222224
public synchronized void shutdown() {
223225
if (!destroyed) {
224226
destroyed = true;
225-
cleanable.clean();
226227
// internal errors can cause shutdown without marking the engine closed
227228
isInboundDone = isOutboundDone = engineClosed = true;
229+
cleanable.clean();
228230
ByteBufferUtils.cleanDirectBuffer(buf);
229231
}
230232
}
@@ -1400,11 +1402,19 @@ public int getApplicationBufferSize() {
14001402
private record OpenSSLState(long ssl, long networkBIO) implements Runnable {
14011403
@Override
14021404
public void run() {
1403-
if (networkBIO != 0) {
1404-
SSL.freeBIO(networkBIO);
1405-
}
1406-
if (ssl != 0) {
1407-
SSL.freeSSL(ssl);
1405+
Lock readLock = AprStatus.getStatusLock().readLock();
1406+
readLock.lock();
1407+
try {
1408+
if (!AprStatus.isAprInitialized()) {
1409+
if (networkBIO != 0) {
1410+
SSL.freeBIO(networkBIO);
1411+
}
1412+
if (ssl != 0) {
1413+
SSL.freeSSL(ssl);
1414+
}
1415+
}
1416+
} finally {
1417+
readLock.unlock();
14081418
}
14091419
}
14101420
}

webapps/docs/changelog.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@
307307
Fix OpenSSL FFM code compatibility with LibreSSL versions below 3.5.
308308
(remm)
309309
</fix>
310+
<fix>
311+
Prevent concurrent release of <code>OpenSSLEngine</code> resources and
312+
the termination of the Tomcat Native library as it might cause crashes
313+
during Tomcat shutdown. (markt)
314+
</fix>
310315
</changelog>
311316
</subsection>
312317
<subsection name="Jasper">

0 commit comments

Comments
 (0)