libbpf: Use strscpy() in kernel code for skel_map_create()
PATCH v5 1/1 libbpf: Use strscpy() in kernel code for skel_map_create()
Linux has deprecated[1] strncpy(), and the use in skel_map_create() is best replaced with strscpy(). Since we still need to build this file in userspace, leave the strncpy() in place in that case. This is the last use of strncpy() in the kernel.
---
v5: Just ifdef for the kernel and use strscpy() there (Alexei)
v4: https://lore.kernel.org/lkml/[email protected]/
v3: https://lore.kernel.org/lkml/[email protected]/
v2: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/
Cc: Alexei Starovoitov <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: sun jian <[email protected]>
Cc: Andrii Nakryiko <[email protected]>
Cc: Eduard Zingerman <[email protected]>
Cc: Daniel Borkmann <[email protected]>
Cc: Martin KaFai Lau <[email protected]>
Cc: Song Liu <[email protected]>
Cc: Yonghong Song <[email protected]>
Cc: John Fastabend <[email protected]>
Cc: KP Singh <[email protected]>
Cc: Stanislav Fomichev <[email protected]>
Cc: Hao Luo <[email protected]>
Cc: <[email protected]>
--- tools/lib/bpf/skel_internal.h | 5 +++++ 1 file changed, 5 insertions(+)
tools/lib/bpf/skel_internal.h +5 -0
@@ -243,7 +243,12 @@ static inline int skel_map_create(enum bpf_map_type map_type, attr.excl_prog_hash = (unsigned long ) excl_prog_hash; attr.excl_prog_hash_size = excl_prog_hash_sz; +#ifdef __KERNEL__ +if (strscpy (attr.map_name, map_name) <0 ) +return -EINVAL ; +#else strncpy (attr.map_name, map_name,sizeof (attr.map_name)); +#endif attr.key_size = key_size; attr.value_size = value_size; attr.max_entries = max_entries;
Signature · 1 line
2.34.1
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 9 lines · On Tue, 12 May 2026 22:08:07 -0700 you wrote:
Linux has deprecated[1]
strncpy(), and the use inskel_map_create()is best replaced withstrscpy(). Since we still need to build this file in userspace, leave thestrncpy()in place in that case. This is the last use ofstrncpy()in the kernel.Signed-off-by Kees Cook <[email protected]>[...]
Here is the summary with links:
- [v5] libbpf: Use strscpy() in kernel code for skel_map_create()
https://git.kernel.org/bpf/bpf-next/c/74a9bb761a43
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The use of the 2-argument `strscpy` macro in `skel_internal.h` breaks compilation of light…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The use of the 2-argument strscpy macro in skel_internal.h breaks compilation of light skeletons for out-of-tree kernel modules on kernels older than 6.8. --
commit 056f88b29a55562ae428a73e0cbb771aa82987a6
Author: Kees Cook <[email protected]>
libbpf: Use strscpy() in kernel code for skel_map_create()
This commit replaces a deprecated strncpy() call with strscpy() in skel_map_create() when building for the kernel, leaving the user-space version unchanged.
Quoted 3 lines
tools/lib/bpf/skel_internal.h +0
-0
[ ... ]
tools/lib/bpf/skel_internal.h static inline int skel_map_create(enum bpf_map_type map_type, · line 247
Quoted 6 lines
tools/lib/bpf/skel_internal.h +2
-0@@ -243,7 +243,12 @@ static inline int skel_map_create(enum bpf_map_type map_type, attr.excl_prog_hash = (unsigned long ) excl_prog_hash; attr.excl_prog_hash_size = excl_prog_hash_sz; +#ifdef __KERNEL__ +if (strscpy (attr.map_name, map_name) <0 )
Does this break backwards compatibility for out-of-tree kernel modules using generated light skeletons on kernels older than 6.8?
The 2-argument strscpy macro was introduced in Linux 6.8. Older kernels like 6.1 LTS and 6.6 LTS only provide the 3-argument version:ssize_t strscpy(char *dest, const char *src, size_t count).
Compiling this generated skeleton code on older kernels will result in a compiler error due to too few arguments.
Would it be better to use the 3-argument version here instead? strscpy(attr.map_name, map_name, sizeof(attr.map_name))
Quoted 4 lines
tools/lib/bpf/skel_internal.h +3
-0+return -EINVAL ; +#else strncpy (attr.map_name, map_name,sizeof (attr.map_name)); +#endif
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 2 lines · On Thu, May 14, 2026 at 01:56:09AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The use of the 2-argumentstrscpymacro inskel_internal.hbreaks compilation of light skeletons for out-of-tree kernel modules on kernels older than 6.8.
True, but this is strictly for 7.1 or later. :)
Signature · 1 line
Kees Cook