A few places in lib/ clear a buffer with memset() just before the buffer
goes away. The store is dead by then, so the compiler drops it and
whatever was there stays on the stack.

Patch 1 imports memzero_explicit() from Linux, where it is a static inline
in include/linux/string.h, and links to the upstream commit it comes from,
so a later fix there can be picked up. Patches 2 to 5 use it, one function
each.

For sha1_hmac() the caller is the TPM 1.2 code, where the buffer holds a
TPM key's usage auth. Getting it back needs something that reads the
stack afterwards, so what this fixes is how long the secret lives, not a
way to read it from outside.

Tested with GCC 13.3.0 against main (211de43): each file compiled on its
own with -fdump-tree-dse-details; the dead store pass removes the clears
in all four files before the series and none of them after it. checkpatch
is clean on the five patches.

Changes in v3:
- the import is its own patch and links to the upstream Linux commit
- one patch per function instead of grouping them together

Changes in v2:
- use the Linux version of memzero_explicit(), a static inline in
  include/linux/string.h, instead of adding a function to lib/string.c
- shorter commit messages, function names instead of line numbers

Xiaoyi Suo (5):
  lib: add memzero_explicit() from Linux
  lib: sha1: use memzero_explicit() for the HMAC key schedule
  lib: sha256: use memzero_explicit() for the HMAC key schedule
  lib: mbedtls: sha1: use memzero_explicit() for the HMAC key schedule
  lib: sm3: use memzero_explicit() for the message schedule

 include/linux/string.h | 21 +++++++++++++++++++++
 lib/mbedtls/sha1.c     |  8 ++++----
 lib/sha1.c             |  8 ++++----
 lib/sha256.c           | 10 +++++-----
 lib/sm3.c              |  4 ++--
 5 files changed, 36 insertions(+), 15 deletions(-)

-- 
2.43.0

Reply via email to