Skip to contentSkip to conversation

dm crypt: replace open-coded kmemdup_nul

PATCH Applied Justin Stitt 3 participants 5 messages Sep 25, 2023 – Oct 24, 2023 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dm crypt: replace open-coded kmemdup_nul

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]).

---
Note: build-tested only.
Similar-to: https://lore.kernel.org/all/20230925-strncpy-drivers-hwmon-acpi_power_meter-c-v4-1-3bac7534f10f@google.com/

---
 drivers/md/dm-crypt.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/md/dm-crypt.c +1 -2
index f2662c21a6df..8a03b3590733 100644
@@ -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);

---
base-commit: 6465e260f48790807eef06b583b38ca9789b6072
change-id: 20230925-strncpy-drivers-md-dm-crypt-c-da71d87b8cdc

Best regards,
--
Justin Stitt <[email protected]>

Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Justin Stitt

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]).

This looks correct to me. Thanks!

Signature · 1 line
Kees Cook
Applied
#2 lore raw
In reply to Justin Stitt
Quoted 5 lines · Justin Stitt

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]).

Applied to for-next/hardening, thanks!

[1/1] dm crypt: replace open-coded kmemdup_nul

      https://git.kernel.org/kees/c/17348b0a6a6d

Take care,

Signature · 1 line
Kees Cook
maintainer
#3 lore raw
Quoted 7 lines · Kees Cook
Quoted 5 lines · Justin Stitt

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]).

Quoted 10 lines

Applied to for-next/hardening, thanks!

[1/1] dm crypt: replace open-coded kmemdup_nul

      https://git.kernel.org/kees/c/17348b0a6a6d

Take care,

Signature · 1 line
Kees Cook

Hey,

I had picked up this patch and the 3 others in my local tree yesterday, was going to push shortly... do you still want them to go through your hardening tree?

Mike

#4 lore raw
Quoted 17 lines · Mike Snitzer
Quoted 14 lines · Kees Cook
Quoted 5 lines · Justin Stitt

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]).

Applied to for-next/hardening, thanks!

[1/1] dm crypt: replace open-coded kmemdup_nul

      https://git.kernel.org/kees/c/17348b0a6a6d

Take care,

Quoted 7 lines
Quoted 3 lines
Signature · 1 line
Kees Cook

I had picked up this patch and the 3 others in my local tree yesterday, was going to push shortly... do you still want them to go through your hardening tree?

Oh! Great; thank you! I'll drop them from my tree. :)

-Kees

Signature · 1 line
Kees Cook