Skip to contentSkip to conversation

bpf/helpers: bpf_strnstr: Exact match length

PATCH Superseded Rong Tao 2 participants 4 messages Aug 28, 2025 1 message not in the archive

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Rong Tao

author
#1 lore raw

PATCH v3 1/2 bpf/helpers: bpf_strnstr: Exact match length

In reply to a message not in the archive

From: Rong Tao <[email protected]>

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.

---
 kernel/bpf/helpers.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/bpf/helpers.c +3 -1
index 401b4932cc49..ced7132980fe 100644
@@ -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++) {
 			__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;
Signature · 1 line
2.51.0
author
#3 lore raw

PATCH v3 2/2 selftests/bpf: Add tests for bpf_strnstr

In reply to a message not in the archive

From: Rong Tao <[email protected]>

Add two tests for bpf_strnstr():

    bpf_strnstr("", "", 0) = 0
    bpf_strnstr("hello world", "hello", 5) = 0
---
 tools/testing/selftests/bpf/progs/string_kfuncs_success.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/progs/string_kfuncs_success.c +3 -1
index 46697f381878..1b56bd5860e9 100644
@@ -30,7 +30,9 @@ __test(2) int test_strcspn(void *ctx) { return bpf_strcspn(str, "lo"); }
 __test(6) int test_strstr_found(void *ctx) { return bpf_strstr(str, "world"); }
 __test(-ENOENT) int test_strstr_notfound(void *ctx) { return bpf_strstr(str, "hi"); }
 __test(0) int test_strstr_empty(void *ctx) { return bpf_strstr(str, ""); }
-__test(0) int test_strnstr_found(void *ctx) { return bpf_strnstr(str, "hello", 6); }
+__test(0) int test_strnstr_found1(void *ctx) { return bpf_strnstr("", "", 0); }
+__test(0) int test_strnstr_found2(void *ctx) { return bpf_strnstr(str, "hello", 5); }
+__test(0) int test_strnstr_found3(void *ctx) { return bpf_strnstr(str, "hello", 6); }
 __test(-ENOENT) int test_strnstr_notfound(void *ctx) { return bpf_strnstr(str, "hi", 10); }
 __test(0) int test_strnstr_empty(void *ctx) { return bpf_strnstr(str, "", 1); }
 
Signature · 1 line
2.51.0
#4 lore raw

Re: [PATCH bpf-next v3 2/2] selftests/bpf: Add tests for bpf_strnstr

Quoted 18 lines · Rong Tao

From: Rong Tao <[email protected]>

Add two tests for bpf_strnstr():

    bpf_strnstr("", "", 0) = 0
    bpf_strnstr("hello world", "hello", 5) = 0
---
 tools/testing/selftests/bpf/progs/string_kfuncs_success.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/progs/string_kfuncs_success.c +0 -0
index 46697f381878..1b56bd5860e9 100644

tools/testing/selftests/bpf/progs/string_kfuncs_success.c __test(2) int test_strcspn(void *ctx) { return bpf_strcspn(str, "lo"); } · line 34

Quoted 6 lines
tools/testing/selftests/bpf/progs/string_kfuncs_success.c +2 -1
@@ -30,7 +30,9 @@ __test(2) int test_strcspn(void *ctx) { return bpf_strcspn(str, "lo"); }
 __test(6) int test_strstr_found(void *ctx) { return bpf_strstr(str, "world"); }
 __test(-ENOENT) int test_strstr_notfound(void *ctx) { return bpf_strstr(str, "hi"); }
 __test(0) int test_strstr_empty(void *ctx) { return bpf_strstr(str, ""); }
-__test(0) int test_strnstr_found(void *ctx) { return bpf_strnstr(str, "hello", 6); }
+__test(0) int test_strnstr_found1(void *ctx) { return bpf_strnstr("", "", 0); }
+__test(0) int test_strnstr_found2(void *ctx) { return bpf_strnstr(str, "hello", 5); }

add (str, "hello", 4) == -ENOENT case?

Also let's add negative ("", "a", 0) == -ENOENT case?

pw-bot: cr

Quoted 7 lines
tools/testing/selftests/bpf/progs/string_kfuncs_success.c +1 -1
+__test(0) int test_strnstr_found3(void *ctx) { return bpf_strnstr(str, "hello", 6); }
 __test(-ENOENT) int test_strnstr_notfound(void *ctx) { return bpf_strnstr(str, "hi", 10); }
 __test(0) int test_strnstr_empty(void *ctx) { return bpf_strnstr(str, "", 1); }
 
--

2.51.0

#2 lore raw
In reply to Rong Tao
Quoted 9 lines · Rong Tao

From: Rong Tao <[email protected]>

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

Quoted 7 lines

This patch makes (1) return 0, indicating a successful match.

---
 kernel/bpf/helpers.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Add Fixes: tag?

kernel/bpf/helpers.c __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len · line 3675

Quoted 10 lines
kernel/bpf/helpers.c +1 -1
index 401b4932cc49..ced7132980fe 100644
@@ -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).

Quoted 11 lines
kernel/bpf/helpers.c +2 -1
                        __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