Skip to content

Commit cbc44ee

Browse files
josephlrnewpavlov
authored andcommitted
Alternative way to detect AMD bug (#48)
1 parent 30ac773 commit cbc44ee

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/rdrand.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ unsafe fn rdrand() -> Result<[u8; WORD_SIZE], Error> {
2323
for _ in 0..RETRY_LIMIT {
2424
let mut el = mem::uninitialized();
2525
if _rdrand64_step(&mut el) == 1 {
26-
// AMD CPUs from families 14h to 16h (pre Ryzen) will sometimes give
27-
// bogus random data. Discard these values and warn the user.
26+
// AMD CPUs from families 14h to 16h (pre Ryzen) sometimes fail to
27+
// set CF on bogus random data, so we check these values explictly.
2828
// See https://github.com/systemd/systemd/issues/11810#issuecomment-489727505
29-
if cfg!(not(target_env = "sgx")) && (el == 0 || el == !0) {
30-
error!("RDRAND returned suspicious value {}, CPU RNG is broken", el);
31-
return Err(Error::UNKNOWN);
29+
// We perform this check regardless of target to guard against
30+
// any implementation that incorrectly fails to set CF.
31+
if el != 0 && el != !0 {
32+
return Ok(el.to_ne_bytes());
3233
}
33-
return Ok(el.to_ne_bytes());
34+
error!("RDRAND returned {:X}, CPU RNG may be broken", el);
35+
// Keep looping in case this was a false positive.
3436
}
3537
}
3638
error!("RDRAND failed, CPU issue likely");

0 commit comments

Comments
 (0)