Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6033513iog; Thu, 23 Jun 2022 09:59:23 -0700 (PDT) X-Google-Smtp-Source: AGRyM1u3dFlUZ6Fhj9g/g32L763+g5tl5MN+yWc/MD9dwZvBTBgEdIH7fXSZ9wAqmvEHBB7uKu90 X-Received: by 2002:a17:907:60d3:b0:711:cef0:f880 with SMTP id hv19-20020a17090760d300b00711cef0f880mr8934029ejc.499.1656003563426; Thu, 23 Jun 2022 09:59:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656003563; cv=none; d=google.com; s=arc-20160816; b=Cw6FtPGaiUHN6Uu4r34SVFFB3QQJ9Haix4b85XNtj/WCnBKHfmOtWntPdXAeqmEOn/ b+dYir9ZDymlEo84Jt6ZYV5Br11qPo4JV3py7bVWhUYvKj9nWXJ6a2AJ2R/kZ2V0zUEs Oki/UCgT6jXH+yOEgBO6eZRUtnCjCZu+BOQ2aJq6QT3H/WADewtMpWkmya5Ysmioj/UE Ic/g0cusFYSifsRlBxtg/RkuHS15SEcwcpSutAJ1AfZNV5BE9OxDVV5kgeoMBBn9Udxq 3CBBCGffRV/86ls3zSwHSVuaNHtLo1IXxJkaOoIMDIyv+q4UmZpvuQC38AWDFHkyRSd6 e1MA== 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=EdDcozm8p+Rd2OAUDRbL+HNskMHgV0a6nkFse/UuWZU=; b=XSBS1+nucs4TooJvKtYsEdLVcj9D219EUSt6KN3td9ZpVmC1FcyLmJblJfSJQxc2fV oxT+uKIOoUVJxSkQRLf7UhMTEUwBvKrzoRTJ0GTpg/sX+0joaS7q8qbUmIm+M9+MyoiH 7+K0ZJGSptPXIJzvhIRDvIoASe9rIDwxebnJApQNeqdeRM2lveOzY9Zh03BKzFLB6xTO +x6kHJaLTFqRQEjPC66T26aebJrcPYABxtgDeEkEsBo8bA6gLg7fDONIwODmyoyd/0qK oHyZ9PIAy5qbEIHyM4Itv2NvdVz6+Smqjekvc4waoKUVuQWJF/sR9Xqj0Ysd6I4yCfxU bSGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="giQBSu/8"; 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 sd23-20020a1709076e1700b007262b3cd387si1185276ejc.421.2022.06.23.09.58.39; Thu, 23 Jun 2022 09:59:23 -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="giQBSu/8"; 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 S232805AbiFWQ4i (ORCPT + 99 others); Thu, 23 Jun 2022 12:56:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229992AbiFWQwV (ORCPT ); Thu, 23 Jun 2022 12:52:21 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C91A2C70; Thu, 23 Jun 2022 09:52:20 -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 sin.source.kernel.org (Postfix) with ESMTPS id 2BEF1CE25DF; Thu, 23 Jun 2022 16:52:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3077DC3411B; Thu, 23 Jun 2022 16:52:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656003137; bh=W5a4qdO3sxhr8X7o0w7ZiEpA44gPr7ERdDCwxwf7rF4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=giQBSu/8Jho0SAEkmBMnXwjWti/qSCxIusMB2F2BCIybzbzldhbXeeYIlH1hoSE9c CZSrs168YkUacfwJkqJgJgBmsijM5jtzbJLMYiWMJFur3WWy6WuBUWd2Fd/99Q/jhL vxaSC5ITXQFMZfhtmRkJozuMAjSjwQoRD4mh2oEA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Theodore Tso , Dominik Brodowski , Eric Biggers , "Jason A. Donenfeld" Subject: [PATCH 4.9 143/264] random: unify early init crng load accounting Date: Thu, 23 Jun 2022 18:42:16 +0200 Message-Id: <20220623164348.111894062@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 da792c6d5f59a76c10a310c5d4c93428fd18f996 upstream. crng_fast_load() and crng_slow_load() have different semantics: - crng_fast_load() xors and accounts with crng_init_cnt. - crng_slow_load() hashes and doesn't account. However add_hwgenerator_randomness() can afford to hash (it's called from a kthread), and it should account. Additionally, ones that can afford to hash don't need to take a trylock but can take a normal lock. So, we combine these into one function, crng_pre_init_inject(), which allows us to control these in a uniform way. This will make it simpler later to simplify this all down when the time comes for that. Cc: Theodore Ts'o Reviewed-by: Dominik Brodowski Reviewed-by: Eric Biggers Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 114 +++++++++++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 55 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -385,7 +385,7 @@ static void crng_make_state(u32 chacha_s * For the fast path, we check whether we're ready, unlocked first, and * then re-check once locked later. In the case where we're really not * ready, we do fast key erasure with the base_crng directly, because - * this is what crng_{fast,slow}_load mutate during early init. + * this is what crng_pre_init_inject() mutates during early init. */ if (unlikely(!crng_ready())) { bool ready; @@ -436,72 +436,75 @@ static void crng_make_state(u32 chacha_s } /* - * This function is for crng_init == 0 only. + * This function is for crng_init == 0 only. It loads entropy directly + * into the crng's key, without going through the input pool. It is, + * generally speaking, not very safe, but we use this only at early + * boot time when it's better to have something there rather than + * nothing. + * + * There are two paths, a slow one and a fast one. The slow one + * hashes the input along with the current key. The fast one simply + * xors it in, and should only be used from interrupt context. + * + * If account is set, then the crng_init_cnt counter is incremented. + * 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). * - * crng_fast_load() can be called by code in the interrupt service - * path. So we can't afford to dilly-dally. Returns the number of - * bytes processed from cp. + * Returns the number of bytes processed from input, which is bounded + * by CRNG_INIT_CNT_THRESH if account is true. */ -static size_t crng_fast_load(const void *cp, size_t len) +static size_t crng_pre_init_inject(const void *input, size_t len, + bool fast, bool account) { static int crng_init_cnt = 0; unsigned long flags; - const u8 *src = (const u8 *)cp; - size_t ret = 0; - if (!spin_trylock_irqsave(&base_crng.lock, flags)) - return 0; + if (fast) { + if (!spin_trylock_irqsave(&base_crng.lock, flags)) + return 0; + } else { + spin_lock_irqsave(&base_crng.lock, flags); + } + if (crng_init != 0) { spin_unlock_irqrestore(&base_crng.lock, flags); return 0; } - while (len > 0 && crng_init_cnt < CRNG_INIT_CNT_THRESH) { - base_crng.key[crng_init_cnt % sizeof(base_crng.key)] ^= *src; - src++; crng_init_cnt++; len--; ret++; - } - if (crng_init_cnt >= CRNG_INIT_CNT_THRESH) { - ++base_crng.generation; - crng_init = 1; - } - spin_unlock_irqrestore(&base_crng.lock, flags); - if (crng_init == 1) - pr_notice("fast init done\n"); - return ret; -} -/* - * This function is for crng_init == 0 only. - * - * crng_slow_load() is called by add_device_randomness, which has two - * attributes. (1) We can't trust the buffer passed to it is - * guaranteed to be unpredictable (so it might not have any entropy at - * all), and (2) it doesn't have the performance constraints of - * crng_fast_load(). - * - * So, we simply hash the contents in with the current key. Finally, - * we do *not* advance crng_init_cnt since buffer we may get may be - * something like a fixed DMI table (for example), which might very - * well be unique to the machine, but is otherwise unvarying. - */ -static void crng_slow_load(const void *cp, size_t len) -{ - unsigned long flags; - struct blake2s_state hash; + if (account) + len = min_t(size_t, len, CRNG_INIT_CNT_THRESH - crng_init_cnt); - blake2s_init(&hash, sizeof(base_crng.key)); - - if (!spin_trylock_irqsave(&base_crng.lock, flags)) - return; - if (crng_init != 0) { - spin_unlock_irqrestore(&base_crng.lock, flags); - return; + if (fast) { + const u8 *src = input; + size_t i; + + for (i = 0; i < len; ++i) + base_crng.key[(crng_init_cnt + i) % + sizeof(base_crng.key)] ^= src[i]; + } else { + struct blake2s_state hash; + + blake2s_init(&hash, sizeof(base_crng.key)); + 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; + if (crng_init_cnt >= CRNG_INIT_CNT_THRESH) { + ++base_crng.generation; + crng_init = 1; + } } - blake2s_update(&hash, base_crng.key, sizeof(base_crng.key)); - blake2s_update(&hash, cp, len); - blake2s_final(&hash, base_crng.key); - spin_unlock_irqrestore(&base_crng.lock, flags); + + if (crng_init == 1) + pr_notice("fast init done\n"); + + return len; } static void _get_random_bytes(void *buf, size_t nbytes) @@ -1014,7 +1017,7 @@ void add_device_randomness(const void *b unsigned long flags; if (!crng_ready() && size) - crng_slow_load(buf, size); + crng_pre_init_inject(buf, size, false, false); spin_lock_irqsave(&input_pool.lock, flags); _mix_pool_bytes(buf, size); @@ -1131,7 +1134,7 @@ void add_hwgenerator_randomness(const vo size_t entropy) { if (unlikely(crng_init == 0)) { - size_t ret = crng_fast_load(buffer, count); + size_t ret = crng_pre_init_inject(buffer, count, false, true); mix_pool_bytes(buffer, ret); count -= ret; buffer += ret; @@ -1294,7 +1297,8 @@ void add_interrupt_randomness(int irq) if (unlikely(crng_init == 0)) { if (new_count >= 64 && - crng_fast_load(fast_pool->pool32, sizeof(fast_pool->pool32)) > 0) { + crng_pre_init_inject(fast_pool->pool32, sizeof(fast_pool->pool32), + true, true) > 0) { atomic_set(&fast_pool->count, 0); fast_pool->last = now; if (spin_trylock(&input_pool.lock)) {