Skip to contentSkip to conversation

bpf/helpers: bpf_strnstr: Exact match length

PATCH Superseded Rong Tao 2 participants 3 messages Aug 27, 2025 – Aug 28, 2025 BPF

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Rong Tao

author
#0 lore raw

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
index 401b4932cc49..65bd0050c560 100644
@@ -3681,6 +3681,8 @@ __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len
 				return -ENOENT;
 			if (c1 != c2)
 				break;
+			if (j == len - 1)
+				return i;
 		}
 		if (j == XATTR_SIZE_MAX)
 			return -E2BIG;
Signature · 1 line
2.51.0
#1 lore raw

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.

---
 kernel/bpf/helpers.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/bpf/helpers.c +0 -0
index 401b4932cc49..65bd0050c560 100644

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 len
                                return -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
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;

pw-bot: cr

Quoted 7 lines
kernel/bpf/helpers.c +0 -1
                }
                if (j == XATTR_SIZE_MAX)
                        return -E2BIG;
--

2.51.0

author
#2 lore raw
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) = 0

please add these cases to the tests

Quoted 7 lines

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

---
  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
index 401b4932cc49..65bd0050c560 100644
@@ -3681,6 +3681,8 @@ __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len
                                 return -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
index 401b4932cc49..ced7132980fe 100644
@@ -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 -1
 
         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;

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