Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1995952pxb; Sun, 31 Oct 2021 05:35:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwrN+mn7mM8AORGZT3QGWqxtRh6Vq1xUU7p2rVaquHSHgHZXJ6i+Mx02tdW1uWwDKIOWGGj X-Received: by 2002:a6b:7802:: with SMTP id j2mr12773729iom.95.1635683720830; Sun, 31 Oct 2021 05:35:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635683720; cv=none; d=google.com; s=arc-20160816; b=IuFWw+doIGxvQUp7a1zJtsYDUXInOR646pFvvt2NKRRqaFXfb1b3VVV3FGgjb4JQNa doTdHTAwEBvSK+cLuoa7eiAlQstM5JJHsYdmqD/3xL5Vo1Db3wKITrTzwxCmXrjJKXZW 7dHY9JiPM8wgzdJ37RL9M/hIuqf4pPV1910qz3JlOuGDZGFb37EHo+qJ9nzsna4PDfpL yh5EdiHy5GUau2J7K/7rUlJhPJ/+y9m6rvtoPCoH0zjPv2fxrjHM6rTcdCR+3+O4TEbB 7kZdjAeLZ1k9kHVcKH/x0bv2HwGz3A3Jm7bG4Qc4AmuoIzX6ohNo7ayh09cHbHu6Fd/F 6lpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=Jc8QRFlrzwApmEjrCk6zfuzAMu1PJTwNxC6Z4zZJcao=; b=jFoOEj4IJ85HL1OcXDo9QpobOa/hybR3xQOa7/tC9UYmOs9/Bmz2D7wUuimDWW3DYN j72lHk9C/x605hjn0gpgQtpqz1Up4GIlpe6Fl6+oYQUaSqDUtSOBlFGEcu48YbnOZvDB NYeusMII8/26pH5exw4RbCh0f6mOVoywc036cmDJ8cxn+yC9ip+Z/SnNOKnozu2FDyi+ AB4VNhP+4RUziUQyUO5IoURZ02/B/tsrX1dURi1yWKaCnkYORHMUJ6cDq85V23IbxT0v DaZw4iNGLmhR/Hx5tQmxEg5Qz9iQN2szchsfDJRMKH8qL7Z4wlfoWesFc5nkcXp7in5J Lp3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Jv5eTWcV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f3si658736ilu.70.2021.10.31.05.35.08; Sun, 31 Oct 2021 05:35:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Jv5eTWcV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230219AbhJaMgS (ORCPT + 99 others); Sun, 31 Oct 2021 08:36:18 -0400 Received: from mail.kernel.org ([198.145.29.99]:35064 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229798AbhJaMgS (ORCPT ); Sun, 31 Oct 2021 08:36:18 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 83CF360FD9; Sun, 31 Oct 2021 12:33:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1635683626; bh=NRhO6hzo10XWUP2bFt6QNKXuMxBJ25uRfWxqDqY/+8M=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=Jv5eTWcVA2z5JT5Ug7EiUMbH52+8dNpb/VkCf+EPr30fedYWXUmNZFRJMIHIC3j+r 6T25N7FKIwciqwuAaqDfxutRyRRXKahzZpE5vN2Z3xenC7srxDMSnGZJTwZfoPxgg3 LFv5mOcXmWURzDBG8n2REorEsFWZtw7e9T/edFX1WZ6y4gNT/vLog7Y45vDUt23c+l afQMNXZ7/dK9gKdCxdTSmyXwBPFB76JE7z0dBOEmLYYERrazqEPj1lz5+vsSI2FMsn jWMcTKZFeojo3DC/wA5FUoQEAD8AXA5WIEJnUGNIxzFy0Z9eVvHITxn2lsNKrXh0dg gD7bmlVX1nChg== Received: by mail-oo1-f50.google.com with SMTP id m37-20020a4a9528000000b002b83955f771so5247282ooi.7; Sun, 31 Oct 2021 05:33:46 -0700 (PDT) X-Gm-Message-State: AOAM530AcGtmbNf9FcxCs6h74fxU5euTx4BNfwxcevcRwh+3+XEL1CEV urVfV43fqww3foh298vIB2Rms63eCeDTEEvNpDY= X-Received: by 2002:a4a:e93d:: with SMTP id a29mr15115163ooe.63.1635683625840; Sun, 31 Oct 2021 05:33:45 -0700 (PDT) MIME-Version: 1.0 References: <20211012082708.121931-1-iivanov@suse.de> In-Reply-To: From: Ard Biesheuvel Date: Sun, 31 Oct 2021 13:33:34 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] random: fix crash on multiple early calls to add_bootloader_randomness() To: Dominik Brodowski Cc: "Theodore Y. Ts'o" , "Ivan T. Ivanov" , Bhupesh Sharma , linux-efi , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, 31 Oct 2021 at 07:31, Dominik Brodowski wrote: > > If add_bootloader_randomness() or add_hwgenerator_randomness() is > called for the first time during early boot, crng_init equals 0. Then, > crng_fast_load() gets called -- which is safe to do even if the input > pool is not yet properly set up. > > If the added entropy suffices to increase crng_init to 1, future calls > to add_bootloader_randomness() or add_hwgenerator_randomness() used to > progress to credit_entropy_bits(). However, if the input pool is not yet > properly set up, the cmpxchg call within that function can lead to an > infinite recursion. This is not only a hypothetical problem, as qemu > on x86 may provide bootloader entropy via EFI and via devicetree. > arm64 not x86 > As crng_global_init_time is set to != 0 once the input pool is properly > set up, check (also) for this condition to determine which branch to take. > > Calls to crng_fast_load() do not modify the input pool; therefore, the > entropy_count for the input pool must not be modified at that early > stage. > > Reported-and-tested-by: Ivan T. Ivanov Nit: fancy tags like this are more difficult to grep for Better to use separate Reported-by and Tested-by tags > Fixes: 18b915ac6b0a ("efi/random: Treat EFI_RNG_PROTOCOL output as bootloader randomness") > Signed-off-by: Dominik Brodowski > Please don't drop the diffstat. Are you using git format-patch? > diff --git a/drivers/char/random.c b/drivers/char/random.c > index 605969ed0f96..4211ff3092f9 100644 > --- a/drivers/char/random.c > +++ b/drivers/char/random.c > @@ -1763,8 +1763,8 @@ static void __init init_std_data(struct entropy_store *r) > } > > /* > - * Note that setup_arch() may call add_device_randomness() > - * long before we get here. This allows seeding of the pools > + * add_device_randomness() or add_bootloader_randomness() may be > + * called long before we get here. This allows seeding of the pools > * with some platform dependent data very early in the boot > * process. But it limits our options here. We must use > * statically allocated structures that already have all > @@ -2274,7 +2274,12 @@ void add_hwgenerator_randomness(const char *buffer, size_t count, > { > struct entropy_store *poolp = &input_pool; > > - if (unlikely(crng_init == 0)) { > + /* We cannot do much with the input pool until it is set up in > + * rand_initalize(); therefore just mix into the crng state. > + * As this does not affect the input pool, we cannot credit > + * entropy for this. > + */ > + if (unlikely(crng_init == 0) || unlikely(crng_global_init_time == 0)) { Can we just drop the unlikely()s here? > crng_fast_load(buffer, count); > return; > }