Skip to content

bgpd: Add some defences for AS4/ENCAP handling - #21777

Merged
mjstapp merged 2 commits into
FRRouting:masterfrom
opensourcerouting:fix/security_hardening_bgpd
Apr 27, 2026
Merged

bgpd: Add some defences for AS4/ENCAP handling#21777
mjstapp merged 2 commits into
FRRouting:masterfrom
opensourcerouting:fix/security_hardening_bgpd

Conversation

@ton31337

Copy link
Copy Markdown
Member

No description provided.

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-apps

greptile-apps Bot commented Apr 27, 2026

Copy link
Copy Markdown

Greptile Summary

This PR adds two defensive hardening changes to BGP attribute parsing: it replaces a crash-inducing assert with graceful bounds-checking in peek_for_as4_capability, and it introduces a sub-TLV count cap (BGP_ENCAP_SUBTLV_MAX = 64) to prevent unbounded XCALLOC churn from crafted Tunnel Encap attributes. Both changes follow the existing error-handling pattern in the file.

Confidence Score: 5/5

Safe 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

Filename Overview
bgpd/bgp_attr.c Adds a per-parse sub-TLV counter that rejects attributes exceeding BGP_ENCAP_SUBTLV_MAX (64); follows the existing bgp_attr_malformed() pattern correctly.
bgpd/bgp_attr.h Introduces BGP_ENCAP_SUBTLV_MAX = 64 macro with an explanatory comment; straightforward header-only addition.
bgpd/bgp_open.c Replaces assert(capd_end <= end) with a bounds-check + goto end, converting a potential assert-crash on malformed OPEN messages into graceful recovery.

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]
Loading

Reviews (1): Last reviewed commit: "bgpd: Define a maximum number of TLVs fo..." | Re-trigger Greptile

@mjstapp mjstapp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@mjstapp

mjstapp commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

@Mergifyio backport stable/10.6 stable/10.5 stable/10.4

@mjstapp
mjstapp merged commit 1fa29d1 into FRRouting:master Apr 27, 2026
25 checks passed
@mergify

mergify Bot commented Apr 27, 2026

Copy link
Copy Markdown

backport stable/10.6 stable/10.5 stable/10.4

✅ Backports have been created

Details

Cherry-pick of 44f3385 has failed:

On branch mergify/bp/stable/10.6/pr-21777
Your branch is ahead of 'origin/stable/10.6' by 1 commit.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit 44f338574.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   bgpd/bgp_attr.c

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   bgpd/bgp_attr.h

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:

On branch mergify/bp/stable/10.5/pr-21777
Your branch is ahead of 'origin/stable/10.5' by 1 commit.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit 44f338574.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   bgpd/bgp_attr.c
	both modified:   bgpd/bgp_attr.h

no changes added to commit (use "git add" and/or "git commit -a")

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:

On branch mergify/bp/stable/10.4/pr-21777
Your branch is ahead of 'origin/stable/10.4' by 1 commit.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit 44f338574.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   bgpd/bgp_attr.c
	both modified:   bgpd/bgp_attr.h

no changes added to commit (use "git add" and/or "git commit -a")

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

@ton31337
ton31337 deleted the fix/security_hardening_bgpd branch April 28, 2026 06:48
mjstapp added a commit that referenced this pull request Apr 28, 2026
…ng_bgp_10.6

bgpd: Add some defences for AS4/ENCAP handling (backport #21777)
mjstapp added a commit that referenced this pull request Apr 28, 2026
…ng_bgp_10.5

bgpd: Add some defences for AS4/ENCAP handling (backport #21777)
mjstapp added a commit that referenced this pull request Apr 28, 2026
…ng_bgp_10.4

bgpd: Add some defences for AS4/ENCAP handling (backport #21777)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants