From patchwork Mon Sep 21 07:58:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolai Stange X-Patchwork-Id: 252972 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80883C43465 for ; Mon, 21 Sep 2020 08:00:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 54CA0214F1 for ; Mon, 21 Sep 2020 08:00:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726620AbgIUH7u (ORCPT ); Mon, 21 Sep 2020 03:59:50 -0400 Received: from mx2.suse.de ([195.135.220.15]:57622 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726524AbgIUH71 (ORCPT ); Mon, 21 Sep 2020 03:59:27 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 2A891B510; Mon, 21 Sep 2020 08:00:00 +0000 (UTC) From: Nicolai Stange To: "Theodore Y. Ts'o" Cc: linux-crypto@vger.kernel.org, LKML , Arnd Bergmann , Greg Kroah-Hartman , "Eric W. Biederman" , "Alexander E. Patrakov" , "Ahmed S. Darwish" , Willy Tarreau , Matthew Garrett , Vito Caputo , Andreas Dilger , Jan Kara , Ray Strode , William Jon McCann , zhangjs , Andy Lutomirski , Florian Weimer , Lennart Poettering , Peter Matthias , Marcelo Henrique Cerri , Roman Drahtmueller , Neil Horman , Randy Dunlap , Julia Lawall , Dan Carpenter , Andy Lavr , Eric Biggers , "Jason A. Donenfeld" , =?utf-8?q?Stephan_M=C3=BCller?= , Torsten Duwe , Petr Tesarik , Nicolai Stange Subject: [RFC PATCH 19/41] random: reintroduce arch_has_random() + arch_has_random_seed() Date: Mon, 21 Sep 2020 09:58:35 +0200 Message-Id: <20200921075857.4424-20-nstange@suse.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200921075857.4424-1-nstange@suse.de> References: <20200921075857.4424-1-nstange@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org A future patch will introduce support for making up for a certain amount of lacking entropy in crng_reseed() by means of arch_get_random_long() or arch_get_random_seed_long() respectively. However, before even the tiniest bit of precious entropy is withdrawn from the input_pool, it should be checked if whether the current arch even has support for these. Reintroduce arch_has_random() + arch_has_random_seed() and implement them for arm64, powerpc, s390 and x86 as appropriate (yeah, I know this should go in separate commits, but this is part of a RFC series). Note that this more or less reverts commits 647f50d5d9d9 ("linux/random.h: Remove arch_has_random, arch_has_random_seed") cbac004995a0 ("powerpc: Remove arch_has_random, arch_has_random_seed") 5e054c820f59 ("s390: Remove arch_has_random, arch_has_random_seed") 5f2ed7f5b99b ("x86: Remove arch_has_random, arch_has_random_seed") Signed-off-by: Nicolai Stange --- arch/arm64/include/asm/archrandom.h | 25 ++++++++++++++++++------- arch/powerpc/include/asm/archrandom.h | 12 +++++++++++- arch/s390/include/asm/archrandom.h | 14 ++++++++++++-- arch/x86/include/asm/archrandom.h | 18 ++++++++++++++---- include/linux/random.h | 8 ++++++++ 5 files changed, 63 insertions(+), 14 deletions(-) diff --git a/arch/arm64/include/asm/archrandom.h b/arch/arm64/include/asm/archrandom.h index 44209f6146aa..055d18713db7 100644 --- a/arch/arm64/include/asm/archrandom.h +++ b/arch/arm64/include/asm/archrandom.h @@ -26,17 +26,13 @@ static inline bool __arm64_rndr(unsigned long *v) return ok; } -static inline bool __must_check arch_get_random_long(unsigned long *v) -{ - return false; -} -static inline bool __must_check arch_get_random_int(unsigned int *v) +static inline bool arch_has_random(void) { return false; } -static inline bool __must_check arch_get_random_seed_long(unsigned long *v) +static inline bool arch_has_random_seed(void) { /* * Only support the generic interface after we have detected @@ -44,7 +40,22 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v) * cpufeature code and with potential scheduling between CPUs * with and without the feature. */ - if (!cpus_have_const_cap(ARM64_HAS_RNG)) + return cpus_have_const_cap(ARM64_HAS_RNG); +} + +static inline bool __must_check arch_get_random_long(unsigned long *v) +{ + return false; +} + +static inline bool __must_check arch_get_random_int(unsigned int *v) +{ + return false; +} + +static inline bool __must_check arch_get_random_seed_long(unsigned long *v) +{ + if (!arch_has_random_seed()) return false; return __arm64_rndr(v); diff --git a/arch/powerpc/include/asm/archrandom.h b/arch/powerpc/include/asm/archrandom.h index 9a53e29680f4..47c2d74e7244 100644 --- a/arch/powerpc/include/asm/archrandom.h +++ b/arch/powerpc/include/asm/archrandom.h @@ -6,6 +6,16 @@ #include +static inline bool arch_has_random(void) +{ + return false; +} + +static inline bool arch_has_random_seed(void) +{ + return ppc_md.get_random_seed; +} + static inline bool __must_check arch_get_random_long(unsigned long *v) { return false; @@ -18,7 +28,7 @@ static inline bool __must_check arch_get_random_int(unsigned int *v) static inline bool __must_check arch_get_random_seed_long(unsigned long *v) { - if (ppc_md.get_random_seed) + if (arch_has_random_seed()) return ppc_md.get_random_seed(v); return false; diff --git a/arch/s390/include/asm/archrandom.h b/arch/s390/include/asm/archrandom.h index de61ce562052..18973845634c 100644 --- a/arch/s390/include/asm/archrandom.h +++ b/arch/s390/include/asm/archrandom.h @@ -21,6 +21,16 @@ extern atomic64_t s390_arch_random_counter; bool s390_arch_random_generate(u8 *buf, unsigned int nbytes); +static inline bool arch_has_random(void) +{ + return false; +} + +static inline bool arch_has_random_seed(void) +{ + return static_branch_likely(&s390_arch_random_available); +} + static inline bool __must_check arch_get_random_long(unsigned long *v) { return false; @@ -33,7 +43,7 @@ static inline bool __must_check arch_get_random_int(unsigned int *v) static inline bool __must_check arch_get_random_seed_long(unsigned long *v) { - if (static_branch_likely(&s390_arch_random_available)) { + if (arch_has_random_seed()) { return s390_arch_random_generate((u8 *)v, sizeof(*v)); } return false; @@ -41,7 +51,7 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v) static inline bool __must_check arch_get_random_seed_int(unsigned int *v) { - if (static_branch_likely(&s390_arch_random_available)) { + if (arch_has_random_seed()) { return s390_arch_random_generate((u8 *)v, sizeof(*v)); } return false; diff --git a/arch/x86/include/asm/archrandom.h b/arch/x86/include/asm/archrandom.h index ebc248e49549..030f46c9e310 100644 --- a/arch/x86/include/asm/archrandom.h +++ b/arch/x86/include/asm/archrandom.h @@ -70,24 +70,34 @@ static inline bool __must_check rdseed_int(unsigned int *v) */ #ifdef CONFIG_ARCH_RANDOM +static inline bool arch_has_random(void) +{ + return static_cpu_has(X86_FEATURE_RDRAND); +} + +static inline bool arch_has_random_seed(void) +{ + return static_cpu_has(X86_FEATURE_RDSEED); +} + static inline bool __must_check arch_get_random_long(unsigned long *v) { - return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_long(v) : false; + return arch_has_random() ? rdrand_long(v) : false; } static inline bool __must_check arch_get_random_int(unsigned int *v) { - return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_int(v) : false; + return arch_has_random() ? rdrand_int(v) : false; } static inline bool __must_check arch_get_random_seed_long(unsigned long *v) { - return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_long(v) : false; + return arch_has_random_seed() ? rdseed_long(v) : false; } static inline bool __must_check arch_get_random_seed_int(unsigned int *v) { - return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_int(v) : false; + return arch_has_random_seed() ? rdseed_int(v) : false; } extern void x86_init_rdrand(struct cpuinfo_x86 *c); diff --git a/include/linux/random.h b/include/linux/random.h index f45b8be3e3c4..d4653422a0c7 100644 --- a/include/linux/random.h +++ b/include/linux/random.h @@ -120,6 +120,14 @@ unsigned long randomize_page(unsigned long start, unsigned long range); #ifdef CONFIG_ARCH_RANDOM # include #else +static inline bool arch_has_random(void) +{ + return false; +} +static inline bool arch_has_random_seed(void) +{ + return false; +} static inline bool __must_check arch_get_random_long(unsigned long *v) { return false;