Added separate exception for packet overflows to limit log output.

Attacking a server with a hacked client causes the log to print a huge amount of stacktraces. This will limit the log output to the error message.
This commit is contained in:
Zartec 2016-04-01 22:33:07 +02:00 committed by md_5
parent 41621193ec
commit d14b96d55e
3 changed files with 33 additions and 5 deletions

View File

@ -1,7 +1,6 @@
package net.md_5.bungee.protocol; package net.md_5.bungee.protocol;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -15,7 +14,10 @@ public abstract class DefinedPacket
public static void writeString(String s, ByteBuf buf) public static void writeString(String s, ByteBuf buf)
{ {
Preconditions.checkArgument( s.length() <= Short.MAX_VALUE, "Cannot send string longer than Short.MAX_VALUE (got %s characters)", s.length() ); if ( s.length() > Short.MAX_VALUE )
{
throw new OverflowPacketException( String.format( "Cannot send string longer than Short.MAX_VALUE (got %s characters)", s.length() ) );
}
byte[] b = s.getBytes( Charsets.UTF_8 ); byte[] b = s.getBytes( Charsets.UTF_8 );
writeVarInt( b.length, buf ); writeVarInt( b.length, buf );
@ -25,7 +27,10 @@ public abstract class DefinedPacket
public static String readString(ByteBuf buf) public static String readString(ByteBuf buf)
{ {
int len = readVarInt( buf ); int len = readVarInt( buf );
Preconditions.checkArgument( len <= Short.MAX_VALUE, "Cannot receive string longer than Short.MAX_VALUE (got %s characters)", len ); if ( len > Short.MAX_VALUE )
{
throw new OverflowPacketException( String.format( "Cannot receive string longer than Short.MAX_VALUE (got %s characters)", len ) );
}
byte[] b = new byte[ len ]; byte[] b = new byte[ len ];
buf.readBytes( b ); buf.readBytes( b );
@ -35,7 +40,10 @@ public abstract class DefinedPacket
public static void writeArray(byte[] b, ByteBuf buf) public static void writeArray(byte[] b, ByteBuf buf)
{ {
Preconditions.checkArgument( b.length <= Short.MAX_VALUE, "Cannot send byte array longer than Short.MAX_VALUE (got %s bytes)", b.length ); if ( b.length > Short.MAX_VALUE )
{
throw new OverflowPacketException( String.format( "Cannot send byte array longer than Short.MAX_VALUE (got %s bytes)", b.length ) );
}
writeVarInt( b.length, buf ); writeVarInt( b.length, buf );
buf.writeBytes( b ); buf.writeBytes( b );
} }
@ -48,7 +56,10 @@ public abstract class DefinedPacket
public static byte[] readArray(ByteBuf buf, int limit) public static byte[] readArray(ByteBuf buf, int limit)
{ {
int len = readVarInt( buf ); int len = readVarInt( buf );
Preconditions.checkArgument( len <= limit, "Cannot receive byte array longer than %s (got %s bytes)", limit, len ); if ( len > limit )
{
throw new OverflowPacketException( String.format( "Cannot receive byte array longer than %s (got %s bytes)", limit, len ) );
}
byte[] ret = new byte[ len ]; byte[] ret = new byte[ len ];
buf.readBytes( ret ); buf.readBytes( ret );
return ret; return ret;

View File

@ -0,0 +1,10 @@
package net.md_5.bungee.protocol;
public class OverflowPacketException extends RuntimeException
{
public OverflowPacketException(String message)
{
super( message );
}
}

View File

@ -13,6 +13,7 @@ import net.md_5.bungee.connection.CancelSendSignal;
import net.md_5.bungee.connection.InitialHandler; import net.md_5.bungee.connection.InitialHandler;
import net.md_5.bungee.connection.PingHandler; import net.md_5.bungee.connection.PingHandler;
import net.md_5.bungee.protocol.BadPacketException; import net.md_5.bungee.protocol.BadPacketException;
import net.md_5.bungee.protocol.OverflowPacketException;
/** /**
* This class is a primitive wrapper for {@link PacketHandler} instances tied to * This class is a primitive wrapper for {@link PacketHandler} instances tied to
@ -104,6 +105,12 @@ public class HandlerBoss extends ChannelInboundHandlerAdapter
{ {
handler, cause.getCause().getMessage() handler, cause.getCause().getMessage()
} ); } );
} else if ( cause instanceof DecoderException && cause.getCause() instanceof OverflowPacketException )
{
ProxyServer.getInstance().getLogger().log( Level.WARNING, "{0} - overflow in packet detected! {1}", new Object[]
{
handler, cause.getCause().getMessage()
} );
} else if ( cause instanceof IOException ) } else if ( cause instanceof IOException )
{ {
ProxyServer.getInstance().getLogger().log( Level.WARNING, "{0} - IOException: {1}", new Object[] ProxyServer.getInstance().getLogger().log( Level.WARNING, "{0} - IOException: {1}", new Object[]