Skip to content

Commit

Permalink
core: crypto: libtomcrypt: fix LTC_CLEAN_STACK bug
Browse files Browse the repository at this point in the history
LTC_CLEAN_STACK uses burn_stack() API that uses a recursive call which
leads to approx. double the size of stack cleaned than expected on ARM64,
because it consumes stack space in 32-byte chunks and assumes only buf
is pushed onto the stack while ignoring any other data such as lr, fp,
etc.. This causes stack overflow corrupting canaries in case we perform
a SHA512 hash operation which utilizes maximum stack as compared to other
libtomcrypt APIs. So get rid of this recursive call via using variable
length array to clean stack.

Also, convert zeromem() API as a wrapper to call memzero_explicit().

Fixes: ad56511 ("core: crypto: libtomcrypt: enable LTC_CLEAN_STACK")
Suggested-by: Daniel Thompson <daniel.thompson@linaro.org>
Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
Acked-by: Jerome Forissier <jerome.forissier@linaro.org>
  • Loading branch information
b49020 authored and jforissier committed Jul 1, 2019
1 parent c4a5739 commit 03121b2
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 7 deletions.
4 changes: 1 addition & 3 deletions core/lib/libtomcrypt/src/misc/burn_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@
*/
void burn_stack(unsigned long len)
{
unsigned char buf[32];
unsigned char buf[len];
zeromem(buf, sizeof(buf));
if (len > (unsigned long)sizeof(buf))
burn_stack(len - sizeof(buf));
}


Expand Down
6 changes: 2 additions & 4 deletions core/lib/libtomcrypt/src/misc/zeromem.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
#include "tomcrypt.h"
#include <string_ext.h>

/**
@file zeromem.c
Expand All @@ -50,11 +51,8 @@
*/
void zeromem(volatile void *out, size_t outlen)
{
volatile char *mem = out;
LTC_ARGCHKVD(out != NULL);
while (outlen-- > 0) {
*mem++ = 0;
}
memzero_explicit((void *)out, outlen);
}

/* $Source: /cvs/libtom/libtomcrypt/src/misc/zeromem.c,v $ */
Expand Down

0 comments on commit 03121b2

Please sign in to comment.