Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3724437pxb; Mon, 24 Jan 2022 16:23:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJxvGmbw5xOW/Ln9Ewb38a2UFgcpZ5cm3lIaO53fOKCDdX81N5pByd9GdN/a6YyV9eMSunyk X-Received: by 2002:a63:b147:: with SMTP id g7mr13535317pgp.4.1643070203665; Mon, 24 Jan 2022 16:23:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643070203; cv=none; d=google.com; s=arc-20160816; b=tJM5bPXcPeglgxj63vKgh8abLgWGQlqww/bGg3ejh+7Y06NydZBI2GMDaqDUzhTSgC MUL/UtUI57SmOZXFIO0W602UwkBbMSFGgJDROGXxrJQZ82yriBeiXWzAw07+xSBYDkMH y5h5RMSmdE15QfyG4H79UaegvbrsQK8u8xNHNy37GWnKQY2QmDld4jWR1CcI/4/Xe3+y hf+LP+shaD67EC45LNXjjQ2gCthpy2jqAwURhc1x4n6Qykuov+KN7PhVoCi9mr7Iy1h7 VTHLp38/oxkSG9rIOslFpQY6FNbYIjeSNgyBw+yZzfSSWRHlsOgmivgFB/3xNK12o0qr dxbg== 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=roqZ+9KEYOpAZsw9dLC3BUy84nGro+AakHCqEenSFLU=; b=i4setyCUT64Pl2tZn9uE86ykEKYrZhk3PVLdQsXmIp2gxGo+sdVObVlGaAoB7cbr5B sA5fC/hLWjy6pq/2HQG6JpRQRhaSk8QEoJtpdXPXmrFaf55K9I3q9b7quEqyzdmz7u9p x7coRnExQQ+UJHvxgN0r3eVBT7hej0IZRsLVcZdtJyqcPnWQuMZDq3Dty0QcI9MoXG8u VMI+KXaEXLxH539ZMSbqqCx45xcvvY3tgA6fYQZ1l/8h+2w3qberGYbgNG4Asa1qZ4q6 Q37q+fFvx4zlPScjsHVuwUtKlQyQxXj1Knh0QMgBObit7elTOPStil6031wILT7TMFaF ZmUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=lUGvagDk; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w11si13528297plp.584.2022.01.24.16.23.11; Mon, 24 Jan 2022 16:23:23 -0800 (PST) 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=@linuxfoundation.org header.s=korg header.b=lUGvagDk; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2374298AbiAYAQo (ORCPT + 99 others); Mon, 24 Jan 2022 19:16:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383051AbiAXX2E (ORCPT ); Mon, 24 Jan 2022 18:28:04 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A768CC01D7F3; Mon, 24 Jan 2022 13:31:24 -0800 (PST) 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 480A2614F3; Mon, 24 Jan 2022 21:31:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 30D27C340E9; Mon, 24 Jan 2022 21:31:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643059883; bh=OBTuXiREeeRTt0kQKSBldCdfbCJ4bnAco+QXL8c1owQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lUGvagDkcMAsSO7hKt1pFZDtAyzUCFiYiY6ofM7Hm+cSAHHUFLS8kxQrrN86XqKOj orG1xFw7XseDgT/FVZzn9mfk0j7r+pOMD4Pd9DIJD9SECRyk+8N/ERSgwnsjVZeycS crPTzo/6coEbwagCkuMRToZKVtc7lBgdFjf+UJnw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dominik Brodowski , "Jason A. Donenfeld" , Sasha Levin Subject: [PATCH 5.16 0724/1039] random: do not throw away excess input to crng_fast_load Date: Mon, 24 Jan 2022 19:41:53 +0100 Message-Id: <20220124184149.668774250@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184125.121143506@linuxfoundation.org> References: <20220124184125.121143506@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jason A. Donenfeld [ Upstream commit 73c7733f122e8d0107f88655a12011f68f69e74b ] When crng_fast_load() is called by add_hwgenerator_randomness(), we currently will advance to crng_init==1 once we've acquired 64 bytes, and then throw away the rest of the buffer. Usually, that is not a problem: When add_hwgenerator_randomness() gets called via EFI or DT during setup_arch(), there won't be any IRQ randomness. Therefore, the 64 bytes passed by EFI exactly matches what is needed to advance to crng_init==1. Usually, DT seems to pass 64 bytes as well -- with one notable exception being kexec, which hands over 128 bytes of entropy to the kexec'd kernel. In that case, we'll advance to crng_init==1 once 64 of those bytes are consumed by crng_fast_load(), but won't continue onward feeding in bytes to progress to crng_init==2. This commit fixes the issue by feeding any leftover bytes into the next phase in add_hwgenerator_randomness(). [linux@dominikbrodowski.net: rewrite commit message] Signed-off-by: Dominik Brodowski Signed-off-by: Jason A. Donenfeld Signed-off-by: Sasha Levin --- drivers/char/random.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index 7470ee24db2f9..a27ae3999ff32 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -912,12 +912,14 @@ static struct crng_state *select_crng(void) /* * crng_fast_load() can be called by code in the interrupt service - * path. So we can't afford to dilly-dally. + * path. So we can't afford to dilly-dally. Returns the number of + * bytes processed from cp. */ -static int crng_fast_load(const char *cp, size_t len) +static size_t crng_fast_load(const char *cp, size_t len) { unsigned long flags; char *p; + size_t ret = 0; if (!spin_trylock_irqsave(&primary_crng.lock, flags)) return 0; @@ -928,7 +930,7 @@ static int crng_fast_load(const char *cp, size_t len) p = (unsigned char *) &primary_crng.state[4]; while (len > 0 && crng_init_cnt < CRNG_INIT_CNT_THRESH) { p[crng_init_cnt % CHACHA_KEY_SIZE] ^= *cp; - cp++; crng_init_cnt++; len--; + cp++; crng_init_cnt++; len--; ret++; } spin_unlock_irqrestore(&primary_crng.lock, flags); if (crng_init_cnt >= CRNG_INIT_CNT_THRESH) { @@ -936,7 +938,7 @@ static int crng_fast_load(const char *cp, size_t len) crng_init = 1; pr_notice("fast init done\n"); } - return 1; + return ret; } /* @@ -1287,7 +1289,7 @@ void add_interrupt_randomness(int irq, int irq_flags) if (unlikely(crng_init == 0)) { if ((fast_pool->count >= 64) && crng_fast_load((char *) fast_pool->pool, - sizeof(fast_pool->pool))) { + sizeof(fast_pool->pool)) > 0) { fast_pool->count = 0; fast_pool->last = now; } @@ -2295,8 +2297,11 @@ void add_hwgenerator_randomness(const char *buffer, size_t count, struct entropy_store *poolp = &input_pool; if (unlikely(crng_init == 0)) { - crng_fast_load(buffer, count); - return; + size_t ret = crng_fast_load(buffer, count); + count -= ret; + buffer += ret; + if (!count || crng_init == 0) + return; } /* Suspend writing if we're above the trickle threshold. -- 2.34.1