linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] USB: gadget: Replace deprecated strncpy() with strscpy()
@ 2025-03-20 16:56 Thorsten Blum
  2025-04-07 18:54 ` Kees Cook
  0 siblings, 1 reply; 2+ messages in thread
From: Thorsten Blum @ 2025-03-20 16:56 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Krzysztof Kozlowski, Al Viro
  Cc: Thorsten Blum, linux-hardening, linux-usb, linux-kernel

strncpy() is deprecated for NUL-terminated destination buffers; use
strscpy() instead. Since kzalloc() already zeroes out the destination
buffer, the potential NUL-padding by strncpy() is unnecessary. strscpy()
copies only the required characters and guarantees NUL-termination.

Since the destination buffer has a fixed length, strscpy() automatically
determines its size using sizeof() when the argument is omitted. This
makes an explicit sizeof() call unnecessary.

The source string is also NUL-terminated and meets the __must_be_cstr()
requirement of strscpy().

No functional changes intended.

Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 drivers/usb/gadget/legacy/inode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/gadget/legacy/inode.c b/drivers/usb/gadget/legacy/inode.c
index b6a30d88a800..fcce84a726f2 100644
--- a/drivers/usb/gadget/legacy/inode.c
+++ b/drivers/usb/gadget/legacy/inode.c
@@ -1615,7 +1615,7 @@ static int activate_ep_files (struct dev_data *dev)
 		mutex_init(&data->lock);
 		init_waitqueue_head (&data->wait);
 
-		strncpy (data->name, ep->name, sizeof (data->name) - 1);
+		strscpy(data->name, ep->name);
 		refcount_set (&data->count, 1);
 		data->dev = dev;
 		get_dev (dev);

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

* Re: [PATCH] USB: gadget: Replace deprecated strncpy() with strscpy()
  2025-03-20 16:56 [PATCH] USB: gadget: Replace deprecated strncpy() with strscpy() Thorsten Blum
@ 2025-04-07 18:54 ` Kees Cook
  0 siblings, 0 replies; 2+ messages in thread
From: Kees Cook @ 2025-04-07 18:54 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Greg Kroah-Hartman, Krzysztof Kozlowski, Al Viro, linux-hardening,
	linux-usb, linux-kernel

On Thu, Mar 20, 2025 at 05:56:44PM +0100, Thorsten Blum wrote:
> strncpy() is deprecated for NUL-terminated destination buffers; use
> strscpy() instead. Since kzalloc() already zeroes out the destination
> buffer, the potential NUL-padding by strncpy() is unnecessary. strscpy()
> copies only the required characters and guarantees NUL-termination.

Looks right to me; thanks for the details!

> 
> Since the destination buffer has a fixed length, strscpy() automatically
> determines its size using sizeof() when the argument is omitted. This
> makes an explicit sizeof() call unnecessary.
> 
> The source string is also NUL-terminated and meets the __must_be_cstr()
> requirement of strscpy().
> 
> No functional changes intended.
> 
> Link: https://github.com/KSPP/linux/issues/90
> Cc: linux-hardening@vger.kernel.org
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>

Reviewed-by: Kees Cook <kees@kernel.org>


> ---
>  drivers/usb/gadget/legacy/inode.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/gadget/legacy/inode.c b/drivers/usb/gadget/legacy/inode.c
> index b6a30d88a800..fcce84a726f2 100644
> --- a/drivers/usb/gadget/legacy/inode.c
> +++ b/drivers/usb/gadget/legacy/inode.c
> @@ -1615,7 +1615,7 @@ static int activate_ep_files (struct dev_data *dev)
>  		mutex_init(&data->lock);
>  		init_waitqueue_head (&data->wait);
>  
> -		strncpy (data->name, ep->name, sizeof (data->name) - 1);
> +		strscpy(data->name, ep->name);
>  		refcount_set (&data->count, 1);
>  		data->dev = dev;
>  		get_dev (dev);

-- 
Kees Cook

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

end of thread, other threads:[~2025-04-07 18:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-20 16:56 [PATCH] USB: gadget: Replace deprecated strncpy() with strscpy() Thorsten Blum
2025-04-07 18:54 ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).