fix: call validatePathAttributeFlags after parsing and setting path attribute length #2731
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I recently started using gobgp package libraries to parse BMP messages from a third party source (BIRD). BIRD has a bug whereby the BMP messages it emits do not have the transitive flag set on well known path attributes on route monitoring messages. GoBGP correctly checks for this condition here. There is a subtle bug that shows up when parsing a BGP update message that fails this check which is that
PathAttribute.DecodeFromBytes()
exits early before it sets thePathAttribute.Length
.Highlighting this with an example this is a byte slice from an update pack after gobgp has stripped out everything from the packet byte slice leaving Path Attributes and NLRI:
Byte slice for the packet in the picture above looks like this (path attributes and NLRI is all that remains):
0 1 1 0
at the beginning of the slice is the origin attribute.0
are the flags which is invalid given the transitive flag should be set but it is not. As a result of this in the current gobgp master branch we return an error but this does not exit the loop processing all path attributes (the function waits for the end and returns the strongest error. Instead we continue and slice the byte slice by theLen()
function here:gobgp/pkg/packet/bgp/bgp.go
Line 14358 in 1b975be
But if the
Length
attribute is not set we end up slicing by3 + int(p.Length)
(3 + 0) in my case instead of 4 which is incorrect:gobgp/pkg/packet/bgp/bgp.go
Lines 10009 to 10014 in 1b975be
This means we cannot decode the next attribute because we have sliced incorrectly. The next attribute is unknown
BGPAttrType(64)
as the slice now looks like this:In the above it thinks the flags for the next attribute is actually the type field
But should instead be:
This is easy to resolve by moving the path attribute flag validations a few lines down after we have recorded the length of a path attribute.