* [RFC PATCH bpf-next v2 0/2] bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs
@ 2026-11-22 12:34 Abhishek Khade
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
0 siblings, 2 replies; 4+ messages in thread
From: Abhishek Khade @ 2026-11-22 12:34 UTC (permalink / raw)
To: bpf; +Cc: ast, daniel, andrii, memxor, linux-kernel, Abhishek Khade
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* [RFC PATCH bpf-next v2 1/2] bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs
2026-11-22 12:34 [RFC PATCH bpf-next v2 0/2] bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs Abhishek Khade
@ 2026-11-22 12:34 ` Abhishek Khade
2026-11-22 12:55 ` sashiko-bot
2026-11-22 12:34 ` [RFC PATCH bpf-next v2 2/2] selftests/bpf: Add selftests for " Abhishek Khade
1 sibling, 1 reply; 4+ messages in thread
From: Abhishek Khade @ 2026-11-22 12:34 UTC (permalink / raw)
To: bpf; +Cc: ast, daniel, andrii, memxor, linux-kernel, Abhishek Khade
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
floating-offset 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.
Currently, developers attempting to search variable-length binary
payloads must choose between:
1. Writing a loop using bpf_loop() or open-coded iterators: Even with
verifier inlining, this executes 1 subprogram call per byte and
cannot leverage 64-bit word-at-a-time SIMD scanning, making it
significantly slower than kernel-internal scanning.
2. Streaming raw buffers to user space via BPF ring buffers: This
induces heavy memory copying and context-switch latency, often
leading to ring buffer drops under high-throughput workloads.
To address this gap, this RFC proposes two complementary kfuncs in
kernel/bpf/helpers.c:
- bpf_memmem(): Searches for a byte sequence within a flat memory slice.
- bpf_dynptr_memmem(): Searches for a byte sequence within a dynamic
pointer (struct bpf_dynptr).
Both functions wrap an internal allocation-free scanning routine based
on the kernel's optimized memchr() followed by memcmp(), returning the
matched byte offset or -ENOENT. Buffer sizes are capped at
BPF_MEMMEM_MAX_LEN (2 MB) and needle patterns at BPF_MEMMEM_MAX_NEEDLE_SZ
(4 KB). To prevent pathological quadratic O(N * M) RCU CPU stalls,
cumulative byte comparisons across memcmp calls are strictly bounded by
BPF_MEMMEM_MAX_LEN. 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.
Signed-off-by: Abhishek Khade <abhishekkhade69@gmail.com>
---
kernel/bpf/helpers.c | 116 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 116 insertions(+)
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 6788489a2..1f27ad21a 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -3406,6 +3406,120 @@ __bpf_kfunc int bpf_dynptr_memset(const struct bpf_dynptr *p, u64 offset, u64 si
return 0;
}
+#define BPF_MEMMEM_MAX_LEN (2 * 1024 * 1024)
+#define BPF_MEMMEM_MAX_NEEDLE_SZ 4096
+
+/* In-kernel binary byte sequence search using memchr() and memcmp() */
+static void *__bpf_memmem(const void *haystack, size_t haystack_sz,
+ const void *needle, size_t needle_sz)
+{
+ const char *h = haystack;
+ const char *n = needle;
+ const char *end;
+ size_t total_cmp = 0;
+
+ if (needle_sz == 0)
+ return (void *)haystack;
+ if (needle_sz > haystack_sz)
+ return NULL;
+ if (needle_sz == 1)
+ return memchr(haystack, n[0], haystack_sz);
+
+ end = h + haystack_sz - needle_sz + 1;
+ while ((h = memchr(h, n[0], end - h)) != NULL) {
+ total_cmp += needle_sz;
+ if (total_cmp > BPF_MEMMEM_MAX_LEN)
+ return NULL;
+ if (memcmp(h, n, needle_sz) == 0)
+ return (void *)h;
+ h++;
+ }
+
+ return NULL;
+}
+
+/**
+ * bpf_memmem - search for needle byte pattern in a flat memory buffer
+ * @haystack: pointer to haystack memory buffer
+ * @haystack__sz: size of haystack buffer in bytes
+ * @needle: pointer to byte pattern to search for
+ * @needle__sz: size of pattern in bytes
+ * @flags: reserved for future options (must be 0)
+ *
+ * Return:
+ * * >=0 - 0-based byte offset of match in haystack
+ * * %-ENOENT - needle was not found in haystack
+ * * %-EINVAL - invalid flags or parameters
+ * * %-E2BIG - haystack exceeds 2 MB or needle exceeds 4 KB
+ */
+__bpf_kfunc long bpf_memmem(const void *haystack, u32 haystack__sz,
+ const void *needle, u32 needle__sz,
+ u64 flags)
+{
+ const void *match;
+
+ if (flags)
+ return -EINVAL;
+ if (!haystack || !needle || needle__sz == 0 || haystack__sz == 0)
+ return -EINVAL;
+ if (needle__sz > haystack__sz)
+ return -EINVAL;
+ if (haystack__sz > BPF_MEMMEM_MAX_LEN || needle__sz > BPF_MEMMEM_MAX_NEEDLE_SZ)
+ return -E2BIG;
+
+ match = __bpf_memmem(haystack, haystack__sz, needle, needle__sz);
+ if (!match)
+ return -ENOENT;
+
+ return (long)((const char *)match - (const char *)haystack);
+}
+
+/**
+ * bpf_dynptr_memmem - search for needle within a dynamic pointer
+ * @haystack: pointer to dynptr representing the search buffer
+ * @needle: pointer to pattern to search for
+ * @needle__sz: size of pattern in bytes
+ * @flags: reserved for future options (must be 0)
+ *
+ * Return:
+ * * >=0 - 0-based byte offset of match in dynptr buffer
+ * * %-ENOENT - needle was not found
+ * * %-EINVAL - invalid flags, non-linear buffer, or parameters
+ * * %-E2BIG - dynptr buffer exceeds 2 MB or needle exceeds 4 KB
+ */
+__bpf_kfunc long bpf_dynptr_memmem(const struct bpf_dynptr *haystack,
+ const void *needle, u32 needle__sz,
+ u64 flags)
+{
+ struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)haystack;
+ const void *data;
+ const void *match;
+ u64 size;
+
+ if (flags)
+ return -EINVAL;
+ if (!ptr || !needle || needle__sz == 0)
+ return -EINVAL;
+ if (needle__sz > BPF_MEMMEM_MAX_NEEDLE_SZ)
+ return -E2BIG;
+
+ size = __bpf_dynptr_size(ptr);
+ if (size == 0 || needle__sz > size)
+ return -EINVAL;
+ if (size > BPF_MEMMEM_MAX_LEN)
+ return -E2BIG;
+
+ data = __bpf_dynptr_data(ptr, size);
+ if (!data)
+ return -EINVAL;
+
+ match = __bpf_memmem(data, size, needle, needle__sz);
+ if (!match)
+ return -ENOENT;
+
+ return (long)((const char *)match - (const char *)data);
+}
+
__bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
{
return obj;
@@ -5214,6 +5328,8 @@ BTF_ID_FLAGS(func, bpf_dynptr_size)
BTF_ID_FLAGS(func, bpf_dynptr_clone)
BTF_ID_FLAGS(func, bpf_dynptr_copy)
BTF_ID_FLAGS(func, bpf_dynptr_memset)
+BTF_ID_FLAGS(func, bpf_memmem)
+BTF_ID_FLAGS(func, bpf_dynptr_memmem)
#ifdef CONFIG_NET
BTF_ID_FLAGS(func, bpf_modify_return_test_tp)
#endif
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [RFC PATCH bpf-next v2 2/2] selftests/bpf: Add selftests for bpf_memmem and bpf_dynptr_memmem kfuncs
2026-11-22 12:34 [RFC PATCH bpf-next v2 0/2] bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs Abhishek Khade
2026-11-22 12:34 ` [RFC PATCH bpf-next v2 1/2] " Abhishek Khade
@ 2026-11-22 12:34 ` Abhishek Khade
1 sibling, 0 replies; 4+ messages in thread
From: Abhishek Khade @ 2026-11-22 12:34 UTC (permalink / raw)
To: bpf; +Cc: ast, daniel, andrii, memxor, linux-kernel, Abhishek Khade
Add automated BPF selftests validating in-kernel binary byte sequence
matching for bpf_memmem() and bpf_dynptr_memmem().
Covered test cases:
1. Binary pattern match across embedded null bytes (0x00).
2. Match at buffer start (offset 0).
3. Match at buffer end (haystack_sz - needle_sz).
4. Single-byte needle fastpath.
5. Non-matching pattern returning -ENOENT.
6. Multiple candidate occurrences returning the first match.
7. Overlapping candidate prefixes returning the correct match.
8. Exact match where needle equals haystack.
9. Needles larger than haystack returning -EINVAL.
10. Empty needle returning -EINVAL.
11. Invalid flags returning -EINVAL.
12. Oversized haystack (> 2 MB) returning -E2BIG.
13. Exact 2 MB boundary full scan.
14. Zero-length haystack returning -EINVAL.
15. Dynamic pointer (bpf_dynptr) searching across embedded nulls,
start, end, not found, size boundary, and error cases.
Signed-off-by: Abhishek Khade <abhishekkhade69@gmail.com>
---
.../selftests/bpf/prog_tests/memmem_kfunc.c | 9 +
.../selftests/bpf/progs/memmem_kfunc.c | 259 ++++++++++++++++++
2 files changed, 268 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/memmem_kfunc.c
create mode 100644 tools/testing/selftests/bpf/progs/memmem_kfunc.c
diff --git a/tools/testing/selftests/bpf/prog_tests/memmem_kfunc.c b/tools/testing/selftests/bpf/prog_tests/memmem_kfunc.c
new file mode 100644
index 000000000..d27c9a2a2
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/memmem_kfunc.c
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Abhishek Khade */
+#include <test_progs.h>
+#include "memmem_kfunc.skel.h"
+
+void test_memmem_kfunc(void)
+{
+ RUN_TESTS(memmem_kfunc);
+}
diff --git a/tools/testing/selftests/bpf/progs/memmem_kfunc.c b/tools/testing/selftests/bpf/progs/memmem_kfunc.c
new file mode 100644
index 000000000..8abd06e97
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/memmem_kfunc.c
@@ -0,0 +1,259 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Abhishek Khade */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "errno.h"
+
+char _license[] SEC("license") = "GPL";
+
+/* kfunc prototypes */
+long bpf_memmem(const void *haystack, u32 haystack__sz,
+ const void *needle, u32 needle__sz,
+ u64 flags) __ksym;
+
+long bpf_dynptr_memmem(const struct bpf_dynptr *haystack,
+ const void *needle, u32 needle__sz,
+ u64 flags) __ksym;
+
+static char bin_data[] = {
+ 0x16, 0x03, 0x01, 0x00, 0x02, 0xaa, 0xbb, 0x00, 0xff, 0x00, 0x55
+};
+
+static char needle_null[] = { 0x00, 0xff, 0x00 };
+static char needle_start[] = { 0x16, 0x03, 0x01 };
+static char needle_end[] = { 0x00, 0x55 };
+static char needle_single[] = { 0xaa };
+static char needle_notfound[] = { 0xde, 0xad };
+static char needle_repeat[] = { 0xaa, 0xbb, 0x00, 0xaa, 0xbb };
+static char needle_sub_repeat[] = { 0xaa, 0xbb };
+static char overlap_haystack[] = { 0xaa, 0xaa, 0xaa, 0xbb };
+static char overlap_needle[] = { 0xaa, 0xaa, 0xbb };
+static char oversized_buf[2 * 1024 * 1024 + 1];
+
+/* 1. Flat memory buffer tests */
+
+SEC("syscall")
+__success __retval(7)
+int test_memmem_embedded_null(void *ctx)
+{
+ return bpf_memmem(bin_data, sizeof(bin_data),
+ needle_null, sizeof(needle_null), 0);
+}
+
+SEC("syscall")
+__success __retval(0)
+int test_memmem_start(void *ctx)
+{
+ return bpf_memmem(bin_data, sizeof(bin_data),
+ needle_start, sizeof(needle_start), 0);
+}
+
+SEC("syscall")
+__success __retval(9)
+int test_memmem_end(void *ctx)
+{
+ return bpf_memmem(bin_data, sizeof(bin_data),
+ needle_end, sizeof(needle_end), 0);
+}
+
+SEC("syscall")
+__success __retval(5)
+int test_memmem_single_byte(void *ctx)
+{
+ return bpf_memmem(bin_data, sizeof(bin_data),
+ needle_single, sizeof(needle_single), 0);
+}
+
+SEC("syscall")
+__success __retval(-ENOENT)
+int test_memmem_not_found(void *ctx)
+{
+ return bpf_memmem(bin_data, sizeof(bin_data),
+ needle_notfound, sizeof(needle_notfound), 0);
+}
+
+SEC("syscall")
+__success __retval(0)
+int test_memmem_first_occurrence(void *ctx)
+{
+ return bpf_memmem(needle_repeat, sizeof(needle_repeat),
+ needle_sub_repeat, sizeof(needle_sub_repeat), 0);
+}
+
+SEC("syscall")
+__success __retval(1)
+int test_memmem_overlapping_prefix(void *ctx)
+{
+ return bpf_memmem(overlap_haystack, sizeof(overlap_haystack),
+ overlap_needle, sizeof(overlap_needle), 0);
+}
+
+SEC("syscall")
+__success __retval(0)
+int test_memmem_exact_match(void *ctx)
+{
+ return bpf_memmem(needle_start, sizeof(needle_start),
+ needle_start, sizeof(needle_start), 0);
+}
+
+SEC("syscall")
+__success __retval(-EINVAL)
+int test_memmem_needle_too_large(void *ctx)
+{
+ return bpf_memmem(needle_start, sizeof(needle_start),
+ bin_data, sizeof(bin_data), 0);
+}
+
+SEC("syscall")
+__success __retval(-EINVAL)
+int test_memmem_empty_needle(void *ctx)
+{
+ return bpf_memmem(bin_data, sizeof(bin_data),
+ needle_start, 0, 0);
+}
+
+SEC("syscall")
+__success __retval(-EINVAL)
+int test_memmem_invalid_flags(void *ctx)
+{
+ return bpf_memmem(bin_data, sizeof(bin_data),
+ needle_start, sizeof(needle_start), 1);
+}
+
+SEC("syscall")
+__success __retval(-E2BIG)
+int test_memmem_oversized_haystack(void *ctx)
+{
+ return bpf_memmem(oversized_buf, sizeof(oversized_buf),
+ needle_single, sizeof(needle_single), 0);
+}
+
+SEC("syscall")
+__success __retval(-ENOENT)
+int test_memmem_exact_2mb(void *ctx)
+{
+ return bpf_memmem(oversized_buf, 2 * 1024 * 1024,
+ needle_single, sizeof(needle_single), 0);
+}
+
+SEC("syscall")
+__success __retval(-EINVAL)
+int test_memmem_zero_len_haystack(void *ctx)
+{
+ return bpf_memmem(bin_data, 0,
+ needle_single, sizeof(needle_single), 0);
+}
+
+/* 2. Dynamic pointer (bpf_dynptr) tests */
+
+SEC("syscall")
+__success __retval(7)
+int test_dynptr_memmem_embedded_null(void *ctx)
+{
+ struct bpf_dynptr ptr;
+
+ bpf_dynptr_from_mem((void *)bin_data, sizeof(bin_data), 0, &ptr);
+ return bpf_dynptr_memmem(&ptr, needle_null, sizeof(needle_null), 0);
+}
+
+SEC("syscall")
+__success __retval(0)
+int test_dynptr_memmem_start(void *ctx)
+{
+ struct bpf_dynptr ptr;
+
+ bpf_dynptr_from_mem((void *)bin_data, sizeof(bin_data), 0, &ptr);
+ return bpf_dynptr_memmem(&ptr, needle_start, sizeof(needle_start), 0);
+}
+
+SEC("syscall")
+__success __retval(9)
+int test_dynptr_memmem_end(void *ctx)
+{
+ struct bpf_dynptr ptr;
+
+ bpf_dynptr_from_mem((void *)bin_data, sizeof(bin_data), 0, &ptr);
+ return bpf_dynptr_memmem(&ptr, needle_end, sizeof(needle_end), 0);
+}
+
+SEC("syscall")
+__success __retval(0)
+int test_dynptr_memmem_exact_match(void *ctx)
+{
+ struct bpf_dynptr ptr;
+
+ bpf_dynptr_from_mem((void *)needle_start, sizeof(needle_start), 0, &ptr);
+ return bpf_dynptr_memmem(&ptr, needle_start, sizeof(needle_start), 0);
+}
+
+SEC("syscall")
+__success __retval(-EINVAL)
+int test_dynptr_memmem_needle_too_large(void *ctx)
+{
+ struct bpf_dynptr ptr;
+
+ bpf_dynptr_from_mem((void *)needle_start, sizeof(needle_start), 0, &ptr);
+ return bpf_dynptr_memmem(&ptr, bin_data, sizeof(bin_data), 0);
+}
+
+SEC("syscall")
+__success __retval(-ENOENT)
+int test_dynptr_memmem_not_found(void *ctx)
+{
+ struct bpf_dynptr ptr;
+
+ bpf_dynptr_from_mem((void *)bin_data, sizeof(bin_data), 0, &ptr);
+ return bpf_dynptr_memmem(&ptr, needle_notfound, sizeof(needle_notfound), 0);
+}
+
+SEC("syscall")
+__success __retval(-EINVAL)
+int test_dynptr_memmem_empty_needle(void *ctx)
+{
+ struct bpf_dynptr ptr;
+
+ bpf_dynptr_from_mem((void *)bin_data, sizeof(bin_data), 0, &ptr);
+ return bpf_dynptr_memmem(&ptr, needle_start, 0, 0);
+}
+
+SEC("syscall")
+__success __retval(-EINVAL)
+int test_dynptr_memmem_invalid_flags(void *ctx)
+{
+ struct bpf_dynptr ptr;
+
+ bpf_dynptr_from_mem((void *)bin_data, sizeof(bin_data), 0, &ptr);
+ return bpf_dynptr_memmem(&ptr, needle_start, sizeof(needle_start), 1);
+}
+
+SEC("syscall")
+__success __retval(-E2BIG)
+int test_dynptr_memmem_oversized_haystack(void *ctx)
+{
+ struct bpf_dynptr ptr;
+
+ bpf_dynptr_from_mem((void *)oversized_buf, sizeof(oversized_buf), 0, &ptr);
+ return bpf_dynptr_memmem(&ptr, needle_single, sizeof(needle_single), 0);
+}
+
+SEC("syscall")
+__success __retval(-ENOENT)
+int test_dynptr_memmem_exact_2mb(void *ctx)
+{
+ struct bpf_dynptr ptr;
+
+ bpf_dynptr_from_mem((void *)oversized_buf, 2 * 1024 * 1024, 0, &ptr);
+ return bpf_dynptr_memmem(&ptr, needle_single, sizeof(needle_single), 0);
+}
+
+SEC("syscall")
+__success __retval(-EINVAL)
+int test_dynptr_memmem_zero_len_haystack(void *ctx)
+{
+ struct bpf_dynptr ptr;
+
+ bpf_dynptr_from_mem((void *)bin_data, 0, 0, &ptr);
+ return bpf_dynptr_memmem(&ptr, needle_single, sizeof(needle_single), 0);
+}
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [RFC PATCH bpf-next v2 1/2] bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs
2026-11-22 12:34 ` [RFC PATCH bpf-next v2 1/2] " Abhishek Khade
@ 2026-11-22 12:55 ` sashiko-bot
0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-22 12:55 UTC (permalink / raw)
To: Abhishek Khade, bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] bpf: premature budget abort and false -ENOENT in __bpf_memmem()
--
commit f4f47306c408f1db78f7a970c2e8068a144b300f
Author: Abhishek Khade <abhishekkhade69@gmail.com>
bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs
This commit adds the bpf_memmem() and bpf_dynptr_memmem() kfuncs to search
for binary byte sequences within flat memory buffers and dynamic pointers.
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 937a0d7d68390..46336909c48a3 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -3409,6 +3409,120 @@ __bpf_kfunc int bpf_dynptr_memset(const struct bpf_dynptr *p, u64 offset, u64 si
> return 0;
> }
>
> +#define BPF_MEMMEM_MAX_LEN (2 * 1024 * 1024)
> +#define BPF_MEMMEM_MAX_NEEDLE_SZ 4096
> +
> +/* In-kernel binary byte sequence search using memchr() and memcmp() */
> +static void *__bpf_memmem(const void *haystack, size_t haystack_sz,
> + const void *needle, size_t needle_sz)
> +{
> + const char *h = haystack;
> + const char *n = needle;
> + const char *end;
> + size_t total_cmp = 0;
> +
> + if (needle_sz == 0)
> + return (void *)haystack;
> + if (needle_sz > haystack_sz)
> + return NULL;
> + if (needle_sz == 1)
> + return memchr(haystack, n[0], haystack_sz);
> +
> + end = h + haystack_sz - needle_sz + 1;
> + while ((h = memchr(h, n[0], end - h)) != NULL) {
> + total_cmp += needle_sz;
> + if (total_cmp > BPF_MEMMEM_MAX_LEN)
> + return NULL;
> + if (memcmp(h, n, needle_sz) == 0)
> + return (void *)h;
> + h++;
> + }
> +
> + return NULL;
> +}
[Severity: High]
Can __bpf_memmem() exhaust total_cmp prematurely and cause bpf_memmem() and
bpf_dynptr_memmem() to return a false -ENOENT when the needle is present?
Adding the full needle_sz to total_cmp on every memchr() match charges for
needle_sz comparisons even when memcmp() mismatches on h[1]. With a 4096
byte needle, 512 occurrences of n[0] before the real match push total_cmp
past BPF_MEMMEM_MAX_LEN on the 513th occurrence and abort the scan early.
Because __bpf_memmem() returns NULL on this budget abort, both callers turn
it into -ENOENT, making a complexity bailout indistinguishable from a
missing pattern and allowing trivial evasion when scanning untrusted
payloads.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261122123427.287521-1-abhishekkhade69@gmail.com?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-22 12:55 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-22 12:34 [RFC PATCH bpf-next v2 0/2] bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs Abhishek Khade
2026-11-22 12:34 ` [RFC PATCH bpf-next v2 1/2] " Abhishek Khade
2026-11-22 12:55 ` sashiko-bot
2026-11-22 12:34 ` [RFC PATCH bpf-next v2 2/2] selftests/bpf: Add selftests for " Abhishek Khade
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).