Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6158426iog; Thu, 23 Jun 2022 12:34:23 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sglGEDxeJyf4lNTJUxZFz08PlmyDyce/iQUefWa48EvzUoVMf7NWrnbWrrq5XSAvHmIaJF X-Received: by 2002:a17:90b:17c1:b0:1ed:1b79:8c04 with SMTP id me1-20020a17090b17c100b001ed1b798c04mr1191797pjb.53.1656012863410; Thu, 23 Jun 2022 12:34:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656012863; cv=none; d=google.com; s=arc-20160816; b=MTCmCT4RjxvOOWgMl7KOp21MfXi88w6JBefXHmcK7NdlvmYsOrCp9dKEHKuR9D8UA6 vb2rsHUQCWdmLwxMP2eo/zT/ld76SEqPJHGRGpwn5arb9U5fEq1SgLTWVX+mXKjHTi0b 7SGIwq7/kUFKBO85TUlYerr+fUwM4MLXeUJkL/+AWWTixbtWksGoGCDbBZSFlye2mGqq X0qVFt2kml4SzbjEeToO/e+cd9Ao5SHHhY1vMJMy0rqXqh3IDyo2TgFY6pxbMjsQbyeu /3cFx1vFufso5Ix5RMsXrJFLDbgoGxssozHfpkSHCHmLv0vlEw+6fhlnGfv9ezx/fKk4 4unA== 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=aRzo0HwQiS0qAlKBBNXn2FIKGUMViZyX8wrvT8Y4sas=; b=0AWDq+9TV6Cw62b8tDyC+EgHC+AQL4LHF+QoZDVifc1YqTcSjqgCEiNCVTUPIlKfST NglXynvFNiVfPReJAC/K9JPlNgU035xbsIK7CL8p2DXpOfA38ncaPPR5Wl9DDcw4jZuo rJyO9UJWHjEtxmnfqD+5uJSIT4syZtafANw6/Fu1wqbRETNWc9wxJABugBxYk1hzJNNy bx6VXrRQ9bMSjJo2tK1GO5YAmdQ8qcyqAVB3tfEeqz125IvquJ+vfwm+CZXaJGePHdOx 6yv7keTfHGqMQ92w5E5EV1RPB9SIYbAejG0oogX9lZEodLRyRLyKVq79R9WIW2Hn4qCf VSGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=NjtjTKWz; 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 i136-20020a639d8e000000b003fabebb91b0si27650047pgd.753.2022.06.23.12.34.11; Thu, 23 Jun 2022 12:34: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=NjtjTKWz; 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 S233959AbiFWSMF (ORCPT + 99 others); Thu, 23 Jun 2022 14:12:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236221AbiFWSLb (ORCPT ); Thu, 23 Jun 2022 14:11:31 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 873C8BC9B1; Thu, 23 Jun 2022 10:20:14 -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 BA4546159A; Thu, 23 Jun 2022 17:20:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8502BC3411B; Thu, 23 Jun 2022 17:20:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004813; bh=S6Z2YbLFfMIg2ntUa7Kq61N1Hh/pySifdolHTwUZwt4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NjtjTKWzU0KsKm/8Tpi1xd9AwqModMvqMu2b/U4iVbv+KC8kBK+YC4WkW0L3oQAga 6/6jauve0tkfffEcMVWfc7/lsuY2rL7zTjyfvOhLuIOxKwgaCg8cnLDF9Ebum2IlDv fbEa6UECvEsGB77P7b8oOuwl9KqW0Vtf44XWfOwk= 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 157/234] random: avoid initializing twice in credit race Date: Thu, 23 Jun 2022 18:43:44 +0200 Message-Id: <20220623164347.497386473@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 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 @@ -264,7 +264,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)); @@ -281,28 +280,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; - } - } } /* @@ -818,7 +799,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) @@ -828,13 +809,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;