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 — Guard against out-of-bounds reads when parsing ClientHello SNI extens… · netty/netty@34f17db

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src
    • main/java/io/netty/handler/ssl
      • AbstractSniHandler.java
    • test/java/io/netty/handler/ssl
      • AbstractSniHandlerTest.java
      • SniHandlerTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/AbstractSniHandler.java
+54-46Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
*/
3636
public abstract class AbstractSniHandler<T> extends SslClientHelloHandler<T> {
3737

38-
private static String extractSniHostname(ByteBuf in) {
38+
// Package-private for testing purposes.
39+
static String extractSniHostname(ByteBuf in) {
3940
// See https://tools.ietf.org/html/rfc5246#section-7.4.1.2
4041
//
4142
// Decode the ssl client hello packet.
@@ -64,57 +65,64 @@ private static String extractSniHostname(ByteBuf in) {
6465
final int sessionIdLength = in.getUnsignedByte(offset);
6566
offset += sessionIdLength + 1;
6667

67-
final int cipherSuitesLength = in.getUnsignedShort(offset);
68-
offset += cipherSuitesLength + 2;
68+
if (endOffset - offset >= 2) {
69+
final int cipherSuitesLength = in.getUnsignedShort(offset);
70+
offset += cipherSuitesLength + 2;
6971

70-
final int compressionMethodLength = in.getUnsignedByte(offset);
71-
offset += compressionMethodLength + 1;
72+
if (endOffset - offset >= 1) {
73+
final int compressionMethodLength = in.getUnsignedByte(offset);
74+
offset += compressionMethodLength + 1;
7275

73-
final int extensionsLength = in.getUnsignedShort(offset);
74-
offset += 2;
75-
final int extensionsLimit = offset + extensionsLength;
76-
77-
// Extensions should never exceed the record boundary.
78-
if (extensionsLimit <= endOffset) {
79-
while (extensionsLimit - offset >= 4) {
80-
final int extensionType = in.getUnsignedShort(offset);
81-
offset += 2;
82-
83-
final int extensionLength = in.getUnsignedShort(offset);
84-
offset += 2;
85-
86-
if (extensionsLimit - offset < extensionLength) {
87-
break;
88-
}
89-
90-
// SNI
91-
// See https://tools.ietf.org/html/rfc6066#page-6
92-
if (extensionType == 0) {
76+
if (endOffset - offset >= 2) {
77+
final int extensionsLength = in.getUnsignedShort(offset);
9378
offset += 2;
94-
if (extensionsLimit - offset < 3) {
95-
break;
96-
}
97-
98-
final int serverNameType = in.getUnsignedByte(offset);
99-
offset++;
100-
101-
if (serverNameType == 0) {
102-
final int serverNameLength = in.getUnsignedShort(offset);
103-
offset += 2;
104-
105-
if (extensionsLimit - offset < serverNameLength) {
106-
break;
79+
final int extensionsLimit = offset + extensionsLength;
80+
81+
// Extensions should never exceed the record boundary.
82+
if (extensionsLimit <= endOffset) {
83+
while (extensionsLimit - offset >= 4) {
84+
final int extensionType = in.getUnsignedShort(offset);
85+
offset += 2;
86+
87+
final int extensionLength = in.getUnsignedShort(offset);
88+
offset += 2;
89+
90+
if (extensionsLimit - offset < extensionLength) {
91+
break;
92+
}
93+
94+
// SNI
95+
// See https://tools.ietf.org/html/rfc6066#page-6
96+
if (extensionType == 0) {
97+
offset += 2;
98+
if (extensionsLimit - offset < 3) {
99+
break;
100+
}
101+
102+
final int serverNameType = in.getUnsignedByte(offset);
103+
offset++;
104+
105+
if (serverNameType == 0) {
106+
final int serverNameLength = in.getUnsignedShort(offset);
107+
offset += 2;
108+
109+
if (extensionsLimit - offset < serverNameLength) {
110+
break;
111+
}
112+
113+
final String hostname = in.toString(
114+
offset, serverNameLength, CharsetUtil.US_ASCII);
115+
return hostname.toLowerCase(Locale.US);
116+
} else {
117+
// invalid enum value
118+
break;
119+
}
120+
}
121+
122+
offset += extensionLength;
107123
}
108-
109-
final String hostname = in.toString(offset, serverNameLength, CharsetUtil.US_ASCII);
110-
return hostname.toLowerCase(Locale.US);
111-
} else {
112-
// invalid enum value
113-
break;
114124
}
115125
}
116-
117-
offset += extensionLength;
118126
}
119127
}
120128
}
Collapse file

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

Copy file name to clipboard
+109Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2026 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.handler.ssl;
17+
18+
import io.netty.buffer.ByteBuf;
19+
import io.netty.buffer.Unpooled;
20+
import io.netty.util.CharsetUtil;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import static org.junit.jupiter.api.Assertions.assertNull;
25+
26+
public class AbstractSniHandlerTest {
27+
28+
@Test
29+
public void testExtractSniHostnameParsesWellFormedClientHello() {
30+
ByteBuf buffer = validClientHelloBody("netty.io");
31+
try {
32+
assertEquals("netty.io", AbstractSniHandler.extractSniHostname(buffer));
33+
} finally {
34+
buffer.release();
35+
}
36+
}
37+
38+
// See https://github.com/netty/netty security report: a ClientHello whose SessionID length
39+
// field is attacker-controlled and points past the end of the record used to make the
40+
// subsequent getUnsignedShort(...) call for cipher_suites throw an IndexOutOfBoundsException
41+
// instead of extractSniHostname() gracefully returning null.
42+
@Test
43+
public void testExtractSniHostnameDoesNotThrowOnOversizedSessionIdLength() {
44+
ByteBuf buffer = Unpooled.buffer();
45+
try {
46+
buffer.writeZero(34); // client_version (2) + random (32)
47+
buffer.writeByte(0xFF); // SessionID length: far larger than the remaining record
48+
buffer.writeZero(5); // trailing bytes, mirroring a minimal non-fragmented record
49+
50+
assertNull(AbstractSniHandler.extractSniHostname(buffer));
51+
} finally {
52+
buffer.release();
53+
}
54+
}
55+
56+
// Same issue as above but for the cipher_suites length field feeding the unguarded read of
57+
// compression_methods length.
58+
@Test
59+
public void testExtractSniHostnameDoesNotThrowOnOversizedCipherSuitesLength() {
60+
ByteBuf buffer = Unpooled.buffer();
61+
try {
62+
buffer.writeZero(34); // client_version (2) + random (32)
63+
buffer.writeByte(0); // SessionID length
64+
buffer.writeShort(0xFFFF); // cipher_suites length: far larger than the remaining record
65+
66+
assertNull(AbstractSniHandler.extractSniHostname(buffer));
67+
} finally {
68+
buffer.release();
69+
}
70+
}
71+
72+
// Same issue as above but for the compression_methods length field feeding the unguarded read
73+
// of the extensions length.
74+
@Test
75+
public void testExtractSniHostnameDoesNotThrowOnOversizedCompressionMethodLength() {
76+
ByteBuf buffer = Unpooled.buffer();
77+
try {
78+
buffer.writeZero(34); // client_version (2) + random (32)
79+
buffer.writeByte(0); // SessionID length
80+
buffer.writeShort(0); // cipher_suites length
81+
buffer.writeByte(0xFF); // compression_methods length: far larger than the remaining record
82+
83+
assertNull(AbstractSniHandler.extractSniHostname(buffer));
84+
} finally {
85+
buffer.release();
86+
}
87+
}
88+
89+
private static ByteBuf validClientHelloBody(String hostname) {
90+
byte[] hostBytes = hostname.getBytes(CharsetUtil.US_ASCII);
91+
int serverNameListLength = 1 + 2 + hostBytes.length; // name type (1) + name length (2) + name
92+
int serverNameExtensionDataLength = 2 + serverNameListLength; // server_name_list length (2) + list
93+
int extensionsLength = 2 + 2 + serverNameExtensionDataLength; // ext type (2) + ext length (2) + data
94+
95+
ByteBuf buffer = Unpooled.buffer();
96+
buffer.writeZero(34); // client_version (2) + random (32)
97+
buffer.writeByte(0); // SessionID length
98+
buffer.writeShort(0); // cipher_suites length
99+
buffer.writeByte(0); // compression_methods length
100+
buffer.writeShort(extensionsLength);
101+
buffer.writeShort(0); // extension type: server_name
102+
buffer.writeShort(serverNameExtensionDataLength);
103+
buffer.writeShort(serverNameListLength);
104+
buffer.writeByte(0); // server name type: host_name
105+
buffer.writeShort(hostBytes.length);
106+
buffer.writeBytes(hostBytes);
107+
return buffer;
108+
}
109+
}
Collapse file

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

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty/handler/ssl/SniHandlerTest.java
+82Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,88 @@ public void testFallbackToDefaultContext(SslProvider provider) throws Exception
354354
}
355355
}
356356

357+
@ParameterizedTest(name = "{index}: sslProvider={0}")
358+
@MethodSource("data")
359+
public void testMalformedClientHelloWithOversizedSessionIdLengthFallsBackToDefaultContext(SslProvider provider)
360+
throws Exception {
361+
SslContext nettyContext = makeSslContext(provider, false);
362+
SslContext secureContext = makeSslContext(provider, false);
363+
364+
try {
365+
DomainNameMapping<SslContext> mapping = new DomainNameMappingBuilder<SslContext>(nettyContext)
366+
.add("secure.example", secureContext)
367+
.build();
368+
369+
// decode() swallows any exception thrown while resolving the SslContext for a record and
370+
// silently falls back to the default context (see testFallbackToDefaultContext above), so an
371+
// IndexOutOfBoundsException thrown while SniHandler parses the SNI extension would never be
372+
// observable from the pipeline's perspective. Intercept the internal ByteBuf-based lookup()
373+
// call -- where extractSniHostname() actually runs -- to assert it completes without throwing.
374+
final AtomicReference<Throwable> clientHelloLookupFailure = new AtomicReference<Throwable>();
375+
SniHandler handler = new SniHandler(mapping) {
376+
@Override
377+
protected Future<SslContext> lookup(ChannelHandlerContext ctx, ByteBuf clientHello)
378+
throws Exception {
379+
try {
380+
return super.lookup(ctx, clientHello);
381+
} catch (Exception e) {
382+
clientHelloLookupFailure.compareAndSet(null, e);
383+
throw e;
384+
}
385+
}
386+
};
387+
EmbeddedChannel ch = new EmbeddedChannel(handler);
388+
try {
389+
// A single, non-fragmented ClientHello record whose SessionID length field (0xFF)
390+
// pushes the offset for the subsequent cipher_suites/compression_methods/extensions
391+
// reads past the end of the record. This used to throw an IndexOutOfBoundsException
392+
// while SniHandler was still parsing the SNI extension, instead of gracefully falling
393+
// back to the default SslContext.
394+
ByteBuf buffer = ch.alloc().buffer();
395+
buffer.writeByte(0x16); // Content Type: Handshake
396+
buffer.writeShort(0x0303); // TLS 1.2
397+
buffer.writeShort(44); // Record length: 4 (handshake header) + 40 (body)
398+
buffer.writeByte(0x01); // Handshake Type: ClientHello
399+
buffer.writeMedium(40); // Handshake length
400+
buffer.writeZero(34); // client_version (2) + random (32)
401+
buffer.writeByte(0xFF); // SessionID length -- attacker controlled, way too big
402+
buffer.writeZero(5); // padding so the body is exactly 40 bytes
403+
404+
try {
405+
// Once SniHandler resolves the (default) SslContext it hands the record off to a
406+
// real SslHandler, which may legitimately reject it as it isn't a well-formed
407+
// ClientHello. That failure is unrelated to the bug under test and is asserted
408+
// against separately via clientHelloLookupFailure.
409+
ch.writeInbound(buffer);
410+
} catch (Exception e) {
411+
// expected: the garbage ClientHello isn't valid enough for a real SSLEngine handshake
412+
}
413+
414+
assertNull(clientHelloLookupFailure.get(), "extractSniHostname() must not throw: "
415+
+ clientHelloLookupFailure.get());
416+
417+
ch.close();
418+
419+
// Discard any outbound alert bytes produced while shutting down the SSLEngine.
420+
for (;;) {
421+
ByteBuf buf = ch.readOutbound();
422+
if (buf == null) {
423+
break;
424+
}
425+
buf.release();
426+
}
427+
428+
assertFalse(ch.finish());
429+
assertNull(handler.hostname());
430+
assertEquals(nettyContext, handler.sslContext());
431+
} finally {
432+
ch.finishAndReleaseAll();
433+
}
434+
} finally {
435+
releaseAll(secureContext, nettyContext);
436+
}
437+
}
438+
357439
@ParameterizedTest(name = "{index}: sslProvider={0}")
358440
@MethodSource("data")
359441
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — 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.