Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6135452iog; Thu, 23 Jun 2022 12:05:22 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uHD1Y8CoZBJCi7KwvgQtZvNWdNuYlStVc6lMFrQGQJPIKihmmrfXxPbVJzPQmxfj8RbuJF X-Received: by 2002:a17:907:168f:b0:726:2bd0:1091 with SMTP id hc15-20020a170907168f00b007262bd01091mr1504887ejc.137.1656011122752; Thu, 23 Jun 2022 12:05:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011122; cv=none; d=google.com; s=arc-20160816; b=sGWKRIb2vatSKW/hD/wZ8fIQ8Gi4SE+h0W9eLlombvQMS4WaYjjQUAe3urx7mUkt8J UtNMVIah8+6XxMPHMAgKRZQGVbHf7QNVhbCdAG+wB8lxtDniI1C0dFLlQAI2Z26pF+sn JI9B5RPOjoMhXpufAJsgd6NCZexJjzOtIVQrMh6nxqyhHydjV/CbCgwtSF/ylbabjVI5 XYPCvw1QwIqd31oMqsKY+rsIr9w/hxHaF0n1DiqSLOSN1suUc9xm/UhCOZyu5xZ/gH2K TZxP0l6spsLsWSvZ3QDj/6+/ALcqWzvsBFmQfIsNWgNjML9xcx3D4v+7MXRSaURaTSg3 0ULw== 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=9eK0yCzflI/26f09/uee2TrjUIvijhGEojeum+mGqeU=; b=IE0uiuhiRE6v4xrAs4CC13Yjk0PrGVWvJPb972vk0fbhI4tlY45tNoIhchrPPwQEGW qO+ETd5CJP6nKFSw6xfM430y/c9xb8VbYl4s9FMovXsGARFiHaX5NDtjRtBakj4egcK8 zPdIEQEMPV5niU808kFmtn6b3oEnXDRpnufMJ1eNtcSUgEv5wUIRah2hjJ/2Nc1Q+eCJ E3SKcNNQYP1uQmURLB10nkZoXew4lA8iDcygviBSx2ZElefqUPXSAMp/xyY6f7Z8jwpW mwnkWHckRlGwrvM/HfiKrHmwfJHuYE6Dp0unV/6bwMBN7JOJT84Wybs9y11s+DA7KkIM C/2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tdfB09fG; 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 sc18-20020a1709078a1200b00711f55a31casi2229858ejc.775.2022.06.23.12.04.57; Thu, 23 Jun 2022 12:05:22 -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=tdfB09fG; 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 S229572AbiFWRI6 (ORCPT + 99 others); Thu, 23 Jun 2022 13:08:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229706AbiFWRGn (ORCPT ); Thu, 23 Jun 2022 13:06:43 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B9E251E66; Thu, 23 Jun 2022 09:55:51 -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 CBD0B60B2C; Thu, 23 Jun 2022 16:55:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AB957C3411B; Thu, 23 Jun 2022 16:55:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656003327; bh=02IjaI2jy4Sdxza1cTrUsvwE/1GGViB3DQwt3c8E154=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tdfB09fGu9V/jhKzLUiKSAT04ViOqcgWMKe0IbTvxuT+3m5FnBTc22+0nyOsOmNqF 0cgODG4eLCY/WxmEhQyYm95xzffKD++VwGvEyhgymNgWubrYmumhM/A2rTbiLWGNVz LxXmdLqBNdL71RxG6GIIlM/wb+mgQmjDWYmexTjE= 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 201/264] random: avoid initializing twice in credit race Date: Thu, 23 Jun 2022 18:43:14 +0200 Message-Id: <20220623164349.757391312@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 fed7ef061686cc813b1f3d8d0edc6c35b4d3537b upstream. Since all changes of crng_init now go through credit_init_bits(), we can fix a long standing race in which two concurrent callers of credit_init_bits() have the new bit count >= some threshold, but are doing so with crng_init as a lower threshold, checked outside of a lock, resulting in crng_reseed() or similar being called twice. In order to fix this, we can use the original cmpxchg value of the bit count, and only change crng_init when the bit count transitions from below a threshold to meeting the threshold. Reviewed-by: Dominik Brodowski Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 48 ++++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 26 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -265,7 +265,6 @@ static void crng_reseed(void) unsigned long flags; unsigned long next_gen; u8 key[CHACHA20_KEY_SIZE]; - bool finalize_init = false; extract_entropy(key, sizeof(key)); @@ -282,28 +281,10 @@ static void crng_reseed(void) ++next_gen; WRITE_ONCE(base_crng.generation, next_gen); WRITE_ONCE(base_crng.birth, jiffies); - if (!crng_ready()) { + if (!crng_ready()) crng_init = CRNG_READY; - finalize_init = true; - } spin_unlock_irqrestore(&base_crng.lock, flags); memzero_explicit(key, sizeof(key)); - if (finalize_init) { - 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("%d get_random_xx warning(s) missed due to ratelimiting\n", - unseeded_warning.missed); - unseeded_warning.missed = 0; - } - if (urandom_warning.missed) { - pr_notice("%d urandom warning(s) missed due to ratelimiting\n", - urandom_warning.missed); - urandom_warning.missed = 0; - } - } } /* @@ -819,7 +800,7 @@ static void extract_entropy(void *buf, s static void credit_init_bits(size_t nbits) { - unsigned int init_bits, orig, add; + unsigned int new, orig, add; unsigned long flags; if (crng_ready() || !nbits) @@ -829,13 +810,28 @@ static void credit_init_bits(size_t nbit do { orig = READ_ONCE(input_pool.init_bits); - init_bits = min_t(unsigned int, POOL_BITS, orig + add); - } while (cmpxchg(&input_pool.init_bits, orig, init_bits) != orig); + new = min_t(unsigned int, POOL_BITS, orig + add); + } while (cmpxchg(&input_pool.init_bits, orig, new) != orig); - if (!crng_ready() && init_bits >= POOL_READY_BITS) - crng_reseed(); - else if (unlikely(crng_init == CRNG_EMPTY && init_bits >= POOL_EARLY_BITS)) { + if (orig < POOL_READY_BITS && new >= POOL_READY_BITS) { + crng_reseed(); /* Sets crng_init to CRNG_READY under base_crng.lock. */ + 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("%d get_random_xx warning(s) missed due to ratelimiting\n", + unseeded_warning.missed); + unseeded_warning.missed = 0; + } + if (urandom_warning.missed) { + pr_notice("%d urandom warning(s) missed due to ratelimiting\n", + urandom_warning.missed); + urandom_warning.missed = 0; + } + } else if (orig < POOL_EARLY_BITS && new >= POOL_EARLY_BITS) { spin_lock_irqsave(&base_crng.lock, flags); + /* Check if crng_init is CRNG_EMPTY, to avoid race with crng_reseed(). */ if (crng_init == CRNG_EMPTY) { extract_entropy(base_crng.key, sizeof(base_crng.key)); crng_init = CRNG_EARLY;