From 165cffb9086e47d9b2d43985f14ffe0fec677056 Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Fri, 9 Aug 2019 03:40:22 +0000 Subject: [PATCH 2/4] Simplify Intel RDRAND/RDSEED and VIA C3 RNG API. Push it all into MD x86 code to keep it simpler, until we have other examples on other CPUs. Simplify RDSEED-to-RDRAND fallback. Eliminate cpu_earlyrng in favour of just using entropy_extract, which is available early now. --- sys/arch/amd64/amd64/machdep.c | 2 + sys/arch/i386/i386/machdep.c | 2 + sys/arch/x86/include/cpu_rng.h | 8 +- sys/arch/x86/x86/cpu_rng.c | 166 ++++++++++++++++----------------- sys/arch/x86/x86/pmap.c | 6 +- 5 files changed, 91 insertions(+), 93 deletions(-) diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index d8653f7f69e5..49abd090a797 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -167,6 +167,7 @@ __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.342 2019/12/06 08:35:21 maxv Exp $"); #include #include +#include #include #include #include @@ -1689,6 +1690,7 @@ init_x86_64(paddr_t first_avail) uvm_lwp_setuarea(&lwp0, lwp0uarea); cpu_probe(&cpu_info_primary); + cpu_rng_init(); #ifdef SVS svs_init(); #endif diff --git a/sys/arch/i386/i386/machdep.c b/sys/arch/i386/i386/machdep.c index edf47a211f73..fb67f4ada2f8 100644 --- a/sys/arch/i386/i386/machdep.c +++ b/sys/arch/i386/i386/machdep.c @@ -122,6 +122,7 @@ __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.823 2019/10/18 01:38:28 manu Exp $"); #include #include +#include #include #include #include @@ -1149,6 +1150,7 @@ init386(paddr_t first_avail) uvm_lwp_setuarea(&lwp0, lwp0uarea); cpu_probe(&cpu_info_primary); + cpu_rng_init(); cpu_init_msrs(&cpu_info_primary, true); #ifndef XEN cpu_speculation_init(&cpu_info_primary); diff --git a/sys/arch/x86/include/cpu_rng.h b/sys/arch/x86/include/cpu_rng.h index 5a108e55216f..d6e96dc5393f 100644 --- a/sys/arch/x86/include/cpu_rng.h +++ b/sys/arch/x86/include/cpu_rng.h @@ -32,12 +32,6 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include - -typedef uint64_t cpu_rng_t; - -bool cpu_rng_init(void); -size_t cpu_rng(cpu_rng_t *); -void cpu_earlyrng(void *, size_t); +void cpu_rng_init(void); #endif /* _X86_CPU_RNG_H_ */ diff --git a/sys/arch/x86/x86/cpu_rng.c b/sys/arch/x86/x86/cpu_rng.c index 47ebaecbdc75..45f1a433551a 100644 --- a/sys/arch/x86/x86/cpu_rng.c +++ b/sys/arch/x86/x86/cpu_rng.c @@ -38,6 +38,9 @@ #include #include #include +#include +#include +#include #include #include @@ -45,41 +48,51 @@ #include #include #include +#include -static enum { +static enum cpu_rng_mode { CPU_RNG_NONE = 0, CPU_RNG_RDRAND, CPU_RNG_RDSEED, + CPU_RNG_RDSEED_RDRAND, CPU_RNG_VIA } cpu_rng_mode __read_mostly = CPU_RNG_NONE; -static bool has_rdrand; +static const char *const cpu_rng_name[] = { + [CPU_RNG_RDRAND] = "rdrand", + [CPU_RNG_RDSEED] = "rdseed", + [CPU_RNG_RDSEED_RDRAND] = "rdrand/rdseed", + [CPU_RNG_VIA] = "via", +}; -bool -cpu_rng_init(void) -{ +static struct krndsource cpu_rng_source __read_mostly; - if (cpu_feature[5] & CPUID_SEF_RDSEED) { - cpu_rng_mode = CPU_RNG_RDSEED; - aprint_normal("cpu_rng: RDSEED\n"); - return true; - } else if (cpu_feature[1] & CPUID2_RDRAND) { - cpu_rng_mode = CPU_RNG_RDRAND; - aprint_normal("cpu_rng: RDRAND\n"); - return true; - } else if (cpu_feature[4] & CPUID_VIA_HAS_RNG) { - cpu_rng_mode = CPU_RNG_VIA; - aprint_normal("cpu_rng: VIA\n"); - return true; - } - return false; +static enum cpu_rng_mode +cpu_rng_detect(void) +{ + bool has_rdseed = (cpu_feature[5] & CPUID_SEF_RDSEED); + bool has_rdrand = (cpu_feature[1] & CPUID2_RDRAND); + bool has_viarng = (cpu_feature[4] & CPUID_VIA_HAS_RNG); + + if (has_rdseed && has_rdrand) + return CPU_RNG_RDSEED_RDRAND; + else if (has_rdseed) + return CPU_RNG_RDSEED; + else if (has_rdrand) + return CPU_RNG_RDRAND; + else if (has_viarng) + return CPU_RNG_VIA; + else + return CPU_RNG_NONE; } static size_t -cpu_rng_rdrand(cpu_rng_t *out) +cpu_rng_rdrand(uint64_t *out) { uint8_t rndsts; + /* XXX Intel sez to try up to 10 times before giving up. */ + #ifdef __i386__ uint32_t lo, hi; @@ -102,7 +115,7 @@ cpu_rng_rdrand(cpu_rng_t *out) } static size_t -cpu_rng_rdseed(cpu_rng_t *out) +cpu_rng_rdseed(uint64_t *out) { uint8_t rndsts; @@ -111,10 +124,10 @@ cpu_rng_rdseed(cpu_rng_t *out) __asm __volatile("rdseed %0; setc %1" : "=r"(lo), "=qm"(rndsts)); if (rndsts != 1) - goto exhausted; + return 0; __asm __volatile("rdseed %0; setc %1" : "=r"(hi), "=qm"(rndsts)); if (rndsts != 1) - goto exhausted; + return 0; *out = (uint64_t)lo | ((uint64_t)hi << 32); explicit_memset(&lo, 0, sizeof(lo)); @@ -123,24 +136,24 @@ cpu_rng_rdseed(cpu_rng_t *out) __asm __volatile("rdseed %0; setc %1" : "=r"(*out), "=qm"(rndsts)); #endif if (rndsts != 1) - goto exhausted; + return 0; return sizeof(*out) * NBBY; +} - /* - * Userspace could have exhausted RDSEED, but the - * CPU-internal generator feeding RDRAND is guaranteed - * to be seeded even in this case. - */ -exhausted: - if (has_rdrand) - return cpu_rng_rdrand(out); - else - return 0; +static size_t +cpu_rng_rdseed_rdrand(uint64_t *out) +{ + size_t n = cpu_rng_rdseed(out); + + if (n == 0) + n = cpu_rng_rdrand(out); + + return n; } static size_t -cpu_rng_via(cpu_rng_t *out) +cpu_rng_via(uint64_t *out) { uint32_t creg0, rndsts; @@ -179,72 +192,59 @@ cpu_rng_via(cpu_rng_t *out) * 0.75 bits of entropy per output bit and advises users to * be "even more conservative". */ - return rndsts & 0xf ? 0 : sizeof(cpu_rng_t) * NBBY / 2; + return (rndsts & 0xf) ? 0 : sizeof(uint64_t) * NBBY/2; } -size_t -cpu_rng(cpu_rng_t *out) +static size_t +cpu_rng(enum cpu_rng_mode mode, uint64_t *out) { - switch (cpu_rng_mode) { + switch (mode) { case CPU_RNG_NONE: return 0; case CPU_RNG_RDSEED: return cpu_rng_rdseed(out); case CPU_RNG_RDRAND: return cpu_rng_rdrand(out); + case CPU_RNG_RDSEED_RDRAND: + return cpu_rng_rdseed_rdrand(out); case CPU_RNG_VIA: return cpu_rng_via(out); default: - panic("cpu_rng: unknown mode %d", (int)cpu_rng_mode); + panic("cpu_rng: unknown mode %d", (int)mode); } } -/* -------------------------------------------------------------------------- */ - -static uint64_t earlyrng_state; - -/* - * Small PRNG, that can be used very early. The only requirement is that - * cpu_probe got called before. - */ -void __noasan -cpu_earlyrng(void *out, size_t sz) +static void +cpu_rng_get(size_t nbytes, void *cookie) { - uint8_t digest[SHA512_DIGEST_LENGTH]; - SHA512_CTX ctx; - cpu_rng_t buf[8]; - uint64_t val; - int i; - - bool has_rdseed = (cpu_feature[5] & CPUID_SEF_RDSEED) != 0; - has_rdrand = (cpu_feature[1] & CPUID2_RDRAND) != 0; - - KASSERT(sz + sizeof(uint64_t) <= SHA512_DIGEST_LENGTH); - - SHA512_Init(&ctx); - - SHA512_Update(&ctx, (uint8_t *)&earlyrng_state, sizeof(earlyrng_state)); - if (has_rdseed) { - for (i = 0; i < 8; i++) { - if (cpu_rng_rdseed(&buf[i]) == 0) { - break; - } - } - SHA512_Update(&ctx, (uint8_t *)buf, i * sizeof(cpu_rng_t)); - } else if (has_rdrand) { - for (i = 0; i < 8; i++) { - if (cpu_rng_rdrand(&buf[i]) == 0) { - break; - } - } - SHA512_Update(&ctx, (uint8_t *)buf, i * sizeof(cpu_rng_t)); + const unsigned N = howmany(RND_POOLBITS, 64); + uint64_t buf[2*N]; + unsigned i, nbits = 0; + + for (i = 0; i < N; i++) + nbits += cpu_rng(cpu_rng_mode, &buf[i]); + for (; i < 2*N; i++) + (void)cpu_rng(cpu_rng_mode, &buf[i]); + + if (consttime_memequal(buf, buf + N, N)) { + printf("cpu_rng %s: failed repetition test\n", + cpu_rng_name[cpu_rng_mode]); + nbits = 0; } - val = rdtsc(); - SHA512_Update(&ctx, (uint8_t *)&val, sizeof(val)); - SHA512_Final(digest, &ctx); + rnd_add_data_sync(&cpu_rng_source, buf, sizeof buf, nbits); +} + +void +cpu_rng_init(void) +{ - memcpy(out, digest, sz); - memcpy(&earlyrng_state, &digest[sz], sizeof(earlyrng_state)); + cpu_rng_mode = cpu_rng_detect(); + if (cpu_rng_mode == CPU_RNG_NONE) + return; + aprint_normal("cpu_rng: %s\n", cpu_rng_name[cpu_rng_mode]); + rndsource_setcb(&cpu_rng_source, cpu_rng_get, NULL); + rnd_attach_source(&cpu_rng_source, cpu_rng_name[cpu_rng_mode], + RND_TYPE_RNG, RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB); } diff --git a/sys/arch/x86/x86/pmap.c b/sys/arch/x86/x86/pmap.c index 8c76daf9fd38..120eea5c7266 100644 --- a/sys/arch/x86/x86/pmap.c +++ b/sys/arch/x86/x86/pmap.c @@ -151,6 +151,7 @@ __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.343 2019/12/08 20:42:48 ad Exp $"); #include #include #include +#include #include #include @@ -162,7 +163,6 @@ __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.343 2019/12/08 20:42:48 ad Exp $"); #include #include #include -#include #include #include @@ -1358,7 +1358,7 @@ slotspace_rand(int type, size_t sz, size_t align) } /* Select a hole. */ - cpu_earlyrng(&hole, sizeof(hole)); + entropy_extract(&hole, sizeof(hole), 0); #ifdef NO_X86_ASLR hole = 0; #endif @@ -1368,7 +1368,7 @@ slotspace_rand(int type, size_t sz, size_t align) startva = VA_SIGN_NEG(startsl * NBPD_L4); /* Select an area within the hole. */ - cpu_earlyrng(&va, sizeof(va)); + entropy_extract(&va, sizeof(va), 0); #ifdef NO_X86_ASLR va = 0; #endif -- 2.19.1