Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6147674iog; Thu, 23 Jun 2022 12:19:42 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uphXbbop/N2lhVJVlZRtUPsecweTMRP7Mk30Fwn+W5NzjG9A7+fgcED1tLdNLFSGt1kxGs X-Received: by 2002:a17:906:4794:b0:722:f10e:6240 with SMTP id cw20-20020a170906479400b00722f10e6240mr9424036ejc.704.1656011982002; Thu, 23 Jun 2022 12:19:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011981; cv=none; d=google.com; s=arc-20160816; b=iGtduI/FYvfWeIXV/0xDGcUuT0gbne1DM/OFDwZUrL3C44s2dU5FtqzxYe2l5XGBwF Oo/MksN52svLPWppghFxbcS7mQTOhSnNfl8AWBvxqcWOX/XHZFTi4CznmO8/egVsIyT6 gl+70ucrtLyaG7gTMYAMHEWuWpJf9hgQIZ6RvvfQxO5sfHWFLUK8Ql2sUoIkQ92pT2oD YpqgfFfEZrAEUdv8sd246OLC4QVPVxOlxcbcQwYvVMmjKnKa14ZHMWR8DBO8GdRZtTii D3a0cxCwh2YaPQKxDNw30Zw0BD7m42xv32qXznPdCg8bI7dlvm/xw6UoaJ6w8M8tZLgC Fc/A== 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=Yy38nVTgQW/pe72inl1UgX9V4LZFjhVur+RgnN8UnQ8=; b=ffqgkS/YJQyGK92kyQffMHb5Yp3UGu55of3ZYEgkQWeY/bNc0PBgJNSuo/D3Ycgex4 T193tuxLaMRe0gvGg6xenxxYnELFbGmrKUdauBFxMPGNFlDRuz7OURVJG1KMR8epghqm mQZYgI6qyeUCwRxs2/4N3baVhuK4Ec2rsp1hq5O5xWqkxMweSTgpE6IpvxE7D2Zj8+Vl ooa7fmBhfa4R068aQ4Eqkjby3gGC+yFLj85l8B9bEnVGvHX5pGx3oM0P64NvJ83BhrG+ k3M4gScJ8xaQTxtFFdIL87SOaArTP4E8oy54tE5OtyoqSzONK1vkXaEOtGmhQvwglU3a jR0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QTKEkQ5O; 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 e16-20020a17090658d000b00722df7631c5si175575ejs.749.2022.06.23.12.19.15; Thu, 23 Jun 2022 12:19:41 -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=QTKEkQ5O; 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 S235937AbiFWSN7 (ORCPT + 99 others); Thu, 23 Jun 2022 14:13:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236659AbiFWSMf (ORCPT ); Thu, 23 Jun 2022 14:12:35 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 165559B74F; Thu, 23 Jun 2022 10:20:56 -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 8EA4D61E0D; Thu, 23 Jun 2022 17:20:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52840C3411B; Thu, 23 Jun 2022 17:20:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004854; bh=B1KcBXihG84hUbxR5ankUmNavS3EMHs70nPIAU0OEnI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QTKEkQ5OhT17pasUF5HcyA44D68EUaDNd0zj1Z7RRpnpWuc7e4GzYGttQtAhjw27W XLApEHVP5nzop/fWagPFyM9BBHUKTCXbt51tbHUvlSXrJYe/icflhQO2+FS4yPsjVI KQM+UeaWcmKFeAmVb0NS/9O0fhuDrPnW2ENXIhkE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Theodore Tso , Dominik Brodowski , "Jason A. Donenfeld" Subject: [PATCH 4.19 122/234] random: check for signal and try earlier when generating entropy Date: Thu, 23 Jun 2022 18:43:09 +0200 Message-Id: <20220623164346.509575988@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 3e504d2026eb6c8762cd6040ae57db166516824a upstream. Rather than waiting a full second in an interruptable waiter before trying to generate entropy, try to generate entropy first and wait second. While waiting one second might give an extra second for getting entropy from elsewhere, we're already pretty late in the init process here, and whatever else is generating entropy will still continue to contribute. This has implications on signal handling: we call try_to_generate_entropy() from wait_for_random_bytes(), and wait_for_random_bytes() always uses wait_event_interruptible_timeout() when waiting, since it's called by userspace code in restartable contexts, where signals can pend. Since try_to_generate_entropy() now runs first, if a signal is pending, it's necessary for try_to_generate_entropy() to check for signals, since it won't hit the wait until after try_to_generate_entropy() has returned. And even before this change, when entering a busy loop in try_to_generate_entropy(), we should have been checking to see if any signals are pending, so that a process doesn't get stuck in that loop longer than expected. Cc: Theodore Ts'o Reviewed-by: Dominik Brodowski Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -127,10 +127,11 @@ int wait_for_random_bytes(void) { while (!crng_ready()) { int ret; + + try_to_generate_entropy(); ret = wait_event_interruptible_timeout(crng_init_wait, crng_ready(), HZ); if (ret) return ret > 0 ? 0 : ret; - try_to_generate_entropy(); } return 0; } @@ -1366,7 +1367,7 @@ static void try_to_generate_entropy(void return; timer_setup_on_stack(&stack.timer, entropy_timer, 0); - while (!crng_ready()) { + while (!crng_ready() && !signal_pending(current)) { if (!timer_pending(&stack.timer)) mod_timer(&stack.timer, jiffies + 1); mix_pool_bytes(&stack.cycles, sizeof(stack.cycles));