scsi: target: Replace strlcpy with strscpy
Subsystems (2)
PATCH 1/1 scsi: target: 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
--- drivers/target/target_core_configfs.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/target/target_core_configfs.c +4 -5
@@ -1392,16 +1392,15 @@ static ssize_t target_wwn_vendor_id_store(struct config_item *item,/* +2 to allow for a trailing (stripped) '\n' and null-terminator */ unsigned char buf[INQUIRY_VENDOR_LEN +2 ];char *stripped =NULL ; -size_t len; +ssize_t len;ssize_t ret; - len =strlcpy (buf, page,sizeof (buf)); -if (len <sizeof (buf)) { + len =strscpy (buf, page,sizeof (buf)); +if (len >0 ) {/* Strip any newline added from userspace. */ stripped =strstrip (buf); - len =strlen (stripped); } -if (len >INQUIRY_VENDOR_LEN ) { +if (len <0 ||strlen (stripped) >INQUIRY_VENDOR_LEN ) { pr_err("Emulated T10 Vendor Identification exceeds" " INQUIRY_VENDOR_LEN: " __stringify(INQUIRY_VENDOR_LEN)"\n" );
--
2.42.0.283.g2d96d420d3-goog
Please ignore this patch. There are more strlcpy()->strscpy() replacement in the same file that did not get included here. Will send out a v2 with all the changes to make it easier for reviewers.
Quoted 48 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).
[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]>--- drivers/target/target_core_configfs.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)drivers/target/target_core_configfs.c +4
-6@@ -1392,16 +1392,15 @@ static ssize_t target_wwn_vendor_id_store(struct config_item *item,/* +2 to allow for a trailing (stripped) '\n' and null-terminator */ unsigned char buf[INQUIRY_VENDOR_LEN +2 ];char *stripped =NULL ; -size_t len; +ssize_t len;ssize_t ret; - len =strlcpy (buf, page,sizeof (buf)); -if (len <sizeof (buf)) { + len =strscpy (buf, page,sizeof (buf)); +if (len >0 ) {/* Strip any newline added from userspace. */ stripped =strstrip (buf); - len =strlen (stripped); } -if (len >INQUIRY_VENDOR_LEN ) { +if (len <0 ||strlen (stripped) >INQUIRY_VENDOR_LEN ) { pr_err("Emulated T10 Vendor Identification exceeds" " INQUIRY_VENDOR_LEN: " __stringify(INQUIRY_VENDOR_LEN)"\n" ); --2.42.0.283.g2d96d420d3-goog