Skip to content

Commit c32d33f

Browse files
author
Emma Stensland
committed
harden forcezero and compiler barriers
1 parent 5bd7348 commit c32d33f

2 files changed

Lines changed: 40 additions & 25 deletions

File tree

wolfcrypt/src/misc.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -693,33 +693,34 @@ WC_MISC_STATIC WC_INLINE void xorbuf(void* buf, const void* mask, word32 count)
693693
with zeros. It ensures compiler optimization doesn't skip it. */
694694
WC_MISC_STATIC WC_INLINE void ForceZero(void* mem, size_t len)
695695
{
696-
byte *zb = (byte *)mem;
697-
unsigned long *zl;
696+
/* volatile prevents dead-store elimination; WC_BARRIER() prevents
697+
* reordering. */
698+
volatile byte *zb = (volatile byte *)mem;
699+
volatile unsigned long *zl;
698700

699-
XFENCE();
701+
WC_BARRIER();
700702

701-
while ((wc_ptr_t)zb & (wc_ptr_t)(sizeof(unsigned long) - 1U)) {
702-
if (len == 0)
703-
return;
703+
while (len > 0 &&
704+
((wc_ptr_t)zb & (wc_ptr_t)(sizeof(unsigned long) - 1U))) {
704705
*zb++ = 0;
705706
--len;
706707
}
707708

708-
zl = (unsigned long *)zb;
709+
zl = (volatile unsigned long *)zb;
709710

710711
while (len >= sizeof(unsigned long)) {
711712
*zl++ = 0;
712713
len -= sizeof(unsigned long);
713714
}
714715

715-
zb = (byte *)zl;
716+
zb = (volatile byte *)zl;
716717

717718
while (len) {
718719
*zb++ = 0;
719720
--len;
720721
}
721722

722-
XFENCE();
723+
WC_BARRIER();
723724
}
724725
#endif
725726

wolfssl/wolfcrypt/wc_port.h

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,16 +1918,30 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
19181918
#endif
19191919
#endif
19201920

1921-
#ifdef WOLF_C99
1922-
/* use alternate keyword for compatibility with -std=c99 */
1923-
#define XASM_VOLATILE(a) __asm__ volatile(a)
1924-
#elif defined(__IAR_SYSTEMS_ICC__)
1921+
/* XASM_VOLATILE_MB() adds a "memory" clobber. IAR/KEIL checked before
1922+
* WOLF_C99 since both may define __STDC_VERSION__ under --c99.
1923+
* XASM_VOLATILE() has no in-tree callers now; kept for out-of-tree ports. */
1924+
/* NOLINTBEGIN(bugprone-macro-parentheses) */
1925+
#if defined(__IAR_SYSTEMS_ICC__)
1926+
/* clobber list supported: see sp_cortexm.c's __asm__/asm redefine. */
19251927
#define XASM_VOLATILE(a) asm volatile(a)
1928+
#define XASM_VOLATILE_MB(a) asm volatile(a ::: "memory")
19261929
#elif defined(__KEIL__)
1930+
/* clobber list supported: see sp_cortexm.c's __asm__/__asm redefine. */
19271931
#define XASM_VOLATILE(a) __asm volatile(a)
1932+
#define XASM_VOLATILE_MB(a) __asm volatile(a ::: "memory")
1933+
#elif defined(WOLF_C99)
1934+
#define XASM_VOLATILE(a) __asm__ volatile(a)
1935+
#define XASM_VOLATILE_MB(a) __asm__ volatile(a ::: "memory")
1936+
#elif defined(__GNUC__) || defined(__clang__)
1937+
#define XASM_VOLATILE(a) __asm__ __volatile__(a)
1938+
#define XASM_VOLATILE_MB(a) __asm__ __volatile__(a ::: "memory")
19281939
#else
1940+
/* unknown compiler: assume basic asm only, no clobber list. */
19291941
#define XASM_VOLATILE(a) __asm__ __volatile__(a)
1942+
#define XASM_VOLATILE_MB(a) XASM_VOLATILE(a)
19301943
#endif
1944+
/* NOLINTEND(bugprone-macro-parentheses) */
19311945

19321946
#ifndef WOLFSSL_NO_FENCE
19331947
#ifdef XFENCE
@@ -1948,33 +1962,33 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
19481962
#elif defined(WOLFSSL_NO_ASM)
19491963
#define XFENCE() WC_DO_NOTHING
19501964
#elif defined (__i386__) || defined(__x86_64__)
1951-
#define XFENCE() XASM_VOLATILE("lfence")
1965+
#define XFENCE() XASM_VOLATILE_MB("lfence")
19521966
#elif defined (__arm__) && (__ARM_ARCH > 6)
1953-
#define XFENCE() XASM_VOLATILE("isb")
1967+
#define XFENCE() XASM_VOLATILE_MB("isb")
19541968
#elif defined(_MSC_VER) && defined(_M_ARM64)
19551969
/* MSVC on ARM64 has no __asm__; use the ISB intrinsic barrier. */
19561970
#include <intrin.h>
19571971
#define XFENCE() __isb(_ARM64_BARRIER_SY)
19581972
#elif defined(__aarch64__)
19591973
/* Change ".inst 0xd50330ff" to "sb" when compilers support it. */
19601974
#ifdef WOLFSSL_ARMASM_BARRIER_SB
1961-
#define XFENCE() XASM_VOLATILE(".inst 0xd50330ff")
1975+
#define XFENCE() XASM_VOLATILE_MB(".inst 0xd50330ff")
19621976
#elif defined(WOLFSSL_ARMASM_BARRIER_DETECT)
19631977
extern int aarch64_use_sb;
1964-
#define XFENCE() \
1965-
do { \
1966-
if (aarch64_use_sb) \
1967-
XASM_VOLATILE(".inst 0xd50330ff"); \
1968-
else \
1969-
XASM_VOLATILE("isb"); \
1978+
#define XFENCE() \
1979+
do { \
1980+
if (aarch64_use_sb) \
1981+
XASM_VOLATILE_MB(".inst 0xd50330ff"); \
1982+
else \
1983+
XASM_VOLATILE_MB("isb"); \
19701984
} while (0)
19711985
#else
1972-
#define XFENCE() XASM_VOLATILE("isb")
1986+
#define XFENCE() XASM_VOLATILE_MB("isb")
19731987
#endif
19741988
#elif defined(__riscv)
1975-
#define XFENCE() XASM_VOLATILE("fence")
1989+
#define XFENCE() XASM_VOLATILE_MB("fence")
19761990
#elif defined(__PPC__) || defined(__POWERPC__)
1977-
#define XFENCE() XASM_VOLATILE("isync; sync")
1991+
#define XFENCE() XASM_VOLATILE_MB("isync; sync")
19781992
#else
19791993
#define XFENCE() WC_DO_NOTHING
19801994
#endif

0 commit comments

Comments
 (0)