bpf/helpers: bpf_strnstr: Exact match length
PATCH 1/1 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, 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 | 2 ++ 1 file changed, 2 insertions(+)
kernel/bpf/helpers.c +2 -0
@@ -3681,6 +3681,8 @@ __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t lenreturn -ENOENT ;if (c1 != c2)break ; +if (j == len -1 ) +return i; }if (j ==XATTR_SIZE_MAX )return -E2BIG ;
Signature · 1 line
2.51.0
cc'ing Viktor as well
Quoted 8 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
please add these cases to the tests
Quoted 13 lines
This patch makes (1) return 0, indicating a successful match.
Signed-off-by Rong Tao <[email protected]>--- kernel/bpf/helpers.c | 2 ++ 1 file changed, 2 insertions(+)kernel/bpf/helpers.c +0
-0
kernel/bpf/helpers.c __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len · line 3685
Quoted 5 lines
kernel/bpf/helpers.c +2
-0@@ -3681,6 +3681,8 @@ __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t lenreturn -ENOENT ;if (c1 != c2)break ; +if (j == len -1 ) +return i;
But this seems like a wrong fix. The API assumes that s2 is well-formed zero-terminated string, and so we shouldn't just randomly truncate it. Along the examples above, what will happen to bpf_strnstr("openat", "open", 3)? With your fix it will return success, right? But it shouldn't, IMO, because "open" wasn't really found in the first 3 characters of, effectively, "ope".
We should also test bpf_strnstr("", "", 0)... ;)
So maybe something like this (but I haven't really tested it):
kernel/bpf/helpers.c +0 -0
@@ -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;pw-bot: cr
Quoted 7 lines
kernel/bpf/helpers.c +0
-1}if (j ==XATTR_SIZE_MAX )return -E2BIG ; --2.51.0
Quoted 19 lines · Andrii Nakryiko
cc'ing Viktor as well
Quoted 7 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)= 0please add these cases to the tests
Quoted 7 lines
This patch makes (1) return 0, indicating a successful match.
Signed-off-by Rong Tao <[email protected]>--- kernel/bpf/helpers.c | 2 ++ 1 file changed, 2 insertions(+)
Quoted 11 lines
kernel/bpf/helpers.c __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len · line 3685
Quoted 10 lines
kernel/bpf/helpers.c +2
-0@@ -3681,6 +3681,8 @@ __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t lenreturn -ENOENT ;if (c1 != c2)break ; +if (j == len -1 ) +return i;But this seems like a wrong fix. The API assumes that s2 is
Thanks a lot Andrii Nakryiko, I just submit V2, please review.
Quoted 18 lines
well-formed zero-terminated string, and so we shouldn't just randomly truncate it. Along the examples above, what will happen to
bpf_strnstr("openat", "open", 3)? With your fix it will return success, right? But it shouldn't, IMO, because "open" wasn't really found in the first 3 characters of, effectively, "ope".We should also test
bpf_strnstr("", "", 0)... ;)So maybe something like this (but I haven't really tested it):
kernel/bpf/helpers.c +0
-0@@ -3672,10 +3672,12 @@ __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len
kernel/bpf/helpers.c __bpf_kfunc int bpf_strnstr(const char · line 3681
Quoted 10 lines
kernel/bpf/helpers.c +3
-1guard (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 ;
It's works fine, thanks.
Quoted 15 lines
kernel/bpf/helpers.c +0
-0__get_kernel_nofault (&c1, s1__ign + j, char, err_out);if (c1 =='\0' )return -ENOENT;pw-bot: cr
Quoted 7 lines
kernel/bpf/helpers.c +0
-1}if (j ==XATTR_SIZE_MAX )return -E2BIG ; --2.51.0