[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Tue, 12 Dec 2023 13:23:01 -0800
From: Kees Cook <keescook@...omium.org>
To: Justin Stitt <justinstitt@...gle.com>
Cc: Michael Cyr <mikecyr@...ux.ibm.com>,
"James E.J. Bottomley" <jejb@...ux.ibm.com>,
"Martin K. Petersen" <martin.petersen@...cle.com>,
linux-scsi@...r.kernel.org, target-devel@...r.kernel.org,
linux-kernel@...r.kernel.org, linux-hardening@...r.kernel.org
Subject: Re: [PATCH v2] scsi: ibmvscsi_tgt: replace deprecated strncpy with
strscpy
On Tue, Dec 12, 2023 at 01:20:20AM +0000, Justin Stitt wrote:
> strncpy() is deprecated for use on NUL-terminated destination strings
> [1] and as such we should prefer more robust and less ambiguous string
> interfaces.
>
> We don't need the NUL-padding behavior that strncpy() provides as vscsi
> is NUL-allocated in ibmvscsis_probe() which proceeds to call
> ibmvscsis_adapter_info():
> | vscsi = kzalloc(sizeof(*vscsi), GFP_KERNEL);
>
> ibmvscsis_probe() -> ibmvscsis_handle_crq() -> ibmvscsis_parse_command()
> -> ibmvscsis_mad() -> ibmvscsis_process_mad() -> ibmvscsis_adapter_info()
>
> Following the same idea, `partition_name` is defiend as:
> | static char partition_name[PARTITION_NAMELEN] = "UNKNOWN";
> ... which is NUL-padded already, meaning strscpy() is the best option.
>
> Considering the above, a suitable replacement is `strscpy` [2] due to
> the fact that it guarantees NUL-termination on the destination buffer
> without unnecessarily NUL-padding.
>
> However, for cap->name and info let's use strscpy_pad as they are
> allocated via dma_alloc_coherent():
> | cap = dma_alloc_coherent(&vscsi->dma_dev->dev, olen, &token,
> | GFP_ATOMIC);
> &
> | info = dma_alloc_coherent(&vscsi->dma_dev->dev, sizeof(*info), &token,
> | GFP_ATOMIC);
>
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
> Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2]
> Link: https://github.com/KSPP/linux/issues/90
> Cc: linux-hardening@...r.kernel.org
> Signed-off-by: Justin Stitt <justinstitt@...gle.com>
This looks good to me. The only question that I haven't seen an answer
to from the maintainers is whether this is a __nonstring or not. It
really looks like it should be a C String, so with that assumption:
Reviewed-by: Kees Cook <keescook@...omium.org>
-Kees
--
Kees Cook
Powered by blists - more mailing lists