A BGP message is a length-prefixed container of length-prefixed containers, and every length is chosen by the sender. The flags byte decides well-known from optional; the type code decides what the parser does next. Three jobs — framing, semantics, error strategy — and the third is where this bug lives.

A BGP message is a length-prefixed container of length-prefixed containers.
Every one of those lengths is chosen by the peer. Parsing BGP is the job of
trusting the structure while never trusting the author.
Every BGP message begins with the same 19-byte header:
Marker 16 bytes all ones (or authentication data)
Length 2 bytes total message length, 19 to 4096
Type 1 byte OPEN=1 UPDATE=2 NOTIFICATION=3
KEEPALIVE=4 ROUTE-REFRESH=5
The length field bounds the message. The type field selects the parser. An
UPDATE is where the variable structure lives.
An UPDATE carries three things:
Withdrawn Routes Length 2 bytes
Withdrawn Routes variable
Total Path Attribute Len 2 bytes
Path Attributes variable
NLRI variable (the announced prefixes)
The path attributes are a list. Each attribute is:
Flags 1 byte bit 7 Optional, bit 6 Transitive,
bit 5 Partial, bit 4 Extended Length
Type 1 byte
Length 1 or 2 bytes (2 when Extended Length is set)
Value Length bytes
The flags byte is where the protocol's semantics live. If the Optional bit
(0x80) is clear, the attribute is Well-known: every BGP speaker is
supposed to recognise it. If the bit is set, the attribute is optional and a
speaker may ignore one it does not implement.
RFC 4271 defines the well-known set — ORIGIN (1), AS_PATH (2), NEXT_HOP (3),
LOCAL_PREF (5), ATOMIC_AGGREGATE (6) — and the rules for unknown attributes.
For an attribute with the Optional bit clear whose Type Code a speaker does not
recognise, the required behaviour is a NOTIFICATION with the appropriate
error subcode. The session is supposed to fail cleanly. That is the protocol's
own safety valve: two speakers with mismatched capabilities should stop talking,
not exchange routes they cannot interpret.
A parser of this shape has three distinct jobs, and each is a place a bug can
live:
The first two are conventional parser work, and Go's slice bounds checks catch
many framing mistakes by refusing the read. The third is the one that turns a
bug into an outage, because Go's default for an unrecovered panic is to end the
process.
GoBGP splits the work across layers:
pkg/packet/bgp decodes messages and attributes from the wire.pkg/server/fsm.go is the session state machine. ItsrecvMessageloop receives parsed messages and drives the session.internal/pkg/table holds RIB logic. It is where AS4_PATH is merged intoUpdatePathAttrs4ByteAs).Two of this series' defects live at the boundary between those layers:
UpdatePathAttrs4ByteAs when AS4_PATH precedesNeither is a failure to understand BGP. Both are failures of the error
strategy at a layer boundary: one layer knows the message is invalid, the
next layer does not get told, and the code proceeds on an assumption that is
no longer true.
A network daemon's parser has a contract stricter than a library's. It is fed
by parties it does not control, at a rate it does not choose, over a session
that is expected to stay up for years. "This should never happen" is not a
valid assumption; it is a description of the input.
The next part climbs the ladder of smaller failures that shows how often the
assumption was wrong.

Next: Part 6 — the parser that panics: four smaller failures on the way to
the CVE.