Re: [PATCH 1/2] x86/rdrand: implement sanity check for RDSEED

From: Borislav Petkov
Date: Wed Mar 12 2025 - 12:33:03 EST


On Wed, Mar 12, 2025 at 03:31:29PM +0300, Mikhail Paulyshka wrote:
> +/*
> + * RDSEED has Built-In-Self-Test (BIST) that runs on every invocation.
> + * Run the instruction a few times as a sanity check. Also make sure
> + * it's not outputting the same value over and over, which has happened
> + * as a result of past CPU bugs.
> + *
> + * If it fails, it is simple to disable RDSEED here.
> + */
> +
> +void x86_init_rdseed(struct cpuinfo_x86 *c)
> +{
> + unsigned long sample, prev;
> + bool failure = false;
> + size_t i, changed;
> +
> + if (!cpu_has(c, X86_FEATURE_RDSEED))
> + return;
> +
> + for (changed = 0, i = 0; i < SAMPLES; ++i) {
> + if (!rdseed_long(&sample)) {
> + failure = true;
> + break;
> + }
> + changed += i && sample != prev;
> + prev = sample;
> + }
> + if (changed < MIN_CHANGE)
> + failure = true;
> +
> + if (failure) {
> + clear_cpu_cap(c, X86_FEATURE_RDSEED);
> + pr_emerg("RDSEED is not reliable on this platform; disabling.\n");
> + }
> +}

This one basically duplicates x86_init_rdrand() and I'm sure you can use
a single function to test both.

But more importantly, lemme ask around internally whether that is even
a reliable test to detect RDSEED performs properly or not.

Stay tuned...

Thx.

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette