Skip to contentSkip to conversation

usbip: usbip_host: Replace strlcpy with strscpy

PATCH In review Azeem Shaikh 3 participants 3 messages Jun 15, 2023 – Jun 22, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 usbip: usbip_host: Replace strlcpy with strscpy

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

---
v3:
* Removed declaration of @len to fix build failures in v2.

v2:
* https://lore.kernel.org/all/[email protected]/
* Use "< 0" instead of "== -E2BIG".

v1:
* https://lore.kernel.org/all/[email protected]/

 drivers/usb/usbip/stub_main.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/usb/usbip/stub_main.c +1 -3
index e8c3131a8543..0a6624d37929 100644
@@ -167,15 +167,13 @@ static ssize_t match_busid_show(struct device_driver *drv, char *buf)
 static ssize_t match_busid_store(struct device_driver *dev, const char *buf,
 				 size_t count)
 {
-	int len;
 	char busid[BUSID_SIZE];
 
 	if (count < 5)
 		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)) {
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
Reviewed-by on v3 1/1
#1 lore raw
Quoted 7 lines · Azeem Shaikh

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().

Quoted 7 lines

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

Signature · 1 line
Kees Cook
maintainer Acked-by
#2 lore raw
Quoted 11 lines · Kees Cook
Quoted 10 lines · Azeem Shaikh

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).

Quoted 7 lines
Quoted 4 lines

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

thanks,
-- Shuah