Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp2933209iog; Mon, 20 Jun 2022 07:46:37 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tYJtU2N/jIIz6aSvbZ16v+ASMT7A7bLlIMdxexNjgrH6yjqX6VdLt/70TEz+kY272kkfM9 X-Received: by 2002:a63:4447:0:b0:3fc:d3d1:cea9 with SMTP id t7-20020a634447000000b003fcd3d1cea9mr21590368pgk.269.1655736397136; Mon, 20 Jun 2022 07:46:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655736397; cv=none; d=google.com; s=arc-20160816; b=pKfdKhzMO2ZslBICT8wXeab9452+o0a9kOPj7FCNig+vO6XmCQmWXR3aJ6nR0xziu5 /qJblNva5qTfysbpt0ygz0JLC1kkpZ5wd+aswjYXvBrat8s7Y05bsD+yTSwS6wxcjHv6 4FaWlR3L+/ldkcLu1cJ3J+kU06CXukyZg/0PTBrHS5nN7Tyla0/gcppZKd02OxRXrHrr wmq+L/246pwGMXP8tZkCIXRGoUSH5NaEPPyO3gqZaO/H23iEiLUENfADj/ntRbrnKGwI r5eeZSJ6XkCPZvfKOqP6rTHcGpIYBHmupV3Gt9n/8BRh50L1K2oqHMCwuCOlu3HIetdP m1Kg== 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=fkAFMc845jX4CmL+Ad1EMydUwovZY+wXkUd2tNCtKEw=; b=mOMnZptWOw1emMvUcN8EbwEs98ub0oipoicLToSrlckcbOd3tYvfC5jiFWMYKY4b1l KhMIBPgG+xllww3NaAa1PpsHO1lOuESKTrSEMvDy/EMOG7uCfBnjqHY1MUPm5oW+ipof Ky98Et6QGZZnvIfvQW8s1LQRF8azwCRCbpiqOkZLSsqW+e6c2wFAx8gF+BMKeceKoFZU lnmH4osuKXqiUpXGbR9POt8pZWVD4bIPWrVARd9dcg6gZDcNu7BBA6a1TlVpyGO/mW+A MDR9sYT0l/r6ww3sbGHHH/lnFapMWd/jBo2+8ZG9zSDEbvhBVON+u1ThUGbAxILrha1Z LmaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=qmjjAKh3; 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 s9-20020a17090a880900b001df3fb83daesi18334127pjn.157.2022.06.20.07.46.24; Mon, 20 Jun 2022 07:46:37 -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=qmjjAKh3; 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 S1348000AbiFTNlK (ORCPT + 99 others); Mon, 20 Jun 2022 09:41:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348973AbiFTNj3 (ORCPT ); Mon, 20 Jun 2022 09:39:29 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 138152983C; Mon, 20 Jun 2022 06: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 dfw.source.kernel.org (Postfix) with ESMTPS id 0FDE960ABE; Mon, 20 Jun 2022 13:14:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 041A4C3411B; Mon, 20 Jun 2022 13:14:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655730879; bh=OFK+I4hC4CbXFovI2d2Y6mlAc9oHx06iwqtm1T1Gx8k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qmjjAKh3rfyE8FXiHLO/QsMs66udFOF2da0lKUmMS3g43DcpGWf0kTv7WdIbCKEFF cIF0TXGiKAqP15K7btFvrEBgvBCAasXRHJnHu4EPqzN0yc5imCr0qZWkuH+anshRxV B+JbBmFsJ5zuiSxUOQM5lmYWb0yf41d7Omxf/tF8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dominik Brodowski , Eric Biggers , "Jason A. Donenfeld" Subject: [PATCH 5.4 084/240] random: fix locking for crng_init in crng_reseed() Date: Mon, 20 Jun 2022 14:49:45 +0200 Message-Id: <20220620124741.344640221@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220620124737.799371052@linuxfoundation.org> References: <20220620124737.799371052@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: Dominik Brodowski commit 7191c628fe07b70d3f37de736d173d1b115396ed upstream. crng_init is protected by primary_crng->lock. Therefore, we need to hold this lock when increasing crng_init to 2. As we shouldn't hold this lock for too long, only hold it for those parts which require protection. Signed-off-by: Dominik Brodowski Reviewed-by: Eric Biggers Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -500,6 +500,7 @@ static void crng_reseed(void) int entropy_count; unsigned long next_gen; u8 key[CHACHA_KEY_SIZE]; + bool finalize_init = false; /* * First we make sure we have POOL_MIN_BITS of entropy in the pool, @@ -527,12 +528,14 @@ static void crng_reseed(void) ++next_gen; WRITE_ONCE(base_crng.generation, next_gen); WRITE_ONCE(base_crng.birth, jiffies); - spin_unlock_irqrestore(&base_crng.lock, flags); - memzero_explicit(key, sizeof(key)); - if (crng_init < 2) { invalidate_batched_entropy(); crng_init = 2; + 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);