net: limit priority VLAN untag depth
PATCH 1/1 net: limit priority VLAN untag depth
In reply to a message not in the archive[ Upstream commit d3ff07f5acc573e5f72bf1c581bae5d05191ba61 ]
The priority VLAN receive path can repeatedly remove VID 0 tags and retry packet processing. Unlike the read-only VLAN protocol parser, this path has no limit on the number of nested tags it consumes.
Each skb_vlan_untag() advances skb->data and resets header offsets. The offsets are stored in 16-bit fields. With enough nested tags, the network or transport offset can wrap. A later protocol handler can then derive a large pull length from the wrapped offsets and access memory outside the skb allocation.
Apply the existing VLAN_MAX_DEPTH limit to the exceptional VID 0 retry. Count the tag already stored in skb VLAN metadata as the first level, then stop before untagging a ninth level. Keep the accounting inside this uncommon branch so the regular VLAN receive path is unaffected.
A single VID 0 priority tag before a real VLAN tag is valid. No normal deployment was found that intentionally emits multiple consecutive VID 0 tags; permitting eight levels is therefore conservative.
The issue was found while researching the Linux network receive path for Google kvmCTF, with OpenAI Codex (gpt-5.6-sol) assisting the analysis. The affected path, reproducer and fix were manually reviewed and tested.
[ Backport to 5.15.y: __netif_receive_skb_core() has no local
drop_reason variable, and this branch does not define SKB_DROP_REASON_RECURSION_LIMIT. Omit the upstream assignment and keep the existing drop path, which frees the skb with kfree_skb(). ]
---
Backport based on linux-5.15.y 29c6cdaac04a6989a7ed4ae4275ce4244e617f58 (Linux 5.15.225). W=1 net/core/dev.o built without warnings (gcc 12.5). Full
KASAN/ftrace bzImage built as well.
Boundary test on an isolated KVM host: the eight-tag packet
was delivered and the nine-tag packet was dropped.
Original nested vhost/TAP trigger: ftrace recorded eightskb_vlan_untag() calls and zero tcp_v4_rcv() calls; no KASAN
report, oops or panic was observed.
Unpatched 5.15.225 on the same host: the nine-tag packet was
delivered, and the trigger removed all 16364 tags and triggered
a KASAN use-after-free report in tcp_v4_rcv().
net/core/dev.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/core/dev.c +6 -0
@@ -5460,6 +5460,8 @@ static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc, }if (unlikely (skb_vlan_tag_present (skb)) && !netdev_uses_dsa (skb->dev)) { +unsigned int priority_tag_depth =1 ; + check_vlan_id:if (skb_vlan_tag_get_id (skb)) {/* Vlan id is non 0 and vlan_do_receive() above couldn't
@@ -5471,6 +5473,10 @@ static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc,* 802.1Q or 802.1AD and vlan_do_receive() above could * not find vlan dev for vlan id 0. */ +if (unlikely (priority_tag_depth >=VLAN_MAX_DEPTH )) + goto drop; + + priority_tag_depth++;__vlan_hwaccel_clear_tag (skb); skb =skb_vlan_untag (skb);if (unlikely (!skb))