Skip to contentSkip to conversation

crypto: Replace strlcpy with strscpy

PATCH In review Azeem Shaikh 2 participants 3 messages Jun 13, 2023 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: 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 -E2BIG 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

---
 crypto/lrw.c |    4 ++--
 crypto/xts.c |    4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
crypto/lrw.c +2 -2
index 1b0f76ba3eb5..bb8c1575645b 100644
@@ -359,8 +359,8 @@ static int lrw_create(struct crypto_template *tmpl, struct rtattr **tb)
 	if (!strncmp(cipher_name, "ecb(", 4)) {
 		unsigned len;
 
-		len = strlcpy(ecb_name, cipher_name + 4, sizeof(ecb_name));
-		if (len < 2 || len >= sizeof(ecb_name))
+		len = strscpy(ecb_name, cipher_name + 4, sizeof(ecb_name));
+		if (len < 2)
 			goto err_free_inst;
 
 		if (ecb_name[len - 1] != ')')
crypto/xts.c +2 -2
index 09be909a6a1a..8a9f9653426e 100644
@@ -398,8 +398,8 @@ static int xts_create(struct crypto_template *tmpl, struct rtattr **tb)
 	if (!strncmp(cipher_name, "ecb(", 4)) {
 		unsigned len;
 
-		len = strlcpy(ctx->name, cipher_name + 4, sizeof(ctx->name));
-		if (len < 2 || len >= sizeof(ctx->name))
+		len = strscpy(ctx->name, cipher_name + 4, sizeof(ctx->name));
+		if (len < 2)
 			goto err_free_inst;
 
 		if (ctx->name[len - 1] != ')')
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
#1 lore raw

crypto/lrw.c static int lrw_create(struct crypto_template *tmpl, struct rtattr **tb) · line 364

Quoted 13 lines · Azeem Shaikh
crypto/lrw.c +2 -2
index 1b0f76ba3eb5..bb8c1575645b 100644
@@ -359,8 +359,8 @@ static int lrw_create(struct crypto_template *tmpl, struct rtattr **tb)
 	if (!strncmp(cipher_name, "ecb(", 4)) {
 		unsigned len;
 
-		len = strlcpy(ecb_name, cipher_name + 4, sizeof(ecb_name));
-		if (len < 2 || len >= sizeof(ecb_name))
+		len = strscpy(ecb_name, cipher_name + 4, sizeof(ecb_name));
+		if (len < 2)
 			goto err_free_inst;

This is wrong because 'len' is unsigned. So the -EFBIG return value is not being checked for, so this patch actually removes the overflow check.

It looks like you've sent over 60 strscpy conversion patches recently
(https://lore.kernel.org/linux-hardening/?q=f%3Aazeemshaikh38+s%3Astrscpy).
That's concerning because this is the only one that I happened to review, and it introduces a bug. There are 60 others that could have this same bug. Can you please review all your patches for this same bug?

- Eric

#2 lore raw
Quoted 10 lines · Eric Biggers
Quoted 8 lines · Azeem Shaikh
crypto/lrw.c +0 -0
index 1b0f76ba3eb5..bb8c1575645b 100644
@@ -359,8 +359,8 @@ static int lrw_create(struct crypto_template *tmpl, struct rtattr **tb)
      if (!strncmp(cipher_name, "ecb(", 4)) {
              unsigned len;
 
Quoted 9 lines

crypto/lrw.c static int lrw_create(struct crypto_template *tmpl, struct rtattr **tb) · line 364

Quoted 5 lines
crypto/lrw.c +2 -2
-             len = strlcpy(ecb_name, cipher_name + 4, sizeof(ecb_name));
-             if (len < 2 || len >= sizeof(ecb_name))
+             len = strscpy(ecb_name, cipher_name + 4, sizeof(ecb_name));
+             if (len < 2)
                      goto err_free_inst;

This is wrong because 'len' is unsigned. So the -EFBIG return value is not being checked for, so this patch actually removes the overflow check.

Thanks for catching this Eric. I can send over a v2 with "unsigned len" -> "int len" if that works?

Quoted 6 lines

It looks like you've sent over 60 strscpy conversion patches recently
(https://lore.kernel.org/linux-hardening/?q=f%3Aazeemshaikh38+s%3Astrscpy).
That's concerning because this is the only one that I happened to review, and it introduces a bug. There are 60 others that could have this same bug. Can you please review all your patches for this same bug?

My previous patches replace strlcpy where the return value is ignored. So this same bug could not have occurred in those. This is the first set of patches where I'm attempting to replace strlcpy whose return value is used by callers. For reference below are all my patches where the return value of strlcpy is used. I have checked that none of these have the same bug as here.

https://lore.kernel.org/all/[email protected]/
// Uses int
https://lore.kernel.org/all/[email protected]/
// Uses int
https://lore.kernel.org/all/[email protected]/
// Replaces ">= buflen" with "== -E2BIG"
https://lore.kernel.org/all/[email protected]/
// Uses int