
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.
The message envelope
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.
The UPDATE body
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.
Three places the parser can be wrong
A parser of this shape has three distinct jobs, and each is a place a bug can
live:
- Framing — find the attribute boundaries from the length fields, without
reading past the message. - Semantics — decide what each attribute means: is this AS_PATH
well-formed, is this AS4_PATH consistent, is this attribute allowed for this
address family. - Error strategy — when one of the above fails, what happens next? A
NOTIFICATION, a treat-as-withdraw, a dropped attribute — or a panic.
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's path through this
GoBGP splits the work across layers:
pkg/packet/bgpdecodes messages and attributes from the wire.pkg/server/fsm.gois the session state machine. Its
recvMessageloopreceives parsed messages and drives the session.internal/pkg/tableholds RIB logic. It is where AS4_PATH is merged into
AS_PATH for 4-byte AS support (UpdatePathAttrs4ByteAs).
Two of this series' defects live at the boundary between those layers:
- #3308 panics inside
UpdatePathAttrs4ByteAswhen AS4_PATH precedes
AS_PATH and carries a one-byte value: the merge deletes an attribute, the
slice shifts, and a stale index is used. - CVE-2026-41642 panics in the FSM loop when the parsing layer has already
flagged an attribute error but the loop continues and dereferences the body
of the rejected message — a nil pointer.
Neither 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.
The design question
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.

Comments (0)