* [PATCH bpf v2 1/2] bpf: reject incompatible socket assignments
@ 2026-09-11 17:23 Shihuang Liu
2026-09-11 17:23 ` [PATCH bpf v2 2/2] bpf: revalidate assigned sockets after protocol change Shihuang Liu
0 siblings, 1 reply; 2+ messages in thread
From: Shihuang Liu @ 2026-09-11 17:23 UTC (permalink / raw)
To: netdev
Cc: bpf, linux-kernel, ast, daniel, andrii, eddyz87, memxor,
martin.lau, song, yonghong.song, jolsa, emil, ihor.solodrai,
john.fastabend, sdf, davem, edumazet, kuba, pabeni, horms, akpm,
leon.hwang, yatsenko, kpsingh, dmitry.baryshkov, jordan, nhudson,
avinash.duduskar, rongtao, joe, Shihuang Liu
bpf_sk_assign() permits TC ingress programs to associate an IPv6 packet
with an AF_INET socket. The receive path can then interpret IPv6 skb
control data as IPv4 metadata. When IP_RETOPTS is enabled, this can cause
__ip_options_echo() to copy beyond its stack buffer.
Reject incompatible packet and socket families in bpf_sk_assign() and
bpf_sk_assign_tcp_reqsk(). Continue to allow IPv4 packets to use
dual-stack AF_INET6 sockets.
Check request sockets against rsk_ops->family, since their sk_family is
inherited from the listener and sk_ipv6only is not initialized.
Fixes: cf7fbe660f2d ("bpf: Add socket assign support")
Assisted-by: LLM
Signed-off-by: Shihuang Liu <shlomojune6@gmail.com>
---
Changes since v1:
- Move family validation out of the IPv6 receive fast path and into
bpf_sk_assign() and bpf_sk_assign_tcp_reqsk().
- Preserve IPv4 assignments to dual-stack AF_INET6 sockets.
- Check request sockets using rsk_ops->family.
- Split the fix into two patches and target the BPF fixes tree.
v1:
https://lore.kernel.org/netdev/20260823101809.26802-1-shlomojune6@gmail.com/
include/uapi/linux/bpf.h | 4 ++++
net/core/filter.c | 31 +++++++++++++++++++++++++++++++
tools/include/uapi/linux/bpf.h | 4 ++++
3 files changed, 39 insertions(+)
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 732b35cc08d1c..5d8f5e2c8db38 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -4568,6 +4568,10 @@ union bpf_attr {
* **-EOPNOTSUPP** if the operation is not supported, for example
* a call from outside of TC ingress.
*
+ * **-EAFNOSUPPORT** if the socket family is not compatible with
+ * the network layer of the packet, for example an **AF_INET**
+ * socket and an IPv6 packet.
+ *
* long bpf_sk_assign(struct bpf_sk_lookup *ctx, struct bpf_sock *sk, u64 flags)
* Description
* Helper is overloaded depending on BPF program type. This
diff --git a/net/core/filter.c b/net/core/filter.c
index 61940e7535523..e9cc76b775c0c 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -3491,6 +3491,32 @@ static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
return -ENOTSUPP;
}
+static bool bpf_sk_assign_family_ok(const struct sk_buff *skb,
+ const struct sock *sk)
+{
+ unsigned short family;
+
+ switch (skb->protocol) {
+ case htons(ETH_P_IP):
+ family = AF_INET;
+ break;
+ case htons(ETH_P_IPV6):
+ family = AF_INET6;
+ break;
+ default:
+ return true;
+ }
+
+ /* Requests inherit the listener family, but have family-specific ops. */
+ if (sk->sk_state == TCP_NEW_SYN_RECV)
+ return inet_reqsk(sk)->rsk_ops->family == family;
+
+ return sk->sk_family == family ||
+ (family == AF_INET &&
+ sk->sk_family == AF_INET6 &&
+ !ipv6_only_sock(sk));
+}
+
BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
u64, flags)
{
@@ -7989,6 +8015,8 @@ BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
return -ENETUNREACH;
if (sk_unhashed(sk))
return -EOPNOTSUPP;
+ if (!bpf_sk_assign_family_ok(skb, sk))
+ return -EAFNOSUPPORT;
if (sk_is_refcounted(sk) &&
unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
return -ENOENT;
@@ -12526,6 +12554,9 @@ __bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct __sk_buff *s, struct sock *sk,
if (net != sock_net(sk))
return -ENETUNREACH;
+ if (!bpf_sk_assign_family_ok(skb, sk))
+ return -EAFNOSUPPORT;
+
switch (skb->protocol) {
case htons(ETH_P_IP):
ops = &tcp_request_sock_ops;
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 732b35cc08d1c..5d8f5e2c8db38 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -4568,6 +4568,10 @@ union bpf_attr {
* **-EOPNOTSUPP** if the operation is not supported, for example
* a call from outside of TC ingress.
*
+ * **-EAFNOSUPPORT** if the socket family is not compatible with
+ * the network layer of the packet, for example an **AF_INET**
+ * socket and an IPv6 packet.
+ *
* long bpf_sk_assign(struct bpf_sk_lookup *ctx, struct bpf_sock *sk, u64 flags)
* Description
* Helper is overloaded depending on BPF program type. This
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [PATCH bpf v2 2/2] bpf: revalidate assigned sockets after protocol change
2026-09-11 17:23 [PATCH bpf v2 1/2] bpf: reject incompatible socket assignments Shihuang Liu
@ 2026-09-11 17:23 ` Shihuang Liu
0 siblings, 0 replies; 2+ messages in thread
From: Shihuang Liu @ 2026-09-11 17:23 UTC (permalink / raw)
To: netdev
Cc: bpf, linux-kernel, ast, daniel, andrii, eddyz87, memxor,
martin.lau, song, yonghong.song, jolsa, emil, ihor.solodrai,
john.fastabend, sdf, davem, edumazet, kuba, pabeni, horms, akpm,
leon.hwang, yatsenko, kpsingh, dmitry.baryshkov, jordan, nhudson,
avinash.duduskar, rongtao, joe, Shihuang Liu
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@gmail.com>
---
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@gmail.com/
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
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-09-11 17:24 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-11 17:23 [PATCH bpf v2 1/2] bpf: reject incompatible socket assignments Shihuang Liu
2026-09-11 17:23 ` [PATCH bpf v2 2/2] bpf: revalidate assigned sockets after protocol change Shihuang Liu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox