[PATCH bpf v2 2/2] bpf: revalidate assigned sockets after protocol change
From: Shihuang Liu
Date: Fri Sep 11 2026 - 13:24:15 EST
An IPv4 packet can be assigned to an AF_INET socket and then translated
to IPv6 by bpf_skb_change_proto(). Since the translation preserves the
socket assignment, the IPv6 packet can still be delivered to the IPv4
socket, bypassing the family check in bpf_sk_assign().
After a successful protocol change, recheck any prefetched socket
against the new protocol and call skb_orphan() if its address family
is incompatible. This releases the assignment through the existing
skb destructor, allowing normal socket lookup or a new assignment
by the BPF program.
Fixes: cf7fbe660f2d ("bpf: Add socket assign support")
Assisted-by: LLM
Signed-off-by: Shihuang Liu <shlomojune6@xxxxxxxxx>
---
Changes since v1:
- Revalidate prefetched sockets after bpf_skb_change_proto() changes the
packet protocol, closing a bypass of assignment-time validation.
- Preserve compatible dual-stack assignments and release incompatible
assignments through their existing skb destructor.
- Split the fix into two patches and target the BPF fixes tree.
v1:
https://lore.kernel.org/netdev/20260823101809.26802-1-shlomojune6@xxxxxxxxx/
include/uapi/linux/bpf.h | 4 ++++
net/core/filter.c | 5 +++++
tools/include/uapi/linux/bpf.h | 4 ++++
3 files changed, 13 insertions(+)
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 5d8f5e2c8db38..0de7967077a2e 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -2659,6 +2659,10 @@ union bpf_attr {
* checked and segments are recalculated by the GSO/GRO engine.
* The size for GSO target is adapted as well.
*
+ * On success, an assigned socket is released if its address
+ * family is incompatible with the new protocol. Assign a
+ * compatible socket after translation if required.
+ *
* All values for *flags* are reserved for future usage, and must
* be left at zero.
*
diff --git a/net/core/filter.c b/net/core/filter.c
index e9cc76b775c0c..79a0e484d9dd4 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -3547,6 +3547,11 @@ BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
if (ret)
return ret;
+ /* Protocol translation can invalidate an earlier socket assignment. */
+ if (skb_sk_is_prefetched(skb) &&
+ !bpf_sk_assign_family_ok(skb, skb->sk))
+ skb_orphan(skb);
+
if (skb_valid_dst(skb))
skb_dst_drop(skb);
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 5d8f5e2c8db38..0de7967077a2e 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -2659,6 +2659,10 @@ union bpf_attr {
* checked and segments are recalculated by the GSO/GRO engine.
* The size for GSO target is adapted as well.
*
+ * On success, an assigned socket is released if its address
+ * family is incompatible with the new protocol. Assign a
+ * compatible socket after translation if required.
+ *
* All values for *flags* are reserved for future usage, and must
* be left at zero.
*
--
2.43.0