usbip: usbip_host: Replace strlcpy with strscpy
Subsystems (2)
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
@@ -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
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 removestrlcpy()completely [2], replacestrlcpy()here withstrscpy().
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/89Signed-off-by Azeem Shaikh <[email protected]>
Signature · 1 line
Kees Cook
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 removestrlcpy()completely [2], replacestrlcpy()here withstrscpy().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/89Signed-off-by Azeem Shaikh <[email protected]>Reviewed-by Kees Cook <[email protected]>
thanks,
-- Shuah