From 9c969eb7633c1a024f5fad10153683e348b28c8a Mon Sep 17 00:00:00 2001 From: Linus Mellberg Date: Wed, 12 Apr 2023 14:35:12 +0200 Subject: [PATCH] fixes #2645 Panic when adding FlowspecComponentItem with to large value --- pkg/packet/bgp/bgp.go | 7 ++----- pkg/packet/bgp/bgp_test.go | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/pkg/packet/bgp/bgp.go b/pkg/packet/bgp/bgp.go index f0610d9b2..d9fca1788 100644 --- a/pkg/packet/bgp/bgp.go +++ b/pkg/packet/bgp/bgp.go @@ -4374,13 +4374,10 @@ func NewFlowSpecComponentItem(op uint8, value uint64) *FlowSpecComponentItem { return uint32(i) } } - // return invalid order - return 4 + // Return 8 octet order + return 3 }() } - if order > 3 { - return nil - } v.Op = uint8(uint32(v.Op) | order<<4) return v } diff --git a/pkg/packet/bgp/bgp_test.go b/pkg/packet/bgp/bgp_test.go index a26e9e790..441e0911d 100644 --- a/pkg/packet/bgp/bgp_test.go +++ b/pkg/packet/bgp/bgp_test.go @@ -18,6 +18,7 @@ package bgp import ( "bytes" "encoding/binary" + "math" "net" "os" "path/filepath" @@ -443,6 +444,28 @@ func Test_FlowSpecNlri(t *testing.T) { assert.Equal(n1, n2) } +func Test_NewFlowSpecComponentItemLength(t *testing.T) { + item := NewFlowSpecComponentItem(0, 0) + assert.Equal(t, 1, item.Len()) + item = NewFlowSpecComponentItem(0, math.MaxUint8) + assert.Equal(t, 1, item.Len()) + + item = NewFlowSpecComponentItem(0, math.MaxUint8+1) + assert.Equal(t, 2, item.Len()) + item = NewFlowSpecComponentItem(0, math.MaxUint16) + assert.Equal(t, 2, item.Len()) + + item = NewFlowSpecComponentItem(0, math.MaxUint16+1) + assert.Equal(t, 4, item.Len()) + item = NewFlowSpecComponentItem(0, math.MaxUint32) + assert.Equal(t, 4, item.Len()) + + item = NewFlowSpecComponentItem(0, math.MaxUint32+1) + assert.Equal(t, 8, item.Len()) + item = NewFlowSpecComponentItem(0, math.MaxUint64) + assert.Equal(t, 8, item.Len()) +} + func Test_LinkBandwidthExtended(t *testing.T) { assert := assert.New(t) exts := make([]ExtendedCommunityInterface, 0)