From: Abhishek Khade <abhishekkhade69@gmail.com>
To: bpf@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, ast@kernel.org,
daniel@iogearbox.net, andrii@kernel.org,
Abhishek Khade <abhishekkhade69@gmail.com>
Subject: [PATCH bpf-next v3 0/2] bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs
Date: Sun, 22 Nov 2026 20:08:13 +0530 [thread overview]
Message-ID: <20261122143815.330528-1-abhishekkhade69@gmail.com> (raw)
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
next reply other threads:[~2026-11-22 14:38 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-22 14:38 Abhishek Khade [this message]
2026-11-22 14:38 ` [PATCH bpf-next v3 1/2] " Abhishek Khade
2026-11-22 15:17 ` bot+bpf-ci
2026-11-22 14:38 ` [PATCH bpf-next v3 2/2] selftests/bpf: Add selftests for " Abhishek Khade
2026-11-22 15:17 ` bot+bpf-ci
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=20261122143815.330528-1-abhishekkhade69@gmail.com \
--to=abhishekkhade69@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=linux-kernel@vger.kernel.org \
/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
all inboxes | Powered by JetHome®