diff mbox series

[bpf-next,v2] bpf: Replace strncpy() with strscpy()

Message ID 20220304070408.233658-1-ytcoode@gmail.com (mailing list archive)
State Accepted
Commit 03b9c7fa3f15f51bcd07f3828c2a01311e7746c4
Delegated to: BPF
Headers show
Series [bpf-next,v2] bpf: Replace strncpy() with strscpy() | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 9 this patch: 9
netdev/cc_maintainers success CCed 10 of 10 maintainers
netdev/build_clang success Errors and warnings before: 18 this patch: 18
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 14 this patch: 14
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 15 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next success VM_Test

Commit Message

Yuntao Wang March 4, 2022, 7:04 a.m. UTC
Using strncpy() on NUL-terminated strings is considered deprecated[1].
Moreover, if the length of 'task->comm' is less than the destination buffer
size, strncpy() will NUL-pad the destination buffer, which is a needless
performance penalty.

Replacing strncpy() with strscpy() fixes all these issues.

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings

Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
---
v1 -> v2: replace strncpy() with strscpy() instead of strscpy_pad()

 kernel/bpf/helpers.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

Comments

Yonghong Song March 4, 2022, 4:07 p.m. UTC | #1
On 3/3/22 11:04 PM, Yuntao Wang wrote:
> Using strncpy() on NUL-terminated strings is considered deprecated[1].
> Moreover, if the length of 'task->comm' is less than the destination buffer
> size, strncpy() will NUL-pad the destination buffer, which is a needless
> performance penalty.
> 
> Replacing strncpy() with strscpy() fixes all these issues.
> 
> [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
> 
> Signed-off-by: Yuntao Wang <ytcoode@gmail.com>

Acked-by: Yonghong Song <yhs@fb.com>
patchwork-bot+netdevbpf@kernel.org March 8, 2022, 6:10 a.m. UTC | #2
Hello:

This patch was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Fri,  4 Mar 2022 15:04:08 +0800 you wrote:
> Using strncpy() on NUL-terminated strings is considered deprecated[1].
> Moreover, if the length of 'task->comm' is less than the destination buffer
> size, strncpy() will NUL-pad the destination buffer, which is a needless
> performance penalty.
> 
> Replacing strncpy() with strscpy() fixes all these issues.
> 
> [...]

Here is the summary with links:
  - [bpf-next,v2] bpf: Replace strncpy() with strscpy()
    https://git.kernel.org/bpf/bpf-next/c/03b9c7fa3f15

You are awesome, thank you!
diff mbox series

Patch

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index ae64110a98b5..315053ef6a75 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -225,13 +225,8 @@  BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
 	if (unlikely(!task))
 		goto err_clear;
 
-	strncpy(buf, task->comm, size);
-
-	/* Verifier guarantees that size > 0. For task->comm exceeding
-	 * size, guarantee that buf is %NUL-terminated. Unconditionally
-	 * done here to save the size test.
-	 */
-	buf[size - 1] = 0;
+	/* Verifier guarantees that size > 0 */
+	strscpy(buf, task->comm, size);
 	return 0;
 err_clear:
 	memset(buf, 0, size);