Module Name:    src
Committed By:   taca
Date:           Fri Nov  1 15:01:27 UTC 2019

Modified Files:
        src/sys/arch/x86/x86: cpu_rng.c

Log Message:
Check CPU support of RDRAND before calling cpu_rng_rdrand().

cpu_earlyrng() checks CPU support of RDSEED and RDRAND before calling
cpu_rng_rdseed() and cpu_rng_rdrand().

But cpu_rng_rdseed() did not check CPU support of RDRAND and system had
crashed on such an environment.  There is no such case with real CPU but
some VM environment.

Fix kern/54655 and confirmed by msaitoh@.

Needs pullup to netbsd-9.


To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/sys/arch/x86/x86/cpu_rng.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/cpu_rng.c
diff -u src/sys/arch/x86/x86/cpu_rng.c:1.9 src/sys/arch/x86/x86/cpu_rng.c:1.10
--- src/sys/arch/x86/x86/cpu_rng.c:1.9	Wed Aug 22 12:07:43 2018
+++ src/sys/arch/x86/x86/cpu_rng.c	Fri Nov  1 15:01:27 2019
@@ -1,4 +1,4 @@
-/* $NetBSD: cpu_rng.c,v 1.9 2018/08/22 12:07:43 maxv Exp $ */
+/* $NetBSD: cpu_rng.c,v 1.10 2019/11/01 15:01:27 taca Exp $ */
 
 /*-
  * Copyright (c) 2015 The NetBSD Foundation, Inc.
@@ -53,6 +53,8 @@ static enum {
 	CPU_RNG_VIA
 } cpu_rng_mode __read_mostly = CPU_RNG_NONE;
 
+static bool has_rdrand;
+
 bool
 cpu_rng_init(void)
 {
@@ -131,7 +133,10 @@ cpu_rng_rdseed(cpu_rng_t *out)
 	 * to be seeded even in this case.
 	 */
 exhausted:
-	return cpu_rng_rdrand(out);
+	if (has_rdrand)
+		return cpu_rng_rdrand(out);
+	else
+		return 0;
 }
 
 static size_t
@@ -213,7 +218,7 @@ cpu_earlyrng(void *out, size_t sz)
 	int i;
 
 	bool has_rdseed = (cpu_feature[5] & CPUID_SEF_RDSEED) != 0;
-	bool has_rdrand = (cpu_feature[1] & CPUID2_RDRAND) != 0;
+	has_rdrand = (cpu_feature[1] & CPUID2_RDRAND) != 0;
 
 	KASSERT(sz + sizeof(uint64_t) <= SHA512_DIGEST_LENGTH);
 

Reply via email to