Skip to content

Commit

Permalink
compat: test for memset_explicit, explicit_bzero and memset_s
Browse files Browse the repository at this point in the history
These won't be optimised away by the compiler and our arc4random
compat function should use them *if* available.
If none are then a warning will be emitted to say it's potentially insecure.

Hopefully only uclibc users will see this message.

Fixes #252.
  • Loading branch information
rsmarples committed Oct 23, 2023
1 parent beace2c commit 65190fa
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
11 changes: 10 additions & 1 deletion compat/arc4random.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,16 @@ _rs_stir(void)
_rs_init(rnd, sizeof(rnd));
else
_rs_rekey(rnd, sizeof(rnd));
memset(rnd, 0, sizeof(rnd)); /* discard source seed */
#if defined(HAVE_EXPLICIT_BZERO)
explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */
#elif defined(HAVE_MEMSET_EXPLICIT)
(void)memset_explicit(rnd, 0, sizeof(rnd));
#elif defined(HAVE_MEMSET_S)
(void)memset_s(rnd, sizeof(rnd), 0, sizeof(rnd));
#else
#warning potentially insecure use of memset discarding the source seed
(void)memset(rnd, 0, sizeof(rnd)); /* discard source seed */
#endif

/* invalidate rs_buf */
rs->rs_have = 0;
Expand Down
68 changes: 68 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,74 @@ if [ "$ARC4RANDOM_UNIFORM" = no ]; then
echo "#include \"compat/arc4random_uniform.h\"" >>$CONFIG_H
fi

# Our arc4random compat needs memset_explicit, explicit_bzero or memset_s
if [ -z "$MEMSET_EXPLICIT" ]; then
printf "Testing for memset_explicit ... "
cat <<EOF >_memset_explicit.c
#include <string.h>
int main(void) {
int a;
(void)memset_explicit(&a, 0, sizeof(a));
return 0;
}
EOF
if $XCC __memset_explicit.c -o _memset_explicit 2>&3; then
MEMSET_EXPLICIT=yes
else
MEMSET_EXPLICIT=no
fi
echo "$MEMSET_EXPLICIT"
rm -f _memset_explicit.c _memset_explicit
fi
if [ "$MEMSET_EXPLICIT" = yes ]; then
echo "#define HAVE_MEMSET_EXPLICIT" >>$CONFIG_H
fi

if [ -z "$EXPLICIT_BZERO" ]; then
printf "Testing for explicit_bzero ... "
cat <<EOF >_explicit_bzero.c
#define _BSD_SOURCE // musl, will be added for Linux in config.h
#include <string.h>
int main(void) {
int a;
explicit_bzero(&a, sizeof(a));
return 0;
}
EOF
if $XCC _explicit_bzero.c -o _explicit_bzero 2>&3; then
EXPLICIT_BZERO=yes
else
EXPLICIT_BZERO=no
fi
echo "$EXPLICIT_BZERO"
rm -f _explicit_bzero.c _explicit_bzero
fi
if [ "$EXPLICIT_BZERO" = yes ]; then
echo "#define HAVE_EXPLICIT_BZERO" >>$CONFIG_H
fi

if [ -z "$MEMSET_S" ]; then
printf "Testing for memset_s ... "
cat <<EOF >_memset_s.c
#include <string.h>
int main(void) {
int a;
memset_s(&a, sizeof(a), 0, sizeof(a));
return 0;
}
EOF
if $XCC __memset_s.c -o _memset_s 2>&3; then
MEMSET_S=yes
else
MEMSET_S=no
fi
echo "$MEMSET_S"
rm -f _memset_s.c _memset_s
fi
if [ "$MEMSET_S" = yes ]; then
echo "#define HAVE_MEMSET_S" >>$CONFIG_H
fi

if [ -z "$OPEN_MEMSTREAM" ]; then
printf "Testing for open_memstream ... "
cat <<EOF >_open_memstream.c
Expand Down

0 comments on commit 65190fa

Please sign in to comment.