From 3dc23d3075f13f2976afd255ef2fd9b1410acc77 Mon Sep 17 00:00:00 2001 From: FUJITA Tomonori Date: Sat, 14 Jul 2018 19:45:35 +0900 Subject: pkg/packet/bgp: fix ParsePmsiTunnel() and NewPathAttributePmsiTunnel() crash Once we create an object, we should not mutate it. This fixes the following in a different way. https://github.com/osrg/gobgp/pull/1778 Signed-off-by: FUJITA Tomonori --- pkg/packet/bgp/bgp.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/pkg/packet/bgp/bgp.go b/pkg/packet/bgp/bgp.go index 01251167..1be42ec5 100644 --- a/pkg/packet/bgp/bgp.go +++ b/pkg/packet/bgp/bgp.go @@ -8443,6 +8443,9 @@ func (p *PathAttributePmsiTunnel) MarshalJSON() ([]byte, error) { } func NewPathAttributePmsiTunnel(typ PmsiTunnelType, isLeafInfoRequired bool, label uint32, id PmsiTunnelIDInterface) *PathAttributePmsiTunnel { + if id == nil { + return nil + } // Flags(1) + TunnelType(1) + Label(3) + TunnelID(variable) l := 5 + id.Len() t := BGP_ATTR_TYPE_PMSI_TUNNEL @@ -8466,22 +8469,22 @@ func ParsePmsiTunnel(args []string) (*PathAttributePmsiTunnel, error) { return nil, fmt.Errorf("invalid pmsi tunnel arguments: %s", args) } - pmsi := NewPathAttributePmsiTunnel(0, false, 0, nil) - + var tunnelType PmsiTunnelType + var isLeafInfoRequired bool switch args[0] { case "ingress-repl": - pmsi.TunnelType = PMSI_TUNNEL_TYPE_INGRESS_REPL + tunnelType = PMSI_TUNNEL_TYPE_INGRESS_REPL default: typ, err := strconv.ParseUint(args[0], 10, 8) if err != nil { return nil, fmt.Errorf("invalid pmsi tunnel type: %s", args[0]) } - pmsi.TunnelType = PmsiTunnelType(typ) + tunnelType = PmsiTunnelType(typ) } indx := 1 if args[indx] == "leaf-info-required" { - pmsi.IsLeafInfoRequired = true + isLeafInfoRequired = true indx++ } @@ -8489,21 +8492,21 @@ func ParsePmsiTunnel(args []string) (*PathAttributePmsiTunnel, error) { if err != nil { return nil, fmt.Errorf("invalid pmsi tunnel label: %s", args[indx]) } - pmsi.Label = uint32(label) indx++ - switch pmsi.TunnelType { + var id PmsiTunnelIDInterface + switch tunnelType { case PMSI_TUNNEL_TYPE_INGRESS_REPL: ip := net.ParseIP(args[indx]) if ip == nil { return nil, fmt.Errorf("invalid pmsi tunnel identifier: %s", args[indx]) } - pmsi.TunnelID = &IngressReplTunnelID{Value: ip} + id = &IngressReplTunnelID{Value: ip} default: - pmsi.TunnelID = &DefaultPmsiTunnelID{Value: []byte(args[indx])} + id = &DefaultPmsiTunnelID{Value: []byte(args[indx])} } - return pmsi, nil + return NewPathAttributePmsiTunnel(tunnelType, isLeafInfoRequired, uint32(label), id), nil } type PathAttributeIP6ExtendedCommunities struct { -- cgit v1.2.3