Skip to content

Commit 0715f71

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, fix just the `len == strlen(s2)` case. And fix a more general case when s2 is a suffix of the first len characters of s1. Fixes: e913705 ("bpf: Add kfuncs for read-only string operations") Signed-off-by: Rong Tao <[email protected]>
1 parent 27199db commit 0715f71

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

kernel/bpf/helpers.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3672,10 +3672,17 @@ __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+
* We allow reading an extra byte from s2 (note the
3681+
* `i + j <= len` above) to cover the case when s2 is
3682+
* a suffix of the first len chars of s1.
3683+
*/
3684+
if (i + j == len)
3685+
break;
36793686
__get_kernel_nofault(&c1, s1__ign + j, char, err_out);
36803687
if (c1 == '\0')
36813688
return -ENOENT;

0 commit comments

Comments
 (0)