scsi: target: Replace strlcpy with strscpy
Subsystems (2)
PATCH v2 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
---
v2:
* Replace all instances of strlcpy in this file instead of just 1.
v1:
* https://lore.kernel.org/all/[email protected]/
drivers/target/target_core_configfs.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-)
drivers/target/target_core_configfs.c +12 -15
@@ -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" );
@@ -1448,16 +1447,15 @@ static ssize_t target_wwn_product_id_store(struct config_item *item,/* +2 to allow for a trailing (stripped) '\n' and null-terminator */ unsigned char buf[INQUIRY_MODEL_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_MODEL_LEN ) { +if (len <0 ||strlen (stripped) >INQUIRY_MODEL_LEN ) { pr_err("Emulated T10 Vendor exceeds INQUIRY_MODEL_LEN: " __stringify(INQUIRY_MODEL_LEN)"\n" );
@@ -1504,16 +1502,15 @@ static ssize_t target_wwn_revision_store(struct config_item *item,/* +2 to allow for a trailing (stripped) '\n' and null-terminator */ unsigned char buf[INQUIRY_REVISION_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_REVISION_LEN ) { +if (len <0 ||strlen (stripped) >INQUIRY_REVISION_LEN ) { pr_err("Emulated T10 Revision exceeds INQUIRY_REVISION_LEN: " __stringify(INQUIRY_REVISION_LEN)"\n" );
--
2.42.0.283.g2d96d420d3-goog
Quoted 39 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]>---
v2:
* Replace all instances of strlcpy in this file instead of just 1.v1:
* https://lore.kernel.org/all/[email protected]/drivers/target/target_core_configfs.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-)drivers/target/target_core_configfs.c +2
-3@@ -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));
drivers/target/target_core_configfs.c static ssize_t target_wwn_vendor_id_store(struct config_item *item, · line 1406
Quoted 10 lines
drivers/target/target_core_configfs.c +2
-2+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" );
Should we be explicitly checking for len == -E2BIG instead of the more generic len < 0? Perhaps this is a nitpick but I prefer the former.
...
Quoted 11 lines
drivers/target/target_core_configfs.c +2
-3@@ -1448,16 +1447,15 @@ static ssize_t target_wwn_product_id_store(struct config_item *item,/* +2 to allow for a trailing (stripped) '\n' and null-terminator */ unsigned char buf[INQUIRY_MODEL_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));
drivers/target/target_core_configfs.c static ssize_t target_wwn_product_id_store(struct config_item *item, · line 1461
Quoted 10 lines
drivers/target/target_core_configfs.c +2
-2+if (len >0 ) {/* Strip any newline added from userspace. */ stripped =strstrip (buf); - len =strlen (stripped); } -if (len >INQUIRY_MODEL_LEN ) { +if (len <0 ||strlen (stripped) >INQUIRY_MODEL_LEN ) { pr_err("Emulated T10 Vendor exceeds INQUIRY_MODEL_LEN: " __stringify(INQUIRY_MODEL_LEN)"\n" );
... and here
Quoted 11 lines
drivers/target/target_core_configfs.c +2
-3@@ -1504,16 +1502,15 @@ static ssize_t target_wwn_revision_store(struct config_item *item,/* +2 to allow for a trailing (stripped) '\n' and null-terminator */ unsigned char buf[INQUIRY_REVISION_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));
drivers/target/target_core_configfs.c static ssize_t target_wwn_revision_store(struct config_item *item, · line 1516
Quoted 10 lines
drivers/target/target_core_configfs.c +2
-2+if (len >0 ) {/* Strip any newline added from userspace. */ stripped =strstrip (buf); - len =strlen (stripped); } -if (len >INQUIRY_REVISION_LEN ) { +if (len <0 ||strlen (stripped) >INQUIRY_REVISION_LEN ) { pr_err("Emulated T10 Revision exceeds INQUIRY_REVISION_LEN: " __stringify(INQUIRY_REVISION_LEN)"\n" );
... and here
Quoted 4 lines
--
2.42.0.283.g2d96d420d3-goog
At any rate, this is a worthwhile change for the reasons you mentioned.
Quoted 38 lines · Justin Stitt
Quoted 36 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]>---
v2:
* Replace all instances of strlcpy in this file instead of just 1.v1:
* https://lore.kernel.org/all/[email protected]/drivers/target/target_core_configfs.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-)drivers/target/target_core_configfs.c +1
-1@@ -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;
Quoted 17 lines
drivers/target/target_core_configfs.c static ssize_t target_wwn_vendor_id_store(struct config_item *item, · line 1406
Quoted 13 lines
drivers/target/target_core_configfs.c +3
-4- 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" );Should we be explicitly checking for
len == -E2BIGinstead of the more genericlen < 0? Perhaps this is a nitpick but I prefer the former.
Most maintainers to whom I have sent such patches in the past have preferred len < 0 since it is not "rigid" in checking for -E2BIG which is an implementation detail of strscpy.
Quoted 37 lines · Justin Stitt
Quoted 36 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]>---
v2:
* Replace all instances of strlcpy in this file instead of just 1.v1:
* https://lore.kernel.org/all/[email protected]/drivers/target/target_core_configfs.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-)drivers/target/target_core_configfs.c +1
-1@@ -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;
Quoted 16 lines
drivers/target/target_core_configfs.c static ssize_t target_wwn_vendor_id_store(struct config_item *item, · line 1406
Quoted 13 lines
drivers/target/target_core_configfs.c +3
-4- 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" );Should we be explicitly checking for
len == -E2BIGinstead of the more genericlen < 0? Perhaps this is a nitpick but I prefer the former.
For robustness, we want to just do the "< 0" test -- that way if strscpy() gains a new errno (for some unimaginable reason) then all the code testing for "< 0" will continue to work sanely.
-Kees
Signature · 1 line
Kees Cook
Quoted 39 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]>---
v2:
* Replace all instances of strlcpy in this file instead of just 1.v1:
* https://lore.kernel.org/all/[email protected]/drivers/target/target_core_configfs.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-)drivers/target/target_core_configfs.c +2
-3@@ -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));
drivers/target/target_core_configfs.c static ssize_t target_wwn_vendor_id_store(struct config_item *item, · line 1406
Quoted 10 lines
drivers/target/target_core_configfs.c +2
-2+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" );
I would find these more readable if the "len = strlen" were left and the final "if" becomes:
if (len < 0 || len > INQUIRY_VENDOR_LEN)
Quoted 46 lines
drivers/target/target_core_configfs.c +8
-11@@ -1448,16 +1447,15 @@ static ssize_t target_wwn_product_id_store(struct config_item *item,/* +2 to allow for a trailing (stripped) '\n' and null-terminator */ unsigned char buf[INQUIRY_MODEL_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_MODEL_LEN ) { +if (len <0 ||strlen (stripped) >INQUIRY_MODEL_LEN ) { pr_err("Emulated T10 Vendor exceeds INQUIRY_MODEL_LEN: " __stringify(INQUIRY_MODEL_LEN)"\n" );@@ -1504,16 +1502,15 @@ static ssize_t target_wwn_revision_store(struct config_item *item,/* +2 to allow for a trailing (stripped) '\n' and null-terminator */ unsigned char buf[INQUIRY_REVISION_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_REVISION_LEN ) { +if (len <0 ||strlen (stripped) >INQUIRY_REVISION_LEN ) { pr_err("Emulated T10 Revision exceeds INQUIRY_REVISION_LEN: " __stringify(INQUIRY_REVISION_LEN)"\n" ); --2.42.0.283.g2d96d420d3-goog
Signature · 1 line
Kees Cook