bpf/helpers: bpf_strnstr: Exact match length
Subsystems (3)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v4 1/2 bpf/helpers: bpf_strnstr: Exact match length
From: Rong Tao <[email protected]>
strnstr should not treat the ending '\0' of s2 as a matching character if the parameter 'len' equal to s2 string length, 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 | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
kernel/bpf/helpers.c +9 -1
@@ -3672,10 +3672,18 @@ __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t lenguard (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 +*/ +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
Quoted 4 lines · Rong Tao
From: Rong Tao <[email protected]>
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.
Quoted 21 lines
1.
bpf_strnstr("openat", "open", 4)=-ENOENT
2.bpf_strnstr("openat", "open", 5)= 0This patch makes (1) return 0, indicating a successful match.
Fixes e91370550f1f ("bpf: Add kfuncs for read-only string operations")Signed-off-by Rong Tao <[email protected]>--- kernel/bpf/helpers.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)kernel/bpf/helpers.c +0
-1@@ -3672,10 +3672,18 @@ __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t lenguard (pagefault)();for (i =0 ; i <XATTR_SIZE_MAX ; i++) { -for (j =0 ; i + j < len && j <XATTR_SIZE_MAX ; j++) {
kernel/bpf/helpers.c __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len · line 3684
Quoted 10 lines
kernel/bpf/helpers.c +7
-0+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.
*/
kernel/bpf/helpers.c __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len · line 3686
Quoted 2 lines
kernel/bpf/helpers.c +2
-0+if (i + j == len) +break ;
Viktor
Quoted 3 lines
kernel/bpf/helpers.c +0
-0__get_kernel_nofault (&c1, s1__ign + j, char, err_out);if (c1 =='\0' )return -ENOENT ;
PATCH v4 2/2 selftests/bpf: Add tests for bpf_strnstr
In reply to a message not in the archiveFrom: Rong Tao <[email protected]>
Add tests for bpf_strnstr():
bpf_strnstr("", "", 0) = 0
bpf_strnstr("hello world", "hello", 5) = 0
bpf_strnstr(str, "hello", 4) = -ENOENT
bpf_strnstr("", "a", 0) = -ENOENT--- tools/testing/selftests/bpf/progs/string_kfuncs_success.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/string_kfuncs_success.c +6 -2
@@ -30,8 +30,12 @@ __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(-ENOENT) int test_strnstr_notfound(void *ctx) { return bpf_strnstr(str, "hi", 10); } +__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_notfound1(void *ctx) { return bpf_strnstr(str, "hi", 10); } +__test(-ENOENT) int test_strnstr_notfound2(void *ctx) { return bpf_strnstr(str, "hello", 4); } +__test(-ENOENT) int test_strnstr_notfound3(void *ctx) { return bpf_strnstr("", "a", 0); } __test(0) int test_strnstr_empty(void *ctx) { return bpf_strnstr(str, "", 1); } char _license[] SEC("license") = "GPL";
Signature · 1 line
2.51.0