Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2718072pxb; Fri, 5 Nov 2021 03:36:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwkqsCp+pT/+8ax9RXhr8cvlZ87BH9maYXMBNHcPUoVmQ3P/LbnPBWf/RCUtRh58aHZSFja X-Received: by 2002:a17:906:1599:: with SMTP id k25mr15248097ejd.298.1636108598018; Fri, 05 Nov 2021 03:36:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1636108598; cv=none; d=google.com; s=arc-20160816; b=z79VNz4g2KqPstkT9nE71Y/7Baqt3wFLP/A+bAe0bWzvy6VLd+hLMuSjrl7FLIWP6B 1tSiqMbp7SrvUJFUGcNDwI21cKvV0y7qs6UW8fNP0pOImE/U12Eb0MSLk+2w+etm637s JhoUSYEOW5m3MpPYNQFM6AldiYNqu+WzRriOu1kZIeQLuL4fV3w3+CcSuPZtt1pF8SPC 37qp89J5+fTnwTxlaveuHkSnw8jkVe47SEw1Qf91L9sCL6x2LGhEijIrg+ALOl41nhTR pNTBNNtQQa3C4aPeLydBq9LniG5sNYTCij2Rz9jTyKMGuNr7o85MxgooEfW2o/fPO5Hn NAqA== 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=ED5hqMRTcKkR8duvSfDADS8w2Yi9GFl+urwsketXi4c=; b=dt7YoqLIqExwwSEKljIhfTi/mSQkzRyqNwtnCsszC1KT/M484T9XTACfdyoPu2Z2fi qNj9+Wu7aBB6kgPdWUtp+nsh3G3UCvjqf/dBruJKFwvHWU5CkZx4CBauJV+BFM8qv5Um Ca4NWzekYAKyG+FHIaa5CYf9EL5t2uztz/VJEngWaVFtcyjZPm1CVylTG8k373cIHwtz oFXEDWTI7Wx7ST4DQ9MpXxL//Af/8iw2tDpMAsGXMOKSvnGb2HpOBJoOy/rQtUJ/WNJN UvA0A4tA117AXuhTxX4+OUViaJr38HMDPNmyb+RjIeR7rJuETs3yDeCgEJYyaqk7Tgrr 6oHA== 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 c7si12578756edm.578.2021.11.05.03.36.14; Fri, 05 Nov 2021 03:36:38 -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 S232233AbhKEH4j (ORCPT + 99 others); Fri, 5 Nov 2021 03:56:39 -0400 Received: from isilmar-4.linta.de ([136.243.71.142]:37664 "EHLO isilmar-4.linta.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230395AbhKEH4i (ORCPT ); Fri, 5 Nov 2021 03:56:38 -0400 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 C0941201332; Fri, 5 Nov 2021 07:53:57 +0000 (UTC) Received: by light.dominikbrodowski.net (Postfix, from userid 1000) id 5AD76204B5; Fri, 5 Nov 2021 07:04:36 +0100 (CET) Date: Fri, 5 Nov 2021 07:04:36 +0100 From: Dominik Brodowski To: tytso@mit.edu Cc: "Ivan T. Ivanov" , Ard Biesheuvel , linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] 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 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 arm64 may provide bootloader entropy via EFI and via devicetree. 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-by: Ivan T. Ivanov Fixes: 18b915ac6b0a ("efi/random: Treat EFI_RNG_PROTOCOL output as bootloader randomness") Tested-by: Ivan T. Ivanov Signed-off-by: Dominik Brodowski --- v2->v3: onle one unlikely (Ard Biesheuvel) v1->v2: fix commit message; unmerge Reported-and-tested-by-tag (Ard Biesheuvel) drivers/char/random.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index 605969ed0f96..18fe804c1bf8 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 || crng_global_init_time == 0)) { crng_fast_load(buffer, count); return; }