crypto: Replace strlcpy with strscpy
PATCH v2 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 -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:
* Use "int len" instead of "unsigned len" to hold return value of strscpy.
v1:
* https://lore.kernel.org/all/[email protected]/
crypto/lrw.c | 6 +++--- crypto/xts.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-)
crypto/lrw.c +3 -3
@@ -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 ] !=')' )
crypto/xts.c +3 -3
@@ -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 ] !=')' )
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
Quoted 7 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().
Quoted 7 lines
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]>
Thanks for fixing up the variable type. (And thank you Eric for catching the signedness problem!)
Signature · 1 line
Kees Cook
Quoted 15 lines · Kees Cook
Quoted 13 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/89
Quoted 7 lines
Quoted 1 line
Signed-off-by Azeem Shaikh <[email protected]>Thanks for fixing up the variable type. (And thank you Eric for catching the signedness problem!)
Reviewed-by Kees Cook <[email protected]>
Friendly ping on this.
Herbert Xu
maintainerQuoted 18 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:
* Use "int len" instead of "unsigned len" to hold return value of strscpy.
Quoted 6 lines
v1:
* https://lore.kernel.org/all/[email protected]/crypto/lrw.c | 6 +++--- crypto/xts.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-)
Patch applied. Thanks.
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt