public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] scsi: csiostor: replace deprecated strncpy with strscpy
@ 2023-10-23 20:26 Justin Stitt
  2023-10-25  0:30 ` Kees Cook
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Justin Stitt @ 2023-10-23 20:26 UTC (permalink / raw)
  To: James E.J. Bottomley, Martin K. Petersen
  Cc: linux-scsi, linux-kernel, linux-hardening, Justin Stitt

strncpy() is deprecated for use on NUL-terminated destination strings
[1] and as such we should prefer more robust and less ambiguous string
interfaces.

`hw` is kzalloc'd just before this string assignment:
|       hw = kzalloc(sizeof(struct csio_hw), GFP_KERNEL);

... which means any NUL-padding is redundant.

Since  CSIO_DRV_VERSION is a small string literal (smaller than
sizeof(dest)):

... there is functionally no change in this swap from strncpy() to
strscpy(). Nonetheless, let's make the change for robustness' sake -- as
it will ensure that drv_version is _always_ NUL-terminated.

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@vger.kernel.org
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
Note: build-tested only.

Found with: $ rg "strncpy\("
---
 drivers/scsi/csiostor/csio_init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/csiostor/csio_init.c b/drivers/scsi/csiostor/csio_init.c
index 0c32faefad7c..d649b7a2a879 100644
--- a/drivers/scsi/csiostor/csio_init.c
+++ b/drivers/scsi/csiostor/csio_init.c
@@ -521,7 +521,8 @@ static struct csio_hw *csio_hw_alloc(struct pci_dev *pdev)
 		goto err;
 
 	hw->pdev = pdev;
-	strncpy(hw->drv_version, CSIO_DRV_VERSION, 32);
+	strscpy(hw->drv_version, CSIO_DRV_VERSION,
+		sizeof(hw->drv_version));
 
 	/* memory pool/DMA pool allocation */
 	if (csio_resource_alloc(hw))

---
base-commit: 9c5d00cb7b6bbc5a7965d9ab7d223b5402d1f02c
change-id: 20231023-strncpy-drivers-scsi-csiostor-csio_init-c-9c96b2f77e22

Best regards,
--
Justin Stitt <justinstitt@google.com>


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] scsi: csiostor: replace deprecated strncpy with strscpy
  2023-10-23 20:26 [PATCH] scsi: csiostor: replace deprecated strncpy with strscpy Justin Stitt
@ 2023-10-25  0:30 ` Kees Cook
  2023-11-15 14:06 ` Martin K. Petersen
  2023-11-25  2:54 ` Martin K. Petersen
  2 siblings, 0 replies; 4+ messages in thread
From: Kees Cook @ 2023-10-25  0:30 UTC (permalink / raw)
  To: Justin Stitt
  Cc: James E.J. Bottomley, Martin K. Petersen, linux-scsi,
	linux-kernel, linux-hardening

On Mon, Oct 23, 2023 at 08:26:13PM +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.
> 
> `hw` is kzalloc'd just before this string assignment:
> |       hw = kzalloc(sizeof(struct csio_hw), GFP_KERNEL);
> 
> ... which means any NUL-padding is redundant.
> 
> Since  CSIO_DRV_VERSION is a small string literal (smaller than
> sizeof(dest)):
> 
> ... there is functionally no change in this swap from strncpy() to
> strscpy(). Nonetheless, let's make the change for robustness' sake -- as
> it will ensure that drv_version is _always_ NUL-terminated.
> 
> 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@vger.kernel.org
> Signed-off-by: Justin Stitt <justinstitt@google.com>

Another direct replacement.

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] scsi: csiostor: replace deprecated strncpy with strscpy
  2023-10-23 20:26 [PATCH] scsi: csiostor: replace deprecated strncpy with strscpy Justin Stitt
  2023-10-25  0:30 ` Kees Cook
@ 2023-11-15 14:06 ` Martin K. Petersen
  2023-11-25  2:54 ` Martin K. Petersen
  2 siblings, 0 replies; 4+ messages in thread
From: Martin K. Petersen @ 2023-11-15 14:06 UTC (permalink / raw)
  To: Justin Stitt
  Cc: James E.J. Bottomley, Martin K. Petersen, linux-scsi,
	linux-kernel, linux-hardening


Justin,

> strncpy() is deprecated for use on NUL-terminated destination strings
> [1] and as such we should prefer more robust and less ambiguous string
> interfaces.

Applied to 6.8/scsi-staging, thanks!

-- 
Martin K. Petersen	Oracle Linux Engineering

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] scsi: csiostor: replace deprecated strncpy with strscpy
  2023-10-23 20:26 [PATCH] scsi: csiostor: replace deprecated strncpy with strscpy Justin Stitt
  2023-10-25  0:30 ` Kees Cook
  2023-11-15 14:06 ` Martin K. Petersen
@ 2023-11-25  2:54 ` Martin K. Petersen
  2 siblings, 0 replies; 4+ messages in thread
From: Martin K. Petersen @ 2023-11-25  2:54 UTC (permalink / raw)
  To: James E.J. Bottomley, Justin Stitt
  Cc: Martin K . Petersen, linux-scsi, linux-kernel, linux-hardening

On Mon, 23 Oct 2023 20:26:13 +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.
> 
> `hw` is kzalloc'd just before this string assignment:
> |       hw = kzalloc(sizeof(struct csio_hw), GFP_KERNEL);
> 
> [...]

Applied to 6.8/scsi-queue, thanks!

[1/1] scsi: csiostor: replace deprecated strncpy with strscpy
      https://git.kernel.org/mkp/scsi/c/4592411784cc

-- 
Martin K. Petersen	Oracle Linux Engineering

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-11-25  2:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-23 20:26 [PATCH] scsi: csiostor: replace deprecated strncpy with strscpy Justin Stitt
2023-10-25  0:30 ` Kees Cook
2023-11-15 14:06 ` Martin K. Petersen
2023-11-25  2:54 ` Martin K. Petersen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox