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 fixes the crash by checking whether the parseCIDR method returned
an error.
  • Loading branch information
rodrigopv committed Sep 7, 2023
1 parent e7e0007 commit 4e2816d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
16 changes: 10 additions & 6 deletions pkg/packet/bgp/bgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9604,26 +9604,30 @@ func GetRouteFamily(name string) (RouteFamily, error) {
func NewPrefixFromRouteFamily(afi uint16, safi uint8, prefixStr ...string) (prefix AddrPrefixInterface, err error) {
family := AfiSafiToRouteFamily(afi, safi)

f := func(s string) AddrPrefixInterface {
addr, net, _ := net.ParseCIDR(s)
f := func(s string) (AddrPrefixInterface, error) {
addr, net, err := net.ParseCIDR(s)
if err != nil {
fmt.Printf("Invalid CIDR: %v", s)
return nil, err
}
len, _ := net.Mask.Size()
switch family {
case RF_IPv4_UC, RF_IPv4_MC:
return NewIPAddrPrefix(uint8(len), addr.String())
return NewIPAddrPrefix(uint8(len), addr.String()), nil
}
return NewIPv6AddrPrefix(uint8(len), addr.String())
return NewIPv6AddrPrefix(uint8(len), addr.String()), nil
}

switch family {
case RF_IPv4_UC, RF_IPv4_MC:
if len(prefixStr) > 0 {
prefix = f(prefixStr[0])
prefix, err = f(prefixStr[0])
} else {
prefix = NewIPAddrPrefix(0, "")
}
case RF_IPv6_UC, RF_IPv6_MC:
if len(prefixStr) > 0 {
prefix = f(prefixStr[0])
prefix, err = f(prefixStr[0])
} else {
prefix = NewIPv6AddrPrefix(0, "")
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/packet/bgp/bgp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,33 @@ 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)

var tests = []struct {
inPrefix string
routeFamily RouteFamily
want AddrPrefixInterface
err bool
}{
{"129.6.128/22", RF_IPv4_UC, nil, true},
{"foo", RF_IPv4_UC, nil, true},
{"3343:faba:3903:128::::/63", RF_IPv6_UC, nil, true},
{"foo", RF_IPv6_UC, nil, true},
}

for _, test := range tests {
afi, safi := RouteFamilyToAfiSafi(RF_IPv4_UC)
p, err := NewPrefixFromRouteFamily(afi, safi, test.inPrefix)
if test.err {
assert.Error(err)
} else {
assert.Equal(test.want, p)
}
}

}

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

0 comments on commit 4e2816d

Please sign in to comment.