1*aa487188Stb /* $OpenBSD: explicit_bzero.c,v 1.9 2022/02/10 08:39:32 tb Exp $ */
2a790b3e2Smatthew /*
3a790b3e2Smatthew * Copyright (c) 2014 Google Inc.
4a790b3e2Smatthew *
5a790b3e2Smatthew * Permission to use, copy, modify, and distribute this software for any
6a790b3e2Smatthew * purpose with or without fee is hereby granted, provided that the above
7a790b3e2Smatthew * copyright notice and this permission notice appear in all copies.
8a790b3e2Smatthew *
9a790b3e2Smatthew * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10a790b3e2Smatthew * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11a790b3e2Smatthew * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12a790b3e2Smatthew * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13a790b3e2Smatthew * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14a790b3e2Smatthew * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15a790b3e2Smatthew * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16a790b3e2Smatthew */
17a790b3e2Smatthew
18a790b3e2Smatthew #include <assert.h>
19a790b3e2Smatthew #include <errno.h>
20a790b3e2Smatthew #include <signal.h>
21c781ba80Sbcook #include <stdlib.h>
22a790b3e2Smatthew #include <string.h>
23a790b3e2Smatthew #include <unistd.h>
24a790b3e2Smatthew
25a790b3e2Smatthew #define ASSERT_EQ(a, b) assert((a) == (b))
26a790b3e2Smatthew #define ASSERT_NE(a, b) assert((a) != (b))
27a790b3e2Smatthew #define ASSERT_GE(a, b) assert((a) >= (b))
28a790b3e2Smatthew
29*aa487188Stb #if defined(__has_feature)
30*aa487188Stb #if __has_feature(address_sanitizer)
31*aa487188Stb #define __SANITIZE_ADDRESS__
32*aa487188Stb #endif
33*aa487188Stb #endif
34*aa487188Stb #ifdef __SANITIZE_ADDRESS__
35*aa487188Stb #define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
36*aa487188Stb #else
37*aa487188Stb #define ATTRIBUTE_NO_SANITIZE_ADDRESS
38*aa487188Stb #endif
39*aa487188Stb
40ccf34311Smatthew /* 128 bits of random data. */
41ccf34311Smatthew static const char secret[16] = {
42ccf34311Smatthew 0xa0, 0x6c, 0x0c, 0x81, 0xba, 0xd8, 0x5b, 0x0c,
43ccf34311Smatthew 0xb0, 0xd6, 0xd4, 0xe3, 0xeb, 0x52, 0x5f, 0x96,
44ccf34311Smatthew };
45ccf34311Smatthew
46ccf34311Smatthew enum {
47ccf34311Smatthew SECRETCOUNT = 64,
48ccf34311Smatthew SECRETBYTES = SECRETCOUNT * sizeof(secret)
49ccf34311Smatthew };
50ccf34311Smatthew
51c781ba80Sbcook /*
52c781ba80Sbcook * As of glibc 2.34, when _GNU_SOURCE is defined, SIGSTKSZ is no longer
53c781ba80Sbcook * constant on Linux. SIGSTKSZ is redefined to sysconf (_SC_SIGSTKSZ).
54c781ba80Sbcook */
55c781ba80Sbcook static char *altstack;
56c781ba80Sbcook #define ALTSTACK_SIZE (SIGSTKSZ + SECRETBYTES)
57475bbe2eSmatthew
58a790b3e2Smatthew static void
setup_stack(void)59475bbe2eSmatthew setup_stack(void)
60475bbe2eSmatthew {
61c781ba80Sbcook altstack = calloc(1, ALTSTACK_SIZE);
62c781ba80Sbcook ASSERT_NE(NULL, altstack);
63c781ba80Sbcook
64475bbe2eSmatthew const stack_t sigstk = {
65475bbe2eSmatthew .ss_sp = altstack,
66c781ba80Sbcook .ss_size = ALTSTACK_SIZE
67475bbe2eSmatthew };
68475bbe2eSmatthew
69475bbe2eSmatthew ASSERT_EQ(0, sigaltstack(&sigstk, NULL));
70475bbe2eSmatthew }
71475bbe2eSmatthew
72475bbe2eSmatthew static void
cleanup_stack(void)73c781ba80Sbcook cleanup_stack(void)
74c781ba80Sbcook {
75c781ba80Sbcook free(altstack);
76c781ba80Sbcook }
77c781ba80Sbcook
78c781ba80Sbcook static void
assert_on_stack(void)796211cbf5Smatthew assert_on_stack(void)
806211cbf5Smatthew {
816211cbf5Smatthew stack_t cursigstk;
826211cbf5Smatthew ASSERT_EQ(0, sigaltstack(NULL, &cursigstk));
836211cbf5Smatthew ASSERT_EQ(SS_ONSTACK, cursigstk.ss_flags & (SS_DISABLE|SS_ONSTACK));
846211cbf5Smatthew }
856211cbf5Smatthew
866211cbf5Smatthew static void
call_on_stack(void (* fn)(int))87475bbe2eSmatthew call_on_stack(void (*fn)(int))
88a790b3e2Smatthew {
89a790b3e2Smatthew /*
90a790b3e2Smatthew * This is a bit more complicated than strictly necessary, but
91a790b3e2Smatthew * it ensures we don't have any flaky test failures due to
92a790b3e2Smatthew * inherited signal masks/actions/etc.
93a790b3e2Smatthew *
94a790b3e2Smatthew * On systems where SA_ONSTACK is not supported, this could
95a790b3e2Smatthew * alternatively be implemented using makecontext() or
96a790b3e2Smatthew * pthread_attr_setstack().
97a790b3e2Smatthew */
98a790b3e2Smatthew
99a790b3e2Smatthew const struct sigaction sigact = {
100a790b3e2Smatthew .sa_handler = fn,
101a790b3e2Smatthew .sa_flags = SA_ONSTACK,
102a790b3e2Smatthew };
103a790b3e2Smatthew struct sigaction oldsigact;
104a790b3e2Smatthew sigset_t sigset, oldsigset;
105a790b3e2Smatthew
106a790b3e2Smatthew /* First, block all signals. */
107a790b3e2Smatthew ASSERT_EQ(0, sigemptyset(&sigset));
108a790b3e2Smatthew ASSERT_EQ(0, sigfillset(&sigset));
109a790b3e2Smatthew ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &sigset, &oldsigset));
110a790b3e2Smatthew
111475bbe2eSmatthew /* Next setup the signal handler for SIGUSR1. */
112a790b3e2Smatthew ASSERT_EQ(0, sigaction(SIGUSR1, &sigact, &oldsigact));
113a790b3e2Smatthew
114a790b3e2Smatthew /* Raise SIGUSR1 and momentarily unblock it to run the handler. */
115a790b3e2Smatthew ASSERT_EQ(0, raise(SIGUSR1));
116a790b3e2Smatthew ASSERT_EQ(0, sigdelset(&sigset, SIGUSR1));
117a790b3e2Smatthew ASSERT_EQ(-1, sigsuspend(&sigset));
118a790b3e2Smatthew ASSERT_EQ(EINTR, errno);
119a790b3e2Smatthew
120a790b3e2Smatthew /* Restore the original signal action, stack, and mask. */
121a790b3e2Smatthew ASSERT_EQ(0, sigaction(SIGUSR1, &oldsigact, NULL));
122a790b3e2Smatthew ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &oldsigset, NULL));
123a790b3e2Smatthew }
124a790b3e2Smatthew
125a790b3e2Smatthew static void
populate_secret(char * buf,size_t len)126a790b3e2Smatthew populate_secret(char *buf, size_t len)
127a790b3e2Smatthew {
128a790b3e2Smatthew int i, fds[2];
129a790b3e2Smatthew ASSERT_EQ(0, pipe(fds));
130a790b3e2Smatthew
131a790b3e2Smatthew for (i = 0; i < SECRETCOUNT; i++)
132a790b3e2Smatthew ASSERT_EQ(sizeof(secret), write(fds[1], secret, sizeof(secret)));
133a790b3e2Smatthew ASSERT_EQ(0, close(fds[1]));
134a790b3e2Smatthew
135a790b3e2Smatthew ASSERT_EQ(len, read(fds[0], buf, len));
136a790b3e2Smatthew ASSERT_EQ(0, close(fds[0]));
137a790b3e2Smatthew }
138a790b3e2Smatthew
139ccf34311Smatthew static int
count_secrets(const char * buf)140ccf34311Smatthew count_secrets(const char *buf)
141ccf34311Smatthew {
142ccf34311Smatthew int res = 0;
143ccf34311Smatthew size_t i;
144ccf34311Smatthew for (i = 0; i < SECRETCOUNT; i++) {
145ccf34311Smatthew if (memcmp(buf + i * sizeof(secret), secret,
146ccf34311Smatthew sizeof(secret)) == 0)
147ccf34311Smatthew res += 1;
148ccf34311Smatthew }
149ccf34311Smatthew return (res);
150ccf34311Smatthew }
151ccf34311Smatthew
152*aa487188Stb ATTRIBUTE_NO_SANITIZE_ADDRESS static char *
test_without_bzero(void)15308218f21Stb test_without_bzero(void)
154a790b3e2Smatthew {
155a790b3e2Smatthew char buf[SECRETBYTES];
1566211cbf5Smatthew assert_on_stack();
157a790b3e2Smatthew populate_secret(buf, sizeof(buf));
158c781ba80Sbcook char *res = memmem(altstack, ALTSTACK_SIZE, buf, sizeof(buf));
159ccf34311Smatthew ASSERT_NE(NULL, res);
160ccf34311Smatthew return (res);
161ccf34311Smatthew }
162ccf34311Smatthew
163*aa487188Stb ATTRIBUTE_NO_SANITIZE_ADDRESS static char *
test_with_bzero(void)16408218f21Stb test_with_bzero(void)
165ccf34311Smatthew {
166ccf34311Smatthew char buf[SECRETBYTES];
167ccf34311Smatthew assert_on_stack();
168ccf34311Smatthew populate_secret(buf, sizeof(buf));
169c781ba80Sbcook char *res = memmem(altstack, ALTSTACK_SIZE, buf, sizeof(buf));
170ccf34311Smatthew ASSERT_NE(NULL, res);
171ccf34311Smatthew explicit_bzero(buf, sizeof(buf));
172ccf34311Smatthew return (res);
173a790b3e2Smatthew }
174a790b3e2Smatthew
175a790b3e2Smatthew static void
do_test_without_bzero(int signo)176ccf34311Smatthew do_test_without_bzero(int signo)
177a790b3e2Smatthew {
178ccf34311Smatthew char *buf = test_without_bzero();
179ccf34311Smatthew ASSERT_GE(count_secrets(buf), 1);
180ccf34311Smatthew }
181ccf34311Smatthew
182ccf34311Smatthew static void
do_test_with_bzero(int signo)183ccf34311Smatthew do_test_with_bzero(int signo)
184ccf34311Smatthew {
185ff84e1f9Smatthew char *buf = test_with_bzero();
186ff84e1f9Smatthew ASSERT_EQ(count_secrets(buf), 0);
187a790b3e2Smatthew }
188a790b3e2Smatthew
189a790b3e2Smatthew int
main(void)19008218f21Stb main(void)
191a790b3e2Smatthew {
192475bbe2eSmatthew setup_stack();
193475bbe2eSmatthew
194a790b3e2Smatthew /*
195ccf34311Smatthew * Solaris and OS X clobber the signal stack after returning to the
196ccf34311Smatthew * normal stack, so we need to inspect altstack while we're still
197ccf34311Smatthew * running on it. Unfortunately, this means we risk clobbering the
198ccf34311Smatthew * buffer ourselves.
199ccf34311Smatthew *
200ccf34311Smatthew * To minimize this risk, test_with{,out}_bzero() are responsible for
201ccf34311Smatthew * locating the offset of their buf variable within altstack, and
202ccf34311Smatthew * and returning that address. Then we can simply memcmp() repeatedly
203ccf34311Smatthew * to count how many instances of secret we found.
204ccf34311Smatthew */
205ccf34311Smatthew
206ccf34311Smatthew /*
207a790b3e2Smatthew * First, test that if we *don't* call explicit_bzero, that we
208ccf34311Smatthew * *are* able to find at least one instance of the secret data still
209ccf34311Smatthew * on the stack. This sanity checks that call_on_stack() and
210ccf34311Smatthew * populate_secret() work as intended.
211a790b3e2Smatthew */
212c781ba80Sbcook memset(altstack, 0, ALTSTACK_SIZE);
213ccf34311Smatthew call_on_stack(do_test_without_bzero);
214a790b3e2Smatthew
215a790b3e2Smatthew /*
216a790b3e2Smatthew * Now test with a call to explicit_bzero() and check that we
217ccf34311Smatthew * *don't* find any instances of the secret data.
218a790b3e2Smatthew */
219c781ba80Sbcook memset(altstack, 0, ALTSTACK_SIZE);
220ccf34311Smatthew call_on_stack(do_test_with_bzero);
221a790b3e2Smatthew
222c781ba80Sbcook cleanup_stack();
223c781ba80Sbcook
224a790b3e2Smatthew return (0);
225a790b3e2Smatthew }
226