You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2022/05/24 09:19:36 UTC
[shardingsphere] branch master updated: Fix packet length overflow when decoding MySQL packet (#17898)
This is an automated email from the ASF dual-hosted git repository.
jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 528214d4a07 Fix packet length overflow when decoding MySQL packet (#17898)
528214d4a07 is described below
commit 528214d4a07a0bd515843e63d30e580f40131144
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Tue May 24 17:19:28 2022 +0800
Fix packet length overflow when decoding MySQL packet (#17898)
---
.../protocol/mysql/codec/MySQLPacketCodecEngine.java | 2 +-
.../mysql/codec/MySQLPacketCodecEngineTest.java | 20 +++++++++++++++++---
2 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngine.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngine.java
index 7f7644884e7..33b09cf8689 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngine.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngine.java
@@ -45,7 +45,7 @@ public final class MySQLPacketCodecEngine implements DatabasePacketCodecEngine<M
@Override
public void decode(final ChannelHandlerContext context, final ByteBuf in, final List<Object> out) {
- int payloadLength = in.markReaderIndex().readMediumLE();
+ int payloadLength = in.markReaderIndex().readUnsignedMediumLE();
int remainPayloadLength = SEQUENCE_LENGTH + payloadLength;
if (in.readableBytes() < remainPayloadLength) {
in.resetReaderIndex();
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngineTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngineTest.java
index a9d6c10bc42..44b7598abc9 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngineTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngineTest.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.db.protocol.mysql.codec;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;
import org.apache.shardingsphere.db.protocol.mysql.packet.MySQLPacket;
@@ -31,6 +32,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
@@ -72,7 +74,7 @@ public final class MySQLPacketCodecEngineTest {
@Test
public void assertDecode() {
when(byteBuf.markReaderIndex()).thenReturn(byteBuf);
- when(byteBuf.readMediumLE()).thenReturn(50);
+ when(byteBuf.readUnsignedMediumLE()).thenReturn(50);
when(byteBuf.readableBytes()).thenReturn(51);
when(byteBuf.readRetainedSlice(51)).thenReturn(byteBuf);
List<Object> out = new LinkedList<>();
@@ -84,7 +86,7 @@ public final class MySQLPacketCodecEngineTest {
public void assertDecodeWithEmptyPacket() {
when(byteBuf.markReaderIndex()).thenReturn(byteBuf);
when(byteBuf.readableBytes()).thenReturn(1);
- when(byteBuf.readMediumLE()).thenReturn(0);
+ when(byteBuf.readUnsignedMediumLE()).thenReturn(0);
List<Object> out = new LinkedList<>();
new MySQLPacketCodecEngine().decode(context, byteBuf, out);
assertThat(out.size(), is(1));
@@ -93,12 +95,24 @@ public final class MySQLPacketCodecEngineTest {
@Test
public void assertDecodeWithStickyPacket() {
when(byteBuf.markReaderIndex()).thenReturn(byteBuf);
- when(byteBuf.readMediumLE()).thenReturn(50);
+ when(byteBuf.readUnsignedMediumLE()).thenReturn(50);
List<Object> out = new LinkedList<>();
new MySQLPacketCodecEngine().decode(context, byteBuf, out);
assertTrue(out.isEmpty());
}
+ @Test
+ public void assertDecodeMaxLengthPacket() {
+ byte[] packetData = new byte[(1 << 24) + 4];
+ packetData[0] = packetData[1] = packetData[2] = (byte) 0xff;
+ packetData[3] = (byte) 0;
+ ByteBuf input = Unpooled.wrappedBuffer(packetData);
+ List<Object> actual = new ArrayList<>(1);
+ new MySQLPacketCodecEngine().decode(null, input, actual);
+ assertThat(actual.size(), is(1));
+ assertThat(((ByteBuf) actual.get(0)).readableBytes(), is(1 << 24));
+ }
+
@Test
public void assertEncode() {
when(byteBuf.writeInt(anyInt())).thenReturn(byteBuf);