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 v2 1/2] bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs
Date: Sun, 22 Nov 2026 12:55:02 +0000	[thread overview]
Message-ID: <sashiko-outbox-227936@kernel.org> (raw)
In-Reply-To: <20261122123427.287521-2-abhishekkhade69@gmail.com>

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

  reply	other threads:[~2026-11-22 12:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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=sashiko-outbox-227936@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