diff mbox series

[bpf-next] bpf: Replace strncpy() with strscpy_pad()

Message ID 20220303081800.82653-1-ytcoode@gmail.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series [bpf-next] bpf: Replace strncpy() with strscpy_pad() | expand

Checks

Context Check Description
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, 14 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-PR success PR summary
bpf/vmtest-bpf-next success VM_Test

Commit Message

Yuntao Wang March 3, 2022, 8:18 a.m. UTC
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(-)

Comments

Yonghong Song March 3, 2022, 6:17 p.m. UTC | #1
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);
diff mbox series

Patch

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