From: "Björn Töpel" <bjorn@kernel.org>
To: "David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Jonathan Corbet <corbet@lwn.net>,
Alexander Duyck <alexanderduyck@fb.com>,
Andrew Lunn <andrew+netdev@lunn.ch>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Jesper Dangaard Brouer <hawk@kernel.org>,
Stanislav Fomichev <sdf@fomichev.me>,
Magnus Karlsson <magnus.karlsson@intel.com>,
Maciej Fijalkowski <maciej.fijalkowski@intel.com>,
Pavel Begunkov <asml.silence@gmail.com>,
netdev@vger.kernel.org, bpf@vger.kernel.org
Cc: "Björn Töpel" <bjorn@kernel.org>,
"Simon Horman" <horms@kernel.org>,
"Shuah Khan" <skhan@linuxfoundation.org>,
"Randy Dunlap" <rdunlap@infradead.org>,
kernel-team@meta.com, "John Fastabend" <john.fastabend@gmail.com>,
"Martin KaFai Lau" <martin.lau@linux.dev>,
"Song Liu" <song@kernel.org>,
"Yonghong Song" <yonghong.song@linux.dev>,
"Jiri Olsa" <jolsa@kernel.org>,
"Emil Tsalapatis" <emil@etsalapatis.com>,
"Ihor Solodrai" <ihor.solodrai@linux.dev>,
"Weiming Shi" <bestswngs@gmail.com>,
"Nikolay Aleksandrov" <razor@blackwall.org>,
"David Wei" <dw@davidwei.uk>,
"Bobby Eshleman" <bobbyeshleman@meta.com>,
linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org, io-uring@vger.kernel.org,
"Mina Almasry" <almasrymina@google.com>
Subject: [PATCH net-next 1/7] bpf: Keep XDP helpers within readable data
Date: Tue, 17 Nov 2026 17:46:52 +0100 [thread overview]
Message-ID: <20261117164700.2157816-2-bjorn@kernel.org> (raw)
In-Reply-To: <20261117164700.2157816-1-bjorn@kernel.org>
Devmem and io_uring zero-copy RX put payload in fragments that the
kernel cannot read. XDP helpers must not treat these fragments as
normal pages.
Reject operations that read or write these fragments. Access to the
linear part still works. Tail trimming does not read the payload;
clear the unreadable flag after it removes every fragment.
Drivers currently use these buffers to build skbs without running XDP.
These checks are needed before native XDP can run on provider queues.
Signed-off-by: Björn Töpel <bjorn@kernel.org>
---
include/linux/filter.h | 9 +++++----
include/net/xdp.h | 12 ++++++++++++
kernel/bpf/helpers.c | 4 +++-
net/core/filter.c | 37 +++++++++++++++++++++++++------------
4 files changed, 45 insertions(+), 17 deletions(-)
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 4f0cd67caccb..5c2f57ec3940 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -1994,8 +1994,8 @@ int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from,
int __bpf_xdp_load_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len);
int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len);
void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len);
-void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
- void *buf, unsigned long len, bool flush);
+int bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
+ void *buf, unsigned long len, bool flush);
int __bpf_skb_meta_store_bytes(struct sk_buff *skb, u32 offset,
const void *from, u32 len, u64 flags);
void *bpf_skb_meta_pointer(struct sk_buff *skb, u32 offset);
@@ -2029,9 +2029,10 @@ static inline void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
return NULL;
}
-static inline void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off, void *buf,
- unsigned long len, bool flush)
+static inline int bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
+ void *buf, unsigned long len, bool flush)
{
+ return -EOPNOTSUPP;
}
static inline int __bpf_skb_meta_store_bytes(struct sk_buff *skb, u32 offset,
diff --git a/include/net/xdp.h b/include/net/xdp.h
index 07231adfb5f8..16693cf9653e 100644
--- a/include/net/xdp.h
+++ b/include/net/xdp.h
@@ -136,6 +136,18 @@ static __always_inline void xdp_buff_set_frag_unreadable(struct xdp_buff *xdp)
xdp->flags |= XDP_FLAGS_FRAGS_UNREADABLE;
}
+static __always_inline void
+xdp_buff_clear_frag_unreadable(struct xdp_buff *xdp)
+{
+ xdp->flags &= ~XDP_FLAGS_FRAGS_UNREADABLE;
+}
+
+static __always_inline bool
+xdp_buff_is_frag_unreadable(const struct xdp_buff *xdp)
+{
+ return !!(xdp->flags & XDP_FLAGS_FRAGS_UNREADABLE);
+}
+
static __always_inline u32 xdp_buff_get_skb_flags(const struct xdp_buff *xdp)
{
return xdp->flags;
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 6788489a2608..8c9e4640bcfe 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -3140,7 +3140,9 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr *p, u64 offset,
if (!buffer__nullable)
return NULL;
- bpf_xdp_copy_buf(ptr->data, ptr->offset + offset, buffer__nullable, len, false);
+ if (bpf_xdp_copy_buf(ptr->data, ptr->offset + offset,
+ buffer__nullable, len, false))
+ return NULL;
return buffer__nullable;
}
case BPF_DYNPTR_TYPE_SKB_META:
diff --git a/net/core/filter.c b/net/core/filter.c
index f42ecfb5841f..9a55f61fbcdf 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -4269,8 +4269,8 @@ static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
.arg2_type = ARG_ANYTHING,
};
-void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
- void *buf, unsigned long len, bool flush)
+int bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
+ void *buf, unsigned long len, bool flush)
{
unsigned long ptr_len, ptr_off = 0;
skb_frag_t *next_frag, *end_frag;
@@ -4282,9 +4282,12 @@ void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
src = flush ? buf : xdp->data + off;
dst = flush ? xdp->data + off : buf;
memcpy(dst, src, len);
- return;
+ return 0;
}
+ if (unlikely(xdp_buff_is_frag_unreadable(xdp)))
+ return -EFAULT;
+
sinfo = xdp_get_shared_info_from_buff(xdp);
end_frag = &sinfo->frags[sinfo->nr_frags];
next_frag = &sinfo->frags[0];
@@ -4314,6 +4317,8 @@ void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
ptr_len = skb_frag_size(next_frag);
next_frag++;
}
+
+ return 0;
}
void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
@@ -4332,6 +4337,9 @@ void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
if (likely(offset < size)) /* linear area */
goto out;
+ if (unlikely(xdp_buff_is_frag_unreadable(xdp)))
+ return ERR_PTR(-EFAULT);
+
sinfo = xdp_get_shared_info_from_buff(xdp);
offset -= size;
for (i = 0; i < sinfo->nr_frags; i++) { /* paged area */
@@ -4358,10 +4366,9 @@ BPF_CALL_4(bpf_xdp_load_bytes, struct xdp_buff *, xdp, u32, offset,
return PTR_ERR(ptr);
if (!ptr)
- bpf_xdp_copy_buf(xdp, offset, buf, len, false);
- else
- memmove(buf, ptr, len);
+ return bpf_xdp_copy_buf(xdp, offset, buf, len, false);
+ memmove(buf, ptr, len);
return 0;
}
@@ -4390,11 +4397,9 @@ BPF_CALL_4(bpf_xdp_store_bytes, struct xdp_buff *, xdp, u32, offset,
return PTR_ERR(ptr);
if (!ptr)
- bpf_xdp_copy_buf(xdp, offset, buf, len, true);
- else
- /* through a dynptr the source may be an overlapping slice */
- memmove(ptr, buf, len);
+ return bpf_xdp_copy_buf(xdp, offset, buf, len, true);
+ memmove(ptr, buf, len);
return 0;
}
@@ -4422,6 +4427,8 @@ static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset)
if (!rxq->frag_size || rxq->frag_size > xdp->frame_sz)
return -EOPNOTSUPP;
+ if (unlikely(xdp_buff_is_frag_unreadable(xdp)))
+ return -EFAULT;
tailroom = rxq->frag_size - skb_frag_size(frag) -
skb_frag_off(frag) % rxq->frag_size;
@@ -4503,6 +4510,7 @@ static int bpf_xdp_frags_shrink_tail(struct xdp_buff *xdp, int offset)
if (unlikely(!sinfo->nr_frags)) {
xdp_buff_clear_frags_flag(xdp);
xdp_buff_clear_frag_pfmemalloc(xdp);
+ xdp_buff_clear_frag_unreadable(xdp);
xdp->data_end -= offset;
}
@@ -5355,8 +5363,7 @@ static unsigned long bpf_xdp_copy(void *dst, const void *ctx,
{
struct xdp_buff *xdp = (struct xdp_buff *)ctx;
- bpf_xdp_copy_buf(xdp, off, dst, len, false);
- return 0;
+ return bpf_xdp_copy_buf(xdp, off, dst, len, false) ? len : 0;
}
BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
@@ -5369,6 +5376,9 @@ BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
if (unlikely(!xdp || xdp_size > xdp_get_buff_len(xdp)))
return -EFAULT;
+ if (unlikely(xdp_size > xdp->data_end - xdp->data &&
+ xdp_buff_is_frag_unreadable(xdp)))
+ return -EFAULT;
return bpf_event_output(map, flags, meta, meta_size, xdp,
xdp_size, bpf_xdp_copy);
@@ -12797,6 +12807,7 @@ __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops,
* Return:
* * %0 - success
* * %-EINVAL - invalid len
+ * * %-EFAULT - the kernel cannot read the fragments
*/
__bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len)
{
@@ -12812,6 +12823,8 @@ __bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len)
if (unlikely(len > xdp_get_buff_len(xdp)))
return -EINVAL;
+ if (unlikely(xdp_buff_is_frag_unreadable(xdp)))
+ return -EFAULT;
start = xdp_data_meta_unsupported(xdp) ? xdp->data : xdp->data_meta;
--
2.56.1
next prev parent reply other threads:[~2026-11-17 16:47 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 16:46 [PATCH net-next 0/7] XDP with io_uring zero-copy RX and devmem Björn Töpel
2026-11-17 16:46 ` Björn Töpel [this message]
2026-11-17 16:46 ` [PATCH net-next 2/7] xdp: Reject frames with unreadable fragments Björn Töpel
2026-11-17 16:46 ` [PATCH net-next 3/7] net: Admit native XDP on provider queues Björn Töpel
2026-11-17 16:46 ` [PATCH net-next 4/7] eth: fbnic: Run " Björn Töpel
2026-11-17 16:46 ` [PATCH net-next 5/7] netdevsim: Run native XDP next to devmem Björn Töpel
2026-11-17 16:46 ` [PATCH net-next 6/7] selftests: net: Test XDP with devmem bindings Björn Töpel
2026-11-17 16:46 ` [PATCH net-next 7/7] selftests: net: Test XDP on io_uring RX Björn Töpel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261117164700.2157816-2-bjorn@kernel.org \
--to=bjorn@kernel.org \
--cc=alexanderduyck@fb.com \
--cc=almasrymina@google.com \
--cc=andrew+netdev@lunn.ch \
--cc=andrii@kernel.org \
--cc=asml.silence@gmail.com \
--cc=ast@kernel.org \
--cc=bestswngs@gmail.com \
--cc=bobbyeshleman@meta.com \
--cc=bpf@vger.kernel.org \
--cc=corbet@lwn.net \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=dw@davidwei.uk \
--cc=eddyz87@gmail.com \
--cc=edumazet@kernel.org \
--cc=emil@etsalapatis.com \
--cc=hawk@kernel.org \
--cc=horms@kernel.org \
--cc=ihor.solodrai@linux.dev \
--cc=io-uring@vger.kernel.org \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kernel-team@meta.com \
--cc=kuba@kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=maciej.fijalkowski@intel.com \
--cc=magnus.karlsson@intel.com \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=razor@blackwall.org \
--cc=rdunlap@infradead.org \
--cc=sdf@fomichev.me \
--cc=skhan@linuxfoundation.org \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox