Skip to contentSkip to conversation

bpf: Replace strncpy() with strscpy_pad()

PATCH Superseded Yuntao Wang 3 participants 5 messages Mar 3, 2022 – Mar 8, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Replace strncpy() with strscpy_pad()

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

---
 kernel/bpf/helpers.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
kernel/bpf/helpers.c +1 -7
index ae64110a98b5..d03b28761a67 100644
@@ -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);
Signature · 1 line
2.35.1
#1 lore raw
Quoted 16 lines · 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

---
  kernel/bpf/helpers.c | 8 +-------
  1 file changed, 1 insertion(+), 7 deletions(-)
kernel/bpf/helpers.c +0 -0
index ae64110a98b5..d03b28761a67 100644
@@ -225,13 +225,7 @@ BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
  	if (unlikely(!task))

kernel/bpf/helpers.c BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size) · line 228

Quoted 10 lines
kernel/bpf/helpers.c +1 -7
  		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.

Quoted 3 lines
kernel/bpf/helpers.c +0 -0
  	return 0;
  err_clear:
  	memset(buf, 0, size);
#2 lore raw

PATCH v2 1/1 bpf: Replace strncpy() with strscpy()

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

---
v1 -> v2: replace strncpy() with strscpy() instead of strscpy_pad()

 kernel/bpf/helpers.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
kernel/bpf/helpers.c +2 -7
index ae64110a98b5..315053ef6a75 100644
@@ -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);
Signature · 1 line
2.35.1
Acked-by on v2 1/1
#3 lore raw

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

Quoted 10 lines · Yuntao Wang

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

#4 lore raw

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

In reply to Yuntao Wang
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 8 lines · 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!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html