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