mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Viktor Malik <vmalik@redhat.com>
To: Rong Tao <rtoax@foxmail.com>,
	andrii.nakryiko@gmail.com, ast@kernel.org, daniel@iogearbox.net
Cc: Rong Tao <rongtao@cestc.cn>, Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@fomichev.me>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>, Mykola Lysenko <mykolal@fb.com>,
	Shuah Khan <shuah@kernel.org>,
	"open list:BPF [GENERAL] (Safe Dynamic Programs and Tools)"
	<bpf@vger.kernel.org>, open list <linux-kernel@vger.kernel.org>,
	"open list:KERNEL SELFTEST FRAMEWORK"
	<linux-kselftest@vger.kernel.org>
Subject: Re: [PATCH bpf-next v4 1/2] bpf/helpers: bpf_strnstr: Exact match length
Date: Fri, 29 Aug 2025 12:56:36 +0200	[thread overview]
Message-ID: <9a1973bf-88d7-4270-b979-d3ea0280a80b@redhat.com> (raw)
In-Reply-To: <tencent_CBD40091C14056E8298BE3725B65EE156405@qq.com>

On 8/29/25 04:12, Rong Tao wrote:
> From: Rong Tao <rongtao@cestc.cn>
> 
> strnstr should not treat the ending '\0' of s2 as a matching character
> if the parameter 'len' equal to s2 string length, for example:

A good catch, thanks!

But this doesn't fix just the `len == strlen(s2)` case but a more
general case when s2 is a suffix of the first len characters of s1,
right? The commit message should reflect that.

> 
>     1. bpf_strnstr("openat", "open", 4) = -ENOENT
>     2. bpf_strnstr("openat", "open", 5) = 0
> 
> This patch makes (1) return 0, indicating a successful match.
> 
> Fixes: e91370550f1f ("bpf: Add kfuncs for read-only string operations")
> Signed-off-by: Rong Tao <rongtao@cestc.cn>
> ---
>  kernel/bpf/helpers.c | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 401b4932cc49..bf04881f96ec 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -3672,10 +3672,18 @@ __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len
>  
>  	guard(pagefault)();
>  	for (i = 0; i < XATTR_SIZE_MAX; i++) {
> -		for (j = 0; i + j < len && j < XATTR_SIZE_MAX; j++) {
> +		for (j = 0; i + j <= len && j < XATTR_SIZE_MAX; j++) {
>  			__get_kernel_nofault(&c2, s2__ign + j, char, err_out);
>  			if (c2 == '\0')
>  				return i;
> +			/**
> +			 * corner case i+j==len to ensure that we matched
> +			 * entire s2. for example, param len=3:
> +			 *     s1: A B C D E F  -> i==1
> +			 *     s2:   B C D      -> j==2
> +			 */

This is not really a good example as it's not clear whether D is a null
byte or not. How about something like:

/**
 * We allow reading an extra byte from s2 (note the
 * `i + j <= len` above) to cover the case when s2 is
 * a suffix of the first len chars of s1.
 */

> +			if (i + j == len)
> +				break;

Viktor

>  			__get_kernel_nofault(&c1, s1__ign + j, char, err_out);
>  			if (c1 == '\0')
>  				return -ENOENT;


  reply	other threads:[~2025-08-29 10:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cover.1756433400.git.rongtao@cestc.cn>
2025-08-29  2:12 ` Rong Tao
2025-08-29 10:56   ` Viktor Malik [this message]
2025-08-29  2:13 ` [PATCH bpf-next v4 2/2] selftests/bpf: Add tests for bpf_strnstr Rong Tao

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=9a1973bf-88d7-4270-b979-d3ea0280a80b@redhat.com \
    --to=vmalik@redhat.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mykolal@fb.com \
    --cc=rongtao@cestc.cn \
    --cc=rtoax@foxmail.com \
    --cc=sdf@fomichev.me \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@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

all inboxes | Powered by JetHome®