Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3976088pxb; Tue, 25 Jan 2022 00:26:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJzL7tcvgg7CY8MdpsA+VZvDmiLoF53NAkd+lgnRrv2212CmZco9ueajoBARHm3IDopTOgLq X-Received: by 2002:a17:907:9712:: with SMTP id jg18mr15965262ejc.15.1643099186681; Tue, 25 Jan 2022 00:26:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643099186; cv=none; d=google.com; s=arc-20160816; b=GnM44h6giLXBeyK3+a5BPg7n2UwdqzKI4Et3eFPMrw2Nn8/luLkLUGu7GkBOJjOQDH iFiUOL9MmA3OcYvhzDbiDD073ayv/vf2fkIHVGUf7JLESxGp1RUnlOV2pu6t0KchY+/J KaZdmU1LQBJ+/WKENnyB2xdOLx9tyDEQbYfgQuounSiPKDrxVFwP2QLGt0cRqjqRnYIh 579B+6ZxV7/Ol4MxOXQPRA00gqBkX/FRpX/S5+YhdmkKrMrtCjtzZqJudQGpamjAzjXX PRMGPSLuFJW6/jTsxfiw4m2gflZkvQZqBn4iMmc6cb1ZPz4Khu8d1OzgVySqCDvWmGkk 6Q3Q== 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=8LYqJAzzAJZxehnKRMF2melppoaas8c91zDq1oTrDTE=; b=zZuu2whY8CcScVDdSFmXqjFgCHSEzrL49VZz0/j+E5rgZ8+1YLc7Tz6StzsXfmm0vA gFGQ79ORvjwnvubxoN3far72niusDvj6Ih8SDmfXrEiSn/oFtQLDq8ysBtgL6tlz/l9x /YhR7SXJiITVBNBFAbK3FgNHUYmci64z/6IMeyLU53Y3YBKiY+YGM339scAAFizTSc3L BceGJee/5zpWdg+gVkaLzlFfgNwPQ921qKFk4iGVvxK9YLbLG22GMGcPDzO1Mo0ZejFO 6lv0ZOCsbgypaf596RTfS8UqwqODSE9Rxave2gCkNPwhZ79rb5G7kUwxBuM5AolZNNML n1ag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=vfpfjgUk; 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 go12si9221445ejc.678.2022.01.25.00.26.01; Tue, 25 Jan 2022 00:26:26 -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=vfpfjgUk; 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 S3420595AbiAYCYw (ORCPT + 99 others); Mon, 24 Jan 2022 21:24:52 -0500 Received: from ams.source.kernel.org ([145.40.68.75]:38196 "EHLO ams.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346079AbiAXTM3 (ORCPT ); Mon, 24 Jan 2022 14:12:29 -0500 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 ams.source.kernel.org (Postfix) with ESMTPS id 28D1DB8123D; Mon, 24 Jan 2022 19:12:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 259BBC340E5; Mon, 24 Jan 2022 19:12:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643051545; bh=Vo4fc08dYU7Q2vdyaqh6jXfagzpgeNQG+pLuG1tBzYU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vfpfjgUkKnZPe4a5s4Pgfj64YPwMBfrHw1/ZCCJ2GD2lq4Q5+BEzRbFXrD9Tij+nL tEUs1XKAky8kELDicjfM7vegjBqLaZEMhO8jVBelg80lLvbSDHTSb481rxtEp9nHBr m9Esrd/QJP0JmdYdTv50XJLpMciHVpD1Y+9NcZgE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Biggers , "Paul E. McKenney" , "Jason A. Donenfeld" Subject: [PATCH 4.19 010/239] random: fix data race on crng init time Date: Mon, 24 Jan 2022 19:40:48 +0100 Message-Id: <20220124183943.439160423@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124183943.102762895@linuxfoundation.org> References: <20220124183943.102762895@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: Eric Biggers commit 009ba8568be497c640cab7571f7bfd18345d7b24 upstream. _extract_crng() does plain loads of crng->init_time and crng_global_init_time, which causes undefined behavior if crng_reseed() and RNDRESEEDCRNG modify these corrently. Use READ_ONCE() and WRITE_ONCE() to make the behavior defined. Don't fix the race on crng->init_time by protecting it with crng->lock, since it's not a problem for duplicate reseedings to occur. I.e., the lockless access with READ_ONCE() is fine. Fixes: d848e5f8e1eb ("random: add new ioctl RNDRESEEDCRNG") Fixes: e192be9d9a30 ("random: replace non-blocking pool with a Chacha20-based CRNG") Cc: stable@vger.kernel.org Signed-off-by: Eric Biggers Acked-by: Paul E. McKenney Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -969,7 +969,7 @@ static void crng_reseed(struct crng_stat crng->state[i+4] ^= buf.key[i] ^ rv; } memzero_explicit(&buf, sizeof(buf)); - crng->init_time = jiffies; + WRITE_ONCE(crng->init_time, jiffies); spin_unlock_irqrestore(&crng->lock, flags); if (crng == &primary_crng && crng_init < 2) { invalidate_batched_entropy(); @@ -996,12 +996,15 @@ static void crng_reseed(struct crng_stat static void _extract_crng(struct crng_state *crng, __u8 out[CHACHA20_BLOCK_SIZE]) { - unsigned long v, flags; + unsigned long v, flags, init_time; - if (crng_ready() && - (time_after(crng_global_init_time, crng->init_time) || - time_after(jiffies, crng->init_time + CRNG_RESEED_INTERVAL))) - crng_reseed(crng, crng == &primary_crng ? &input_pool : NULL); + if (crng_ready()) { + init_time = READ_ONCE(crng->init_time); + if (time_after(READ_ONCE(crng_global_init_time), init_time) || + time_after(jiffies, init_time + CRNG_RESEED_INTERVAL)) + crng_reseed(crng, crng == &primary_crng ? + &input_pool : NULL); + } spin_lock_irqsave(&crng->lock, flags); if (arch_get_random_long(&v)) crng->state[14] ^= v; @@ -2074,7 +2077,7 @@ static long random_ioctl(struct file *f, if (crng_init < 2) return -ENODATA; crng_reseed(&primary_crng, &input_pool); - crng_global_init_time = jiffies - 1; + WRITE_ONCE(crng_global_init_time, jiffies - 1); return 0; default: return -EINVAL;