ecryptfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH linux-next] ecryptfs: crypto: use strscpy() to instead of strncpy()
@ 2022-12-28  1:36 yang.yang29
  2022-12-29 20:51 ` Eric Biggers
  0 siblings, 1 reply; 2+ messages in thread
From: yang.yang29 @ 2022-12-28  1:36 UTC (permalink / raw
  To: code
  Cc: ira.weiny, fmdefrancesco, ecryptfs, linux-kernel, xu.panda,
	yang.yang29

From: Xu Panda <xu.panda@zte.com.cn>

The implementation of strscpy() is more robust and safer.
That's now the recommended way to copy NUL-terminated strings.

Signed-off-by: Xu Panda <xu.panda@zte.com.cn>
Signed-off-by: Yang Yang <yang.yang29@zte.com>
---
 fs/ecryptfs/crypto.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/fs/ecryptfs/crypto.c b/fs/ecryptfs/crypto.c
index 03263ebcccc6..c618a6878584 100644
--- a/fs/ecryptfs/crypto.c
+++ b/fs/ecryptfs/crypto.c
@@ -1630,9 +1630,8 @@ ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
 		goto out;
 	}
 	mutex_init(&tmp_tfm->key_tfm_mutex);
-	strncpy(tmp_tfm->cipher_name, cipher_name,
-		ECRYPTFS_MAX_CIPHER_NAME_SIZE);
-	tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
+	strscpy(tmp_tfm->cipher_name, cipher_name,
+		ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1);
 	tmp_tfm->key_size = key_size;
 	rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
 					 tmp_tfm->cipher_name,
-- 
2.15.2

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

* Re: [PATCH linux-next] ecryptfs: crypto: use strscpy() to instead of strncpy()
  2022-12-28  1:36 [PATCH linux-next] ecryptfs: crypto: use strscpy() to instead of strncpy() yang.yang29
@ 2022-12-29 20:51 ` Eric Biggers
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Biggers @ 2022-12-29 20:51 UTC (permalink / raw
  To: yang.yang29
  Cc: code, ira.weiny, fmdefrancesco, ecryptfs, linux-kernel, xu.panda

On Wed, Dec 28, 2022 at 09:36:54AM +0800, yang.yang29@zte.com.cn wrote:
> From: Xu Panda <xu.panda@zte.com.cn>
> 
> The implementation of strscpy() is more robust and safer.
> That's now the recommended way to copy NUL-terminated strings.

Unlike strncpy(), strscpy() does not initialize the full destination array, so
it is unsafe in some situations, such as initializing structures that are then
copied to userspace or to disk.  Please include an actual explanation for why
this patch is correct.

- Eric

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

end of thread, other threads:[~2022-12-29 20:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-28  1:36 [PATCH linux-next] ecryptfs: crypto: use strscpy() to instead of strncpy() yang.yang29
2022-12-29 20:51 ` Eric Biggers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).