Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6147793iog; Thu, 23 Jun 2022 12:19:49 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vCx69HM96GRTepPAnsQkfP+iRKyBXxaElkzZre1YIgTRZSRTjZwdN0PmLHYdsqA0wp4602 X-Received: by 2002:a05:6402:2753:b0:431:9c8b:5635 with SMTP id z19-20020a056402275300b004319c8b5635mr12759240edd.152.1656011989738; Thu, 23 Jun 2022 12:19:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011989; cv=none; d=google.com; s=arc-20160816; b=Q+XASbwVGKGt3+YzJQKKl3yKehCQgxytMWG5unAN0zVjpLkkZWRQlj5udOyC7DO7zp ZwmhO4uk95bve6PqQWIXvQK438934WTg/nRPbbtJcxPe36p7K1ZMbsZz0o7SdG1BhADJ QVJdHx846akXdzDCua9PWE5oAPjtHVWeiFM8Q4mB9cJwg0GAEEpOTXhpFxQ5EZunE9kE KePH4Mgd1/4PlZDvNxTuc02wJfhZ9VroM+aUOawGjikoaXzFDSeV3dSU2YX8bXMdkKJw 7Ex3+3EcfWU3Wt5D8+nFWTUSQUZSPzkhF2OhpgDfbFnVO/RuT6CLsgNDijA9fAzgO533 ts5g== 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=Jqog6cpAsj43A3PD/0GcObwgvVjKOFMqHXj09u2zmCA=; b=sZHpxdvNNzH37QxssNm3MVNFxv++WIk/lbv9cz+MnCe/pvpIMpL80tVgcuv8f8QILv knK4MwprI9OeANytJXAwK0t9WdNQ0GLhhn7zVJPtxbezlKAF8uAD/PAa6k3d4gvPVgtK LT23SqsHEvEh6YmMZFCowaT7LeT1LSA62yBSbPN2I3y7KbvepCI99tSBcCJLMCZlUgDc zwrQMCKNDqF6BwHIcGJQW3anzd/4oa+NggZ6o0vAf1BuwjllwZJNgY8hyVoGh6t8vYJX Qw/fezT9/AA1R8HW/cvmz8zO/eNykhTaeGLn5hbWL7PMWFjpCg60JMLVTasuIGkCIB5v t+yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BtGbv7y1; 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 ss2-20020a170907c00200b0070d8de4e431si19419814ejc.206.2022.06.23.12.19.21; Thu, 23 Jun 2022 12:19:49 -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=BtGbv7y1; 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 S236142AbiFWR6q (ORCPT + 99 others); Thu, 23 Jun 2022 13:58:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236043AbiFWRxs (ORCPT ); Thu, 23 Jun 2022 13:53:48 -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 95CACAC8ED; Thu, 23 Jun 2022 10:14:45 -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 54B7EB82498; Thu, 23 Jun 2022 17:14:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 98A5BC3411B; Thu, 23 Jun 2022 17:14:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004483; bh=SBSSfxu8g2eVVuh6cumCML5KKehtqyO2vIV50o57wAc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BtGbv7y1ZnXxu8wY9nukQlao0gwwdxnE7+NRb454uq0r4lmlrUjzbhiEWJBSvcVXJ ow5oRMaCexhstEk4kPswu9sX1qOx3/CszG4ZU5BA7TLDZAnArjrAl4h+cu0ZRaWJEs MxdKMg3j5ZKdGl+ZTZKe1wS0T/KaOIpgB+mYKyJw= 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.19 053/234] random: mix bootloader randomness into pool Date: Thu, 23 Jun 2022 18:42:00 +0200 Message-Id: <20220623164344.566690427@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: "Jason A. Donenfeld" commit 57826feeedb63b091f807ba8325d736775d39afd upstream. If we're trusting bootloader randomness, crng_fast_load() is called by add_hwgenerator_randomness(), which sets us to crng_init==1. However, usually it is only called once for an initial 64-byte push, so bootloader entropy will not mix any bytes into the input pool. So it's conceivable that crng_init==1 when crng_initialize_primary() is called later, but then the input pool is empty. When that happens, the crng state key will be overwritten with extracted output from the empty input pool. That's bad. In contrast, if we're not trusting bootloader randomness, we call crng_slow_load() *and* we call mix_pool_bytes(), so that later crng_initialize_primary() isn't drawing on nothing. In order to prevent crng_initialize_primary() from extracting an empty pool, have the trusted bootloader case mirror that of the untrusted bootloader case, mixing the input into the pool. [linux@dominikbrodowski.net: rewrite commit message] Signed-off-by: Dominik Brodowski Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -2295,8 +2295,12 @@ void add_hwgenerator_randomness(const ch struct entropy_store *poolp = &input_pool; if (unlikely(crng_init == 0)) { - crng_fast_load(buffer, count); - return; + size_t ret = crng_fast_load(buffer, count); + mix_pool_bytes(poolp, buffer, ret); + count -= ret; + buffer += ret; + if (!count || crng_init == 0) + return; } /* Suspend writing if we're above the trickle threshold.