Skip to contentSkip to conversation

Drop packets with invalid headers to prevent KMSAN infoleak

PATCH In review Daniel Yang 2 participants 2 messages Nov 4, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Drop packets with invalid headers to prevent KMSAN infoleak

KMSAN detects uninitialized memory stored to memory by bpf_clone_redirect(). Adding a check to the transmission path to find malformed headers prevents this issue. Specifically, we check if the length of the data stored in skb is less than the minimum device header length. If so, drop the packet since the skb cannot contain a valid device header. Also check if mac_header_len(skb) is outside the range provided of valid device header lengths.

Testing this patch with syzbot removes the bug.

Macro added to not affect normal builds.

Fixes 88264981f208 ("Merge tag 'sched_ext-for-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext")
Reported-by [email protected]

---
v1: Enclosed in macro to not affect normal builds

 net/core/filter.c | 8 ++++++++
 1 file changed, 8 insertions(+)
net/core/filter.c +8 -0
index cd3524cb3..9c5786f9c 100644
@@ -2191,6 +2191,14 @@ static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
 		return -ERANGE;
 	}
 
+#if IS_ENABLED(CONFIG_KMSAN)
+	if (unlikely(skb->len < dev->min_header_len ||
+		     skb_mac_header_len(skb) < dev->min_header_len ||
+		     skb_mac_header_len(skb) > dev->hard_header_len)) {
+		kfree_skb(skb);
+		return -ERANGE;
+	}
+#endif
 	bpf_push_mac_rcsum(skb);
 	return flags & BPF_F_INGRESS ?
 	       __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
Signature · 1 line
2.39.2
#1 lore raw
Quoted 43 lines · Daniel Yang

KMSAN detects uninitialized memory stored to memory by bpf_clone_redirect(). Adding a check to the transmission path to find malformed headers prevents this issue. Specifically, we check if the length of the data stored in skb is less than the minimum device header length. If so, drop the packet since the skb cannot contain a valid device header. Also check if mac_header_len(skb) is outside the range provided of valid device header lengths.

Testing this patch with syzbot removes the bug.

Macro added to not affect normal builds.

Fixes 88264981f208 ("Merge tag 'sched_ext-for-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext")
Reported-by [email protected]

---
v1: Enclosed in macro to not affect normal builds

 net/core/filter.c | 8 ++++++++
 1 file changed, 8 insertions(+)
net/core/filter.c +8 -1
index cd3524cb3..9c5786f9c 100644
@@ -2191,6 +2191,14 @@ static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
                return -ERANGE;
        }
 
+#if IS_ENABLED(CONFIG_KMSAN)
+       if (unlikely(skb->len < dev->min_header_len ||
+                    skb_mac_header_len(skb) < dev->min_header_len ||
+                    skb_mac_header_len(skb) > dev->hard_header_len)) {
+               kfree_skb(skb);
+               return -ERANGE;
+       }
+#endif
        bpf_push_mac_rcsum(skb);
        return flags & BPF_F_INGRESS ?
               __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
--
Quoted 2 lines

2.39.2

I am not a BPF maintainer, but for the record I think it is wrong to silence KMSAN and give the impression a bug is 'removed'.