public inbox for linux-crypto@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] crypto: Replace strlcpy with strscpy
@ 2023-06-20 20:08 Azeem Shaikh
  2023-06-20 20:12 ` Kees Cook
  2023-07-14  8:51 ` Herbert Xu
  0 siblings, 2 replies; 4+ messages in thread
From: Azeem Shaikh @ 2023-06-20 20:08 UTC (permalink / raw)
  To: Herbert Xu, David S. Miller, Eric Biggers
  Cc: linux-hardening, Azeem Shaikh, linux-crypto, linux-kernel

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

Signed-off-by: Azeem Shaikh <azeemshaikh38@gmail.com>
---
v2:
 * Use "int len" instead of "unsigned len" to hold return value of strscpy.

v1:
 * https://lore.kernel.org/all/20230613002258.3535506-1-azeemshaikh38@gmail.com/

 crypto/lrw.c |    6 +++---
 crypto/xts.c |    6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/crypto/lrw.c b/crypto/lrw.c
index 1b0f76ba3eb5..59260aefed28 100644
--- a/crypto/lrw.c
+++ b/crypto/lrw.c
@@ -357,10 +357,10 @@ static int lrw_create(struct crypto_template *tmpl, struct rtattr **tb)
 	 * cipher name.
 	 */
 	if (!strncmp(cipher_name, "ecb(", 4)) {
-		unsigned len;
+		int 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] != ')')
diff --git a/crypto/xts.c b/crypto/xts.c
index 09be909a6a1a..548b302c6c6a 100644
--- a/crypto/xts.c
+++ b/crypto/xts.c
@@ -396,10 +396,10 @@ static int xts_create(struct crypto_template *tmpl, struct rtattr **tb)
 	 * cipher name.
 	 */
 	if (!strncmp(cipher_name, "ecb(", 4)) {
-		unsigned len;
+		int 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] != ')')
-- 
2.41.0.162.gfafddb0af9-goog



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] crypto: Replace strlcpy with strscpy
  2023-06-20 20:08 [PATCH v2] crypto: Replace strlcpy with strscpy Azeem Shaikh
@ 2023-06-20 20:12 ` Kees Cook
  2023-07-10  2:36   ` Azeem Shaikh
  2023-07-14  8:51 ` Herbert Xu
  1 sibling, 1 reply; 4+ messages in thread
From: Kees Cook @ 2023-06-20 20:12 UTC (permalink / raw)
  To: Azeem Shaikh
  Cc: Herbert Xu, David S. Miller, Eric Biggers, linux-hardening,
	linux-crypto, linux-kernel

On Tue, Jun 20, 2023 at 08:08:32PM +0000, Azeem Shaikh wrote:
> 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
> 
> Signed-off-by: Azeem Shaikh <azeemshaikh38@gmail.com>

Thanks for fixing up the variable type. (And thank you Eric for catching
the signedness problem!)

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] crypto: Replace strlcpy with strscpy
  2023-06-20 20:12 ` Kees Cook
@ 2023-07-10  2:36   ` Azeem Shaikh
  0 siblings, 0 replies; 4+ messages in thread
From: Azeem Shaikh @ 2023-07-10  2:36 UTC (permalink / raw)
  To: Kees Cook
  Cc: Herbert Xu, David S. Miller, Eric Biggers, linux-hardening,
	linux-crypto, linux-kernel

On Tue, Jun 20, 2023 at 4:12 PM Kees Cook <keescook@chromium.org> wrote:
>
> On Tue, Jun 20, 2023 at 08:08:32PM +0000, Azeem Shaikh wrote:
> > 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
> >
> > Signed-off-by: Azeem Shaikh <azeemshaikh38@gmail.com>
>
> Thanks for fixing up the variable type. (And thank you Eric for catching
> the signedness problem!)
>
> Reviewed-by: Kees Cook <keescook@chromium.org>
>

Friendly ping on this.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] crypto: Replace strlcpy with strscpy
  2023-06-20 20:08 [PATCH v2] crypto: Replace strlcpy with strscpy Azeem Shaikh
  2023-06-20 20:12 ` Kees Cook
@ 2023-07-14  8:51 ` Herbert Xu
  1 sibling, 0 replies; 4+ messages in thread
From: Herbert Xu @ 2023-07-14  8:51 UTC (permalink / raw)
  To: Azeem Shaikh
  Cc: David S. Miller, Eric Biggers, linux-hardening, linux-crypto,
	linux-kernel

On Tue, Jun 20, 2023 at 08:08:32PM +0000, Azeem Shaikh wrote:
> 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
> 
> Signed-off-by: Azeem Shaikh <azeemshaikh38@gmail.com>
> ---
> v2:
>  * Use "int len" instead of "unsigned len" to hold return value of strscpy.
> 
> v1:
>  * https://lore.kernel.org/all/20230613002258.3535506-1-azeemshaikh38@gmail.com/
> 
>  crypto/lrw.c |    6 +++---
>  crypto/xts.c |    6 +++---
>  2 files changed, 6 insertions(+), 6 deletions(-)

Patch applied.  Thanks.
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-07-14  8:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-20 20:08 [PATCH v2] crypto: Replace strlcpy with strscpy Azeem Shaikh
2023-06-20 20:12 ` Kees Cook
2023-07-10  2:36   ` Azeem Shaikh
2023-07-14  8:51 ` Herbert Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox