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

From: Abhishek Khade

Date: Sun Nov 22 2026 - 09:38:32 EST


This patch series introduces two new BPF kfuncs for binary-safe
substring searching in flat memory buffers and BPF dynptrs:
- bpf_memmem(): Searches for a binary needle within a memory buffer with
verifier bounds safety.
- bpf_dynptr_memmem(): Performs substring searching across BPF dynptrs.

Both kfuncs utilize an allocation-free scanning routine based on the
Crochemore-Perrin Two-Way string matching algorithm. By computing the
critical factorization of the needle in O(m) time and O(1) space, the
Two-Way algorithm guarantees strict O(N) worst-case linear time (at most
2*N comparisons), preventing quadratic O(N*M) RCU CPU stalls without
requiring artificial comparison budget counters or risking premature
abort false negatives.

Buffer sizes are capped at BPF_MEMMEM_MAX_LEN (2 MB) and needle patterns
at BPF_MEMMEM_MAX_NEEDLE_SZ (4 KB). Verifier bounds safety is enforced via
the standard __sz naming convention and dynptr type tracking. Both
kfuncs are registered in common_btf_ids to allow access across all program
types.

Changes in v3:
- Replaced naive loop with the Crochemore-Perrin Two-Way string matching
algorithm: guarantees O(N) linear-time execution (at most 2*N
comparisons) with strictly O(1) space, eliminating quadratic RCU stall
risks without artificial comparison budgets, premature aborts, or false
-ENOENTs on repetitive patterns.
- Expanded automated BPF selftest suite to 29 test cases covering all
boundary, size, zero-length, periodic, and pathological repetitive
patterns for both flat buffers and dynptrs.
- Formally verified all 5 safety and liveness invariants with Z3 solver.
- Verified 100% equivalence against glibc memmem across 50,000 differential
fuzzing iterations.

Changes in v2:
- Moved __bpf_memmem(), bpf_memmem(), and bpf_dynptr_memmem() outside
#ifdef CONFIG_CGROUPS in kernel/bpf/helpers.c to fix build failures
when CONFIG_CGROUPS is disabled.
- Removed direct access to 'ptr->data' in bpf_dynptr_memmem() to preserve
the dynptr encapsulation layer, delegating buffer resolution and checks
to __bpf_dynptr_data().
- Fixed 64-bit size truncation in bpf_dynptr_memmem() by changing
'u32 size' to 'u64 size' so dynptr sizes exceeding 4 GB cannot bypass
the BPF_MEMMEM_MAX_LEN boundary check.
- Registered bpf_memmem and bpf_dynptr_memmem in common_btf_ids (instead
of generic_btf_ids) so they are accessible to all program types
including KPROBE, SOCKET_FILTER, NETFILTER, and TC/XDP.

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 | 247 +++++++++++++++
.../selftests/bpf/prog_tests/memmem_kfunc.c | 9 +
.../selftests/bpf/progs/memmem_kfunc.c | 299 ++++++++++++++++++
3 files changed, 555 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