Skip to content

Commit

Permalink
Fix crash on ListPathRequest with malformed prefix
Browse files Browse the repository at this point in the history
When ListPathRequest is done by a gRPC client including a malformed prefix,
 the server would crash an invalid memory address reference.

This commit avoid the crash by checking whether the parseCIDR method returned
an error.
  • Loading branch information
rodrigopv committed Aug 5, 2023
1 parent d3a46b8 commit bdce520
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/packet/bgp/bgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9582,7 +9582,15 @@ func NewPrefixFromRouteFamily(afi uint16, safi uint8, prefixStr ...string) (pref
family := AfiSafiToRouteFamily(afi, safi)

f := func(s string) AddrPrefixInterface {
addr, net, _ := net.ParseCIDR(s)
addr, net, err := net.ParseCIDR(s)
if err != nil {
fmt.Printf("Invalid CIDR: %v", s)
switch family {
case RF_IPv4_UC, RF_IPv4_MC:
return NewIPAddrPrefix(0, "")
}
return NewIPv6AddrPrefix(0, "")
}
len, _ := net.Mask.Size()
switch family {
case RF_IPv4_UC, RF_IPv4_MC:
Expand Down
19 changes: 19 additions & 0 deletions pkg/packet/bgp/bgp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ func Test_IPAddrPrefixString(t *testing.T) {
assert.Equal(t, "3343:faba:3903:128::/63", ipv6.String())
}

func Test_MalformedPrefixLookup(t *testing.T) {
assert := assert.New(t)
afi, safi := RouteFamilyToAfiSafi(RF_IPv4_UC)
prefix, err := NewPrefixFromRouteFamily(afi, safi, "129.6.128/22")
assert.Equal(nil, err)
assert.Equal("0.0.0.0/0", prefix.String())
prefix, err = NewPrefixFromRouteFamily(afi, safi, "foo")
assert.Equal(nil, err)
assert.Equal("0.0.0.0/0", prefix.String())

afi, safi = RouteFamilyToAfiSafi(RF_IPv6_UC)
prefix, err = NewPrefixFromRouteFamily(afi, safi, "3343:faba:3903:128::::/63")
assert.Equal(nil, err)
assert.Equal("::/0", prefix.String())
prefix, err = NewPrefixFromRouteFamily(afi, safi, "foo")
assert.Equal(nil, err)
assert.Equal("::/0", prefix.String())
}

func Test_IPAddrDecode(t *testing.T) {
r := IPAddrPrefixDefault{}
b := make([]byte, 16)
Expand Down

0 comments on commit bdce520

Please sign in to comment.