Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3563929pxb; Mon, 24 Jan 2022 12:18:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJzHdU2ISiepoGo15bR4mgOQKliLrV29uMvAyT6ThqPExMXhG89GlNGgkHgccZY+K5rOc/2h X-Received: by 2002:a17:90a:e7d1:: with SMTP id kb17mr26108pjb.1.1643055525928; Mon, 24 Jan 2022 12:18:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643055525; cv=none; d=google.com; s=arc-20160816; b=wV9iFYbPOkQJyXVYjzw1VmX1FdqYlviSlhLOpeKjIT5FR2PMDKLdXf8kk6fKHs2SVC euZUa05+n3OuM0K3kuyXqgSBgOLMNADa2IIrvM/9EuozyfABP2l/2IDLUN6SUifhFdpd 5RHlzuuUBgiVnE2FCSsAxqs6g2/sZwFXzv0BJ3fcr5q57dCfZfadptcMZ/TgLeCyvrO0 5XQJMSqS7JDIVRoy9MVUZ4dZCYwT29YQEB09Zl4zD+DdcIXuDng1r1PmVi05CevvvGX6 AZz12hR1HZope2skN10p5KvaWqhLxpKUwBPMyuNTcfIeQkMW+sfwAr5QtVFAveOMsqP8 qsSQ== 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=aKwk/70kiCSrHraMzdsPPpc3/jZ4Nesm1l0IFdRCbWs=; b=BeT0a2B9wtfy86/2Pbmvj4tJKitvxIxx/DgwMbSi4DzpUuip3vZPKGw0gcZTUA8ULw RpHXT52He2Et5urZvX/G2BQ/dyo/qPHIca5nwKtNk4Gml38Cl+lrfDo0ww8iZBMRUgXo MxCESNNL+enpE6RdJAkt9cuicbbxygHAJ5mh4WeTBsbEni7xbjUX5T2Q2Gl7BphWLXXK K8g779u4BZA6ta0hh7kxp0Cv+BxB87XzSUazY/N83t1SkHRazL68n1+tqu4XXeIHeMrO tG4TdBDKY7U8yXlBI5DA+FUlKnoVs9y5fjRmzydZ7ZzSknhd5MnUYCmJq3Uhhtu8Zl/n TWhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=H3BX7Z+l; 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 c3si13910519pgi.447.2022.01.24.12.18.31; Mon, 24 Jan 2022 12:18:45 -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=H3BX7Z+l; 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 S1345198AbiAXTXj (ORCPT + 99 others); Mon, 24 Jan 2022 14:23:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344583AbiAXTNI (ORCPT ); Mon, 24 Jan 2022 14:13:08 -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 BEA16C0604E7; Mon, 24 Jan 2022 11:04:17 -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 5E66360010; Mon, 24 Jan 2022 19:04:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3F36FC340E5; Mon, 24 Jan 2022 19:04:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643051056; bh=3njCL0iPs9o1Ejs+3EW/XKFhmRx+GaQarQ66q9kIcNw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=H3BX7Z+lLshRyu0XAwvJvtpSFFsbHPGasPpuEmTWTcpWBMhrP/zRraHdJROxzrg1Y 9sjci4NrNdAB4gCjzHxeQgYgEce1f0bem4+qFei7O4054NXtab2eBkyqgeyIOJ7P/b jKdKuV5Xr0ebyIdZi3NOJobt8BQO1Qv/TrPKgioo= 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.14 008/186] random: fix data race on crng init time Date: Mon, 24 Jan 2022 19:41:23 +0100 Message-Id: <20220124183937.381172045@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124183937.101330125@linuxfoundation.org> References: <20220124183937.101330125@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 @@ -951,7 +951,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(); @@ -978,12 +978,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; @@ -1987,7 +1990,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;