dm crypt: replace open-coded kmemdup_nul
authorJustin Stitt <justinstitt@google.com>
Mon, 25 Sep 2023 06:35:54 +0000 (06:35 +0000)
committerMike Snitzer <snitzer@kernel.org>
Mon, 23 Oct 2023 17:02:25 +0000 (13:02 -0400)
kzalloc() followed by strncpy() on an expected NUL-terminated string is
just kmemdup_nul(). Let's simplify this code (while also dropping a
deprecated strncpy() call [1]).

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
Link: https://github.com/KSPP/linux/issues/90
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Justin Stitt <justinstitt@google.com>
Signed-off-by: Mike Snitzer <snitzer@kernel.org>
drivers/md/dm-crypt.c

index f2662c21a6dfe7ac1c6db10fa21ef94b45dee2ac..8a03b359073327b3268bbf9b2ba63bc3657b6c3c 100644 (file)
@@ -2858,10 +2858,9 @@ static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api)
        if (!start || !end || ++start > end)
                return -EINVAL;
 
-       mac_alg = kzalloc(end - start + 1, GFP_KERNEL);
+       mac_alg = kmemdup_nul(start, end - start, GFP_KERNEL);
        if (!mac_alg)
                return -ENOMEM;
-       strncpy(mac_alg, start, end - start);
 
        mac = crypto_alloc_ahash(mac_alg, 0, CRYPTO_ALG_ALLOCATES_MEMORY);
        kfree(mac_alg);