On Thu, Aug 28, 2025 at 4:07 AM Rong Tao <rt...@foxmail.com> wrote: > > From: Rong Tao <rong...@cestc.cn> > > strnstr should not treat the ending '\0' of s2 as a matching character, > otherwise the parameter 'len' will be meaningless, for example: > > 1. bpf_strnstr("openat", "open", 4) = -ENOENT > 2. bpf_strnstr("openat", "open", 5) = 0 > > This patch makes (1) return 0, indicating a successful match. > > Signed-off-by: Rong Tao <rong...@cestc.cn> > --- > kernel/bpf/helpers.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) >
Add Fixes: tag? > diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c > index 401b4932cc49..ced7132980fe 100644 > --- a/kernel/bpf/helpers.c > +++ b/kernel/bpf/helpers.c > @@ -3672,10 +3672,12 @@ __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++) { that <= is a bit subtle, and combined with extra i+j==len check below is a bit confusing, so I think it would be good to add a short comment explaining this corner case you caught and that we are doing this to ensure that we matched entire s2 (and thus we need to find NUL to confirm). > __get_kernel_nofault(&c2, s2__ign + j, char, err_out); > if (c2 == '\0') > return i; > + if (i + j == len) > + break; > __get_kernel_nofault(&c1, s1__ign + j, char, err_out); > if (c1 == '\0') > return -ENOENT; > -- > 2.51.0 >