public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] dm crypt: replace open-coded kmemdup_nul
@ 2023-09-25  6:35 Justin Stitt
  2023-09-25 18:04 ` Kees Cook
  2023-10-24 21:18 ` Kees Cook
  0 siblings, 2 replies; 5+ messages in thread
From: Justin Stitt @ 2023-09-25  6:35 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, dm-devel
  Cc: linux-kernel, linux-hardening, Kees Cook, 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]).

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Cc: Kees Cook <keescook@chromium.org>
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
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(-)

diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
index f2662c21a6df..8a03b3590733 100644
--- a/drivers/md/dm-crypt.c
+++ b/drivers/md/dm-crypt.c
@@ -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 <justinstitt@google.com>


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

* Re: [PATCH] dm crypt: replace open-coded kmemdup_nul
  2023-09-25  6:35 [PATCH] dm crypt: replace open-coded kmemdup_nul Justin Stitt
@ 2023-09-25 18:04 ` Kees Cook
  2023-10-24 21:18 ` Kees Cook
  1 sibling, 0 replies; 5+ messages in thread
From: Kees Cook @ 2023-09-25 18:04 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Alasdair Kergon, Mike Snitzer, dm-devel, linux-kernel,
	linux-hardening

On Mon, Sep 25, 2023 at 06:35:54AM +0000, Justin Stitt wrote:
> 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 [1]
> Link: https://github.com/KSPP/linux/issues/90
> Cc: linux-hardening@vger.kernel.org
> Cc: Kees Cook <keescook@chromium.org>
> Signed-off-by: Justin Stitt <justinstitt@google.com>

This looks correct to me. Thanks!

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

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

* Re: [PATCH] dm crypt: replace open-coded kmemdup_nul
  2023-09-25  6:35 [PATCH] dm crypt: replace open-coded kmemdup_nul Justin Stitt
  2023-09-25 18:04 ` Kees Cook
@ 2023-10-24 21:18 ` Kees Cook
  2023-10-24 21:33   ` Mike Snitzer
  1 sibling, 1 reply; 5+ messages in thread
From: Kees Cook @ 2023-10-24 21:18 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, dm-devel, Justin Stitt
  Cc: Kees Cook, linux-kernel, linux-hardening

On Mon, 25 Sep 2023 06:35:54 +0000, Justin Stitt wrote:
> 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,

-- 
Kees Cook


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

* Re: dm crypt: replace open-coded kmemdup_nul
  2023-10-24 21:18 ` Kees Cook
@ 2023-10-24 21:33   ` Mike Snitzer
  2023-10-24 21:35     ` Kees Cook
  0 siblings, 1 reply; 5+ messages in thread
From: Mike Snitzer @ 2023-10-24 21:33 UTC (permalink / raw)
  To: Kees Cook
  Cc: Alasdair Kergon, dm-devel, Justin Stitt, linux-hardening,
	linux-kernel

On Tue, Oct 24 2023 at  5:18P -0400,
Kees Cook <keescook@chromium.org> wrote:

> On Mon, 25 Sep 2023 06:35:54 +0000, Justin Stitt wrote:
> > 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,
> 
> -- 
> 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



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

* Re: dm crypt: replace open-coded kmemdup_nul
  2023-10-24 21:33   ` Mike Snitzer
@ 2023-10-24 21:35     ` Kees Cook
  0 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2023-10-24 21:35 UTC (permalink / raw)
  To: Mike Snitzer
  Cc: Alasdair Kergon, dm-devel, Justin Stitt, linux-hardening,
	linux-kernel

On Tue, Oct 24, 2023 at 05:33:12PM -0400, Mike Snitzer wrote:
> On Tue, Oct 24 2023 at  5:18P -0400,
> Kees Cook <keescook@chromium.org> wrote:
> 
> > On Mon, 25 Sep 2023 06:35:54 +0000, Justin Stitt wrote:
> > > 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,
> > 
> > -- 
> > 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

-- 
Kees Cook

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

end of thread, other threads:[~2023-10-24 21:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-25  6:35 [PATCH] dm crypt: replace open-coded kmemdup_nul Justin Stitt
2023-09-25 18:04 ` Kees Cook
2023-10-24 21:18 ` Kees Cook
2023-10-24 21:33   ` Mike Snitzer
2023-10-24 21:35     ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox