BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Abhishek Khade" <abhishekkhade69@gmail.com>, bpf@vger.kernel.org
Subject: Re: [RFC PATCH bpf-next 1/2] bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs
Date: Sun, 22 Nov 2026 10:54:25 +0000	[thread overview]
Message-ID: <sashiko-outbox-227901@kernel.org> (raw)
In-Reply-To: <20261122103838.244974-2-abhishekkhade69@gmail.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] bpf: bpf_memmem() and bpf_dynptr_memmem() defined inside
  CONFIG_CGROUPS
- [High] bpf: quadratic O(N*M) complexity in __bpf_memmem() with 2 MB
  buffer cap
- [Medium] bpf: u64 size truncated to u32 in bpf_dynptr_memmem() before
  bounds check
- [Medium] bpf: bpf_memmem() and bpf_dynptr_memmem() placed in
  generic_btf_ids
--

commit 8544ed400181be07e2f78fbba3ee242f2ebcc135
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 in flat memory buffers and BPF dynptrs.

> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 937a0d7d68390..2f3e349955466 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -2994,6 +2994,119 @@ __bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task,
>  	return ret;
>  }
>  
> +#define BPF_MEMMEM_MAX_LEN (2 * 1024 * 1024) /* 2 MB max search buffer */
> +
> +/*
> + * In-kernel binary byte sequence search.
> + * Scans haystack for the first byte using optimized memchr(),
> + * then validates the remaining needle with 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;
> +
> +	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) {
> +		if (memcmp(h, n, needle_sz) == 0)
> +			return (void *)h;
> +		h++;
> +	}
> +
> +	return NULL;
> +}

[Severity: High]
Can __bpf_memmem() trigger RCU CPU stalls and soft lockups on repetitive
inputs?

Both bpf_memmem() and bpf_dynptr_memmem() allow haystack_sz and needle_sz
up to BPF_MEMMEM_MAX_LEN (2 MB), and __bpf_memmem() advances h by one byte
whenever memcmp() fails.  A 2 MB haystack of repeated zero bytes searched
for a 1 MB needle of zeroes ending in a non-zero byte runs the loop over
10^6 times and compares 1 MB on each step (~1.1 * 10^12 byte comparisons)
in a non-preemptible BPF context.

Also, the commit message notes that memchr() uses 64-bit word-at-a-time
detection, but the generic memchr() in lib/string.c (used on x86_64 and
other architectures without __HAVE_ARCH_MEMCHR) scans byte-by-byte.

[ ... ]

> +__bpf_kfunc long bpf_memmem(const void *haystack, u32 haystack__sz,
> +			    const void *needle, u32 needle__sz,
> +			    u64 flags)
> +{

[Severity: High]
Are __bpf_memmem(), bpf_memmem(), and bpf_dynptr_memmem() unintentionally
placed inside the #ifdef CONFIG_CGROUPS block?

They are inserted between bpf_task_under_cgroup() and
bpf_current_task_under_cgroup(), inside the #ifdef CONFIG_CGROUPS guard
that opens before bpf_cgroup_acquire() and closes after
bpf_task_get_cgroup1(), while their BTF_ID_FLAGS entries in
generic_btf_ids are emitted outside #ifdef CONFIG_CGROUPS.  When
CONFIG_CGROUPS is disabled and CONFIG_DEBUG_INFO_BTF is enabled,
resolve_btfids fails the build on the missing symbols (under
CONFIG_WERROR=y) or leaves zeroed BTF IDs that cause kfunc_init() to fail
registering generic_kfunc_set at boot.  Moving the definitions after the
#endif /* CONFIG_CGROUPS */ line avoids this.

[ ... ]

> +__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;
> +	u32 size;
> +
> +	if (flags)
> +		return -EINVAL;
> +	if (!ptr || !ptr->data || !needle || needle__sz == 0)
> +		return -EINVAL;
> +
> +	size = __bpf_dynptr_size(ptr);
> +	if (needle__sz > size)
> +		return -EINVAL;
> +	if (size > BPF_MEMMEM_MAX_LEN)
> +		return -E2BIG;

[Severity: Medium]
Does storing __bpf_dynptr_size(ptr) into u32 size truncate 64-bit dynptr
sizes before the bounds checks?

__bpf_dynptr_size() returns u64 because BPF_DYNPTR_TYPE_FILE stores a
64-bit size.  If a dynptr has a 64-bit size whose upper 32 bits are
non-zero and lower 32 bits are at most BPF_MEMMEM_MAX_LEN (for example, a
file dynptr adjusted to 4 GiB), truncating size to u32 bypasses the
size > BPF_MEMMEM_MAX_LEN (-E2BIG) check.  Declaring size as u64 avoids
the truncation.

[ ... ]

> @@ -5253,6 +5366,8 @@ BTF_ID_FLAGS(func, bpf_verify_pkcs7_signature, KF_SLEEPABLE)
>  #ifdef CONFIG_S390
>  BTF_ID_FLAGS(func, bpf_get_lowcore)
>  #endif
> +BTF_ID_FLAGS(func, bpf_memmem)
> +BTF_ID_FLAGS(func, bpf_dynptr_memmem)
>  BTF_KFUNCS_END(generic_btf_ids)

[Severity: Medium]
Should bpf_memmem and bpf_dynptr_memmem be registered in common_btf_ids
instead of generic_btf_ids?

kfunc_init() only registers generic_kfunc_set for TRACING, SCHED_CLS, XDP,
STRUCT_OPS, SYSCALL, and CGROUP_SKB, which prevents KPROBE, SOCKET_FILTER,
SK_SKB, SCHED_ACT, NETFILTER, and LWT programs from calling these kfuncs.
The other generic dynptr and string search kfuncs in kernel/bpf/helpers.c
are placed in common_btf_ids so they are registered for
BPF_PROG_TYPE_UNSPEC.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261122103838.244974-1-abhishekkhade69@gmail.com?part=1

  reply	other threads:[~2026-11-22 10:54 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 10:38 [RFC PATCH bpf-next 0/2] bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs Abhishek Khade
2026-11-22 10:38 ` [RFC PATCH bpf-next 1/2] " Abhishek Khade
2026-11-22 10:54   ` sashiko-bot [this message]
2026-11-22 11:26   ` bot+bpf-ci
2026-11-22 10:38 ` [RFC PATCH bpf-next 2/2] selftests/bpf: Add selftests for " Abhishek Khade
2026-11-22 11:26   ` 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=sashiko-outbox-227901@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=abhishekkhade69@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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