Skip to contentSkip to conversation

scsi: target: Replace strlcpy with strscpy

PATCH Applied Azeem Shaikh 3 participants 6 messages Aug 31, 2023 – Sep 14, 2023

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

PATCH v3 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

---
v3:
* Address readability comment.

v2:
* Replace all instances of strlcpy in this file instead of just 1.
* https://lore.kernel.org/all/[email protected]/

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

 drivers/target/target_core_configfs.c |   24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/target/target_core_configfs.c +12 -12
index 936e5ff1b209..d5860c1c1f46 100644
@@ -1392,16 +1392,16 @@ 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 || len > INQUIRY_VENDOR_LEN) {
 		pr_err("Emulated T10 Vendor Identification exceeds"
 			" INQUIRY_VENDOR_LEN: " __stringify(INQUIRY_VENDOR_LEN)
 			"\n");
@@ -1448,16 +1448,16 @@ 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 || len > INQUIRY_MODEL_LEN) {
 		pr_err("Emulated T10 Vendor exceeds INQUIRY_MODEL_LEN: "
 			 __stringify(INQUIRY_MODEL_LEN)
 			"\n");
@@ -1504,16 +1504,16 @@ 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 || len > INQUIRY_REVISION_LEN) {
 		pr_err("Emulated T10 Revision exceeds INQUIRY_REVISION_LEN: "
 			 __stringify(INQUIRY_REVISION_LEN)
 			"\n");

--
2.42.0.283.g2d96d420d3-goog

#1 lore raw
Quoted 40 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).

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

---
v3:
* Address readability comment.

v2:
* Replace all instances of strlcpy in this file instead of just 1.
* https://lore.kernel.org/all/[email protected]/

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

 drivers/target/target_core_configfs.c |   24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/target/target_core_configfs.c +1 -1
index 936e5ff1b209..d5860c1c1f46 100644
@@ -1392,16 +1392,16 @@ 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;
 

drivers/target/target_core_configfs.c static ssize_t target_wwn_vendor_id_store(struct config_item *item, · line 1404

Quoted 10 lines
drivers/target/target_core_configfs.c +3 -3
-	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 || len > INQUIRY_VENDOR_LEN) {

Agh, sorry I missed this before: the first "if" needs to be "len >= 0" otherwise this:

        ret = target_check_inquiry_data(stripped);

will be passing a NULL pointer...

Signature · 1 line
Kees Cook
#2 lore raw
Quoted 56 lines · Kees Cook
Quoted 40 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).

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

---
v3:
* Address readability comment.

v2:
* Replace all instances of strlcpy in this file instead of just 1.
* https://lore.kernel.org/all/[email protected]/

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

 drivers/target/target_core_configfs.c |   24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/target/target_core_configfs.c +1 -1
index 936e5ff1b209..d5860c1c1f46 100644
@@ -1392,16 +1392,16 @@ 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;
 

drivers/target/target_core_configfs.c static ssize_t target_wwn_vendor_id_store(struct config_item *item, · line 1404

Quoted 10 lines
drivers/target/target_core_configfs.c +3 -3
-     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 || len > INQUIRY_VENDOR_LEN) {

Agh, sorry I missed this before: the first "if" needs to be "len >= 0" otherwise this:

Quoted 4 lines
        ret = target_check_inquiry_data(stripped);

will be passing a NULL pointer...

Hmm, the current implementation of strscpy never returns 0. If sizeof(buf) is 0, it'll return -E2BIG. Do you still prefer that I update this to check for len >= 0?

Reviewed-by
#3 lore raw
Quoted 57 lines · Azeem Shaikh
Quoted 56 lines · Kees Cook
Quoted 40 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).

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

---
v3:
* Address readability comment.

v2:
* Replace all instances of strlcpy in this file instead of just 1.
* https://lore.kernel.org/all/[email protected]/

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

 drivers/target/target_core_configfs.c |   24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/target/target_core_configfs.c +1 -1
index 936e5ff1b209..d5860c1c1f46 100644
@@ -1392,16 +1392,16 @@ 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;
 

drivers/target/target_core_configfs.c static ssize_t target_wwn_vendor_id_store(struct config_item *item, · line 1404

Quoted 10 lines
drivers/target/target_core_configfs.c +3 -3
-     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 || len > INQUIRY_VENDOR_LEN) {

Agh, sorry I missed this before: the first "if" needs to be "len >= 0" otherwise this:

Quoted 8 lines
Quoted 4 lines
        ret = target_check_inquiry_data(stripped);

will be passing a NULL pointer...

Hmm, the current implementation of strscpy never returns 0. If sizeof(buf) is 0, it'll return -E2BIG. Do you still prefer that I update this to check for len >= 0?

Oh right! Nevermind, then. Fine as-is. :)

Signature · 1 line
Kees Cook
#4 lore raw
In reply to Azeem Shaikh

Azeem,

Quoted 5 lines

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

Applied to 6.6/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#5 lore raw
In reply to Azeem Shaikh
Quoted 8 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().

[...]

Applied to 6.6/scsi-fixes, thanks!

[1/1] scsi: target: Replace strlcpy with strscpy

      https://git.kernel.org/mkp/scsi/c/5c584fe6098a
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering