13
0
geforkt von Mirrors/Velocity

Always ensure we don't pass a heap ByteBuf to the natives.

Dieser Commit ist enthalten in:
Andrew Steinborn 2018-12-30 04:53:47 -05:00
Ursprung 1c50922cba
Commit 095a478440
5 geänderte Dateien mit 37 neuen und 20 gelöschten Zeilen

Datei anzeigen

@ -25,7 +25,7 @@ public class MoreByteBufUtils {
return buf.retain(); return buf.retain();
} }
// It's not, so we must make a memory copy. // It's not, so we must make a direct copy.
ByteBuf newBuf = alloc.directBuffer(); ByteBuf newBuf = alloc.directBuffer();
newBuf.writeBytes(buf); newBuf.writeBytes(buf);
return newBuf; return newBuf;

Datei anzeigen

@ -2,6 +2,7 @@ package com.velocitypowered.proxy.protocol.netty;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.velocitypowered.natives.encryption.VelocityCipher; import com.velocitypowered.natives.encryption.VelocityCipher;
import com.velocitypowered.natives.util.MoreByteBufUtils;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.ByteToMessageDecoder;
@ -17,7 +18,12 @@ public class MinecraftCipherDecoder extends ByteToMessageDecoder {
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
out.add(cipher.process(ctx, in)); ByteBuf compatible = MoreByteBufUtils.ensureCompatible(ctx.alloc(), cipher, in);
try {
out.add(cipher.process(ctx, compatible));
} finally {
compatible.release();
}
} }
@Override @Override

Datei anzeigen

@ -2,11 +2,13 @@ package com.velocitypowered.proxy.protocol.netty;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.velocitypowered.natives.encryption.VelocityCipher; import com.velocitypowered.natives.encryption.VelocityCipher;
import com.velocitypowered.natives.util.MoreByteBufUtils;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToMessageEncoder;
import java.util.List;
public class MinecraftCipherEncoder extends MessageToByteEncoder<ByteBuf> { public class MinecraftCipherEncoder extends MessageToMessageEncoder<ByteBuf> {
private final VelocityCipher cipher; private final VelocityCipher cipher;
@ -15,14 +17,13 @@ public class MinecraftCipherEncoder extends MessageToByteEncoder<ByteBuf> {
} }
@Override @Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
cipher.process(msg, out); ByteBuf compatible = MoreByteBufUtils.ensureCompatible(ctx.alloc(), cipher, msg);
try {
out.add(cipher.process(ctx, compatible));
} finally {
compatible.release();
} }
@Override
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect)
throws Exception {
return ctx.alloc().directBuffer(msg.readableBytes());
} }
@Override @Override

Datei anzeigen

@ -3,6 +3,7 @@ package com.velocitypowered.proxy.protocol.netty;
import static com.velocitypowered.proxy.protocol.util.NettyPreconditions.checkFrame; import static com.velocitypowered.proxy.protocol.util.NettyPreconditions.checkFrame;
import com.velocitypowered.natives.compression.VelocityCompressor; import com.velocitypowered.natives.compression.VelocityCompressor;
import com.velocitypowered.natives.util.MoreByteBufUtils;
import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.ProtocolUtils;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
@ -22,22 +23,23 @@ public class MinecraftCompressDecoder extends MessageToMessageDecoder<ByteBuf> {
} }
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
int expectedUncompressedSize = ProtocolUtils.readVarInt(msg); int expectedUncompressedSize = ProtocolUtils.readVarInt(in);
if (expectedUncompressedSize == 0) { if (expectedUncompressedSize == 0) {
// Strip the now-useless uncompressed size, this message is already uncompressed. // Strip the now-useless uncompressed size, this message is already uncompressed.
out.add(msg.retainedSlice()); out.add(in.retainedSlice());
msg.skipBytes(msg.readableBytes()); in.skipBytes(in.readableBytes());
return; return;
} }
checkFrame(expectedUncompressedSize >= threshold, checkFrame(expectedUncompressedSize >= threshold,
"Uncompressed size %s is greater than threshold %s", "Uncompressed size %s is greater than threshold %s",
expectedUncompressedSize, threshold); expectedUncompressedSize, threshold);
ByteBuf uncompressed = ctx.alloc() ByteBuf compatibleIn = MoreByteBufUtils.ensureCompatible(ctx.alloc(), compressor, in);
.buffer(Math.min(expectedUncompressedSize, MAXIMUM_INITIAL_BUFFER_SIZE)); ByteBuf uncompressed = ctx.alloc().directBuffer(Math.min(expectedUncompressedSize,
MAXIMUM_INITIAL_BUFFER_SIZE));
try { try {
compressor.inflate(msg, uncompressed); compressor.inflate(compatibleIn, uncompressed);
checkFrame(expectedUncompressedSize == uncompressed.readableBytes(), checkFrame(expectedUncompressedSize == uncompressed.readableBytes(),
"Mismatched compression sizes (got %s, expected %s)", "Mismatched compression sizes (got %s, expected %s)",
uncompressed.readableBytes(), expectedUncompressedSize); uncompressed.readableBytes(), expectedUncompressedSize);
@ -45,6 +47,8 @@ public class MinecraftCompressDecoder extends MessageToMessageDecoder<ByteBuf> {
} catch (Exception e) { } catch (Exception e) {
uncompressed.release(); uncompressed.release();
throw e; throw e;
} finally {
compatibleIn.release();
} }
} }

Datei anzeigen

@ -1,6 +1,7 @@
package com.velocitypowered.proxy.protocol.netty; package com.velocitypowered.proxy.protocol.netty;
import com.velocitypowered.natives.compression.VelocityCompressor; import com.velocitypowered.natives.compression.VelocityCompressor;
import com.velocitypowered.natives.util.MoreByteBufUtils;
import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.ProtocolUtils;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
@ -25,7 +26,12 @@ public class MinecraftCompressEncoder extends MessageToByteEncoder<ByteBuf> {
out.writeBytes(msg); out.writeBytes(msg);
} else { } else {
ProtocolUtils.writeVarInt(out, uncompressed); ProtocolUtils.writeVarInt(out, uncompressed);
compressor.deflate(msg, out); ByteBuf compatibleIn = MoreByteBufUtils.ensureCompatible(ctx.alloc(), compressor, msg);
try {
compressor.deflate(compatibleIn, out);
} finally {
compatibleIn.release();
}
} }
} }