linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next] bpf: Replace strncpy() with strscpy_pad()
@ 2022-03-03  8:18 Yuntao Wang
  2022-03-03 18:17 ` Yonghong Song
  0 siblings, 1 reply; 5+ messages in thread
From: Yuntao Wang @ 2022-03-03  8:18 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, netdev, linux-kernel, Yuntao Wang

Using strncpy() on NUL-terminated strings is considered deprecated[1],
replace it with strscpy_pad().

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

Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
---
 kernel/bpf/helpers.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index ae64110a98b5..d03b28761a67 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -225,13 +225,7 @@ 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;
+	strscpy_pad(buf, task->comm, size);
 	return 0;
 err_clear:
 	memset(buf, 0, size);
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH bpf-next] bpf: Replace strncpy() with strscpy_pad()
  2022-03-03  8:18 [PATCH bpf-next] bpf: Replace strncpy() with strscpy_pad() Yuntao Wang
@ 2022-03-03 18:17 ` Yonghong Song
  2022-03-04  7:04   ` [PATCH bpf-next v2] bpf: Replace strncpy() with strscpy() Yuntao Wang
  0 siblings, 1 reply; 5+ messages in thread
From: Yonghong Song @ 2022-03-03 18:17 UTC (permalink / raw)
  To: Yuntao Wang, bpf
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, John Fastabend, KP Singh, netdev,
	linux-kernel



On 3/3/22 12:18 AM, Yuntao Wang wrote:
> Using strncpy() on NUL-terminated strings is considered deprecated[1],
> replace it with strscpy_pad().
> 
> [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
> 
> Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
> ---
>   kernel/bpf/helpers.c | 8 +-------
>   1 file changed, 1 insertion(+), 7 deletions(-)
> 
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index ae64110a98b5..d03b28761a67 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -225,13 +225,7 @@ 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;
> +	strscpy_pad(buf, task->comm, size);

The precise replacement should be strscpy(...), right?
I am not sure whether we want to do pad here or not, probably
not as it is mostly used by user space for string copy/print
and we don't have cases demanding padding yet.

Please keep the comment
   /* Verifier guarantees that size > 0 */
this is important as strscpy will not do anything if size == 0.


>   	return 0;
>   err_clear:
>   	memset(buf, 0, size);

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH bpf-next v2] bpf: Replace strncpy() with strscpy()
  2022-03-03 18:17 ` Yonghong Song
@ 2022-03-04  7:04   ` Yuntao Wang
  2022-03-04 16:07     ` Yonghong Song
  2022-03-08  6:10     ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 5+ messages in thread
From: Yuntao Wang @ 2022-03-04  7:04 UTC (permalink / raw)
  To: yhs
  Cc: andrii, ast, bpf, daniel, john.fastabend, kafai, kpsingh,
	linux-kernel, netdev, songliubraving, ytcoode

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(-)

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);
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH bpf-next v2] bpf: Replace strncpy() with strscpy()
  2022-03-04  7:04   ` [PATCH bpf-next v2] bpf: Replace strncpy() with strscpy() Yuntao Wang
@ 2022-03-04 16:07     ` Yonghong Song
  2022-03-08  6:10     ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 5+ messages in thread
From: Yonghong Song @ 2022-03-04 16:07 UTC (permalink / raw)
  To: Yuntao Wang
  Cc: andrii, ast, bpf, daniel, john.fastabend, kafai, kpsingh,
	linux-kernel, netdev, songliubraving



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>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH bpf-next v2] bpf: Replace strncpy() with strscpy()
  2022-03-04  7:04   ` [PATCH bpf-next v2] bpf: Replace strncpy() with strscpy() Yuntao Wang
  2022-03-04 16:07     ` Yonghong Song
@ 2022-03-08  6:10     ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-03-08  6:10 UTC (permalink / raw)
  To: Yuntao Wang
  Cc: yhs, andrii, ast, bpf, daniel, john.fastabend, kafai, kpsingh,
	linux-kernel, netdev, songliubraving

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!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-03-08  6:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-03  8:18 [PATCH bpf-next] bpf: Replace strncpy() with strscpy_pad() Yuntao Wang
2022-03-03 18:17 ` Yonghong Song
2022-03-04  7:04   ` [PATCH bpf-next v2] bpf: Replace strncpy() with strscpy() Yuntao Wang
2022-03-04 16:07     ` Yonghong Song
2022-03-08  6:10     ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).