]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
random-util: assume getrandom(GRND_INSECURE) works
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 5 Jan 2026 04:24:27 +0000 (13:24 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 13 Jan 2026 01:21:04 +0000 (10:21 +0900)
GRND_INSECURE was added in kernel 5.6, and our baseline on kernel is
5.10. Let's assume it always works. Even if it does not work, we have
further fallback logics. So, this should be safe.

src/basic/random-util.c

index ece0e419437f57e44f6218b514e84163daed3578..d05be6fa501ad3ff9138a01669b79af587b4f72a 100644 (file)
@@ -68,8 +68,6 @@ static void fallback_random_bytes(void *p, size_t n) {
 }
 
 void random_bytes(void *p, size_t n) {
-        static bool have_grndinsecure = true;
-
         assert(p || n == 0);
 
         if (n == 0)
@@ -78,15 +76,9 @@ void random_bytes(void *p, size_t n) {
         for (;;) {
                 ssize_t l;
 
-                l = getrandom(p, n, have_grndinsecure ? GRND_INSECURE : GRND_NONBLOCK);
-                if (l < 0 && errno == EINVAL && have_grndinsecure) {
-                        /* No GRND_INSECURE; fallback to GRND_NONBLOCK. */
-                        have_grndinsecure = false;
-                        continue;
-                }
+                l = getrandom(p, n, GRND_INSECURE);
                 if (l <= 0)
-                        break; /* Will block (with GRND_NONBLOCK), or unexpected error. Give up and fallback
-                                  to /dev/urandom. */
+                        break; /* Unexpected error. Give up and fallback to /dev/urandom. */
 
                 if ((size_t) l == n)
                         return; /* Done reading, success. */