summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormatthew <>2014-06-21 02:34:26 +0000
committermatthew <>2014-06-21 02:34:26 +0000
commita24bfdad7fac42bad53f1f3d617ecd788ef69bfa (patch)
tree21ea73033a8fdb54b41e35989dd6fd2b13b11219 /src
parenta792fb66dd2e77c75c29042427f4eb524707d788 (diff)
downloadopenbsd-a24bfdad7fac42bad53f1f3d617ecd788ef69bfa.tar.gz
openbsd-a24bfdad7fac42bad53f1f3d617ecd788ef69bfa.tar.bz2
openbsd-a24bfdad7fac42bad53f1f3d617ecd788ef69bfa.zip
Protect explicit_bzero() from link-time optimization
Modern compiler toolchains are capable of optimizing even across translation unit boundaries, so simply moving the memory clearing into a separate function is not guaranteed to clear memory. To avoid this, we take advantage of ELF weak symbol semantics, and insert a call to an empty, weakly named function. The semantics of calling this function aren't determinable until load time, so the compiler and linker need to keep the memset() call. There are still ways a toolchain might defeat this trick (e.g., optimistically expecting the weak symbol to not be overloaded, and only calling memset() if it is; promoting weak symbols to strong symbols at link-time when emitting a static binary because they won't be interposed; implementing load-time optimizations). But at least for the foreseeable future, these seem unlikely. ok deraadt
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/string/explicit_bzero.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/src/lib/libc/string/explicit_bzero.c b/src/lib/libc/string/explicit_bzero.c
index 5124df23ca..3e33ca85b8 100644
--- a/src/lib/libc/string/explicit_bzero.c
+++ b/src/lib/libc/string/explicit_bzero.c
@@ -1,16 +1,19 @@
1/* $OpenBSD: explicit_bzero.c,v 1.2 2014/06/10 04:17:37 deraadt Exp $ */ 1/* $OpenBSD: explicit_bzero.c,v 1.3 2014/06/21 02:34:26 matthew Exp $ */
2/* 2/*
3 * Public domain. 3 * Public domain.
4 * Written by Ted Unangst 4 * Written by Matthew Dempsky.
5 */ 5 */
6 6
7#include <string.h> 7#include <string.h>
8 8
9/* 9__attribute__((weak)) void
10 * explicit_bzero - don't let the compiler optimize away bzero 10__explicit_bzero_hook(void *buf, size_t len)
11 */ 11{
12}
13
12void 14void
13explicit_bzero(void *p, size_t n) 15explicit_bzero(void *buf, size_t len)
14{ 16{
15 bzero(p, n); 17 memset(buf, 0, len);
18 __explicit_bzero_hook(buf, len);
16} 19}