]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/random-util.c
random-util: call initialize_srand() after fork()
[thirdparty/systemd.git] / src / basic / random-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #if defined(__i386__) || defined(__x86_64__)
4 #include <cpuid.h>
5 #endif
6
7 #include <elf.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <pthread.h>
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/time.h>
16
17 #if HAVE_SYS_AUXV_H
18 # include <sys/auxv.h>
19 #endif
20
21 #include "alloc-util.h"
22 #include "fd-util.h"
23 #include "fileio.h"
24 #include "io-util.h"
25 #include "missing_random.h"
26 #include "missing_syscall.h"
27 #include "parse-util.h"
28 #include "random-util.h"
29 #include "siphash24.h"
30 #include "time-util.h"
31
32 static bool srand_called = false;
33
34 int rdrand(unsigned long *ret) {
35
36 /* So, you are a "security researcher", and you wonder why we bother with using raw RDRAND here,
37 * instead of sticking to /dev/urandom or getrandom()?
38 *
39 * Here's why: early boot. On Linux, during early boot the random pool that backs /dev/urandom and
40 * getrandom() is generally not initialized yet. It is very common that initialization of the random
41 * pool takes a longer time (up to many minutes), in particular on embedded devices that have no
42 * explicit hardware random generator, as well as in virtualized environments such as major cloud
43 * installations that do not provide virtio-rng or a similar mechanism.
44 *
45 * In such an environment using getrandom() synchronously means we'd block the entire system boot-up
46 * until the pool is initialized, i.e. *very* long. Using getrandom() asynchronously (GRND_NONBLOCK)
47 * would mean acquiring randomness during early boot would simply fail. Using /dev/urandom would mean
48 * generating many kmsg log messages about our use of it before the random pool is properly
49 * initialized. Neither of these outcomes is desirable.
50 *
51 * Thus, for very specific purposes we use RDRAND instead of either of these three options. RDRAND
52 * provides us quickly and relatively reliably with random values, without having to delay boot,
53 * without triggering warning messages in kmsg.
54 *
55 * Note that we use RDRAND only under very specific circumstances, when the requirements on the
56 * quality of the returned entropy permit it. Specifically, here are some cases where we *do* use
57 * RDRAND:
58 *
59 * • UUID generation: UUIDs are supposed to be universally unique but are not cryptographic
60 * key material. The quality and trust level of RDRAND should hence be OK: UUIDs should be
61 * generated in a way that is reliably unique, but they do not require ultimate trust into
62 * the entropy generator. systemd generates a number of UUIDs during early boot, including
63 * 'invocation IDs' for every unit spawned that identify the specific invocation of the
64 * service globally, and a number of others. Other alternatives for generating these UUIDs
65 * have been considered, but don't really work: for example, hashing uuids from a local
66 * system identifier combined with a counter falls flat because during early boot disk
67 * storage is not yet available (think: initrd) and thus a system-specific ID cannot be
68 * stored or retrieved yet.
69 *
70 * • Hash table seed generation: systemd uses many hash tables internally. Hash tables are
71 * generally assumed to have O(1) access complexity, but can deteriorate to prohibitive
72 * O(n) access complexity if an attacker manages to trigger a large number of hash
73 * collisions. Thus, systemd (as any software employing hash tables should) uses seeded
74 * hash functions for its hash tables, with a seed generated randomly. The hash tables
75 * systemd employs watch the fill level closely and reseed if necessary. This allows use of
76 * a low quality RNG initially, as long as it improves should a hash table be under attack:
77 * the attacker after all needs to to trigger many collisions to exploit it for the purpose
78 * of DoS, but if doing so improves the seed the attack surface is reduced as the attack
79 * takes place.
80 *
81 * Some cases where we do NOT use RDRAND are:
82 *
83 * • Generation of cryptographic key material 🔑
84 *
85 * • Generation of cryptographic salt values 🧂
86 *
87 * This function returns:
88 *
89 * -EOPNOTSUPP → RDRAND is not available on this system 😔
90 * -EAGAIN → The operation failed this time, but is likely to work if you try again a few
91 * times ♻
92 * -EUCLEAN → We got some random value, but it looked strange, so we refused using it.
93 * This failure might or might not be temporary. 😕
94 */
95
96 #if defined(__i386__) || defined(__x86_64__)
97 static int have_rdrand = -1;
98 unsigned long v;
99 uint8_t success;
100
101 if (have_rdrand < 0) {
102 uint32_t eax, ebx, ecx, edx;
103
104 /* Check if RDRAND is supported by the CPU */
105 if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) == 0) {
106 have_rdrand = false;
107 return -EOPNOTSUPP;
108 }
109
110 /* Compat with old gcc where bit_RDRND didn't exist yet */
111 #ifndef bit_RDRND
112 #define bit_RDRND (1U << 30)
113 #endif
114
115 have_rdrand = !!(ecx & bit_RDRND);
116 }
117
118 if (have_rdrand == 0)
119 return -EOPNOTSUPP;
120
121 asm volatile("rdrand %0;"
122 "setc %1"
123 : "=r" (v),
124 "=qm" (success));
125 msan_unpoison(&success, sizeof(success));
126 if (!success)
127 return -EAGAIN;
128
129 /* Apparently on some AMD CPUs RDRAND will sometimes (after a suspend/resume cycle?) report success
130 * via the carry flag but nonetheless return the same fixed value -1 in all cases. This appears to be
131 * a bad bug in the CPU or firmware. Let's deal with that and work-around this by explicitly checking
132 * for this special value (and also 0, just to be sure) and filtering it out. This is a work-around
133 * only however and something AMD really should fix properly. The Linux kernel should probably work
134 * around this issue by turning off RDRAND altogether on those CPUs. See:
135 * https://github.com/systemd/systemd/issues/11810 */
136 if (v == 0 || v == ULONG_MAX)
137 return log_debug_errno(SYNTHETIC_ERRNO(EUCLEAN),
138 "RDRAND returned suspicious value %lx, assuming bad hardware RNG, not using value.", v);
139
140 *ret = v;
141 return 0;
142 #else
143 return -EOPNOTSUPP;
144 #endif
145 }
146
147 int genuine_random_bytes(void *p, size_t n, RandomFlags flags) {
148 static int have_syscall = -1;
149 _cleanup_close_ int fd = -1;
150 bool got_some = false;
151 int r;
152
153 /* Gathers some high-quality randomness from the kernel (or potentially mid-quality randomness from
154 * the CPU if the RANDOM_ALLOW_RDRAND flag is set). This call won't block, unless the RANDOM_BLOCK
155 * flag is set. If RANDOM_MAY_FAIL is set, an error is returned if the random pool is not
156 * initialized. Otherwise it will always return some data from the kernel, regardless of whether the
157 * random pool is fully initialized or not. If RANDOM_EXTEND_WITH_PSEUDO is set, and some but not
158 * enough better quality randomness could be acquired, the rest is filled up with low quality
159 * randomness.
160 *
161 * Of course, when creating cryptographic key material you really shouldn't use RANDOM_ALLOW_DRDRAND
162 * or even RANDOM_EXTEND_WITH_PSEUDO.
163 *
164 * When generating UUIDs it's fine to use RANDOM_ALLOW_RDRAND but not OK to use
165 * RANDOM_EXTEND_WITH_PSEUDO. In fact RANDOM_EXTEND_WITH_PSEUDO is only really fine when invoked via
166 * an "all bets are off" wrapper, such as random_bytes(), see below. */
167
168 if (n == 0)
169 return 0;
170
171 if (FLAGS_SET(flags, RANDOM_ALLOW_RDRAND))
172 /* Try x86-64' RDRAND intrinsic if we have it. We only use it if high quality randomness is
173 * not required, as we don't trust it (who does?). Note that we only do a single iteration of
174 * RDRAND here, even though the Intel docs suggest calling this in a tight loop of 10
175 * invocations or so. That's because we don't really care about the quality here. We
176 * generally prefer using RDRAND if the caller allows us to, since this way we won't upset
177 * the kernel's random subsystem by accessing it before the pool is initialized (after all it
178 * will kmsg log about every attempt to do so)..*/
179 for (;;) {
180 unsigned long u;
181 size_t m;
182
183 if (rdrand(&u) < 0) {
184 if (got_some && FLAGS_SET(flags, RANDOM_EXTEND_WITH_PSEUDO)) {
185 /* Fill in the remaining bytes using pseudo-random values */
186 pseudo_random_bytes(p, n);
187 return 0;
188 }
189
190 /* OK, this didn't work, let's go to getrandom() + /dev/urandom instead */
191 break;
192 }
193
194 m = MIN(sizeof(u), n);
195 memcpy(p, &u, m);
196
197 p = (uint8_t*) p + m;
198 n -= m;
199
200 if (n == 0)
201 return 0; /* Yay, success! */
202
203 got_some = true;
204 }
205
206 /* Use the getrandom() syscall unless we know we don't have it. */
207 if (have_syscall != 0 && !HAS_FEATURE_MEMORY_SANITIZER) {
208
209 for (;;) {
210 r = getrandom(p, n, FLAGS_SET(flags, RANDOM_BLOCK) ? 0 : GRND_NONBLOCK);
211 if (r > 0) {
212 have_syscall = true;
213
214 if ((size_t) r == n)
215 return 0; /* Yay, success! */
216
217 assert((size_t) r < n);
218 p = (uint8_t*) p + r;
219 n -= r;
220
221 if (FLAGS_SET(flags, RANDOM_EXTEND_WITH_PSEUDO)) {
222 /* Fill in the remaining bytes using pseudo-random values */
223 pseudo_random_bytes(p, n);
224 return 0;
225 }
226
227 got_some = true;
228
229 /* Hmm, we didn't get enough good data but the caller insists on good data? Then try again */
230 if (FLAGS_SET(flags, RANDOM_BLOCK))
231 continue;
232
233 /* Fill in the rest with /dev/urandom */
234 break;
235
236 } else if (r == 0) {
237 have_syscall = true;
238 return -EIO;
239
240 } else if (errno == ENOSYS) {
241 /* We lack the syscall, continue with reading from /dev/urandom. */
242 have_syscall = false;
243 break;
244
245 } else if (errno == EAGAIN) {
246 /* The kernel has no entropy whatsoever. Let's remember to use the syscall
247 * the next time again though.
248 *
249 * If RANDOM_MAY_FAIL is set, return an error so that random_bytes() can
250 * produce some pseudo-random bytes instead. Otherwise, fall back to
251 * /dev/urandom, which we know is empty, but the kernel will produce some
252 * bytes for us on a best-effort basis. */
253 have_syscall = true;
254
255 if (got_some && FLAGS_SET(flags, RANDOM_EXTEND_WITH_PSEUDO)) {
256 /* Fill in the remaining bytes using pseudorandom values */
257 pseudo_random_bytes(p, n);
258 return 0;
259 }
260
261 if (FLAGS_SET(flags, RANDOM_MAY_FAIL))
262 return -ENODATA;
263
264 /* Use /dev/urandom instead */
265 break;
266 } else
267 return -errno;
268 }
269 }
270
271 fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
272 if (fd < 0)
273 return errno == ENOENT ? -ENOSYS : -errno;
274
275 return loop_read_exact(fd, p, n, true);
276 }
277
278 static void clear_srand_initialization(void) {
279 srand_called = false;
280 }
281
282 void initialize_srand(void) {
283 static bool pthread_atfork_registered = false;
284 unsigned x;
285 #if HAVE_SYS_AUXV_H
286 const void *auxv;
287 #endif
288 unsigned long k;
289
290 if (srand_called)
291 return;
292
293 #if HAVE_SYS_AUXV_H
294 /* The kernel provides us with 16 bytes of entropy in auxv, so let's try to make use of that to seed
295 * the pseudo-random generator. It's better than nothing... But let's first hash it to make it harder
296 * to recover the original value by watching any pseudo-random bits we generate. After all the
297 * AT_RANDOM data might be used by other stuff too (in particular: ASLR), and we probably shouldn't
298 * leak the seed for that. */
299
300 auxv = ULONG_TO_PTR(getauxval(AT_RANDOM));
301 if (auxv) {
302 static const uint8_t auxval_hash_key[16] = {
303 0x92, 0x6e, 0xfe, 0x1b, 0xcf, 0x00, 0x52, 0x9c, 0xcc, 0x42, 0xcf, 0xdc, 0x94, 0x1f, 0x81, 0x0f
304 };
305
306 x = (unsigned) siphash24(auxv, 16, auxval_hash_key);
307 } else
308 #endif
309 x = 0;
310
311 x ^= (unsigned) now(CLOCK_REALTIME);
312 x ^= (unsigned) gettid();
313
314 if (rdrand(&k) >= 0)
315 x ^= (unsigned) k;
316
317 srand(x);
318 srand_called = true;
319
320 if (!pthread_atfork_registered) {
321 (void) pthread_atfork(NULL, NULL, clear_srand_initialization);
322 pthread_atfork_registered = true;
323 }
324 }
325
326 /* INT_MAX gives us only 31 bits, so use 24 out of that. */
327 #if RAND_MAX >= INT_MAX
328 # define RAND_STEP 3
329 #else
330 /* SHORT_INT_MAX or lower gives at most 15 bits, we just just 8 out of that. */
331 # define RAND_STEP 1
332 #endif
333
334 void pseudo_random_bytes(void *p, size_t n) {
335 uint8_t *q;
336
337 /* This returns pseudo-random data using libc's rand() function. You probably never want to call this
338 * directly, because why would you use this if you can get better stuff cheaply? Use random_bytes()
339 * instead, see below: it will fall back to this function if there's nothing better to get, but only
340 * then. */
341
342 initialize_srand();
343
344 for (q = p; q < (uint8_t*) p + n; q += RAND_STEP) {
345 unsigned rr;
346
347 rr = (unsigned) rand();
348
349 #if RAND_STEP >= 3
350 if ((size_t) (q - (uint8_t*) p + 2) < n)
351 q[2] = rr >> 16;
352 #endif
353 #if RAND_STEP >= 2
354 if ((size_t) (q - (uint8_t*) p + 1) < n)
355 q[1] = rr >> 8;
356 #endif
357 q[0] = rr;
358 }
359 }
360
361 void random_bytes(void *p, size_t n) {
362
363 /* This returns high quality randomness if we can get it cheaply. If we can't because for some reason
364 * it is not available we'll try some crappy fallbacks.
365 *
366 * What this function will do:
367 *
368 * • This function will preferably use the CPU's RDRAND operation, if it is available, in
369 * order to return "mid-quality" random values cheaply.
370 *
371 * • Use getrandom() with GRND_NONBLOCK, to return high-quality random values if they are
372 * cheaply available.
373 *
374 * • This function will return pseudo-random data, generated via libc rand() if nothing
375 * better is available.
376 *
377 * • This function will work fine in early boot
378 *
379 * • This function will always succeed
380 *
381 * What this function won't do:
382 *
383 * • This function will never fail: it will give you randomness no matter what. It might not
384 * be high quality, but it will return some, possibly generated via libc's rand() call.
385 *
386 * • This function will never block: if the only way to get good randomness is a blocking,
387 * synchronous getrandom() we'll instead provide you with pseudo-random data.
388 *
389 * This function is hence great for things like seeding hash tables, generating random numeric UNIX
390 * user IDs (that are checked for collisions before use) and such.
391 *
392 * This function is hence not useful for generating UUIDs or cryptographic key material.
393 */
394
395 if (genuine_random_bytes(p, n, RANDOM_EXTEND_WITH_PSEUDO|RANDOM_MAY_FAIL|RANDOM_ALLOW_RDRAND) >= 0)
396 return;
397
398 /* If for some reason some user made /dev/urandom unavailable to us, or the kernel has no entropy, use a PRNG instead. */
399 pseudo_random_bytes(p, n);
400 }
401
402 size_t random_pool_size(void) {
403 _cleanup_free_ char *s = NULL;
404 int r;
405
406 /* Read pool size, if possible */
407 r = read_one_line_file("/proc/sys/kernel/random/poolsize", &s);
408 if (r < 0)
409 log_debug_errno(r, "Failed to read pool size from kernel: %m");
410 else {
411 unsigned sz;
412
413 r = safe_atou(s, &sz);
414 if (r < 0)
415 log_debug_errno(r, "Failed to parse pool size: %s", s);
416 else
417 /* poolsize is in bits on 2.6, but we want bytes */
418 return CLAMP(sz / 8, RANDOM_POOL_SIZE_MIN, RANDOM_POOL_SIZE_MAX);
419 }
420
421 /* Use the minimum as default, if we can't retrieve the correct value */
422 return RANDOM_POOL_SIZE_MIN;
423 }