Re: [PATCH v2] bna: replace deprecated strncpy with strscpy_pad
From: Kees Cook
Date: Mon Oct 09 2023 - 15:58:40 EST
On Mon, Oct 09, 2023 at 05:45:33PM +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.
>
> bfa_ioc_get_adapter_manufacturer() simply copies a string literal into
> `manufacturer`.
>
> Another implementation of bfa_ioc_get_adapter_manufacturer() from
> drivers/scsi/bfa/bfa_ioc.c uses memset + strscpy:
> | void
> | bfa_ioc_get_adapter_manufacturer(struct bfa_ioc_s *ioc, char *manufacturer)
> | {
> | memset((void *)manufacturer, 0, BFA_ADAPTER_MFG_NAME_LEN);
> | strscpy(manufacturer, BFA_MFG_NAME, BFA_ADAPTER_MFG_NAME_LEN);
> | }
>
> Let's use `strscpy_pad` to eliminate some redundant work while still
> NUL-terminating and NUL-padding the destination buffer.
>
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
> Link: https://github.com/KSPP/linux/issues/90
> Cc: linux-hardening@xxxxxxxxxxxxxxx
> Signed-off-by: Justin Stitt <justinstitt@xxxxxxxxxx>
Thanks for the update! Yeah, this looks safe to me now.
Reviewed-by: Kees Cook <keescook@xxxxxxxxxxxx>
--
Kees Cook