Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6140117iog; Thu, 23 Jun 2022 12:10:48 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vvxYqVgXfww0qaEeYfHqPKQ6ut5REr5SPSqDnWFsZ5645OO+OkqP9aSlLplfjrmKW1qFMh X-Received: by 2002:a17:902:ba90:b0:16a:2863:fb85 with SMTP id k16-20020a170902ba9000b0016a2863fb85mr20693198pls.15.1656011448549; Thu, 23 Jun 2022 12:10:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011448; cv=none; d=google.com; s=arc-20160816; b=jro7y/kgQP4JkKCXazGNO0qJxF35bt0/udX5YxnIv9d6rjrsLQOgxKu+AbRj1Z/v18 BvzIkJ8UTwm+vW0DBT0cqqnrdKJNhSka/6qw6JvTrbWfa0SQ9KEwcF5g2qhsR60oIRXV CjAelmptQKJtgPNz1hjfZQTPTKtq+q8KWyBXknMTHkNk12JUlayelSDXwzvVFqcxtuhX UL+5MCZ/zEhe8LXBMOxcu95Os7iMw0IfIKqfwOkdfyt5bjfU2VB0naD2nJTpHFkzV3X0 Jn7Fa2O7clBAu5ktQno4FoSlgSXGECoWRshD07zSs+PbGT5Dl6tGEcMz+pkW9PI4GL2i sL8g== 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=LVHiJrMngYKezzujnUPQbCVGqqbTU2/kJrKqGiEtbfw=; b=v2sksnfiB9oX7o2U+Nz+hdtvszZdUl+p4RuYJ5B7fXNbnYw71rGJyJGiBJ38mOEDD2 EnXgHCMk1EtflWmtdHAQEv9c0G9/3LOm4Qjhxjcjt6fJjFJAHV/d241Vct8FZ8M0mSjF TC/0e1GKQzfXBm6GymYK/MGDP0yJ9YnpAdK/j3k/0x/lkc1xAGz4yT+gVeQ0G4yJg3ny 2AavPMhr/E9EwI0bqkd7IZbHvRAXGhumfxLnfWxjVhbJjRwuJA2NAXrcC1ns0zKKUyOm +7k1X4h7vYoL0lmvy0SuEOZnSlQHHi7CoYsqk9HYby1crLdLMxbptHC4+84hCp7U76Ae bMBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=X2SK0V7O; 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 g11-20020a63f40b000000b003a1d18a5386si28135238pgi.17.2022.06.23.12.10.35; Thu, 23 Jun 2022 12:10:48 -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=X2SK0V7O; 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 S236507AbiFWSNj (ORCPT + 99 others); Thu, 23 Jun 2022 14:13:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235996AbiFWSMU (ORCPT ); Thu, 23 Jun 2022 14:12:20 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4DBEBEAAB; Thu, 23 Jun 2022 10:20:42 -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 2DDE8B82498; Thu, 23 Jun 2022 17:20:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 95514C3411B; Thu, 23 Jun 2022 17:20:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004840; bh=mAck5Vr2VmbZr42evQmg6KzpMK6Ef6EB3bFmUc9P+Uo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=X2SK0V7O2LjsqBS3BNS90mcA2XiJ0YGoTz5w6iG7/Zi3RDp8tY7fO94Ty8hgSXu/U 0SIoICrTCC1ih2emWvpSkyljgepWUwhD/GbcbT3LDmYl9cs0ohpaGeqR/JNHWyE0YF mNbRsdAQ3+kqcMeAlCHlUBcv5QKMh+vxf0K3YRHg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jan Varho , "Jason A. Donenfeld" Subject: [PATCH 4.19 127/234] random: do not split fast init input in add_hwgenerator_randomness() Date: Thu, 23 Jun 2022 18:43:14 +0200 Message-Id: <20220623164346.652520700@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: Jan Varho commit 527a9867af29ff89f278d037db704e0ed50fb666 upstream. add_hwgenerator_randomness() tries to only use the required amount of input for fast init, but credits all the entropy, rather than a fraction of it. Since it's hard to determine how much entropy is left over out of a non-unformly random sample, either give it all to fast init or credit it, but don't attempt to do both. In the process, we can clean up the injection code to no longer need to return a value. Signed-off-by: Jan Varho [Jason: expanded commit message] Fixes: 73c7733f122e ("random: do not throw away excess input to crng_fast_load") Cc: stable@vger.kernel.org # 5.17+, requires af704c856e88 Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -437,11 +437,8 @@ static void crng_make_state(u32 chacha_s * This shouldn't be set by functions like add_device_randomness(), * where we can't trust the buffer passed to it is guaranteed to be * unpredictable (so it might not have any entropy at all). - * - * Returns the number of bytes processed from input, which is bounded - * by CRNG_INIT_CNT_THRESH if account is true. */ -static size_t crng_pre_init_inject(const void *input, size_t len, bool account) +static void crng_pre_init_inject(const void *input, size_t len, bool account) { static int crng_init_cnt = 0; struct blake2s_state hash; @@ -452,18 +449,15 @@ static size_t crng_pre_init_inject(const spin_lock_irqsave(&base_crng.lock, flags); if (crng_init != 0) { spin_unlock_irqrestore(&base_crng.lock, flags); - return 0; + return; } - if (account) - len = min_t(size_t, len, CRNG_INIT_CNT_THRESH - crng_init_cnt); - blake2s_update(&hash, base_crng.key, sizeof(base_crng.key)); blake2s_update(&hash, input, len); blake2s_final(&hash, base_crng.key); if (account) { - crng_init_cnt += len; + crng_init_cnt += min_t(size_t, len, CRNG_INIT_CNT_THRESH - crng_init_cnt); if (crng_init_cnt >= CRNG_INIT_CNT_THRESH) { ++base_crng.generation; crng_init = 1; @@ -474,8 +468,6 @@ static size_t crng_pre_init_inject(const if (crng_init == 1) pr_notice("fast init done\n"); - - return len; } static void _get_random_bytes(void *buf, size_t nbytes) @@ -1133,12 +1125,9 @@ void add_hwgenerator_randomness(const vo size_t entropy) { if (unlikely(crng_init == 0 && entropy < POOL_MIN_BITS)) { - size_t ret = crng_pre_init_inject(buffer, count, true); - mix_pool_bytes(buffer, ret); - count -= ret; - buffer += ret; - if (!count || crng_init == 0) - return; + crng_pre_init_inject(buffer, count, true); + mix_pool_bytes(buffer, count); + return; } /*