keys: Replace deprecated strncpy in ecryptfs_fill_auth_tok
Subsystems (4)
PATCH v3 1/1 keys: Replace deprecated strncpy in ecryptfs_fill_auth_tok
strncpy() is deprecated for NUL-terminated destination buffers; use strscpy_pad() instead to retain the NUL-padding behavior of strncpy().
The destination buffer is initialized using kzalloc() with a 'signature' size of ECRYPTFS_PASSWORD_SIG_SIZE + 1. strncpy() then copies up to ECRYPTFS_PASSWORD_SIG_SIZE bytes from 'key_desc', NUL-padding any remaining bytes if needed, but expects the last byte to be zero.
strscpy_pad() also copies the source string to 'signature', and NUL-pads the destination buffer if needed, but ensures it's always NUL-terminated without relying on it being zero-initialized.
strscpy_pad() automatically determines the size of the fixed-length destination buffer via sizeof() when the optional size argument is omitted, making an explicit size unnecessary.
In encrypted_init(), the source string 'key_desc' is validated by valid_ecryptfs_desc() before calling ecryptfs_fill_auth_tok(), and is therefore NUL-terminated and satisfies the __must_be_cstr() requirement of strscpy_pad().
---
Changes in v3:
- Improve commit message
- Link to v2: https://lore.kernel.org/lkml/[email protected]/
Changes in v2:
- Improve commit message as suggested by Jarkko and Kees
- Link to v1: https://lore.kernel.org/lkml/[email protected]/
--- security/keys/encrypted-keys/ecryptfs_format.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
security/keys/encrypted-keys/ecryptfs_format.c +1 -2
@@ -54,8 +54,7 @@ int ecryptfs_fill_auth_tok(struct ecryptfs_auth_tok *auth_tok, auth_tok->version = (((uint16_t )(major <<8 ) &0xFF00 ) | ((uint16_t )minor &0x00FF )); auth_tok->token_type =ECRYPTFS_PASSWORD ; -strncpy ((char *)auth_tok->token.password.signature, key_desc, -ECRYPTFS_PASSWORD_SIG_SIZE ); +strscpy_pad (auth_tok->token.password.signature, key_desc); auth_tok->token.password.session_key_encryption_key_bytes =ECRYPTFS_MAX_KEY_BYTES ;/*
Signature · 1 line
2.51.0
Dear Thorsten,
Thank you for the patch.
Am 13.10.25 um 17:26 schrieb Thorsten Blum:
Quoted 42 lines
strncpy()is deprecated for NUL-terminated destination buffers; usestrscpy_pad()instead to retain the NUL-padding behavior ofstrncpy().The destination buffer is initialized using
kzalloc()with a 'signature' size ofECRYPTFS_PASSWORD_SIG_SIZE+ 1.strncpy()then copies up toECRYPTFS_PASSWORD_SIG_SIZEbytes from 'key_desc', NUL-padding any remaining bytes if needed, but expects the last byte to be zero.
strscpy_pad()also copies the source string to 'signature', and NUL-pads the destination buffer if needed, but ensures it's always NUL-terminated without relying on it being zero-initialized.
strscpy_pad()automatically determines the size of the fixed-length destination buffer viasizeof()when the optional size argument is omitted, making an explicit size unnecessary.In
encrypted_init(), the source string 'key_desc' is validated byvalid_ecryptfs_desc()before callingecryptfs_fill_auth_tok(), and is therefore NUL-terminated and satisfies the__must_be_cstr()requirement ofstrscpy_pad().Reviewed-by Kees Cook <[email protected]>Reviewed-by Jarkko Sakkinen <[email protected]>Signed-off-by Thorsten Blum <[email protected]>---
Changes in v3:
- Improve commit message
- Link to v2: https://lore.kernel.org/lkml/[email protected]/Changes in v2:
- Improve commit message as suggested by Jarkko and Kees
- Link to v1: https://lore.kernel.org/lkml/[email protected]/--- security/keys/encrypted-keys/ecryptfs_format.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)security/keys/encrypted-keys/ecryptfs_format.c +0
-0
security/keys/encrypted-keys/ecryptfs_format.c int ecryptfs_fill_auth_tok(struct ecryptfs_auth_tok *auth_tok, · line 60
Quoted 9 lines
security/keys/encrypted-keys/ecryptfs_format.c +1
-2@@ -54,8 +54,7 @@ int ecryptfs_fill_auth_tok(struct ecryptfs_auth_tok *auth_tok, auth_tok->version = (((uint16_t )(major <<8 ) &0xFF00 ) | ((uint16_t )minor &0x00FF )); auth_tok->token_type =ECRYPTFS_PASSWORD ; -strncpy ((char *)auth_tok->token.password.signature, key_desc, -ECRYPTFS_PASSWORD_SIG_SIZE ); +strscpy_pad (auth_tok->token.password.signature, key_desc); auth_tok->token.password.session_key_encryption_key_bytes =ECRYPTFS_MAX_KEY_BYTES ;/*
Kind regards,
Paul
Jarkko Sakkinen
maintainerQuoted 43 lines · Paul Menzel
Dear Thorsten,
Thank you for the patch.
Am 13.10.25 um 17:26 schrieb Thorsten Blum:
Quoted 37 lines
strncpy()is deprecated for NUL-terminated destination buffers; usestrscpy_pad()instead to retain the NUL-padding behavior ofstrncpy().The destination buffer is initialized using
kzalloc()with a 'signature' size ofECRYPTFS_PASSWORD_SIG_SIZE+ 1.strncpy()then copies up toECRYPTFS_PASSWORD_SIG_SIZEbytes from 'key_desc', NUL-padding any remaining bytes if needed, but expects the last byte to be zero.
strscpy_pad()also copies the source string to 'signature', and NUL-pads the destination buffer if needed, but ensures it's always NUL-terminated without relying on it being zero-initialized.
strscpy_pad()automatically determines the size of the fixed-length destination buffer viasizeof()when the optional size argument is omitted, making an explicit size unnecessary.In
encrypted_init(), the source string 'key_desc' is validated byvalid_ecryptfs_desc()before callingecryptfs_fill_auth_tok(), and is therefore NUL-terminated and satisfies the__must_be_cstr()requirement ofstrscpy_pad().Reviewed-by Kees Cook <[email protected]>Reviewed-by Jarkko Sakkinen <[email protected]>Signed-off-by Thorsten Blum <[email protected]>---
Changes in v3:
- Improve commit message
- Link to v2: https://lore.kernel.org/lkml/[email protected]/Changes in v2:
- Improve commit message as suggested by Jarkko and Kees
- Link to v1: https://lore.kernel.org/lkml/[email protected]/--- security/keys/encrypted-keys/ecryptfs_format.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
Quoted 16 lines
Quoted 5 lines
security/keys/encrypted-keys/ecryptfs_format.c +0
-0security/keys/encrypted-keys/ecryptfs_format.c int ecryptfs_fill_auth_tok(struct ecryptfs_auth_tok *auth_tok, · line 60
Quoted 9 lines
security/keys/encrypted-keys/ecryptfs_format.c +1
-2@@ -54,8 +54,7 @@ int ecryptfs_fill_auth_tok(struct ecryptfs_auth_tok *auth_tok, auth_tok->version = (((uint16_t )(major <<8 ) &0xFF00 ) | ((uint16_t )minor &0x00FF )); auth_tok->token_type =ECRYPTFS_PASSWORD ; -strncpy ((char *)auth_tok->token.password.signature, key_desc, -ECRYPTFS_PASSWORD_SIG_SIZE ); +strscpy_pad (auth_tok->token.password.signature, key_desc); auth_tok->token.password.session_key_encryption_key_bytes =ECRYPTFS_MAX_KEY_BYTES ;/* Reviewed-by Paul Menzel <[email protected]>
Thanks for the review Paul.
David: and also this is in my tree now.
Quoted 5 lines
Kind regards,
Paul
BR, Jarkko