protect against internal transport requests that are malformed or wrongly sending HTTP requests to the transport port that can cause OOM (though really, that OOM is harmless in this case), relates to #1955.

This commit is contained in:
Shay Banon 2012-05-25 00:43:08 +02:00
parent 2ea77782a1
commit e50fffdaeb
1 changed files with 10 additions and 0 deletions

View File

@ -25,12 +25,15 @@ import org.elasticsearch.common.io.stream.HandlesStreamInput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.monitor.jvm.JvmInfo;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.*;
import org.elasticsearch.transport.support.TransportStreams;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.*;
import org.jboss.netty.handler.codec.frame.TooLongFrameException;
import java.io.IOException;
import java.io.StreamCorruptedException;
@ -52,6 +55,8 @@ public class MessageChannelHandler extends SimpleChannelUpstreamHandler {
// from FrameDecoder
private ChannelBuffer cumulation;
private static final long NINETY_PER_HEAP_SIZE = (long) (JvmInfo.jvmInfo().mem().heapMax().bytes() * 0.9);
public MessageChannelHandler(NettyTransport transport, ESLogger logger) {
this.threadPool = transport.threadPool();
this.transportServiceAdapter = transport.transportServiceAdapter();
@ -130,6 +135,11 @@ public class MessageChannelHandler extends SimpleChannelUpstreamHandler {
if (dataLen <= 0) {
throw new StreamCorruptedException("invalid data length: " + dataLen);
}
// safety against too large frames being sent
if (dataLen > NINETY_PER_HEAP_SIZE) {
throw new TooLongFrameException(
"transport content length received [" + new ByteSizeValue(dataLen) + "] exceeded [" + new ByteSizeValue(NINETY_PER_HEAP_SIZE) + "]");
}
actualSize = dataLen + 4;
if (buffer.readableBytes() < actualSize) {