Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2887078rwb; Mon, 15 Aug 2022 13:20:00 -0700 (PDT) X-Google-Smtp-Source: AA6agR5vU8DahXHr5+zIQ1BzgDLdP2/oB2THbc+lLE20JX91DJAPkQ3OeBzeQ3B003PibrLe7vL6 X-Received: by 2002:a17:907:7256:b0:730:f074:963c with SMTP id ds22-20020a170907725600b00730f074963cmr11634231ejc.201.1660594717193; Mon, 15 Aug 2022 13:18:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660594717; cv=none; d=google.com; s=arc-20160816; b=BDMv7+j2Xvr4LzR9o99+9zahM19GqrS4whOGhKskR7vcdWGPIhymCyR2pGZI4dy18z dYbhydSU3Wb9ILQe8eF+VuGy3ZpdCBf5Od4txufiR3WH0dyqqfSQN8yo8PQflXPmJhu8 vYqfOuhAeRMw5N+o+WfzQ0nws7oRbwwa6jw3aPzfL7YE9JOXK2odSbExV7FlIP88euqd /WFo+uldKfVT0dYHieuDx810u4knjWYXg1MEAP66VcKg6DQqYR9K61lOoO4KmAsLlz/x wQR2dLB1d51g8kCnHnUDrEFRrj38R0EfJZ9I+1dStSaiOPpuSZ0WnySCXiP21PjIgiu3 85Nw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=A4h3BrmxO7w5qVwxVP+RYB3FDWZg2iVmqpJWRwID5DE=; b=sPGQ3xkhFmoQHPTZbFakleDHZBITVqOX611qFdW82deceUZjA4thGkWxe5ZZO6dQPv T9G6jcVobDr5f8KiXNZTDic6cIAam6uNabjtpbUgmIkdKRgEiLJWumR7XWpHbYcFJaeq wRV2i6mhgtXNSm+LCSgc+J7GbfIVcg0ZJUA8V7CP/O/9Xc29gjS8OizlgHioFpE1QNbn 0KAO1yvlV1UjE1NgXYwn3KeD/rYnMvf+7+plFFMVaqhmRIdh0UZtz2ppplPIkihY7+gk on26TDHT2hZxRt69FVAi4zEDKUwKOFpgL/3C3rvwMYlWQsUXiMjr7nvculJjusrE0wcJ CGEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=SodHEgrP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mp31-20020a1709071b1f00b0072affa39083si8050120ejc.23.2022.08.15.13.18.10; Mon, 15 Aug 2022 13:18:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=SodHEgrP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240858AbiHOSVm (ORCPT + 99 others); Mon, 15 Aug 2022 14:21:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240525AbiHOSUg (ORCPT ); Mon, 15 Aug 2022 14:20:36 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC5372C649; Mon, 15 Aug 2022 11:16:44 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 470CD612A8; Mon, 15 Aug 2022 18:16:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4E0E6C433D7; Mon, 15 Aug 2022 18:16:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660587403; bh=yeoemor4Uw73NOtS6LeNA/ED035gV3a0eFSZ4fQXmWI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SodHEgrPNzSsMgDyLGPqUeRA23ATUSu5nOeGnK0aU0a3c/qB+fKuMHlqSEgeY5Dvf N62X7/8YcS/PMbdpyOysTz/7fZ6OIdBhs3DxeLWr6sJKTVFaAMgzeu8xCRdvrEAjSs thqmLRDj+AHuLKBWmqy364D7IuHtqXQI/Bf5WlTs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Johannes Berg , Anton Ivanov , "Jason A. Donenfeld" Subject: [PATCH 5.15 075/779] um: seed rng using host OS rng Date: Mon, 15 Aug 2022 19:55:19 +0200 Message-Id: <20220815180340.499355405@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180337.130757997@linuxfoundation.org> References: <20220815180337.130757997@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jason A. Donenfeld commit 0b9ba6135d7f18b82f3d8bebb55ded725ba88e0e upstream. UML generally does not provide access to special CPU instructions like RDRAND, and execution tends to be rather deterministic, with no real hardware interrupts, making good randomness really very hard, if not all together impossible. Not only is this a security eyebrow raiser, but it's also quite annoying when trying to do various pieces of UML-based automation that takes a long time to boot, if ever. Fix this by trivially calling getrandom() in the host and using that seed as "bootloader randomness", which initializes the rng immediately at UML boot. The old behavior can be restored the same way as on any other arch, by way of CONFIG_TRUST_BOOTLOADER_RANDOMNESS=n or random.trust_bootloader=0. So seen from that perspective, this just makes UML act like other archs, which is positive in its own right. Additionally, wire up arch_get_random_{int,long}() in the same way, so that reseeds can also make use of the host RNG, controllable by CONFIG_TRUST_CPU_RANDOMNESS and random.trust_cpu, per usual. Cc: stable@vger.kernel.org Acked-by: Johannes Berg Acked-By: Anton Ivanov Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- arch/um/include/asm/archrandom.h | 30 ++++++++++++++++++++++++++++++ arch/um/include/shared/os.h | 7 +++++++ arch/um/kernel/um_arch.c | 8 ++++++++ arch/um/os-Linux/util.c | 6 ++++++ 4 files changed, 51 insertions(+) create mode 100644 arch/um/include/asm/archrandom.h --- /dev/null +++ b/arch/um/include/asm/archrandom.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ASM_UM_ARCHRANDOM_H__ +#define __ASM_UM_ARCHRANDOM_H__ + +#include + +/* This is from , but better not to #include that in a global header here. */ +ssize_t os_getrandom(void *buf, size_t len, unsigned int flags); + +static inline bool __must_check arch_get_random_long(unsigned long *v) +{ + return os_getrandom(v, sizeof(*v), 0) == sizeof(*v); +} + +static inline bool __must_check arch_get_random_int(unsigned int *v) +{ + return os_getrandom(v, sizeof(*v), 0) == sizeof(*v); +} + +static inline bool __must_check arch_get_random_seed_long(unsigned long *v) +{ + return false; +} + +static inline bool __must_check arch_get_random_seed_int(unsigned int *v) +{ + return false; +} + +#endif --- a/arch/um/include/shared/os.h +++ b/arch/um/include/shared/os.h @@ -11,6 +11,12 @@ #include #include #include +/* This is to get size_t */ +#ifndef __UM_HOST__ +#include +#else +#include +#endif #define CATCH_EINTR(expr) while ((errno = 0, ((expr) < 0)) && (errno == EINTR)) @@ -252,6 +258,7 @@ extern void stack_protections(unsigned l extern int raw(int fd); extern void setup_machinename(char *machine_out); extern void setup_hostinfo(char *buf, int len); +extern ssize_t os_getrandom(void *buf, size_t len, unsigned int flags); extern void os_dump_core(void) __attribute__ ((noreturn)); extern void um_early_printk(const char *s, unsigned int n); extern void os_fix_helper_signals(void); --- a/arch/um/kernel/um_arch.c +++ b/arch/um/kernel/um_arch.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -404,6 +405,8 @@ int __init __weak read_initrd(void) void __init setup_arch(char **cmdline_p) { + u8 rng_seed[32]; + stack_protections((unsigned long) &init_thread_info); setup_physmem(uml_physmem, uml_reserved, physmem_size, highmem); mem_total_pages(physmem_size, iomem_size, highmem); @@ -413,6 +416,11 @@ void __init setup_arch(char **cmdline_p) strlcpy(boot_command_line, command_line, COMMAND_LINE_SIZE); *cmdline_p = command_line; setup_hostinfo(host_info, sizeof host_info); + + if (os_getrandom(rng_seed, sizeof(rng_seed), 0) == sizeof(rng_seed)) { + add_bootloader_randomness(rng_seed, sizeof(rng_seed)); + memzero_explicit(rng_seed, sizeof(rng_seed)); + } } void __init check_bugs(void) --- a/arch/um/os-Linux/util.c +++ b/arch/um/os-Linux/util.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -96,6 +97,11 @@ static inline void __attribute__ ((noret exit(127); } +ssize_t os_getrandom(void *buf, size_t len, unsigned int flags) +{ + return getrandom(buf, len, flags); +} + /* * UML helper threads must not handle SIGWINCH/INT/TERM */