kernel-janitors.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] crypto: sha512: remove imaginary and mystifying clearing of variables
@ 2021-08-22 10:31 Lukas Bulwahn
  2021-08-25 22:24 ` Nick Desaulniers
  2021-08-27  8:38 ` Herbert Xu
  0 siblings, 2 replies; 5+ messages in thread
From: Lukas Bulwahn @ 2021-08-22 10:31 UTC (permalink / raw)
  To: Herbert Xu, David S . Miller, linux-crypto
  Cc: Nathan Chancellor, Nick Desaulniers, clang-built-linux,
	kernel-janitors, linux-kernel, Lukas Bulwahn

The function sha512_transform() assigns all local variables to 0 before
returning to its caller with the intent to erase sensitive data.

However, make clang-analyzer warns that all these assignments are dead
stores, and as commit 7a4295f6c9d5 ("crypto: lib/sha256 - Don't clear
temporary variables") already points out for sha256_transform():

  The assignments to clear a through h and t1/t2 are optimized out by the
  compiler because they are unused after the assignments.

  Clearing individual scalar variables is unlikely to be useful, as they
  may have been assigned to registers, and even if stack spilling was
  required, there may be compiler-generated temporaries that are
  impossible to clear in any case.

This applies here again as well. Drop meaningless clearing of local
variables and avoid this way that the code suggests that data is erased,
which simply does not happen.

Signed-off-by: Lukas Bulwahn <lukas.bulwahn@gmail.com>
---
 crypto/sha512_generic.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/crypto/sha512_generic.c b/crypto/sha512_generic.c
index c72d72ad828e..be70e76d6d86 100644
--- a/crypto/sha512_generic.c
+++ b/crypto/sha512_generic.c
@@ -143,9 +143,6 @@ sha512_transform(u64 *state, const u8 *input)
 
 	state[0] += a; state[1] += b; state[2] += c; state[3] += d;
 	state[4] += e; state[5] += f; state[6] += g; state[7] += h;
-
-	/* erase our data */
-	a = b = c = d = e = f = g = h = t1 = t2 = 0;
 }
 
 static void sha512_generic_block_fn(struct sha512_state *sst, u8 const *src,
-- 
2.26.2


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

* Re: [PATCH] crypto: sha512: remove imaginary and mystifying clearing of variables
  2021-08-22 10:31 [PATCH] crypto: sha512: remove imaginary and mystifying clearing of variables Lukas Bulwahn
@ 2021-08-25 22:24 ` Nick Desaulniers
  2021-08-27  8:38 ` Herbert Xu
  1 sibling, 0 replies; 5+ messages in thread
From: Nick Desaulniers @ 2021-08-25 22:24 UTC (permalink / raw)
  To: Lukas Bulwahn
  Cc: Herbert Xu, David S . Miller, linux-crypto, Nathan Chancellor,
	clang-built-linux, kernel-janitors, linux-kernel

On Sun, Aug 22, 2021 at 3:31 AM Lukas Bulwahn <lukas.bulwahn@gmail.com> wrote:
>
> The function sha512_transform() assigns all local variables to 0 before
> returning to its caller with the intent to erase sensitive data.
>
> However, make clang-analyzer warns that all these assignments are dead
> stores, and as commit 7a4295f6c9d5 ("crypto: lib/sha256 - Don't clear
> temporary variables") already points out for sha256_transform():
>
>   The assignments to clear a through h and t1/t2 are optimized out by the
>   compiler because they are unused after the assignments.
>
>   Clearing individual scalar variables is unlikely to be useful, as they
>   may have been assigned to registers, and even if stack spilling was
>   required, there may be compiler-generated temporaries that are
>   impossible to clear in any case.
>
> This applies here again as well. Drop meaningless clearing of local
> variables and avoid this way that the code suggests that data is erased,
> which simply does not happen.
>
> Signed-off-by: Lukas Bulwahn <lukas.bulwahn@gmail.com>

Thanks for the patch!
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

> ---
>  crypto/sha512_generic.c | 3 ---
>  1 file changed, 3 deletions(-)
>
> diff --git a/crypto/sha512_generic.c b/crypto/sha512_generic.c
> index c72d72ad828e..be70e76d6d86 100644
> --- a/crypto/sha512_generic.c
> +++ b/crypto/sha512_generic.c
> @@ -143,9 +143,6 @@ sha512_transform(u64 *state, const u8 *input)
>
>         state[0] += a; state[1] += b; state[2] += c; state[3] += d;
>         state[4] += e; state[5] += f; state[6] += g; state[7] += h;
> -
> -       /* erase our data */
> -       a = b = c = d = e = f = g = h = t1 = t2 = 0;
>  }
>
>  static void sha512_generic_block_fn(struct sha512_state *sst, u8 const *src,
> --
> 2.26.2
>


-- 
Thanks,
~Nick Desaulniers

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

* Re: [PATCH] crypto: sha512: remove imaginary and mystifying clearing of variables
  2021-08-22 10:31 [PATCH] crypto: sha512: remove imaginary and mystifying clearing of variables Lukas Bulwahn
  2021-08-25 22:24 ` Nick Desaulniers
@ 2021-08-27  8:38 ` Herbert Xu
  2021-08-28  7:46   ` Sandy Harris
  1 sibling, 1 reply; 5+ messages in thread
From: Herbert Xu @ 2021-08-27  8:38 UTC (permalink / raw)
  To: Lukas Bulwahn
  Cc: David S . Miller, linux-crypto, Nathan Chancellor,
	Nick Desaulniers, clang-built-linux, kernel-janitors,
	linux-kernel

On Sun, Aug 22, 2021 at 12:31:07PM +0200, Lukas Bulwahn wrote:
> The function sha512_transform() assigns all local variables to 0 before
> returning to its caller with the intent to erase sensitive data.
> 
> However, make clang-analyzer warns that all these assignments are dead
> stores, and as commit 7a4295f6c9d5 ("crypto: lib/sha256 - Don't clear
> temporary variables") already points out for sha256_transform():
> 
>   The assignments to clear a through h and t1/t2 are optimized out by the
>   compiler because they are unused after the assignments.
> 
>   Clearing individual scalar variables is unlikely to be useful, as they
>   may have been assigned to registers, and even if stack spilling was
>   required, there may be compiler-generated temporaries that are
>   impossible to clear in any case.
> 
> This applies here again as well. Drop meaningless clearing of local
> variables and avoid this way that the code suggests that data is erased,
> which simply does not happen.
> 
> Signed-off-by: Lukas Bulwahn <lukas.bulwahn@gmail.com>
> ---
>  crypto/sha512_generic.c | 3 ---
>  1 file changed, 3 deletions(-)

Patch applied.  Thanks.
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

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

* Re: [PATCH] crypto: sha512: remove imaginary and mystifying clearing of variables
  2021-08-27  8:38 ` Herbert Xu
@ 2021-08-28  7:46   ` Sandy Harris
  2021-08-31 18:49     ` Eric Biggers
  0 siblings, 1 reply; 5+ messages in thread
From: Sandy Harris @ 2021-08-28  7:46 UTC (permalink / raw)
  To: Herbert Xu
  Cc: Lukas Bulwahn, David S . Miller, Linux Crypto Mailing List,
	Nathan Chancellor, Nick Desaulniers, clang-built-linux,
	kernel-janitors, LKML

On Fri, Aug 27, 2021 at 4:40 PM Herbert Xu <herbert@gondor.apana.org.au> wrote:
>
> On Sun, Aug 22, 2021 at 12:31:07PM +0200, Lukas Bulwahn wrote:
> > The function sha512_transform() assigns all local variables to 0 before
> > returning to its caller with the intent to erase sensitive data.
> > ....
> >
> >   The assignments to clear a through h and t1/t2 are optimized out by the
> >   compiler because they are unused after the assignments.

Just no.

You are right, there is a problem here. I thank you for pointing it
out & I've already fixed it in some of my own code.

However, I think your solution is dead wrong. You are correct that
these assignments are useless because the compiler will optimise them
out, and that's a problem. However, it is not at all "mistiifying";
they are there for an obvious reason, to avoid leaving state that
might be useful to an enemy. That is quite a small risk, but then it
is a small mitigation, so worth doing.

The correct solution is not to just remove the assignments, but rather
to replace them with code that will not be optimised away, force the
compiler to do what we need. We already do that for operations that
clear various arrays and structures, using memzero_explicit() rather
than memset(). Similarly, we should replace the assignments with calls
to this macro:

/*
    clear a variable
    in a way the compiler will not optimise out
*/
#define clear(x)  memzero_explicit( &x, sizeof(x) )

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

* Re: [PATCH] crypto: sha512: remove imaginary and mystifying clearing of variables
  2021-08-28  7:46   ` Sandy Harris
@ 2021-08-31 18:49     ` Eric Biggers
  0 siblings, 0 replies; 5+ messages in thread
From: Eric Biggers @ 2021-08-31 18:49 UTC (permalink / raw)
  To: Sandy Harris
  Cc: Herbert Xu, Lukas Bulwahn, David S . Miller,
	Linux Crypto Mailing List, Nathan Chancellor, Nick Desaulniers,
	clang-built-linux, kernel-janitors, LKML

On Sat, Aug 28, 2021 at 03:46:50PM +0800, Sandy Harris wrote:
> On Fri, Aug 27, 2021 at 4:40 PM Herbert Xu <herbert@gondor.apana.org.au> wrote:
> >
> > On Sun, Aug 22, 2021 at 12:31:07PM +0200, Lukas Bulwahn wrote:
> > > The function sha512_transform() assigns all local variables to 0 before
> > > returning to its caller with the intent to erase sensitive data.
> > > ....
> > >
> > >   The assignments to clear a through h and t1/t2 are optimized out by the
> > >   compiler because they are unused after the assignments.
> 
> Just no.
> 
> You are right, there is a problem here. I thank you for pointing it
> out & I've already fixed it in some of my own code.
> 
> However, I think your solution is dead wrong. You are correct that
> these assignments are useless because the compiler will optimise them
> out, and that's a problem. However, it is not at all "mistiifying";
> they are there for an obvious reason, to avoid leaving state that
> might be useful to an enemy. That is quite a small risk, but then it
> is a small mitigation, so worth doing.
> 
> The correct solution is not to just remove the assignments, but rather
> to replace them with code that will not be optimised away, force the
> compiler to do what we need. We already do that for operations that
> clear various arrays and structures, using memzero_explicit() rather
> than memset(). Similarly, we should replace the assignments with calls
> to this macro:
> 
> /*
>     clear a variable
>     in a way the compiler will not optimise out
> */
> #define clear(x)  memzero_explicit( &x, sizeof(x) )

Clearing of local variables is never guaranteed to work properly, as the
compiler can create multiple copies and/or put them in registers.  It's much
more likely to work for arrays than simple variables though (and not cause the
variable to be unnecessarily spilled from registers to the stack), so that is
the only one the kernel really bothers with.

- Eric

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

end of thread, other threads:[~2021-08-31 18:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-22 10:31 [PATCH] crypto: sha512: remove imaginary and mystifying clearing of variables Lukas Bulwahn
2021-08-25 22:24 ` Nick Desaulniers
2021-08-27  8:38 ` Herbert Xu
2021-08-28  7:46   ` Sandy Harris
2021-08-31 18:49     ` 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 NNTP newsgroup(s).