Skip to content

Commit 6541c66

Browse files
RtoaxKernel Patches Daemon
authored andcommitted
bpf/helpers: bpf_strnstr: Exact match length
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. Fixes: e913705 ("bpf: Add kfuncs for read-only string operations") Signed-off-by: Rong Tao <[email protected]>
1 parent 27199db commit 6541c66

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

kernel/bpf/helpers.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3672,10 +3672,18 @@ __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len
36723672

36733673
guard(pagefault)();
36743674
for (i = 0; i < XATTR_SIZE_MAX; i++) {
3675-
for (j = 0; i + j < len && j < XATTR_SIZE_MAX; j++) {
3675+
for (j = 0; i + j <= len && j < XATTR_SIZE_MAX; j++) {
36763676
__get_kernel_nofault(&c2, s2__ign + j, char, err_out);
36773677
if (c2 == '\0')
36783678
return i;
3679+
/**
3680+
* corner case i+j==len to ensure that we matched
3681+
* entire s2. for example, param len=3:
3682+
* s1: A B C D E F -> i==1
3683+
* s2: B C D -> j==2
3684+
*/
3685+
if (i + j == len)
3686+
break;
36793687
__get_kernel_nofault(&c1, s1__ign + j, char, err_out);
36803688
if (c1 == '\0')
36813689
return -ENOENT;

0 commit comments

Comments
 (0)