public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ibmvmc: replace deprecated strncpy with strscpy
@ 2023-09-27  5:52 Justin Stitt
  2023-10-02 18:01 ` Kees Cook
  0 siblings, 1 reply; 2+ messages in thread
From: Justin Stitt @ 2023-09-27  5:52 UTC (permalink / raw)
  To: Brad Warrum, Ritu Agarwal, Arnd Bergmann, Greg Kroah-Hartman
  Cc: 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.

A suitable replacement is `strscpy` [2] due to the fact that it
guarantees NUL-termination on the destination buffer without
unnecessarily NUL-padding.

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@vger.kernel.org
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
Note: build-tested only.
---
 drivers/misc/ibmvmc.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/misc/ibmvmc.c b/drivers/misc/ibmvmc.c
index 2101eb12bcba..6d9ed9325f9f 100644
--- a/drivers/misc/ibmvmc.c
+++ b/drivers/misc/ibmvmc.c
@@ -1249,9 +1249,7 @@ static long ibmvmc_ioctl_sethmcid(struct ibmvmc_file_session *session,
 		return -EIO;
 	}
 
-	/* Make sure buffer is NULL terminated before trying to print it */
-	memset(print_buffer, 0, HMC_ID_LEN + 1);
-	strncpy(print_buffer, hmc->hmc_id, HMC_ID_LEN);
+	strscpy(print_buffer, hmc->hmc_id, sizeof(print_buffer));
 	pr_info("ibmvmc: sethmcid: Set HMC ID: \"%s\"\n", print_buffer);
 
 	memcpy(buffer->real_addr_local, hmc->hmc_id, HMC_ID_LEN);

---
base-commit: 6465e260f48790807eef06b583b38ca9789b6072
change-id: 20230927-strncpy-drivers-misc-ibmvmc-c-534349716fa4

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


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

* Re: [PATCH] ibmvmc: replace deprecated strncpy with strscpy
  2023-09-27  5:52 [PATCH] ibmvmc: replace deprecated strncpy with strscpy Justin Stitt
@ 2023-10-02 18:01 ` Kees Cook
  0 siblings, 0 replies; 2+ messages in thread
From: Kees Cook @ 2023-10-02 18:01 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Brad Warrum, Ritu Agarwal, Arnd Bergmann, Greg Kroah-Hartman,
	linux-kernel, linux-hardening

On Wed, Sep 27, 2023 at 05:52:14AM +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.
> 
> A suitable replacement is `strscpy` [2] due to the fact that it
> guarantees NUL-termination on the destination buffer without
> unnecessarily NUL-padding.

This is weird -- I think hmc_id needs to be marked __nonstring, and the
commit log should talk about how it appears to be not %NUL terminated.

The bounce buffer is weird -- it seems like hmc_id could be made a
regular C string by being changed to MHC_ID_LEN + 1, and then everything
would work correctly without needing a bounce buffer, etc. (Well, it
would need explicit %NUL termination after the copy_from_user()).

But if that refactor doesn't sound right to maintainers, then I'd agree
that this patch is fine (though capturing the __nonstring bit might be
nice):

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

-Kees

> 
> 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@vger.kernel.org
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> ---
> Note: build-tested only.
> ---
>  drivers/misc/ibmvmc.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/drivers/misc/ibmvmc.c b/drivers/misc/ibmvmc.c
> index 2101eb12bcba..6d9ed9325f9f 100644
> --- a/drivers/misc/ibmvmc.c
> +++ b/drivers/misc/ibmvmc.c
> @@ -1249,9 +1249,7 @@ static long ibmvmc_ioctl_sethmcid(struct ibmvmc_file_session *session,
>  		return -EIO;
>  	}
>  
> -	/* Make sure buffer is NULL terminated before trying to print it */
> -	memset(print_buffer, 0, HMC_ID_LEN + 1);
> -	strncpy(print_buffer, hmc->hmc_id, HMC_ID_LEN);
> +	strscpy(print_buffer, hmc->hmc_id, sizeof(print_buffer));
>  	pr_info("ibmvmc: sethmcid: Set HMC ID: \"%s\"\n", print_buffer);
>  
>  	memcpy(buffer->real_addr_local, hmc->hmc_id, HMC_ID_LEN);
> 
> ---
> base-commit: 6465e260f48790807eef06b583b38ca9789b6072
> change-id: 20230927-strncpy-drivers-misc-ibmvmc-c-534349716fa4
> 
> Best regards,
> --
> Justin Stitt <justinstitt@google.com>
> 

-- 
Kees Cook

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

end of thread, other threads:[~2023-10-02 18:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-27  5:52 [PATCH] ibmvmc: replace deprecated strncpy with strscpy Justin Stitt
2023-10-02 18:01 ` Kees Cook

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