freebsd-skq/crypto/openssh/openbsd-compat/explicit_bzero.c

58 lines
1.1 KiB
C
Raw Permalink Normal View History

2014-03-22 15:23:38 +00:00
/* OPENBSD ORIGINAL: lib/libc/string/explicit_bzero.c */
/* $OpenBSD: explicit_bzero.c,v 1.1 2014/01/22 21:06:45 tedu Exp $ */
/*
* Public domain.
* Written by Ted Unangst
*/
#include "includes.h"
2017-01-31 12:33:47 +00:00
#include <string.h>
2015-01-05 16:09:55 +00:00
/*
* explicit_bzero - don't let the compiler optimize away bzero
*/
2014-03-22 15:23:38 +00:00
#ifndef HAVE_EXPLICIT_BZERO
2015-01-05 16:09:55 +00:00
#ifdef HAVE_MEMSET_S
void
explicit_bzero(void *p, size_t n)
{
2018-05-06 12:24:45 +00:00
if (n == 0)
return;
2015-01-05 16:09:55 +00:00
(void)memset_s(p, n, 0, n);
}
#else /* HAVE_MEMSET_S */
2014-03-22 15:23:38 +00:00
/*
2015-01-05 16:09:55 +00:00
* Indirect bzero through a volatile pointer to hopefully avoid
* dead-store optimisation eliminating the call.
2014-03-22 15:23:38 +00:00
*/
2015-01-05 16:09:55 +00:00
static void (* volatile ssh_bzero)(void *, size_t) = bzero;
2014-03-22 15:23:38 +00:00
void
explicit_bzero(void *p, size_t n)
{
2018-05-06 12:24:45 +00:00
if (n == 0)
return;
2017-01-31 12:33:47 +00:00
/*
* clang -fsanitize=memory needs to intercept memset-like functions
* to correctly detect memory initialisation. Make sure one is called
2018-08-28 10:47:58 +00:00
* directly since our indirection trick above successfully confuses it.
2017-01-31 12:33:47 +00:00
*/
#if defined(__has_feature)
# if __has_feature(memory_sanitizer)
memset(p, 0, n);
# endif
#endif
2015-01-05 16:09:55 +00:00
ssh_bzero(p, n);
2014-03-22 15:23:38 +00:00
}
2015-01-05 16:09:55 +00:00
#endif /* HAVE_MEMSET_S */
#endif /* HAVE_EXPLICIT_BZERO */