Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6145677iog; Thu, 23 Jun 2022 12:17:14 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t1VAlIEpPQ5y/767hfqEiLMuMHuI+rNT+CwaQyQ+yFr3sphxGuGxf10XdCyWiLgaZw/jku X-Received: by 2002:a63:794e:0:b0:40d:99b:bb4 with SMTP id u75-20020a63794e000000b0040d099b0bb4mr8818763pgc.133.1656011834647; Thu, 23 Jun 2022 12:17:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011834; cv=none; d=google.com; s=arc-20160816; b=0DOuZmcGt55+FEcECFl7p0UgPnDcA9JLniIsGytn6IQi3lQeOQjldRaKiFuucQXuU1 Y58AVTF5AVnuwBaqRoi75oJm+LcxmWPjIdsIig6ZmhG0ZyM/rIi+LxiRzVKd6Blql6UO s0olXKuQhxEX/KS5tLKzay3uuJVEFDK52Porur/FF7GhiSU/0Sr5eNpPK6igVSX50fd9 3HAf9hy1SI/qoQPGQ9HiUZRtdZtkJGLO9hiCPypvBe3+My9p1UtIgt35IPpsi7QYbS/I COmh/G4BLudj5/1CrDBnAlTkjTpxG3ZM2rS1Y+Qp1KMRO2dIH5SKy7EB0FGfUsuP8AXl ceAA== 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=1tcC+ZKcUx5/15enB2o+ux8fyMOkKOHpco7IPET3hy4=; b=GoPM0Mp+R525XkunoBulNnrm70nH3XGASWXcrJUtYNoZ4cqfQQzWjTTWKJv4ldtHcT q9XwSXFes2xsFNtIMM1HO4aGIcloSf+O7Z9yC9/ZBdwLOx16+kESYktA7rh9lawOvYzV Ankclha7grudVeApIMzEVFtrbaUKfoxPLf3zEQRV2CW3x8PMdX9YVVQgUidHhHSi7eRb zysG5faFl9fmnts7v1HwhBpGUMM9NvKnkw9KwtIDq3JT6WcghtXdOrbfpcIXMmns7v4o kK6gfuEy8YhlXA1HXqv4H4Lhur8gi8bByYOrCGN+MDsYgpqRu2n1EdhbwU3GXie6wi82 J+jQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="ErI/5tzV"; 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 f38-20020a635126000000b00408ab3d13a5si27211373pgb.788.2022.06.23.12.17.02; Thu, 23 Jun 2022 12:17:14 -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="ErI/5tzV"; 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 S235471AbiFWRzM (ORCPT + 99 others); Thu, 23 Jun 2022 13:55:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235436AbiFWRwe (ORCPT ); Thu, 23 Jun 2022 13:52:34 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DB2AC5130B; Thu, 23 Jun 2022 10:13:18 -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 86D7AB82489; Thu, 23 Jun 2022 17:13:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C0BE1C3411B; Thu, 23 Jun 2022 17:13:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004396; bh=uJNjvu56mngsJlztc6pVOzxn/BRpjZgNUfk0aaz1okY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ErI/5tzVLPL7aCeNB4dvVW66PNgdREmOJbsCN/aVzTubfM8uk6V+zgd36qyfL2Lyx FfBkFMLDJ5a0HI22K63iKhic4Dh9VFUosLPaEMP3FoXePam5eecTJ9+WUSErCiojL+ rqdEqruFnNQPtFmW3/kqFTJ5ChojjZcz/9Kn5AYU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Ivan T. Ivanov" , Dominik Brodowski , "Jason A. Donenfeld" Subject: [PATCH 4.19 024/234] random: fix crash on multiple early calls to add_bootloader_randomness() Date: Thu, 23 Jun 2022 18:41:31 +0200 Message-Id: <20220623164343.748602926@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164343.042598055@linuxfoundation.org> References: <20220623164343.042598055@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: Dominik Brodowski commit f7e67b8e803185d0aabe7f29d25a35c8be724a78 upstream. Currently, if CONFIG_RANDOM_TRUST_BOOTLOADER is enabled, multiple calls to add_bootloader_randomness() are broken and can cause a NULL pointer dereference, as noted by Ivan T. Ivanov. This is not only a hypothetical problem, as qemu on arm64 may provide bootloader entropy via EFI and via devicetree. On the first call to add_hwgenerator_randomness(), crng_fast_load() is executed, and if the seed is long enough, crng_init will be set to 1. On subsequent calls to add_bootloader_randomness() and then to add_hwgenerator_randomness(), crng_fast_load() will be skipped. Instead, wait_event_interruptible() and then credit_entropy_bits() will be called. If the entropy count for that second seed is large enough, that proceeds to crng_reseed(). However, both wait_event_interruptible() and crng_reseed() depends (at least in numa_crng_init()) on workqueues. Therefore, test whether system_wq is already initialized, which is a sufficient indicator that workqueue_init_early() has progressed far enough. If we wind up hitting the !system_wq case, we later want to do what would have been done there when wqs are up, so set a flag, and do that work later from the rand_initialize() call. Reported-by: Ivan T. Ivanov Fixes: 18b915ac6b0a ("efi/random: Treat EFI_RNG_PROTOCOL output as bootloader randomness") Cc: stable@vger.kernel.org Signed-off-by: Dominik Brodowski [Jason: added crng_need_done state and related logic.] Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 59 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 21 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -496,6 +496,7 @@ static struct crng_state primary_crng = * its value (from 0->1->2). */ static int crng_init = 0; +static bool crng_need_final_init = false; #define crng_ready() (likely(crng_init > 1)) static int crng_init_cnt = 0; static unsigned long crng_global_init_time = 0; @@ -888,6 +889,38 @@ static void crng_initialize(struct crng_ crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1; } +static void crng_finalize_init(struct crng_state *crng) +{ + if (crng != &primary_crng || crng_init >= 2) + return; + if (!system_wq) { + /* We can't call numa_crng_init until we have workqueues, + * so mark this for processing later. */ + crng_need_final_init = true; + return; + } + + invalidate_batched_entropy(); + numa_crng_init(); + crng_init = 2; + process_random_ready_list(); + wake_up_interruptible(&crng_init_wait); + kill_fasync(&fasync, SIGIO, POLL_IN); + pr_notice("crng init done\n"); + if (unseeded_warning.missed) { + pr_notice("random: %d get_random_xx warning(s) missed " + "due to ratelimiting\n", + unseeded_warning.missed); + unseeded_warning.missed = 0; + } + if (urandom_warning.missed) { + pr_notice("random: %d urandom warning(s) missed " + "due to ratelimiting\n", + urandom_warning.missed); + urandom_warning.missed = 0; + } +} + #ifdef CONFIG_NUMA static void do_numa_crng_init(struct work_struct *work) { @@ -1042,26 +1075,7 @@ static void crng_reseed(struct crng_stat memzero_explicit(&buf, sizeof(buf)); WRITE_ONCE(crng->init_time, jiffies); spin_unlock_irqrestore(&crng->lock, flags); - if (crng == &primary_crng && crng_init < 2) { - invalidate_batched_entropy(); - numa_crng_init(); - crng_init = 2; - process_random_ready_list(); - wake_up_interruptible(&crng_init_wait); - pr_notice("random: crng init done\n"); - if (unseeded_warning.missed) { - pr_notice("random: %d get_random_xx warning(s) missed " - "due to ratelimiting\n", - unseeded_warning.missed); - unseeded_warning.missed = 0; - } - if (urandom_warning.missed) { - pr_notice("random: %d urandom warning(s) missed " - "due to ratelimiting\n", - urandom_warning.missed); - urandom_warning.missed = 0; - } - } + crng_finalize_init(crng); } static void _extract_crng(struct crng_state *crng, @@ -1960,6 +1974,8 @@ int __init rand_initialize(void) { init_std_data(&input_pool); init_std_data(&blocking_pool); + if (crng_need_final_init) + crng_finalize_init(&primary_crng); crng_initialize(&primary_crng); crng_global_init_time = jiffies; if (ratelimit_disable) { @@ -2478,7 +2494,8 @@ void add_hwgenerator_randomness(const ch * We'll be woken up again once below random_write_wakeup_thresh, * or when the calling thread is about to terminate. */ - wait_event_interruptible(random_write_wait, kthread_should_stop() || + wait_event_interruptible(random_write_wait, + !system_wq || kthread_should_stop() || ENTROPY_BITS(&input_pool) <= random_write_wakeup_bits); mix_pool_bytes(poolp, buffer, count); credit_entropy_bits(poolp, entropy);