Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6129124iog; Thu, 23 Jun 2022 11:58:40 -0700 (PDT) X-Google-Smtp-Source: AGRyM1trEcoNEyjzYpWbOOCKRIL8f4E5u8KVEF9T57nYPYsIQWhhYcX+DPVFM1ojsEJE0WuAqo8X X-Received: by 2002:a17:907:a40f:b0:71c:3ffc:530a with SMTP id sg15-20020a170907a40f00b0071c3ffc530amr9928862ejc.672.1656010720391; Thu, 23 Jun 2022 11:58:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656010720; cv=none; d=google.com; s=arc-20160816; b=V9tsGTq8H2ocHgwb8x9HBemLxRdmJ5MI4du7BRFr0EtV70Ld9AkO1GK0gTwdU61FDs THpQEjR+8Z/f0yoYOlqCkaelnwYD6KGg/6L/ZpO14MafIuxQbWtRUsMqcy55KjJjW45I 1m6BIhRU1b0GPmAxDAQDzpDaI9hP89qy/QvhDCQcgsqOTcjdiw0uoRQzJwW9yVDqYo+q Oz6jhy54cAYrcrahtQYfpzBbnYRIKgpwG1jvIraaQzDp4r5rvG9M0d4pzySvYkGJYobj zqWNNDG1WHlF0gFD9ZjMCLmG8Bc2Z2Cj7PsTVXREGpN8di5UMuLNIvyc22L+sHd1FTAm 2Knw== 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=XwHqgnkMRPxpqL08gDE8wWsk7hXrSwTWBlEOacVhORQ=; b=pW5gJArel1oVFAW1/ODU+guC2nx8HGJFvmlRC0rqYXFRkkN+O1fvvNTGNB23r52mDH bagMNF0co6P2WIfqHF1aooSp2DUGqvPJ2JGfJu6hUB/tPh3q5aebiOUgoBy7s0JNyGKd 1u8BTz1R09mBI6D4/gMvCk2HOjT7pBex1o33LIx5TDDKOEQBnybODQc5v76TU4SXEBTj sVQ/04mLt9wXetYWJBp2AyW0ChyCVpHmhkK7WQynSfwO7og592z98O6g4IlxRucUrxYZ zXTyXGPifZIXOcSm17AMEHD87ZmK4h6tZbxxatnjJyvfCY9HzvvpfU5c+DGC+7Y2HH1/ 1XrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=egnxixb5; 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 rl24-20020a170907217800b00718c1dc910bsi3599928ejb.719.2022.06.23.11.58.13; Thu, 23 Jun 2022 11:58:40 -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=egnxixb5; 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 S232530AbiFWRAL (ORCPT + 99 others); Thu, 23 Jun 2022 13:00:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232985AbiFWQuA (ORCPT ); Thu, 23 Jun 2022 12:50:00 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40CF4B879; Thu, 23 Jun 2022 09:48:05 -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 ams.source.kernel.org (Postfix) with ESMTPS id B71E8B82493; Thu, 23 Jun 2022 16:48:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1885DC3411B; Thu, 23 Jun 2022 16:48:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656002882; bh=gQpeCEp3qHtpD3/DJHsBlr7qJreT5DuqSKFRftgTxlY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=egnxixb5QQsE8mGFvwMZw+zMA2W2laxBNyJYOGZ/J16oT9J3OQlsY4KgGrpGB2Whs MPjOocHXKXxkpIUTa2FymQZ0YGVl5s07uJWSH0Q6hCkgGOZua6+UHTgsV95xgw57Nb f3rg4J/d7HqDQlCSlzoD9X1yYw2gUBbHGcZOmU3g= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kees Cook , Theodore Tso , "Jason A. Donenfeld" Subject: [PATCH 4.9 040/264] random: move rand_initialize() earlier Date: Thu, 23 Jun 2022 18:40:33 +0200 Message-Id: <20220623164345.203575295@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164344.053938039@linuxfoundation.org> References: <20220623164344.053938039@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 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: Kees Cook commit d55535232c3dbde9a523a9d10d68670f5fe5dec3 upstream. Right now rand_initialize() is run as an early_initcall(), but it only depends on timekeeping_init() (for mixing ktime_get_real() into the pools). However, the call to boot_init_stack_canary() for stack canary initialization runs earlier, which triggers a warning at boot: random: get_random_bytes called from start_kernel+0x357/0x548 with crng_init=0 Instead, this moves rand_initialize() to after timekeeping_init(), and moves canary initialization here as well. Note that this warning may still remain for machines that do not have UEFI RNG support (which initializes the RNG pools during setup_arch()), or for x86 machines without RDRAND (or booting without "random.trust=on" or CONFIG_RANDOM_TRUST_CPU=y). Signed-off-by: Kees Cook Signed-off-by: Theodore Ts'o Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 5 ++--- include/linux/random.h | 1 + init/main.c | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1842,7 +1842,7 @@ EXPORT_SYMBOL(get_random_bytes_arch); * data into the pool to prepare it for use. The pool is not cleared * as that can only decrease the entropy in the pool. */ -static void init_std_data(struct entropy_store *r) +static void __init init_std_data(struct entropy_store *r) { int i; ktime_t now = ktime_get_real(); @@ -1869,7 +1869,7 @@ static void init_std_data(struct entropy * take care not to overwrite the precious per platform data * we were given. */ -static int rand_initialize(void) +int __init rand_initialize(void) { init_std_data(&input_pool); init_std_data(&blocking_pool); @@ -1881,7 +1881,6 @@ static int rand_initialize(void) } return 0; } -early_initcall(rand_initialize); #ifdef CONFIG_BLOCK void rand_initialize_disk(struct gendisk *disk) --- a/include/linux/random.h +++ b/include/linux/random.h @@ -35,6 +35,7 @@ extern void add_interrupt_randomness(int extern void get_random_bytes(void *buf, int nbytes); extern int wait_for_random_bytes(void); +extern int __init rand_initialize(void); extern bool rng_is_initialized(void); extern int add_random_ready_callback(struct random_ready_callback *rdy); extern void del_random_ready_callback(struct random_ready_callback *rdy); --- a/init/main.c +++ b/init/main.c @@ -570,6 +570,20 @@ asmlinkage __visible void __init start_k hrtimers_init(); softirq_init(); timekeeping_init(); + + /* + * For best initial stack canary entropy, prepare it after: + * - setup_arch() for any UEFI RNG entropy and boot cmdline access + * - timekeeping_init() for ktime entropy used in rand_initialize() + * - rand_initialize() to get any arch-specific entropy like RDRAND + * - add_latent_entropy() to get any latent entropy + * - adding command line entropy + */ + rand_initialize(); + add_latent_entropy(); + add_device_randomness(command_line, strlen(command_line)); + boot_init_stack_canary(); + time_init(); sched_clock_postinit(); printk_nmi_init();