Re: [PATCH bpf-next v3 1/2] bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs

From: bot+bpf-ci

Date: Sun Nov 22 2026 - 10:17:53 EST


> bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs

TL;DR: bpf_dynptr_memmem() may return -EINVAL for skb/xdp packets with
non-linear (frag) data, so packet searches can fail even when the needle is
present; the error is also ambiguous.

> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c

[ ... ]

> +__bpf_kfunc long bpf_dynptr_memmem(const struct bpf_dynptr *haystack,
> + const void *needle, u32 needle__sz,
> + u64 flags)
> +{

[ ... ]

> + size = __bpf_dynptr_size(ptr);
> + if (size > BPF_MEMMEM_MAX_LEN)
> + return -E2BIG;
> + if (size == 0 || needle__sz > size)
> + return -EINVAL;
> +
> + data = __bpf_dynptr_data(ptr, size);
> + if (!data)
> + return -EINVAL;

Can this return -EINVAL for the packet inspection cases the commit message
describes?

__bpf_dynptr_data(ptr, size) is bpf_dynptr_slice(p, 0, NULL, size) with no
bounce buffer. For BPF_DYNPTR_TYPE_SKB that ends up in
skb_pointer_if_linear(), which returns NULL whenever size > skb_headlen(skb).

For BPF_DYNPTR_TYPE_XDP, bpf_xdp_pointer() returns NULL or an ERR_PTR when
the range crosses into frags or is larger than 0xffff. For
BPF_DYNPTR_TYPE_FILE, bpf_file_fetch_bytes() returns -EINVAL for a NULL
buffer, so it always fails.

A TC program calling bpf_dynptr_from_skb(skb, 0, &d) gets a dynptr covering
skb->len, and received skbs commonly keep most of their payload in page
frags. For a typical packet larger than the linear head, such as a TLS
ClientHello, the call fails even though the needle is present.

The neighbouring dynptr kfuncs handle non-linear data explicitly.
bpf_dynptr_memset() falls back to __bpf_dynptr_write() in chunks, and
bpf_dynptr_copy() falls back to __bpf_dynptr_read()/__bpf_dynptr_write().

The kernel-doc lists "non-linear buffer" under -EINVAL, but that code is
shared with invalid flags and invalid parameters (including needle__sz >
size). A program cannot tell "call bpf_skb_pull_data() and retry" from a
real argument error, nor from a packet that is simply shorter than the
needle, which arguably should be -ENOENT (bpf_strnstr() returns -ENOENT in
that case).

Should bpf_dynptr_memmem() search non-linear skb/xdp data, for example by
scanning slices with a needle_sz - 1 overlap, or at least return a distinct
error for the non-linear case? Should the commit message say that TC packet
payloads usually have to be linearized first?

> +
> + match = __bpf_memmem(data, size, needle, needle__sz);
> + if (!match)
> + return -ENOENT;

[ ... ]


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45034767598