bgpd: Add some defences for AS4/ENCAP handling - #21777
Conversation
Just to avoid randomly killing bgpd daemon with a malformed packet. Signed-off-by: Donatas Abraitis <donatas@opensourcerouting.org>
Adding just a guard against the memory going crazy (too much malloc()s) with legitimate TLVs, but sort of "malformed"/crafted packet. Signed-off-by: Donatas Abraitis <donatas@opensourcerouting.org>
Greptile SummaryThis PR adds two defensive hardening changes to BGP attribute parsing: it replaces a crash-inducing Confidence Score: 5/5Safe to merge — both changes are strictly defensive and follow existing error-handling patterns. No logic errors, no regressions. The assert-to-bounds-check conversion in bgp_open.c correctly handles the already-guarded case gracefully. The sub-TLV counter in bgp_attr.c uses post-increment correctly, allowing exactly 64 sub-TLVs before rejecting. Both changes are minimal and targeted. No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Receive BGP UPDATE / OPEN message] --> B{Message type}
B -->|OPEN: peek_for_as4_capability| C[Read opt_type + opt_length]
C --> D{opt_length overflows\ncapd_end > end?}
D -->|Yes — was assert crash| E[goto end: restore getp, return as4=0]
D -->|No| F[Parse capabilities\nlooking for AS4]
F --> G[Return AS4 value]
B -->|UPDATE: bgp_attr_encap| H[Read Tunnel Encap outer T/L]
H --> I{length valid?}
I -->|No| J[bgp_attr_malformed → NOTIFY]
I -->|Yes| K[Enter sub-TLV loop]
K --> L{subtlv_count++ >= 64?}
L -->|Yes — NEW guard| M[flog_err + bgp_attr_malformed → NOTIFY]
L -->|No| N[Read subtype + sublength]
N --> O{length checks OK?}
O -->|No| J
O -->|Yes| P[XCALLOC sub-TLV\nattach to attr chain]
P --> Q{more data?}
Q -->|Yes| K
Q -->|No| R[Set encap_tunneltype\nreturn success]
Reviews (1): Last reviewed commit: "bgpd: Define a maximum number of TLVs fo..." | Re-trigger Greptile |
|
@Mergifyio backport stable/10.6 stable/10.5 stable/10.4 |
✅ Backports have been createdDetails
Cherry-pick of 44f3385 has failed: To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally
Cherry-pick of 44f3385 has failed: To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally
Cherry-pick of 44f3385 has failed: To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally |
…ng_bgp_10.6 bgpd: Add some defences for AS4/ENCAP handling (backport #21777)
…ng_bgp_10.5 bgpd: Add some defences for AS4/ENCAP handling (backport #21777)
…ng_bgp_10.4 bgpd: Add some defences for AS4/ENCAP handling (backport #21777)
No description provided.