Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6146099iog; Thu, 23 Jun 2022 12:17:44 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tKAY24G5rcVZmGKZmbtACr5LG+qwGCCQfKIbYCpkoChc65q2r8h7m0sW8ezWIyR3ps3HbA X-Received: by 2002:a63:f202:0:b0:40d:7d34:aecf with SMTP id v2-20020a63f202000000b0040d7d34aecfmr472659pgh.247.1656011864043; Thu, 23 Jun 2022 12:17:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011864; cv=none; d=google.com; s=arc-20160816; b=e7HLozbPdD7tkpdN0/MxjoBdoNyuGtoMNUjkNKQUHb/oXQH/cQ/F8ZJltwGTxHJJ2H Y49EuYpTNA+TfrQGM5QiQ/EmpFfonrrKS7hGhhSpVmyxOnPSGNd7/773DLbnNA9pL6az mN6LZs0Vhmxykq84czHyv/Dr6kE2vfXD/H2FfXLPwokUCaNX5Nd2giiE8C+Rn83VMew8 gaIQvvJYkpQm9IJpXw8H7V82jXRDaFTIBG5QKiu3MJM82eBILI2KCLI8GbDFetuNw5iA jPRnCDYllhvoqh/WcgCeL7BIFEAghdba+YjOsbOLi1JkLns7Hcc6zfH2OACkCv84Jenk Ez/g== 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=Y1kw8sWi5CZkRTwajq+ZRGBpx8vyhK97R6tXJ/ZDJuI=; b=fzR2iX2OYxEk4nbiR6VsXjJG77+v9RWDy54VRmEB8iUkoqiIsXUiLdhO/i60lh8agN gYW1NAYaICDKbzue9Y39sWomeOOasXmAbXpjtb7nSgbzZ/vNv2Wcst3kTdxOQjSEUiVO +4fBwlr4XrMdVmPraPecAUMDVfagRvnXLf/eG6ZLhHkoCp3O38c/OX97hZStj9LG1F1e 6v/sKiQkifzOZulUMQUb4v9gCbc1YNwp34KGjPQHMjonoDO6ObhVWEpNgGoBEtgbVfjs vn/pOeczS976RR465jWIaaYj6cswoYVE2nPJUXRWQY0HQYctbcVk1KGioVTONnBQG4nc 6/kQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=VvJUt9TR; 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 pi4-20020a17090b1e4400b001ec8c9341basi4314947pjb.134.2022.06.23.12.17.31; Thu, 23 Jun 2022 12:17:44 -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=VvJUt9TR; 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 S230042AbiFWRG7 (ORCPT + 99 others); Thu, 23 Jun 2022 13:06:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229584AbiFWRFq (ORCPT ); Thu, 23 Jun 2022 13:05:46 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 673F4515B0; Thu, 23 Jun 2022 09:55:37 -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 4229760B29; Thu, 23 Jun 2022 16:55:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 24E17C3411B; Thu, 23 Jun 2022 16:55:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656003336; bh=kbAgY64gVe9OyuL4x+R4qwOSqVNcXpqeWzXYyNafxMk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VvJUt9TREInyrX4bMzFq2d2Lq1mXY4lh7+Q7SlMAXlklLoqR4f+AOFL11F2SXEzh8 TF5BVgDiXCmJgfPazPg+w4XmvVkav/CwLj3cbzq97+4airKneOEo8irNHFlLyECFtB vuB3PEdyBkC/BT6aB5RGM9075UhiP+5c5dETX3S4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dominik Brodowski , "Jason A. Donenfeld" Subject: [PATCH 4.9 204/264] random: handle latent entropy and command line from random_init() Date: Thu, 23 Jun 2022 18:43:17 +0200 Message-Id: <20220623164349.842953367@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: "Jason A. Donenfeld" commit 2f14062bb14b0fcfcc21e6dc7d5b5c0d25966164 upstream. Currently, start_kernel() adds latent entropy and the command line to the entropy bool *after* the RNG has been initialized, deferring when it's actually used by things like stack canaries until the next time the pool is seeded. This surely is not intended. Rather than splitting up which entropy gets added where and when between start_kernel() and random_init(), just do everything in random_init(), which should eliminate these kinds of bugs in the future. While we're at it, rename the awkwardly titled "rand_initialize()" to the more standard "random_init()" nomenclature. Reviewed-by: Dominik Brodowski Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 13 ++++++++----- include/linux/random.h | 16 +++++++--------- init/main.c | 10 +++------- 3 files changed, 18 insertions(+), 21 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -884,12 +884,13 @@ early_param("random.trust_bootloader", p /* * The first collection of entropy occurs at system boot while interrupts - * are still turned off. Here we push in RDSEED, a timestamp, and utsname(). - * Depending on the above configuration knob, RDSEED may be considered - * sufficient for initialization. Note that much earlier setup may already - * have pushed entropy into the input pool by the time we get here. + * are still turned off. Here we push in latent entropy, RDSEED, a timestamp, + * utsname(), and the command line. Depending on the above configuration knob, + * RDSEED may be considered sufficient for initialization. Note that much + * earlier setup may already have pushed entropy into the input pool by the + * time we get here. */ -int __init rand_initialize(void) +int __init random_init(const char *command_line) { size_t i; ktime_t now = ktime_get_real(); @@ -911,6 +912,8 @@ int __init rand_initialize(void) } _mix_pool_bytes(&now, sizeof(now)); _mix_pool_bytes(utsname(), sizeof(*(utsname()))); + _mix_pool_bytes(command_line, strlen(command_line)); + add_latent_entropy(); if (crng_ready()) crng_reseed(); --- a/include/linux/random.h +++ b/include/linux/random.h @@ -14,26 +14,24 @@ struct notifier_block; extern void add_device_randomness(const void *, size_t); extern void add_bootloader_randomness(const void *, size_t); +extern void add_input_randomness(unsigned int type, unsigned int code, + unsigned int value) __latent_entropy; +extern void add_interrupt_randomness(int irq) __latent_entropy; +extern void add_hwgenerator_randomness(const void *buffer, size_t count, + size_t entropy); #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__) static inline void add_latent_entropy(void) { - add_device_randomness((const void *)&latent_entropy, - sizeof(latent_entropy)); + add_device_randomness((const void *)&latent_entropy, sizeof(latent_entropy)); } #else static inline void add_latent_entropy(void) {} #endif -extern void add_input_randomness(unsigned int type, unsigned int code, - unsigned int value) __latent_entropy; -extern void add_interrupt_randomness(int irq) __latent_entropy; -extern void add_hwgenerator_randomness(const void *buffer, size_t count, - size_t entropy); - extern void get_random_bytes(void *buf, size_t nbytes); extern int wait_for_random_bytes(void); -extern int __init rand_initialize(void); +extern int __init random_init(const char *command_line); extern bool rng_is_initialized(void); extern int register_random_ready_notifier(struct notifier_block *nb); extern int unregister_random_ready_notifier(struct notifier_block *nb); --- a/init/main.c +++ b/init/main.c @@ -583,15 +583,11 @@ asmlinkage __visible void __init start_k /* * 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() + * - timekeeping_init() for ktime entropy used in random_init() * - time_init() for making random_get_entropy() work on some platforms - * - rand_initialize() to get any arch-specific entropy like RDRAND - * - add_latent_entropy() to get any latent entropy - * - adding command line entropy + * - random_init() to initialize the RNG from from early entropy sources */ - rand_initialize(); - add_latent_entropy(); - add_device_randomness(command_line, strlen(command_line)); + random_init(command_line); boot_init_stack_canary(); sched_clock_postinit();