Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp97230pxb; Wed, 3 Nov 2021 00:20:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzMdFxA8bpkz5obgNHCK3lh8g9OWzWgiWbh4aw68I/oIJUatvK9BS9HHv047Tu/bekYmeJ1 X-Received: by 2002:a17:907:6291:: with SMTP id nd17mr51331307ejc.194.1635924019859; Wed, 03 Nov 2021 00:20:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635924019; cv=none; d=google.com; s=arc-20160816; b=mA3mupRfKCRFozLOg4sAFtLMHAM7G/OKIbJgvrKdDp4wgp86Wrim3XiBvn7Ai7W8F7 dHrs8hjk2n7eY2wOPr3PF/NwCjeS8+wgvRLeUQ6+jXOq+L+qqka3EKrayfmdeuJBlixf ZmFmzjM+5kUfRMKUOOOucy9FHelO3SzBRxyj28L48OTJlj5cNhXGppwmafg19zKdDZR5 Db1hWPhcDk1XMOLG0mq65XkkLD5CTBr66lkYo78aY46wCqZYpnGiulB7XeL1MF+W+Tua NZLG6Wyal3KuSB/bY2hQHYIBQk+WYVUfW77XUyid5mANYIBe2Gt31mwfuFMN0HUusDPv 45Aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=jw486jwQ+PO+lPZsoa5B6A9Xr6kjaZNkxkLAg8lzRs4=; b=b1BJxbYeBRmy0ITEN05qjM3yxrRiRRYznZ0sm0a2wJm90+BPcG5Ig0zWn58wVmXVA9 vJ/sUNCZ1+0aKPp62eRjltOHpK4rjGI7fd0LyAzW9QroeKrP+C9HyygNYh63lIc73g5U 0rQcAoTBgctZNy2uxgzuS1iYNmqLqh4cgaK6hXsi/iJ10IL1Sr97ik49hwiWxqCQ0bJw aKKyYcGM9cuqsROiX37p/jB/4vqsmQLqWarrk7/cWFQIQEgErF2aktZyUBRTqXEMj4sN hHJzB8IC7qNtl9JaQIQpUEeBDj2eaV/ahkFsXuyAsdk8WGWFdMNzG/zeNTZhiRxeG7PO XKTg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id go39si2131078ejc.519.2021.11.03.00.19.56; Wed, 03 Nov 2021 00:20:19 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232071AbhKCHU2 (ORCPT + 99 others); Wed, 3 Nov 2021 03:20:28 -0400 Received: from isilmar-4.linta.de ([136.243.71.142]:56834 "EHLO isilmar-4.linta.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230152AbhKCHU1 (ORCPT ); Wed, 3 Nov 2021 03:20:27 -0400 X-isilmar-external: YES X-isilmar-external: YES X-isilmar-external: YES X-isilmar-external: YES X-isilmar-external: YES X-isilmar-external: YES Received: from light.dominikbrodowski.net (brodo.linta [10.2.0.102]) by isilmar-4.linta.de (Postfix) with ESMTPSA id 50D2120130D; Wed, 3 Nov 2021 07:17:49 +0000 (UTC) Received: by light.dominikbrodowski.net (Postfix, from userid 1000) id C1533204AD; Wed, 3 Nov 2021 08:14:02 +0100 (CET) Date: Wed, 3 Nov 2021 08:14:02 +0100 From: Dominik Brodowski To: Ard Biesheuvel Cc: "Theodore Y. Ts'o" , "Ivan T. Ivanov" , Bhupesh Sharma , linux-efi , Linux Kernel Mailing List Subject: Re: [PATCH] random: fix crash on multiple early calls to add_bootloader_randomness() Message-ID: References: <20211012082708.121931-1-iivanov@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Am Sun, Oct 31, 2021 at 01:33:34PM +0100 schrieb Ard Biesheuvel: > 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 Thanks, fixed in v2 > > 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 Well, it's used not all that rarely, but I don't care that much, so updated for v2. > Please don't drop the diffstat. Are you using git format-patch? For singular patches no; but fixed for v2. > > 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? As that would be a different change to the one necessary to resolve the bug, I'd like to defer that decision to the maintainer of random.c. Thanks, Dominik