[RFC PATCH bpf-next v2 0/2] bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs

From: Abhishek Khade

Date: Sun Nov 22 2026 - 07:34:48 EST


This patchset introduces two new BPF kfuncs for binary-safe substring
searching:
- bpf_memmem(): Searches flat memory slices for binary sequences.
- bpf_dynptr_memmem(): Searches BPF dynptrs for binary sequences.

With the introduction of bpf_strstr, bpf_strnstr, bpf_strcasestr, and
bpf_strcasecmp in Linux 6.17–6.19, BPF programs gained solid support for
substring search in null-terminated C-strings.

However, in network packet inspection (TC/XDP, TLS SNI tagging, binary
protocol delimiters) and security monitoring (e.g. BPF LSM hooks
inspecting raw memory or binary execution buffers), data buffers are
frequently not null-terminated and often contain embedded '\0' bytes.
For these use cases, existing string kfuncs terminate prematurely at the
first null byte.

This RFC adds bpf_memmem() and bpf_dynptr_memmem(), wrapping an internal
allocation-free scanning routine based on the kernel's optimized
memchr() followed by memcmp(). Buffer sizes are capped at 2 MB
(BPF_MEMMEM_MAX_LEN) and needle sizes at 4 KB (BPF_MEMMEM_MAX_NEEDLE_SZ).
To prevent pathological quadratic O(N * M) RCU CPU stalls on repetitive
inputs, cumulative byte comparisons across memcmp calls are strictly
bounded by BPF_MEMMEM_MAX_LEN. Both kfuncs are registered in
common_btf_ids under BPF_PROG_TYPE_UNSPEC so all program types can call
them.

A comprehensive selftest suite of 25 test cases is added covering flat
buffers and dynptrs with embedded nulls, offsets, duplicate patterns,
overlapping prefixes, boundary limits, and error conditions.

Changes in v2:
- Move bpf_memmem() and bpf_dynptr_memmem() outside #ifdef CONFIG_CGROUPS
block (reported by Sashiko AI review).
- Register kfuncs in common_btf_ids instead of generic_btf_ids so they
are accessible across all BPF program types (BPF_PROG_TYPE_UNSPEC).
- Fix dynptr size truncation by using u64 for __bpf_dynptr_size() to
handle 64-bit dynptr sizes (e.g. file dynptrs).
- Bound worst-case search complexity to prevent quadratic O(N * M)
RCU CPU stalls: cap needle_sz at 4 KB (BPF_MEMMEM_MAX_NEEDLE_SZ) and
bound cumulative comparisons to BPF_MEMMEM_MAX_LEN (2 MB).
- Safely retrieve dynptr buffer using __bpf_dynptr_data() instead of
direct struct field dereferencing.
- Expand automated selftests from 19 to 25 test cases, validating exact
2 MB boundary scans, oversized buffers (> 2 MB -> -E2BIG), and
zero-length haystacks (-EINVAL) for both flat and dynptr buffers.

Abhishek Khade (2):
bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs
selftests/bpf: Add selftests for bpf_memmem and bpf_dynptr_memmem
kfuncs

kernel/bpf/helpers.c | 116 ++++++++
.../selftests/bpf/prog_tests/memmem_kfunc.c | 9 +
.../selftests/bpf/progs/memmem_kfunc.c | 259 ++++++++++++++++++
3 files changed, 384 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/memmem_kfunc.c
create mode 100644 tools/testing/selftests/bpf/progs/memmem_kfunc.c

--
2.53.0