GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix leak in ReferenceCountedOpenSslEngine.addCredential (#16979) · netty/netty@c1cbdfb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src
    • main/java/io/netty/handler/ssl
      • ReferenceCountedOpenSslEngine.java
      • SslContextBuilder.java
    • test/java/io/netty/handler/ssl
      • OpenSslCredentialBuilderTest.java
      • OpenSslEngineTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.security.AlgorithmConstraints;
4242
import java.security.Principal;
4343
import java.security.cert.Certificate;
44+
import java.util.ArrayList;
4445
import java.util.Arrays;
4546
import java.util.Collection;
4647
import java.util.Collections;
@@ -155,6 +156,8 @@ private enum HandshakeState {
155156
private HandshakeState handshakeState = HandshakeState.NOT_STARTED;
156157
private boolean receivedShutdown;
157158
private volatile boolean destroyed;
159+
// Credentials added via addCredential(); released in shutdown() to balance the retain() in addCredential().
160+
private List<OpenSslCredential> engineCredentials;
158161
private volatile String applicationProtocol;
159162
private volatile boolean needTask;
160163
private boolean hasTLSv13Cipher;
@@ -595,6 +598,12 @@ public final synchronized void shutdown() {
595598
if (engines != null) {
596599
engines.remove(ssl);
597600
}
601+
if (engineCredentials != null) {
602+
for (OpenSslCredential credential : engineCredentials) {
603+
credential.release();
604+
}
605+
engineCredentials = null;
606+
}
598607
SSL.freeSSL(ssl);
599608
ssl = networkBIO = 0;
600609

@@ -2346,6 +2355,10 @@ public void addCredential(OpenSslCredential credential) throws SSLException {
23462355
credential.retain();
23472356
try {
23482357
SSL.addCredential(ssl, pointer.credentialAddress());
2358+
if (engineCredentials == null) {
2359+
engineCredentials = new ArrayList<>();
2360+
}
2361+
engineCredentials.add(credential);
23492362
} catch (Exception e) {
23502363
credential.release();
23512364
throw new SSLException("Failed to add credential to SSL engine", e);
@@ -2360,6 +2373,11 @@ public void addCredential(OpenSslCredential credential) throws SSLException {
23602373
* <p>This method returns the credential that was ultimately chosen by the TLS handshake.
23612374
* It's useful for introspection after the handshake completes.
23622375
*
2376+
* <p><strong>Lifetime warning:</strong> the returned {@link OpenSslCredential} is a
2377+
* <em>borrowed</em> reference backed by a native pointer owned by BoringSSL. It is only valid
2378+
* while this engine is alive. Do <em>not</em> retain the returned credential and use it after
2379+
* {@link #shutdown()} has been called — doing so will access freed native memory.
2380+
*
23632381
* <p>This is a BoringSSL-specific feature.
23642382
*
23652383
* @return the selected credential, or {@code null} if not available
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/SslContextBuilder.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/SslContextBuilder.java
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,11 @@ public SslContextBuilder keyManager(KeyManagerFactory keyManagerFactory) {
512512
* or {@link SslProvider#OPENSSL_REFCNT}.
513513
* Check {@link OpenSslCredential#isAvailable()} to verify that the feature is supported.
514514
*
515+
* <p><strong>Lifetime:</strong> this builder does <em>not</em> retain the credential. The
516+
* caller must ensure the credential remains alive (refcount {@code > 0}) until {@link #build()}
517+
* returns. {@link #build()} will retain its own reference via the constructed
518+
* {@link SslContext}.
519+
*
515520
* @param credential the credential to add
516521
* @return this builder for chaining
517522
* @see OpenSslCredentialBuilder
Collapse file

‎handler/src/test/java/io/netty/handler/ssl/OpenSslCredentialBuilderTest.java‎

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty/handler/ssl/OpenSslCredentialBuilderTest.java
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@
1717

1818
import io.netty.pkitesting.CertificateBuilder;
1919
import io.netty.pkitesting.X509Bundle;
20+
import io.netty.util.test.LeakPresenceExtension;
2021
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
2122
import org.junit.jupiter.api.BeforeAll;
2223
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.api.extension.ExtendWith;
2325

2426
import static org.junit.jupiter.api.Assertions.assertNotNull;
2527
import static org.junit.jupiter.api.Assumptions.assumeTrue;
2628

2729
/**
2830
* Unit tests for {@link OpenSslCredentialBuilder}.
2931
*/
32+
@ExtendWith(LeakPresenceExtension.class)
3033
public class OpenSslCredentialBuilderTest {
3134

3235
private static X509Bundle cert;
Collapse file

‎handler/src/test/java/io/netty/handler/ssl/OpenSslEngineTest.java‎

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty/handler/ssl/OpenSslEngineTest.java
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727
import io.netty.pkitesting.X509Bundle;
2828
import io.netty.util.CharsetUtil;
2929
import io.netty.util.ReferenceCountUtil;
30+
import io.netty.util.test.LeakPresenceExtension;
3031
import io.netty.util.internal.EmptyArrays;
3132
import io.netty.util.internal.PlatformDependent;
3233
import org.junit.jupiter.api.AfterEach;
3334
import org.junit.jupiter.api.BeforeAll;
3435
import org.junit.jupiter.api.Test;
3536
import org.junit.jupiter.api.condition.EnabledIf;
37+
import org.junit.jupiter.api.extension.ExtendWith;
3638
import org.junit.jupiter.api.function.Executable;
3739
import org.junit.jupiter.params.ParameterizedTest;
3840
import org.junit.jupiter.params.provider.MethodSource;
@@ -85,6 +87,7 @@
8587
import static org.junit.jupiter.api.Assumptions.assumeFalse;
8688
import static org.junit.jupiter.api.Assumptions.assumeTrue;
8789

90+
@ExtendWith(LeakPresenceExtension.class)
8891
public class OpenSslEngineTest extends SSLEngineTest {
8992
private static final String PREFERRED_APPLICATION_LEVEL_PROTOCOL = "my-protocol-http2";
9093
private static final String FALLBACK_APPLICATION_LEVEL_PROTOCOL = "my-protocol-http1_1";
@@ -1882,4 +1885,35 @@ public void testHandshakeWithMultipleCredentials(SSLEngineTestParam param) throw
18821885
ReferenceCountUtil.safeRelease(ecdsaCredential);
18831886
}
18841887
}
1888+
1889+
@Test
1890+
public void addCredentialRetainsAndShutdownReleases() throws Exception {
1891+
assumeTrue(OpenSslCredential.isAvailable());
1892+
1893+
OpenSslCredential credential = OpenSslCredentialBuilder
1894+
.forX509(rsaCert.getKeyPair().getPrivate(), rsaCert.getCertificate())
1895+
.build();
1896+
1897+
SslContext serverContext = SslContextBuilder
1898+
.forServer(rsaCert.getKeyPair().getPrivate(), rsaCert.getCertificate())
1899+
.sslProvider(SslProvider.OPENSSL_REFCNT)
1900+
.build();
1901+
1902+
try {
1903+
ReferenceCountedOpenSslEngine engine =
1904+
(ReferenceCountedOpenSslEngine) serverContext.newEngine(UnpooledByteBufAllocator.DEFAULT);
1905+
try {
1906+
assertThat(credential.refCnt()).isEqualTo(1);
1907+
engine.addCredential(credential);
1908+
assertThat(credential.refCnt()).isEqualTo(2);
1909+
engine.shutdown();
1910+
assertThat(credential.refCnt()).isEqualTo(1);
1911+
} finally {
1912+
ReferenceCountUtil.release(engine);
1913+
}
1914+
} finally {
1915+
ReferenceCountUtil.safeRelease(credential);
1916+
ReferenceCountUtil.release(serverContext);
1917+
}
1918+
}
18851919
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.