public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] usbip: usbip_host: Replace strlcpy with strscpy
@ 2023-06-14 14:10 Azeem Shaikh
  2023-06-14 15:02 ` Shuah Khan
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Azeem Shaikh @ 2023-06-14 14:10 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Valentina Manea, Shuah Khan, Shuah Khan,
	Kees Cook
  Cc: linux-hardening, Azeem Shaikh, Hongren Zheng, linux-usb,
	linux-kernel

strlcpy() reads the entire source buffer first.
This read may exceed the destination size limit.
This is both inefficient and can lead to linear read
overflows if a source string is not NUL-terminated [1].
In an effort to remove strlcpy() completely [2], replace
strlcpy() here with strscpy().

Direct replacement is safe here since return value of -errno
is used to check for truncation instead of sizeof(dest).

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
[2] https://github.com/KSPP/linux/issues/89

Signed-off-by: Azeem Shaikh <azeemshaikh38@gmail.com>
---
v1: https://lore.kernel.org/all/20230613004402.3540432-1-azeemshaikh38@gmail.com/

Changes from v1 - uses "< 0" instead of "== -E2BIG".

 drivers/usb/usbip/stub_main.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/usb/usbip/stub_main.c b/drivers/usb/usbip/stub_main.c
index e8c3131a8543..97043b4df275 100644
--- a/drivers/usb/usbip/stub_main.c
+++ b/drivers/usb/usbip/stub_main.c
@@ -174,8 +174,7 @@ static ssize_t match_busid_store(struct device_driver *dev, const char *buf,
 		return -EINVAL;
 
 	/* busid needs to include \0 termination */
-	len = strlcpy(busid, buf + 4, BUSID_SIZE);
-	if (sizeof(busid) <= len)
+	if (strscpy(busid, buf + 4, BUSID_SIZE) < 0)
 		return -EINVAL;
 
 	if (!strncmp(buf, "add ", 4)) {
-- 
2.41.0.162.gfafddb0af9-goog



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

end of thread, other threads:[~2023-06-15 18:08 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-14 14:10 [PATCH v2] usbip: usbip_host: Replace strlcpy with strscpy Azeem Shaikh
2023-06-14 15:02 ` Shuah Khan
2023-06-14 17:48 ` Kees Cook
2023-06-14 23:59 ` kernel test robot
2023-06-15  9:33 ` Greg Kroah-Hartman
2023-06-15 18:08   ` Azeem Shaikh

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