[PATCH 2/2] x86/random: Issue a warning if RDRAND or RDSEED fails

From: Kirill A. Shutemov
Date: Tue Jan 30 2024 - 03:31:10 EST


RDRAND and RDSEED instructions rarely fail. Ten retries should be
sufficient to account for occasional failures.

If the instruction fails more than ten times, it is likely that the
hardware is broken or someone is attempting to exceed the rate at which
the random number generator hardware can provide random numbers.

Issue a warning if ten retries were not enough.

Signed-off-by: Kirill A. Shutemov <kirill.shutemov@xxxxxxxxxxxxxxx>
---
arch/x86/include/asm/archrandom.h | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/arch/x86/include/asm/archrandom.h b/arch/x86/include/asm/archrandom.h
index 918c5880de9e..fc8d837fb3b9 100644
--- a/arch/x86/include/asm/archrandom.h
+++ b/arch/x86/include/asm/archrandom.h
@@ -13,6 +13,12 @@
#include <asm/processor.h>
#include <asm/cpufeature.h>

+#ifdef KASLR_COMPRESSED_BOOT
+#define rd_warn(msg) warn(msg)
+#else
+#define rd_warn(msg) WARN_ONCE(1, msg)
+#endif
+
#define RDRAND_RETRY_LOOPS 10

/* Unconditional execution of RDRAND and RDSEED */
@@ -28,6 +34,9 @@ static inline bool __must_check rdrand_long(unsigned long *v)
if (ok)
return true;
} while (--retry);
+
+ rd_warn("RDRAND failed\n");
+
return false;
}

@@ -45,6 +54,8 @@ static inline bool __must_check rdseed_long(unsigned long *v)
return true;
} while (--retry);

+ rd_warn("RDSEED failed\n");
+
return false;
}

--
2.43.0