BPF List
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: Yuntao Wang <ytcoode@gmail.com>, bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH bpf-next] bpf: Replace strncpy() with strscpy_pad()
Date: Thu, 3 Mar 2022 10:17:16 -0800	[thread overview]
Message-ID: <e1e060a0-898f-1969-abec-ca01c2eb2049@fb.com> (raw)
In-Reply-To: <20220303081800.82653-1-ytcoode@gmail.com>



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

  reply	other threads:[~2022-03-03 18:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-03  8:18 [PATCH bpf-next] bpf: Replace strncpy() with strscpy_pad() Yuntao Wang
2022-03-03 18:17 ` Yonghong Song [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e1e060a0-898f-1969-abec-ca01c2eb2049@fb.com \
    --to=yhs@fb.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=ytcoode@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox