Waterfall/Waterfall-Proxy-Patches/0035-Safe-bytebuf-release.patch
2022-10-31 17:32:47 +01:00

225 lines
11 KiB
Diff

From fcbd2374afc15c2fe67dd158b6fd2ae68487ed6a Mon Sep 17 00:00:00 2001
From: LinsaFTW <25271111+linsaftw@users.noreply.github.com>
Date: Mon, 31 Oct 2022 17:00:58 +0100
Subject: [PATCH] Safe bytebuf release
diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java
index 9a7bf9b5..b43ebc8d 100644
--- a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java
+++ b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java
@@ -31,33 +31,34 @@ public class MinecraftDecoder extends MessageToMessageDecoder<ByteBuf>
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception
{
- // See Varint21FrameDecoder for the general reasoning. We add this here as ByteToMessageDecoder#handlerRemoved()
- // will fire any cumulated data through the pipeline, so we want to try and stop it here.
- if ( !ctx.channel().isActive() || !in.isReadable() ) // FlameCord - Check if connection is readable
- {
- return;
- }
+ // FlameCord - Safe bytebuf release
+ Object packetTypeInfo = null;
+ ByteBuf slice = null;
+ try {
+ // See Varint21FrameDecoder for the general reasoning. We add this here as ByteToMessageDecoder#handlerRemoved()
+ // will fire any cumulated data through the pipeline, so we want to try and stop it here.
+ if ( !ctx.channel().isActive() || !in.isReadable() ) // FlameCord - Check if connection is readable
+ {
+ return;
+ }
- Protocol.DirectionData prot = ( server ) ? protocol.TO_SERVER : protocol.TO_CLIENT;
-
- // FlameCord - Check size before decoding
- if (prot == protocol.TO_SERVER) {
- final int readableBytes = in.readableBytes();
- final int capacity = in.capacity();
-
- if (readableBytes > 2097152) {
- throw new FastDecoderException("Error decoding packet with too many readableBytes: " + readableBytes);
- } else if (capacity > 2097152) {
- throw new FastDecoderException("Error decoding packet with too big capacity: " + capacity);
+ Protocol.DirectionData prot = ( server ) ? protocol.TO_SERVER : protocol.TO_CLIENT;
+
+ // FlameCord - Check size before decoding
+ if (prot == protocol.TO_SERVER) {
+ final int readableBytes = in.readableBytes();
+ final int capacity = in.capacity();
+
+ if (readableBytes > 2097152) {
+ throw new FastDecoderException("Error decoding packet with too many readableBytes: " + readableBytes);
+ } else if (capacity > 2097152) {
+ throw new FastDecoderException("Error decoding packet with too big capacity: " + capacity);
+ }
}
- }
- // FlameCord - Duplicate buf instead of Copy
- ByteBuf slice = in.duplicate(); // Can't slice this one due to EntityMap :(
+ // FlameCord - Duplicate buf instead of Copy
+ slice = in.duplicate(); // Can't slice this one due to EntityMap :(
- Object packetTypeInfo = null;
- try
- {
// Waterfall start
if (in.readableBytes() == 0 && !server) {
return;
diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java
index a63e67f1..4293e2b4 100644
--- a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java
+++ b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java
@@ -286,10 +286,15 @@ public class ServerConnector extends PacketHandler
user.unsafe().sendPacket( new PluginMessage( "MC|Brand", brandString.getBytes( StandardCharsets.UTF_8 ), handshakeHandler.isServerForge() ) );
} else
{
- ByteBuf brand = ByteBufAllocator.DEFAULT.heapBuffer();
- DefinedPacket.writeString( bungee.getName() + " (" + bungee.getVersion() + ")", brand );
- user.unsafe().sendPacket( new PluginMessage( user.getPendingConnection().getVersion() >= ProtocolConstants.MINECRAFT_1_13 ? "minecraft:brand" : "MC|Brand", brand, handshakeHandler.isServerForge() ) );
- brand.release();
+ // FlameCord - Safe bytebuf release
+ ByteBuf brand = null;
+ try {
+ brand = ByteBufAllocator.DEFAULT.heapBuffer();
+ DefinedPacket.writeString( bungee.getName() + " (" + bungee.getVersion() + ")", brand );
+ user.unsafe().sendPacket( new PluginMessage( user.getPendingConnection().getVersion() >= ProtocolConstants.MINECRAFT_1_13 ? "minecraft:brand" : "MC|Brand", brand, handshakeHandler.isServerForge() ) );
+ } finally {
+ if (brand != null) brand.release();
+ }
}
// FlameCord end - 1.7.x support
}
diff --git a/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java
index d662c976..fbbaa1b2 100644
--- a/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java
+++ b/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java
@@ -291,13 +291,24 @@ public class DownstreamBridge extends PacketHandler
{
try
{
- ByteBuf brand = Unpooled.wrappedBuffer(pluginMessage.getData());
- String serverBrand = DefinedPacket.readString(brand);
- brand.release();
- brand = ByteBufAllocator.DEFAULT.heapBuffer();
- DefinedPacket.writeString(bungee.getName() + " <- " + serverBrand, brand ); // Waterfall
- pluginMessage.setData(brand);
- brand.release();
+ // FlameCord - Safe bytebuf release
+ ByteBuf brand = null;
+ String serverBrand;
+ try {
+ brand = Unpooled.wrappedBuffer(pluginMessage.getData());
+ serverBrand = DefinedPacket.readString(brand);
+ } finally {
+ if (brand != null) brand.release();
+ }
+
+ // FlameCord - Safe bytebuf release
+ try {
+ brand = ByteBufAllocator.DEFAULT.heapBuffer();
+ DefinedPacket.writeString(bungee.getName() + " <- " + serverBrand, brand ); // Waterfall
+ pluginMessage.setData(brand);
+ } finally {
+ if (brand != null) brand.release();
+ }
} catch (Exception ProtocolHacksSuck)
{
return;
diff --git a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java
index abca28bd..2fd37c2d 100644
--- a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java
+++ b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java
@@ -84,46 +84,44 @@ public class HandlerBoss extends ChannelInboundHandlerAdapter
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
{
- // FlameCord - Return if channel isn't active
- if (!ctx.channel().isActive()) {
- if (msg instanceof PacketWrapper) {
- ((PacketWrapper) msg).trySingleRelease();
+ // FlameCord - Safe bytebuf release
+ try
+ {
+ // FlameCord - Return if channel isn't active
+ if (!ctx.channel().isActive()) {
+ return;
}
- return;
- }
-
- if ( msg instanceof HAProxyMessage )
- {
- HAProxyMessage proxy = (HAProxyMessage) msg;
- try
+ if ( msg instanceof HAProxyMessage )
{
- if ( proxy.sourceAddress() != null )
+ HAProxyMessage proxy = (HAProxyMessage) msg;
+ try
{
- InetSocketAddress newAddress = new InetSocketAddress( proxy.sourceAddress(), proxy.sourcePort() );
+ if ( proxy.sourceAddress() != null )
+ {
+ InetSocketAddress newAddress = new InetSocketAddress( proxy.sourceAddress(), proxy.sourcePort() );
- // FlameCord - Option to log haproxy
- if ( FlameCord.getInstance().getFlameCordConfiguration().isLoggerHaProxy() )
- ProxyServer.getInstance().getLogger().log( Level.FINE, "Set remote address via PROXY {0} -> {1}", new Object[]
- {
- channel.getRemoteAddress(), newAddress
- } );
+ // FlameCord - Option to log haproxy
+ if ( FlameCord.getInstance().getFlameCordConfiguration().isLoggerHaProxy() )
+ ProxyServer.getInstance().getLogger().log( Level.FINE, "Set remote address via PROXY {0} -> {1}", new Object[]
+ {
+ channel.getRemoteAddress(), newAddress
+ } );
- channel.setRemoteAddress( newAddress );
+ channel.setRemoteAddress( newAddress );
+ }
+ } finally
+ {
+ proxy.release();
}
- } finally
- {
- proxy.release();
+ return;
}
- return;
- }
- if ( handler != null )
- {
PacketWrapper packet = (PacketWrapper) msg;
- boolean sendPacket = handler.shouldHandle( packet );
- try
+
+ if ( handler != null )
{
+ boolean sendPacket = handler.shouldHandle( packet );
if ( sendPacket && packet.packet != null )
{
try
@@ -138,9 +136,11 @@ public class HandlerBoss extends ChannelInboundHandlerAdapter
{
handler.handle( packet );
}
- } finally
- {
- packet.trySingleRelease();
+ }
+ } finally
+ {
+ if (msg instanceof PacketWrapper) {
+ ((PacketWrapper) msg).trySingleRelease();
}
}
}
--
2.37.3.windows.1