Debian Codegraph @

test/getrandom-fallbacks.c

raw symbols SCIP
1 /* Test the fallback logic in get_random_bytes.
2
3 Written by Zack Weinberg <zackw at panix.com> in 2018.
4 To the extent possible under law, Zack Weinberg has waived all
5 copyright and related or neighboring rights to this work.
6
7 See https://creativecommons.org/publicdomain/zero/1.0/ for further
8 details. */
9
10 #include "crypt-port.h"
11
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <errno.h>
16
17 #ifdef HAVE_FCNTL_H
18 #include <fcntl.h>
19 #endif
20 #if defined HAVE_SYS_SYSCALL_H
21 #include <sys/syscall.h>
22 #endif
23 #ifdef HAVE_SYS_STAT_H
24 #include <sys/stat.h>
25 #endif
26
27 /* If arc4random_buf is available, all of the fallback logic is compiled
28 out and this test is unnecessary. If ld --wrap is not available this
29 test will not work. */
30 #if defined HAVE_ARC4RANDOM_BUF || !defined HAVE_LD_WRAP
31
32 int
33 main (void)
34 {
35 return 77;
36 }
37
38 #else
39
40 /* All of the mock system primitives below fill in their buffer with
41 repeats of these bytes, so we can tell where the data came from. */
42 #define MOCK_getentropy 'e'
43 #define MOCK_getrandom 'r'
44 #define MOCK_sys_getentropy 'E'
45 #define MOCK_sys_getrandom 'R'
46 #define MOCK_urandom 'u'
47
48 #ifdef HAVE_GETENTROPY
49 static bool getentropy_should_fail = false;
50 extern int __wrap_getentropy (void *, size_t);
51 int
52 __wrap_getentropy (void *buf, size_t buflen)
53 {
54 if (getentropy_should_fail)
55 {
56 errno = ENOSYS;
57 return -1;
58 }
59 else
60 {
61 memset (buf, MOCK_getentropy, buflen);
62 return 0;
63 }
64 }
65 #endif
66
67 #ifdef HAVE_GETRANDOM
68 static bool getrandom_should_fail = false;
69 extern ssize_t __wrap_getrandom (void *, size_t, unsigned int);
70 ssize_t
71 __wrap_getrandom (void *buf, size_t buflen, unsigned int ARG_UNUSED(flags))
72 {
73 if (getrandom_should_fail)
74 {
75 errno = ENOSYS;
76 return -1;
77 }
78 else
79 {
80 buflen = MIN (buflen, INT16_MAX);
81 memset (buf, MOCK_getrandom, buflen);
82 return (ssize_t)buflen;
83 }
84 }
85 #endif
86
87 #ifdef HAVE_SYSCALL
88 #ifdef SYS_getentropy
89 static bool sys_getentropy_should_fail = false;
90 #endif
91 #ifdef SYS_getrandom
92 static bool sys_getrandom_should_fail = false;
93 #endif
94 static bool other_syscalls = false;
95 extern long __wrap_syscall (long, ...);
96 long
97 __wrap_syscall(long number, ...)
98 {
99 #ifdef SYS_getentropy
100 if (number == SYS_getentropy)
101 {
102 if (sys_getentropy_should_fail)
103 {
104 errno = ENOSYS;
105 return -1;
106 }
107 else
108 {
109 va_list ap;
110 va_start (ap, number);
111 void *buf = va_arg (ap, void *);
112 size_t buflen = va_arg (ap, size_t);
113 va_end (ap);
114 memset (buf, MOCK_sys_getentropy, buflen);
115 return 0;
116 }
117 }
118 #endif
119 #ifdef SYS_getrandom
120 if (number == SYS_getrandom)
121 {
122 if (sys_getrandom_should_fail)
123 {
124 errno = ENOSYS;
125 return -1;
126 }
127 else
128 {
129 va_list ap;
130 va_start (ap, number);
131 void *buf = va_arg (ap, void *);
132 size_t buflen = va_arg (ap, size_t);
133 buflen = MIN (buflen, INT16_MAX);
134 va_end (ap);
135 memset (buf, MOCK_sys_getrandom, buflen);
136 return (ssize_t)buflen;
137 }
138 }
139 #endif
140 /* There is no vsyscall. We just have to hope nobody in this test
141 program wants to use syscall() for anything else. */
142 other_syscalls = true;
143 fprintf (stderr, "ERROR: unexpected syscall(%ld)\n", number);
144 errno = ENOSYS;
145 return -1;
146 }
147 #endif /* HAVE_SYSCALL */
148
149 /* It is not possible to hit both of the code paths that can set the
150 "/dev/urandom doesn't work" flag in a single test program, because
151 there's no way to _clear_ that flag again. This test chooses to
152 exercise the read-failure path, not the open-failure path. */
153 #if defined HAVE_SYS_STAT_H && defined HAVE_FCNTL_H && defined HAVE_UNISTD_H
154
155 static bool open_needs_mode(int flags)
156 {
157 if (flags & O_CREAT)
158 {
159 return true;
160 }
161 #if defined(O_TMPFILE)
162 if ((flags & O_TMPFILE) == O_TMPFILE)
163 {
164 return true;
165 }
166 #endif
167 return false;
168 }
169
170 static bool urandom_should_fail = false;
171 static int urandom_fd = -1;
172 extern int __wrap_open (const char *, int, ...);
173 extern int __real_open (const char *, int, ...);
174 int
175 __wrap_open (const char *path, int flags, ...)
176 {
177 int mode = 0;
178 if (open_needs_mode(flags))
179 {
180 va_list args;
181 va_start(args, flags);
182 mode = va_arg(args, int);
183 va_end(args);
184 }
185
186 int ret = __real_open (path, flags, mode);
187 if (ret == -1)
188 return ret;
189 if (!strcmp (path, "/dev/urandom"))
190 urandom_fd = ret;
191 return ret;
192 }
193
194 #ifdef HAVE_OPEN64
195 extern int __wrap_open64 (const char *, int, ...);
196 extern int __real_open64 (const char *, int, ...);
197 int
198 __wrap_open64 (const char *path, int flags, ...)
199 {
200 int mode = 0;
201 if (open_needs_mode(flags))
202 {
203 va_list args;
204 va_start(args, flags);
205 mode = va_arg(args, int);
206 va_end(args);
207 }
208
209 int ret = __real_open64 (path, flags, mode);
210 if (ret == -1)
211 return ret;
212 if (!strcmp (path, "/dev/urandom"))
213 urandom_fd = ret;
214 return ret;
215 }
216 #endif
217
218 extern int __wrap_close (int);
219 extern int __real_close (int);
220 int
221 __wrap_close (int fd)
222 {
223 if (fd == urandom_fd)
224 urandom_fd = -1;
225 return __real_close (fd);
226 }
227
228 extern ssize_t __wrap_read (int, void *, size_t);
229 extern ssize_t __real_read (int, void *, size_t);
230 ssize_t
231 __wrap_read (int fd, void *buf, size_t count)
232 {
233 if (fd == urandom_fd)
234 {
235 if (urandom_should_fail)
236 {
237 errno = ENOSYS;
238 return -1;
239 }
240 else
241 {
242 count = MIN (count, INT16_MAX);
243 memset (buf, MOCK_urandom, count);
244 return (ssize_t)count;
245 }
246 }
247 else
248 return __real_read (fd, buf, count);
249 }
250
251 #endif
252
253 struct subtest
254 {
255 const char *what;
256 bool *make_fail;
257 char expected;
258 };
259 const struct subtest subtests[] =
260 {
261 { "initial", 0, 'x' },
262
263 #ifdef HAVE_GETENTROPY
264 { "getentropy", &getentropy_should_fail, MOCK_getentropy },
265 #endif
266 #ifdef HAVE_GETRANDOM
267 { "getrandom", &getrandom_should_fail, MOCK_getrandom },
268 #endif
269
270 #ifdef HAVE_SYSCALL
271 #ifdef SYS_getentropy
272 { "sys_getentropy", &sys_getentropy_should_fail, MOCK_sys_getentropy },
273 #endif
274 #ifdef SYS_getrandom
275 { "sys_getrandom", &sys_getrandom_should_fail, MOCK_sys_getrandom },
276 #endif
277 #endif
278
279 #if defined HAVE_SYS_STAT_H && defined HAVE_FCNTL_H && defined HAVE_UNISTD_H
280 { "/dev/urandom", &urandom_should_fail, MOCK_urandom },
281 #endif
282
283 { "final", 0, 0 }
284 };
285
286 int
287 main (void)
288 {
289 char buf[257];
290 char expected[2] = { 0, 0 };
291 memset (buf, 'x', sizeof buf - 1);
292 buf[sizeof buf - 1] = '\0';
293 bool failed = false;
294 const struct subtest *s;
295
296 for (s = subtests; s->expected;)
297 {
298 expected[0] = s->expected;
299 if (strspn (buf, expected) != 256)
300 {
301 printf ("FAIL: %s: buffer not filled with '%c'\n",
302 s->what, s->expected);
303 failed = true;
304 }
305 else
306 printf ("ok: %s (output)\n", s->what);
307
308 if (s->make_fail)
309 *(s->make_fail) = true;
310 s++;
311
312 bool r = get_random_bytes (buf, sizeof buf - 1);
313 buf[sizeof buf - 1] = '\0';
314 if ((s->expected && !r) || (!s->expected && r))
315 {
316 printf ("FAIL: %s: get_random_bytes: %s\n",
317 s->what, strerror (errno));
318 failed = true;
319 }
320 else
321 printf ("ok: %s (return)\n", s->what);
322 }
323 #if HAVE_SYSCALL
324 failed |= other_syscalls;
325 #endif
326 return failed;
327 }
328
329 #endif