From: Abhishek Khade <abhishekkhade69@gmail.com>
To: bpf@vger.kernel.org
Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
memxor@gmail.com, linux-kernel@vger.kernel.org,
Abhishek Khade <abhishekkhade69@gmail.com>
Subject: [RFC PATCH bpf-next v2 0/2] bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs
Date: Sun, 22 Nov 2026 18:04:25 +0530 [thread overview]
Message-ID: <20261122123427.287521-1-abhishekkhade69@gmail.com> (raw)
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
next reply other threads:[~2026-11-22 12:34 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-22 12:34 Abhishek Khade [this message]
2026-11-22 12:34 ` [RFC PATCH bpf-next v2 1/2] " Abhishek Khade
2026-11-22 12:34 ` [RFC PATCH bpf-next v2 2/2] selftests/bpf: Add selftests for " Abhishek Khade
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=20261122123427.287521-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 \
--cc=memxor@gmail.com \
/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®