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