* [PATCH bpf-next v5 1/2] bpf: add bpf_strcat,bpf_strncat kfunc [not found] <cover.1784695085.git.rtoax@foxmail.com> @ 2026-07-22 4:41 ` Rong Tao 2026-07-22 6:19 ` bot+bpf-ci 2026-07-27 22:56 ` Andrii Nakryiko 2026-07-22 4:41 ` [PATCH bpf-next v5 2/2] selftests/bpf: Test bpf_strcat,bpf_strncat kfuncs Rong Tao 1 sibling, 2 replies; 7+ messages in thread From: Rong Tao @ 2026-07-22 4:41 UTC (permalink / raw) To: vmalik, andrii, ast Cc: rtoax, rongtao, Daniel Borkmann, Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis, Shuah Khan, Yuzuki Ishiyama, open list:BPF [GENERAL] (Safe Dynamic Programs and Tools), open list, open list:KERNEL SELFTEST FRAMEWORK From: Rong Tao <rongtao@cestc.cn> Add string concatenation kfuncs, prototype: int bpf_strcat(char *dst, u32 dst__sz, const char *src); int bpf_strncat(char *dst, u32 dst__sz, const char *src, u32 len); This differs from the glibc library functions strcat and strncat, which, for safety reasons, require the size of the target string's memory space as a parameter. The src parameter cannot directly pass a string constant, such as "XYZ", otherwise it will throw an error "write into map forbidden". Signed-off-by: Rong Tao <rongtao@cestc.cn> --- kernel/bpf/helpers.c | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index c18f1e16edee..85afff60365c 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -4195,6 +4195,79 @@ __bpf_kfunc int bpf_strncasestr(const char *s1__ign, const char *s2__ign, return __bpf_strnstr(s1__ign, s2__ign, len, true); } +static int __bpf_strncat(char *dst, u32 dsz, const char *src, u32 sz) +{ + int dlen = 0, slen, space, copied; + + if (sz == 0 || dsz == 0) + return -EINVAL; + + while (dlen < dsz && dst[dlen] != '\0') + dlen++; + + if (dlen >= dsz) + return -E2BIG; + + space = dsz - dlen; + + slen = bpf_strnlen(src, sz); + if (space <= slen) + return -E2BIG; + + copied = strncpy_from_kernel_nofault(dst + dlen, src, sz); + if (copied < 0) + return copied; + else if (copied == 0 || copied == 1) + return dlen; + + /* The copied character count includes '\0'. */ + return dlen + copied - 1; +} + +/** + * bpf_strcat - Append non-null bytes from a source string, and null-terminate + * the result + * @dst: Destination string. + * @dst__sz: Maximum bytes of @dst__ign, includes the trailing NUL. + * @src: Source string. + * + * Return: + * * >=0 - Length of the concatenated string. + * + * * %-EINVAL - String @dst__ign is invalid. + * * %-EFAULT - Cannot read or write one of the strings. + * * %-E2BIG - String @src__ign is too large or the remaining space in + * @dst__ign is too small. + * * %-ERANGE - One of the strings is outside of kernel address space + */ +__bpf_kfunc int bpf_strcat(char *dst, u32 dst__sz, const char *src) +{ + return __bpf_strncat(dst, dst__sz, src, dst__sz); +} + +/** + * bpf_strncat - Append non-null bytes from a source string, and null-terminate + * the result + * @dst: Destination string. + * @dst__sz: Maximum bytes of @dst__ign, includes the trailing NUL. + * @src: Source string. + * @len: the maximum number of characters to concatenate, the null terminator + * \0 of the target string @dst will also be included. + * + * Return: + * * >=0 - Length of the concatenated string. + * + * * %-EINVAL - String @dst__ign is invalid. + * * %-EFAULT - Cannot read or write one of the strings. + * * %-E2BIG - String @src__ign is too large or the remaining space in + * @dst__ign is too small. + * * %-ERANGE - One of the strings is outside of kernel address space + */ +__bpf_kfunc int bpf_strncat(char *dst, u32 dst__sz, const char *src, u32 len) +{ + return __bpf_strncat(dst, dst__sz, src, len); +} + #ifdef CONFIG_KEYS /** * bpf_lookup_user_key - lookup a key by its serial @@ -4958,6 +5031,8 @@ BTF_ID_FLAGS(func, bpf_strstr); BTF_ID_FLAGS(func, bpf_strcasestr); BTF_ID_FLAGS(func, bpf_strnstr); BTF_ID_FLAGS(func, bpf_strncasestr); +BTF_ID_FLAGS(func, bpf_strcat); +BTF_ID_FLAGS(func, bpf_strncat); #if defined(CONFIG_BPF_LSM) && defined(CONFIG_CGROUPS) BTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU) #endif -- 2.55.0 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH bpf-next v5 1/2] bpf: add bpf_strcat,bpf_strncat kfunc 2026-07-22 4:41 ` [PATCH bpf-next v5 1/2] bpf: add bpf_strcat,bpf_strncat kfunc Rong Tao @ 2026-07-22 6:19 ` bot+bpf-ci 2026-07-27 22:56 ` Andrii Nakryiko 1 sibling, 0 replies; 7+ messages in thread From: bot+bpf-ci @ 2026-07-22 6:19 UTC (permalink / raw) To: rtoax, vmalik, andrii, ast Cc: rtoax, rongtao, daniel, eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil, shuah, ishiyama, bpf, linux-kernel, linux-kselftest, ast, andrii, daniel, martin.lau, eddyz87, yonghong.song, clm, ihor.solodrai [-- Attachment #1: Type: text/plain, Size: 1679 bytes --] > diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c > index c18f1e16edee..85afff60365c 100644 > --- a/kernel/bpf/helpers.c > +++ b/kernel/bpf/helpers.c > @@ -4195,6 +4195,79 @@ __bpf_kfunc int bpf_strncasestr(const char *s1__ign, const char *s2__ign, > return __bpf_strnstr(s1__ign, s2__ign, len, true); > } > > +static int __bpf_strncat(char *dst, u32 dsz, const char *src, u32 sz) > +{ > + int dlen = 0, slen, space, copied; > + > + if (sz == 0 || dsz == 0) > + return -EINVAL; > + > + while (dlen < dsz && dst[dlen] != '\0') > + dlen++; > + > + if (dlen >= dsz) > + return -E2BIG; > + > + space = dsz - dlen; > + > + slen = bpf_strnlen(src, sz); This came up in the earlier review of the series: the length of src does not need to be computed here, since strncpy_from_kernel_nofault() already bounds the copy by sz. https://lore.kernel.org/bpf/3e73aeb2-afce-4ae9-9163-51e4dc1ded01@redhat.com/ That bpf_strnlen(src, sz) call was dropped in an earlier revision and has returned in this one. Is the separate src length scan still needed here, or can strncpy_from_kernel_nofault() handle the bound on its own? > + if (space <= slen) > + return -E2BIG; > + > + copied = strncpy_from_kernel_nofault(dst + dlen, src, sz); > + if (copied < 0) > + return copied; > + else if (copied == 0 || copied == 1) > + return dlen; > + > + /* The copied character count includes '\0'. */ > + return dlen + copied - 1; > +} [ ... ] --- AI reviewed your patch. Please fix the bug or email reply why it's not a bug. See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md CI run summary: https://github.com/kernel-patches/bpf/actions/runs/29894636510 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH bpf-next v5 1/2] bpf: add bpf_strcat,bpf_strncat kfunc 2026-07-22 4:41 ` [PATCH bpf-next v5 1/2] bpf: add bpf_strcat,bpf_strncat kfunc Rong Tao 2026-07-22 6:19 ` bot+bpf-ci @ 2026-07-27 22:56 ` Andrii Nakryiko 2026-07-28 2:50 ` Rong Tao 1 sibling, 1 reply; 7+ messages in thread From: Andrii Nakryiko @ 2026-07-27 22:56 UTC (permalink / raw) To: Rong Tao Cc: vmalik, andrii, ast, rongtao, Daniel Borkmann, Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis, Shuah Khan, Yuzuki Ishiyama, open list:BPF [GENERAL] (Safe Dynamic Programs and Tools), open list, open list:KERNEL SELFTEST FRAMEWORK On Tue, Jul 21, 2026 at 9:42 PM Rong Tao <rtoax@foxmail.com> wrote: > > From: Rong Tao <rongtao@cestc.cn> > > Add string concatenation kfuncs, prototype: > > int bpf_strcat(char *dst, u32 dst__sz, const char *src); > int bpf_strncat(char *dst, u32 dst__sz, const char *src, u32 len); > > This differs from the glibc library functions strcat and strncat, which, > for safety reasons, require the size of the target string's memory space > as a parameter. > > The src parameter cannot directly pass a string constant, such as "XYZ", > otherwise it will throw an error "write into map forbidden". > > Signed-off-by: Rong Tao <rongtao@cestc.cn> > --- > kernel/bpf/helpers.c | 75 ++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 75 insertions(+) > > diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c > index c18f1e16edee..85afff60365c 100644 > --- a/kernel/bpf/helpers.c > +++ b/kernel/bpf/helpers.c > @@ -4195,6 +4195,79 @@ __bpf_kfunc int bpf_strncasestr(const char *s1__ign, const char *s2__ign, > return __bpf_strnstr(s1__ign, s2__ign, len, true); > } > > +static int __bpf_strncat(char *dst, u32 dsz, const char *src, u32 sz) > +{ > + int dlen = 0, slen, space, copied; > + > + if (sz == 0 || dsz == 0) > + return -EINVAL; > + > + while (dlen < dsz && dst[dlen] != '\0') > + dlen++; > + > + if (dlen >= dsz) > + return -E2BIG; > + > + space = dsz - dlen; > + > + slen = bpf_strnlen(src, sz); > + if (space <= slen) > + return -E2BIG; > + > + copied = strncpy_from_kernel_nofault(dst + dlen, src, sz); I'm not sure strncpy_from_kernel_nofault() is designed to work correctly if dst can be modified concurrently. Which in BPF case, even though we validate that memory is correct, it still can be modified concurrently in such a way as to cause strncpy_from_kernel_nofault's inner dst[-1] based loop logic to go beyond the boundaries of target buffer. i.e., I don't believe current implementation is concurrency safe pw-bot: cr > + if (copied < 0) > + return copied; > + else if (copied == 0 || copied == 1) > + return dlen; > + > + /* The copied character count includes '\0'. */ > + return dlen + copied - 1; > +} > + > +/** > + * bpf_strcat - Append non-null bytes from a source string, and null-terminate > + * the result > + * @dst: Destination string. > + * @dst__sz: Maximum bytes of @dst__ign, includes the trailing NUL. > + * @src: Source string. > + * > + * Return: > + * * >=0 - Length of the concatenated string. > + * > + * * %-EINVAL - String @dst__ign is invalid. > + * * %-EFAULT - Cannot read or write one of the strings. > + * * %-E2BIG - String @src__ign is too large or the remaining space in > + * @dst__ign is too small. > + * * %-ERANGE - One of the strings is outside of kernel address space > + */ > +__bpf_kfunc int bpf_strcat(char *dst, u32 dst__sz, const char *src) > +{ > + return __bpf_strncat(dst, dst__sz, src, dst__sz); > +} > + > +/** > + * bpf_strncat - Append non-null bytes from a source string, and null-terminate > + * the result > + * @dst: Destination string. > + * @dst__sz: Maximum bytes of @dst__ign, includes the trailing NUL. > + * @src: Source string. > + * @len: the maximum number of characters to concatenate, the null terminator > + * \0 of the target string @dst will also be included. > + * > + * Return: > + * * >=0 - Length of the concatenated string. > + * > + * * %-EINVAL - String @dst__ign is invalid. > + * * %-EFAULT - Cannot read or write one of the strings. > + * * %-E2BIG - String @src__ign is too large or the remaining space in > + * @dst__ign is too small. > + * * %-ERANGE - One of the strings is outside of kernel address space > + */ > +__bpf_kfunc int bpf_strncat(char *dst, u32 dst__sz, const char *src, u32 len) > +{ > + return __bpf_strncat(dst, dst__sz, src, len); > +} > + > #ifdef CONFIG_KEYS > /** > * bpf_lookup_user_key - lookup a key by its serial > @@ -4958,6 +5031,8 @@ BTF_ID_FLAGS(func, bpf_strstr); > BTF_ID_FLAGS(func, bpf_strcasestr); > BTF_ID_FLAGS(func, bpf_strnstr); > BTF_ID_FLAGS(func, bpf_strncasestr); > +BTF_ID_FLAGS(func, bpf_strcat); > +BTF_ID_FLAGS(func, bpf_strncat); > #if defined(CONFIG_BPF_LSM) && defined(CONFIG_CGROUPS) > BTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU) > #endif > -- > 2.55.0 > ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH bpf-next v5 1/2] bpf: add bpf_strcat,bpf_strncat kfunc 2026-07-27 22:56 ` Andrii Nakryiko @ 2026-07-28 2:50 ` Rong Tao 2026-07-28 15:58 ` Andrii Nakryiko 0 siblings, 1 reply; 7+ messages in thread From: Rong Tao @ 2026-07-28 2:50 UTC (permalink / raw) To: Andrii Nakryiko Cc: vmalik, andrii, ast, rongtao, Daniel Borkmann, Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis, Shuah Khan, Yuzuki Ishiyama, open list:BPF [GENERAL] (Safe Dynamic Programs and Tools), open list, open list:KERNEL SELFTEST FRAMEWORK On 7/28/26 06:56, Andrii Nakryiko wrote: > On Tue, Jul 21, 2026 at 9:42 PM Rong Tao <rtoax@foxmail.com> wrote: >> From: Rong Tao <rongtao@cestc.cn> >> >> Add string concatenation kfuncs, prototype: >> >> int bpf_strcat(char *dst, u32 dst__sz, const char *src); >> int bpf_strncat(char *dst, u32 dst__sz, const char *src, u32 len); >> >> This differs from the glibc library functions strcat and strncat, which, >> for safety reasons, require the size of the target string's memory space >> as a parameter. >> >> The src parameter cannot directly pass a string constant, such as "XYZ", >> otherwise it will throw an error "write into map forbidden". >> >> Signed-off-by: Rong Tao <rongtao@cestc.cn> >> --- >> kernel/bpf/helpers.c | 75 ++++++++++++++++++++++++++++++++++++++++++++ >> 1 file changed, 75 insertions(+) >> >> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c >> index c18f1e16edee..85afff60365c 100644 >> --- a/kernel/bpf/helpers.c >> +++ b/kernel/bpf/helpers.c >> @@ -4195,6 +4195,79 @@ __bpf_kfunc int bpf_strncasestr(const char *s1__ign, const char *s2__ign, >> return __bpf_strnstr(s1__ign, s2__ign, len, true); >> } >> >> +static int __bpf_strncat(char *dst, u32 dsz, const char *src, u32 sz) >> +{ >> + int dlen = 0, slen, space, copied; >> + >> + if (sz == 0 || dsz == 0) >> + return -EINVAL; >> + >> + while (dlen < dsz && dst[dlen] != '\0') >> + dlen++; >> + >> + if (dlen >= dsz) >> + return -E2BIG; >> + >> + space = dsz - dlen; >> + >> + slen = bpf_strnlen(src, sz); >> + if (space <= slen) >> + return -E2BIG; >> + >> + copied = strncpy_from_kernel_nofault(dst + dlen, src, sz); > I'm not sure strncpy_from_kernel_nofault() is designed to work > correctly if dst can be modified concurrently. Which in BPF case, even > though we validate that memory is correct, it still can be modified > concurrently in such a way as to cause strncpy_from_kernel_nofault's > inner dst[-1] based loop logic to go beyond the boundaries of target > buffer. > > i.e., I don't believe current implementation is concurrency safe Thank you very much for your reply. There is indeed a thread safety issue. Let's look at a solution. I will submit v6. Thank you ;) Rong Tao > > pw-bot: cr > > >> + if (copied < 0) >> + return copied; >> + else if (copied == 0 || copied == 1) >> + return dlen; >> + >> + /* The copied character count includes '\0'. */ >> + return dlen + copied - 1; >> +} >> + >> +/** >> + * bpf_strcat - Append non-null bytes from a source string, and null-terminate >> + * the result >> + * @dst: Destination string. >> + * @dst__sz: Maximum bytes of @dst__ign, includes the trailing NUL. >> + * @src: Source string. >> + * >> + * Return: >> + * * >=0 - Length of the concatenated string. >> + * >> + * * %-EINVAL - String @dst__ign is invalid. >> + * * %-EFAULT - Cannot read or write one of the strings. >> + * * %-E2BIG - String @src__ign is too large or the remaining space in >> + * @dst__ign is too small. >> + * * %-ERANGE - One of the strings is outside of kernel address space >> + */ >> +__bpf_kfunc int bpf_strcat(char *dst, u32 dst__sz, const char *src) >> +{ >> + return __bpf_strncat(dst, dst__sz, src, dst__sz); >> +} >> + >> +/** >> + * bpf_strncat - Append non-null bytes from a source string, and null-terminate >> + * the result >> + * @dst: Destination string. >> + * @dst__sz: Maximum bytes of @dst__ign, includes the trailing NUL. >> + * @src: Source string. >> + * @len: the maximum number of characters to concatenate, the null terminator >> + * \0 of the target string @dst will also be included. >> + * >> + * Return: >> + * * >=0 - Length of the concatenated string. >> + * >> + * * %-EINVAL - String @dst__ign is invalid. >> + * * %-EFAULT - Cannot read or write one of the strings. >> + * * %-E2BIG - String @src__ign is too large or the remaining space in >> + * @dst__ign is too small. >> + * * %-ERANGE - One of the strings is outside of kernel address space >> + */ >> +__bpf_kfunc int bpf_strncat(char *dst, u32 dst__sz, const char *src, u32 len) >> +{ >> + return __bpf_strncat(dst, dst__sz, src, len); >> +} >> + >> #ifdef CONFIG_KEYS >> /** >> * bpf_lookup_user_key - lookup a key by its serial >> @@ -4958,6 +5031,8 @@ BTF_ID_FLAGS(func, bpf_strstr); >> BTF_ID_FLAGS(func, bpf_strcasestr); >> BTF_ID_FLAGS(func, bpf_strnstr); >> BTF_ID_FLAGS(func, bpf_strncasestr); >> +BTF_ID_FLAGS(func, bpf_strcat); >> +BTF_ID_FLAGS(func, bpf_strncat); >> #if defined(CONFIG_BPF_LSM) && defined(CONFIG_CGROUPS) >> BTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU) >> #endif >> -- >> 2.55.0 >> ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH bpf-next v5 1/2] bpf: add bpf_strcat,bpf_strncat kfunc 2026-07-28 2:50 ` Rong Tao @ 2026-07-28 15:58 ` Andrii Nakryiko 2026-07-29 1:08 ` Rong Tao 0 siblings, 1 reply; 7+ messages in thread From: Andrii Nakryiko @ 2026-07-28 15:58 UTC (permalink / raw) To: Rong Tao Cc: vmalik, andrii, ast, rongtao, Daniel Borkmann, Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis, Shuah Khan, Yuzuki Ishiyama, open list:BPF [GENERAL] (Safe Dynamic Programs and Tools), open list, open list:KERNEL SELFTEST FRAMEWORK On Mon, Jul 27, 2026 at 7:50 PM Rong Tao <rtoax@foxmail.com> wrote: > > > On 7/28/26 06:56, Andrii Nakryiko wrote: > > On Tue, Jul 21, 2026 at 9:42 PM Rong Tao <rtoax@foxmail.com> wrote: > >> From: Rong Tao <rongtao@cestc.cn> > >> > >> Add string concatenation kfuncs, prototype: > >> > >> int bpf_strcat(char *dst, u32 dst__sz, const char *src); > >> int bpf_strncat(char *dst, u32 dst__sz, const char *src, u32 len); > >> > >> This differs from the glibc library functions strcat and strncat, which, > >> for safety reasons, require the size of the target string's memory space > >> as a parameter. > >> > >> The src parameter cannot directly pass a string constant, such as "XYZ", > >> otherwise it will throw an error "write into map forbidden". > >> > >> Signed-off-by: Rong Tao <rongtao@cestc.cn> > >> --- > >> kernel/bpf/helpers.c | 75 ++++++++++++++++++++++++++++++++++++++++++++ > >> 1 file changed, 75 insertions(+) > >> > >> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c > >> index c18f1e16edee..85afff60365c 100644 > >> --- a/kernel/bpf/helpers.c > >> +++ b/kernel/bpf/helpers.c > >> @@ -4195,6 +4195,79 @@ __bpf_kfunc int bpf_strncasestr(const char *s1__ign, const char *s2__ign, > >> return __bpf_strnstr(s1__ign, s2__ign, len, true); > >> } > >> > >> +static int __bpf_strncat(char *dst, u32 dsz, const char *src, u32 sz) > >> +{ > >> + int dlen = 0, slen, space, copied; > >> + > >> + if (sz == 0 || dsz == 0) > >> + return -EINVAL; > >> + > >> + while (dlen < dsz && dst[dlen] != '\0') > >> + dlen++; > >> + > >> + if (dlen >= dsz) > >> + return -E2BIG; > >> + > >> + space = dsz - dlen; > >> + > >> + slen = bpf_strnlen(src, sz); > >> + if (space <= slen) > >> + return -E2BIG; > >> + > >> + copied = strncpy_from_kernel_nofault(dst + dlen, src, sz); > > I'm not sure strncpy_from_kernel_nofault() is designed to work > > correctly if dst can be modified concurrently. Which in BPF case, even > > though we validate that memory is correct, it still can be modified > > concurrently in such a way as to cause strncpy_from_kernel_nofault's > > inner dst[-1] based loop logic to go beyond the boundaries of target > > buffer. > > > > i.e., I don't believe current implementation is concurrency safe > > Thank you very much for your reply. There is indeed a thread safety > issue. Let's look at a solution. I will submit v6. Thank you ;) > tbh, I'm not sure we need these new kfuncs at all. You should be able to easily to do the same with dynptr for dst buffer and existing strlen/probe_read helpers/kfuncs. > Rong Tao > > > > > pw-bot: cr > > > > > >> + if (copied < 0) > >> + return copied; > >> + else if (copied == 0 || copied == 1) > >> + return dlen; > >> + > >> + /* The copied character count includes '\0'. */ > >> + return dlen + copied - 1; > >> +} > >> + > >> +/** > >> + * bpf_strcat - Append non-null bytes from a source string, and null-terminate > >> + * the result > >> + * @dst: Destination string. > >> + * @dst__sz: Maximum bytes of @dst__ign, includes the trailing NUL. > >> + * @src: Source string. > >> + * > >> + * Return: > >> + * * >=0 - Length of the concatenated string. > >> + * > >> + * * %-EINVAL - String @dst__ign is invalid. > >> + * * %-EFAULT - Cannot read or write one of the strings. > >> + * * %-E2BIG - String @src__ign is too large or the remaining space in > >> + * @dst__ign is too small. > >> + * * %-ERANGE - One of the strings is outside of kernel address space > >> + */ > >> +__bpf_kfunc int bpf_strcat(char *dst, u32 dst__sz, const char *src) > >> +{ > >> + return __bpf_strncat(dst, dst__sz, src, dst__sz); > >> +} > >> + > >> +/** > >> + * bpf_strncat - Append non-null bytes from a source string, and null-terminate > >> + * the result > >> + * @dst: Destination string. > >> + * @dst__sz: Maximum bytes of @dst__ign, includes the trailing NUL. > >> + * @src: Source string. > >> + * @len: the maximum number of characters to concatenate, the null terminator > >> + * \0 of the target string @dst will also be included. > >> + * > >> + * Return: > >> + * * >=0 - Length of the concatenated string. > >> + * > >> + * * %-EINVAL - String @dst__ign is invalid. > >> + * * %-EFAULT - Cannot read or write one of the strings. > >> + * * %-E2BIG - String @src__ign is too large or the remaining space in > >> + * @dst__ign is too small. > >> + * * %-ERANGE - One of the strings is outside of kernel address space > >> + */ > >> +__bpf_kfunc int bpf_strncat(char *dst, u32 dst__sz, const char *src, u32 len) > >> +{ > >> + return __bpf_strncat(dst, dst__sz, src, len); > >> +} > >> + > >> #ifdef CONFIG_KEYS > >> /** > >> * bpf_lookup_user_key - lookup a key by its serial > >> @@ -4958,6 +5031,8 @@ BTF_ID_FLAGS(func, bpf_strstr); > >> BTF_ID_FLAGS(func, bpf_strcasestr); > >> BTF_ID_FLAGS(func, bpf_strnstr); > >> BTF_ID_FLAGS(func, bpf_strncasestr); > >> +BTF_ID_FLAGS(func, bpf_strcat); > >> +BTF_ID_FLAGS(func, bpf_strncat); > >> #if defined(CONFIG_BPF_LSM) && defined(CONFIG_CGROUPS) > >> BTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU) > >> #endif > >> -- > >> 2.55.0 > >> > ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH bpf-next v5 1/2] bpf: add bpf_strcat,bpf_strncat kfunc 2026-07-28 15:58 ` Andrii Nakryiko @ 2026-07-29 1:08 ` Rong Tao 0 siblings, 0 replies; 7+ messages in thread From: Rong Tao @ 2026-07-29 1:08 UTC (permalink / raw) To: Andrii Nakryiko Cc: vmalik, andrii, ast, rongtao, Daniel Borkmann, Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis, Shuah Khan, Yuzuki Ishiyama, open list:BPF [GENERAL] (Safe Dynamic Programs and Tools), open list, open list:KERNEL SELFTEST FRAMEWORK On 7/28/26 23:58, Andrii Nakryiko wrote: > On Mon, Jul 27, 2026 at 7:50 PM Rong Tao <rtoax@foxmail.com> wrote: >> >> On 7/28/26 06:56, Andrii Nakryiko wrote: >>> On Tue, Jul 21, 2026 at 9:42 PM Rong Tao <rtoax@foxmail.com> wrote: >>>> From: Rong Tao <rongtao@cestc.cn> >>>> >>>> Add string concatenation kfuncs, prototype: >>>> >>>> int bpf_strcat(char *dst, u32 dst__sz, const char *src); >>>> int bpf_strncat(char *dst, u32 dst__sz, const char *src, u32 len); >>>> >>>> This differs from the glibc library functions strcat and strncat, which, >>>> for safety reasons, require the size of the target string's memory space >>>> as a parameter. >>>> >>>> The src parameter cannot directly pass a string constant, such as "XYZ", >>>> otherwise it will throw an error "write into map forbidden". >>>> >>>> Signed-off-by: Rong Tao <rongtao@cestc.cn> >>>> --- >>>> kernel/bpf/helpers.c | 75 ++++++++++++++++++++++++++++++++++++++++++++ >>>> 1 file changed, 75 insertions(+) >>>> >>>> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c >>>> index c18f1e16edee..85afff60365c 100644 >>>> --- a/kernel/bpf/helpers.c >>>> +++ b/kernel/bpf/helpers.c >>>> @@ -4195,6 +4195,79 @@ __bpf_kfunc int bpf_strncasestr(const char *s1__ign, const char *s2__ign, >>>> return __bpf_strnstr(s1__ign, s2__ign, len, true); >>>> } >>>> >>>> +static int __bpf_strncat(char *dst, u32 dsz, const char *src, u32 sz) >>>> +{ >>>> + int dlen = 0, slen, space, copied; >>>> + >>>> + if (sz == 0 || dsz == 0) >>>> + return -EINVAL; >>>> + >>>> + while (dlen < dsz && dst[dlen] != '\0') >>>> + dlen++; >>>> + >>>> + if (dlen >= dsz) >>>> + return -E2BIG; >>>> + >>>> + space = dsz - dlen; >>>> + >>>> + slen = bpf_strnlen(src, sz); >>>> + if (space <= slen) >>>> + return -E2BIG; >>>> + >>>> + copied = strncpy_from_kernel_nofault(dst + dlen, src, sz); >>> I'm not sure strncpy_from_kernel_nofault() is designed to work >>> correctly if dst can be modified concurrently. Which in BPF case, even >>> though we validate that memory is correct, it still can be modified >>> concurrently in such a way as to cause strncpy_from_kernel_nofault's >>> inner dst[-1] based loop logic to go beyond the boundaries of target >>> buffer. >>> >>> i.e., I don't believe current implementation is concurrency safe >> Thank you very much for your reply. There is indeed a thread safety >> issue. Let's look at a solution. I will submit v6. Thank you ;) >> > tbh, I'm not sure we need these new kfuncs at all. You should be able > to easily to do the same with dynptr for dst buffer and existing > strlen/probe_read helpers/kfuncs. Thank you. You share the same view as Viktor. Indeed, strcat and probe_read are functionally redundant. Let's discard this commit. Thank you. > >> Rong Tao >> >>> pw-bot: cr >>> >>> >>>> + if (copied < 0) >>>> + return copied; >>>> + else if (copied == 0 || copied == 1) >>>> + return dlen; >>>> + >>>> + /* The copied character count includes '\0'. */ >>>> + return dlen + copied - 1; >>>> +} >>>> + >>>> +/** >>>> + * bpf_strcat - Append non-null bytes from a source string, and null-terminate >>>> + * the result >>>> + * @dst: Destination string. >>>> + * @dst__sz: Maximum bytes of @dst__ign, includes the trailing NUL. >>>> + * @src: Source string. >>>> + * >>>> + * Return: >>>> + * * >=0 - Length of the concatenated string. >>>> + * >>>> + * * %-EINVAL - String @dst__ign is invalid. >>>> + * * %-EFAULT - Cannot read or write one of the strings. >>>> + * * %-E2BIG - String @src__ign is too large or the remaining space in >>>> + * @dst__ign is too small. >>>> + * * %-ERANGE - One of the strings is outside of kernel address space >>>> + */ >>>> +__bpf_kfunc int bpf_strcat(char *dst, u32 dst__sz, const char *src) >>>> +{ >>>> + return __bpf_strncat(dst, dst__sz, src, dst__sz); >>>> +} >>>> + >>>> +/** >>>> + * bpf_strncat - Append non-null bytes from a source string, and null-terminate >>>> + * the result >>>> + * @dst: Destination string. >>>> + * @dst__sz: Maximum bytes of @dst__ign, includes the trailing NUL. >>>> + * @src: Source string. >>>> + * @len: the maximum number of characters to concatenate, the null terminator >>>> + * \0 of the target string @dst will also be included. >>>> + * >>>> + * Return: >>>> + * * >=0 - Length of the concatenated string. >>>> + * >>>> + * * %-EINVAL - String @dst__ign is invalid. >>>> + * * %-EFAULT - Cannot read or write one of the strings. >>>> + * * %-E2BIG - String @src__ign is too large or the remaining space in >>>> + * @dst__ign is too small. >>>> + * * %-ERANGE - One of the strings is outside of kernel address space >>>> + */ >>>> +__bpf_kfunc int bpf_strncat(char *dst, u32 dst__sz, const char *src, u32 len) >>>> +{ >>>> + return __bpf_strncat(dst, dst__sz, src, len); >>>> +} >>>> + >>>> #ifdef CONFIG_KEYS >>>> /** >>>> * bpf_lookup_user_key - lookup a key by its serial >>>> @@ -4958,6 +5031,8 @@ BTF_ID_FLAGS(func, bpf_strstr); >>>> BTF_ID_FLAGS(func, bpf_strcasestr); >>>> BTF_ID_FLAGS(func, bpf_strnstr); >>>> BTF_ID_FLAGS(func, bpf_strncasestr); >>>> +BTF_ID_FLAGS(func, bpf_strcat); >>>> +BTF_ID_FLAGS(func, bpf_strncat); >>>> #if defined(CONFIG_BPF_LSM) && defined(CONFIG_CGROUPS) >>>> BTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU) >>>> #endif >>>> -- >>>> 2.55.0 >>>> ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH bpf-next v5 2/2] selftests/bpf: Test bpf_strcat,bpf_strncat kfuncs [not found] <cover.1784695085.git.rtoax@foxmail.com> 2026-07-22 4:41 ` [PATCH bpf-next v5 1/2] bpf: add bpf_strcat,bpf_strncat kfunc Rong Tao @ 2026-07-22 4:41 ` Rong Tao 1 sibling, 0 replies; 7+ messages in thread From: Rong Tao @ 2026-07-22 4:41 UTC (permalink / raw) To: vmalik, andrii, ast Cc: rtoax, rongtao, Daniel Borkmann, Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis, Shuah Khan, Yuzuki Ishiyama, open list:BPF [GENERAL] (Safe Dynamic Programs and Tools), open list, open list:KERNEL SELFTEST FRAMEWORK From: Rong Tao <rongtao@cestc.cn> Add tests for new kfuncs bpf_strcat() and bpf_strncat(). Signed-off-by: Rong Tao <rongtao@cestc.cn> --- .../testing/selftests/bpf/prog_tests/string_kfuncs.c | 2 ++ .../selftests/bpf/progs/string_kfuncs_failure2.c | 3 +++ .../selftests/bpf/progs/string_kfuncs_success.c | 12 ++++++++++++ 3 files changed, 17 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/string_kfuncs.c b/tools/testing/selftests/bpf/prog_tests/string_kfuncs.c index 300032a19445..460567ef622a 100644 --- a/tools/testing/selftests/bpf/prog_tests/string_kfuncs.c +++ b/tools/testing/selftests/bpf/prog_tests/string_kfuncs.c @@ -24,6 +24,8 @@ static const char * const test_cases[] = { "strcasestr", "strnstr", "strncasestr", + "strcat", + "strncat", }; void run_too_long_tests(void) diff --git a/tools/testing/selftests/bpf/progs/string_kfuncs_failure2.c b/tools/testing/selftests/bpf/progs/string_kfuncs_failure2.c index 412c53b87b18..2dbd1349689e 100644 --- a/tools/testing/selftests/bpf/progs/string_kfuncs_failure2.c +++ b/tools/testing/selftests/bpf/progs/string_kfuncs_failure2.c @@ -5,6 +5,7 @@ #include <linux/limits.h> char long_str[XATTR_SIZE_MAX + 1]; +char str[] = "hello"; SEC("syscall") int test_strcmp_too_long(void *ctx) { return bpf_strcmp(long_str, long_str); } SEC("syscall") int test_strcasecmp_too_long(void *ctx) { return bpf_strcasecmp(long_str, long_str); } @@ -23,5 +24,7 @@ SEC("syscall") int test_strstr_too_long(void *ctx) { return bpf_strstr(long_str, SEC("syscall") int test_strcasestr_too_long(void *ctx) { return bpf_strcasestr(long_str, "hello"); } SEC("syscall") int test_strnstr_too_long(void *ctx) { return bpf_strnstr(long_str, "hello", sizeof(long_str)); } SEC("syscall") int test_strncasestr_too_long(void *ctx) { return bpf_strncasestr(long_str, "hello", sizeof(long_str)); } +SEC("syscall") int test_strcat_too_long(void *ctx) { return bpf_strcat(long_str, sizeof(long_str), str); } +SEC("syscall") int test_strncat_too_long(void *ctx) { return bpf_strncat(long_str, sizeof(long_str), str, 3); } char _license[] SEC("license") = "GPL"; diff --git a/tools/testing/selftests/bpf/progs/string_kfuncs_success.c b/tools/testing/selftests/bpf/progs/string_kfuncs_success.c index f65b1226a81a..40583361c655 100644 --- a/tools/testing/selftests/bpf/progs/string_kfuncs_success.c +++ b/tools/testing/selftests/bpf/progs/string_kfuncs_success.c @@ -59,5 +59,17 @@ __test(-ENOENT) int test_strncasestr_notfound1(void *ctx) { return bpf_strncases __test(-ENOENT) int test_strncasestr_notfound2(void *ctx) { return bpf_strncasestr(str, "hello", 4); } __test(-ENOENT) int test_strncasestr_notfound3(void *ctx) { return bpf_strncasestr("", "a", 0); } __test(0) int test_strncasestr_empty(void *ctx) { return bpf_strncasestr(str, "", 1); } +__test(0) int test_strcat_success(void *ctx) +{ + char buf[32] = "hello"; + bpf_strcat(buf, sizeof(buf), str); + return bpf_strcmp(buf, "hellohello world"); +} +__test(0) int test_strncat_success(void *ctx) +{ + char buf[32] = "hello"; + bpf_strncat(buf, sizeof(buf), str, 3); + return bpf_strcmp(buf, "hellohe"); +} char _license[] SEC("license") = "GPL"; -- 2.55.0 ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-07-29 1:09 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <cover.1784695085.git.rtoax@foxmail.com>
2026-07-22 4:41 ` [PATCH bpf-next v5 1/2] bpf: add bpf_strcat,bpf_strncat kfunc Rong Tao
2026-07-22 6:19 ` bot+bpf-ci
2026-07-27 22:56 ` Andrii Nakryiko
2026-07-28 2:50 ` Rong Tao
2026-07-28 15:58 ` Andrii Nakryiko
2026-07-29 1:08 ` Rong Tao
2026-07-22 4:41 ` [PATCH bpf-next v5 2/2] selftests/bpf: Test bpf_strcat,bpf_strncat kfuncs Rong Tao
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®