浏览代码

QUIC sniffer: Fix potential slice panic (#3406)

* QUIC sniffer: Fix potential slice panic

* Fix type err

* Update sniff.go

* Add test and fix more

* Refine
风扇滑翔翼 4 月之前
父节点
当前提交
1f2d76c066
共有 2 个文件被更改,包括 22 次插入0 次删除
  1. 4 0
      common/protocol/quic/sniff.go
  2. 18 0
      common/protocol/quic/sniff_test.go

+ 4 - 0
common/protocol/quic/sniff.go

@@ -116,6 +116,10 @@ func SniffQUIC(b []byte) (*SniffHeader, error) {
 		if err != nil {
 			return nil, errNotQuic
 		}
+		// packet is impossible to shorter than this
+		if packetLen < 4 {
+			return nil, errNotQuic
+		}
 
 		hdrLen := len(b) - int(buffer.Len())
 		if len(b) < hdrLen+int(packetLen) {

+ 18 - 0
common/protocol/quic/sniff_test.go

@@ -239,3 +239,21 @@ func TestSniffQUICComplex(t *testing.T) {
 		})
 	}
 }
+
+func TestSniffFakeQUICPacketWithInvalidPacketNumberLength(t *testing.T) {
+	pkt, err := hex.DecodeString("cb00000001081c8c6d5aeb53d54400000090709b8600000000000000000000000000000000")
+	common.Must(err)
+	_, err = quic.SniffQUIC(pkt)
+	if err == nil {
+		t.Error("failed")
+	}
+}
+
+func TestSniffFakeQUICPacketWithTooShortData(t *testing.T) {
+	pkt, err := hex.DecodeString("cb00000001081c8c6d5aeb53d54400000090709b86")
+	common.Must(err)
+	_, err = quic.SniffQUIC(pkt)
+	if err == nil {
+		t.Error("failed")
+	}
+}