ipv6: Cap TLV scan in ip6_tnl_parse_tlv_enc_lim
Subsystems (3)
PATCH v3 1/1 ipv6: Cap TLV scan in ip6_tnl_parse_tlv_enc_lim
Commit 47d3d7ac656a ("ipv6: Implement limits on Hop-by-Hop and Destination options") added net.ipv6.max_{hbh,dst}_opts_{cnt,len} and applied them in ip6_parse_tlv(), the generic TLV walker invoked from ipv6_destopt_rcv() and ipv6_parse_hopopts().
ip6_tnl_parse_tlv_enc_lim() does not go through ip6_parse_tlv(); it has its own hand-rolled TLV scanner inside its NEXTHDR_DEST branch which looks for IPV6_TLV_TNL_ENCAP_LIMIT. That inner loop is bounded only by optlen, which can be up to 2048 bytes. Stuffing the Destination Options header with 2046 Pad1 (type=0) entries advances the scanner a single byte at a time, yielding ~2000 TLV iterations per extension header.
Reusing max_dst_opts_cnt to bound the TLV iterations, matching the semantics from 47d3d7ac656a, would require duplicating ip6_parse_tlv() to also validate Pad1/PadN payload. It would also mandate enforcing max_dst_opts_len, since otherwise an attacker shifts the axis to few options with a giant PadN and recovers the original DoS. Allowing up to 8 options before the tunnel encapsulation limit TLV is liberal enough; in practice encap limit is the first TLV. Thus, go with a hard-coded limit IP6_TUNNEL_MAX_DEST_TLVS (8).
---
v2->v3:
- Hard code limit of 8 instead of max_dst_opts_cnt (Ido)
v1->v2:
- Use abs() given max_dst_opts_cnt's negative meaning (Justin)
- Remove unlikely (Justin)
net/ipv6/ip6_tunnel.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/ipv6/ip6_tunnel.c +6 -0
@@ -62,6 +62,8 @@ MODULE_LICENSE("GPL");MODULE_ALIAS_RTNL_LINK ("ip6tnl" );MODULE_ALIAS_NETDEV ("ip6tnl0" ); +#define IP6_TUNNEL_MAX_DEST_TLVS 8 +#define IP6_TUNNEL_HASH_SIZE_SHIFT 5#define IP6_TUNNEL_HASH_SIZE (1 << IP6_TUNNEL_HASH_SIZE_SHIFT)
@@ -430,11 +432,15 @@ __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)break ; }if (nexthdr ==NEXTHDR_DEST ) { +int tlv_cnt =0 ;u16 i =2 ;while (1 ) {struct ipv6_tlv_tnl_enc_lim *tel; +if (unlikely (tlv_cnt++ >=IP6_TUNNEL_MAX_DEST_TLVS )) +break ; +/* No more room for encapsulation limit */ if (i +sizeof (*tel) > optlen)break ;
Signature · 1 line
2.43.0
Quoted 23 lines · Daniel Borkmann
Commit
47d3d7ac656a("ipv6: Implement limits on Hop-by-Hop and Destination options") added net.ipv6.max_{hbh,dst}_opts_{cnt,len} and applied them inip6_parse_tlv(), the generic TLV walker invoked fromipv6_destopt_rcv()andipv6_parse_hopopts().
ip6_tnl_parse_tlv_enc_lim()does not go throughip6_parse_tlv(); it has its own hand-rolled TLV scanner inside itsNEXTHDR_DESTbranch which looks forIPV6_TLV_TNL_ENCAP_LIMIT. That inner loop is bounded only by optlen, which can be up to 2048 bytes. Stuffing the Destination Options header with 2046 Pad1 (type=0) entries advances the scanner a single byte at a time, yielding ~2000 TLV iterations per extension header.Reusing
max_dst_opts_cntto bound the TLV iterations, matching the semantics from47d3d7ac656a, would require duplicatingip6_parse_tlv()to also validate Pad1/PadN payload. It would also mandate enforcingmax_dst_opts_len, since otherwise an attacker shifts the axis to few options with a giant PadN and recovers the original DoS. Allowing up to 8 options before the tunnel encapsulation limit TLV is liberal enough; in practice encap limit is the first TLV. Thus, go with a hard-coded limitIP6_TUNNEL_MAX_DEST_TLVS(8).
Quoted 1 line
Signed-off-by Daniel Borkmann <[email protected]>
Given that you are targeting net and that the issue was always present, I would use:
Quoted 23 lines · Daniel Borkmann
Commit
47d3d7ac656a("ipv6: Implement limits on Hop-by-Hop and Destination options") added net.ipv6.max_{hbh,dst}_opts_{cnt,len} and applied them inip6_parse_tlv(), the generic TLV walker invoked fromipv6_destopt_rcv()andipv6_parse_hopopts().
ip6_tnl_parse_tlv_enc_lim()does not go throughip6_parse_tlv(); it has its own hand-rolled TLV scanner inside itsNEXTHDR_DESTbranch which looks forIPV6_TLV_TNL_ENCAP_LIMIT. That inner loop is bounded only by optlen, which can be up to 2048 bytes. Stuffing the Destination Options header with 2046 Pad1 (type=0) entries advances the scanner a single byte at a time, yielding ~2000 TLV iterations per extension header.Reusing
max_dst_opts_cntto bound the TLV iterations, matching the semantics from47d3d7ac656a, would require duplicatingip6_parse_tlv()to also validate Pad1/PadN payload. It would also mandate enforcingmax_dst_opts_len, since otherwise an attacker shifts the axis to few options with a giant PadN and recovers the original DoS. Allowing up to 8 options before the tunnel encapsulation limit TLV is liberal enough; in practice encap limit is the first TLV. Thus, go with a hard-coded limitIP6_TUNNEL_MAX_DEST_TLVS(8).
Quoted 1 line
Signed-off-by Daniel Borkmann <[email protected]>
Thanks, Daniel.
Cheers,
Justin
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 13 lines · On Tue, 21 Apr 2026 22:24:06 +0200 you wrote:
Commit
47d3d7ac656a("ipv6: Implement limits on Hop-by-Hop and Destination options") added net.ipv6.max_{hbh,dst}_opts_{cnt,len} and applied them inip6_parse_tlv(), the generic TLV walker invoked fromipv6_destopt_rcv()andipv6_parse_hopopts().
ip6_tnl_parse_tlv_enc_lim()does not go throughip6_parse_tlv(); it has its own hand-rolled TLV scanner inside itsNEXTHDR_DESTbranch which looks forIPV6_TLV_TNL_ENCAP_LIMIT. That inner loop is bounded only by optlen, which can be up to 2048 bytes. Stuffing the Destination Options header with 2046 Pad1 (type=0) entries advances the scanner a single byte at a time, yielding ~2000 TLV iterations per extension header.
Quoted 1 line
[...]
Here is the summary with links:
- [net,v3] ipv6: Cap TLV scan in ip6_tnl_parse_tlv_enc_lim
https://git.kernel.org/netdev/net/c/076b8cad77aa
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html