Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6030463iog; Thu, 23 Jun 2022 09:55:07 -0700 (PDT) X-Google-Smtp-Source: AGRyM1usBKuivxQsa0hPHLWvH+pZmOXZjqzeR8P5CnZM/DDdBw4Wu3nAAlXcppUvguBNKjhjxO1S X-Received: by 2002:a17:907:2d8b:b0:711:f167:c818 with SMTP id gt11-20020a1709072d8b00b00711f167c818mr9157392ejc.318.1656003307527; Thu, 23 Jun 2022 09:55:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656003307; cv=none; d=google.com; s=arc-20160816; b=zVg5TYi1hEnb4c/YLl/2oKA3L+ApQOqYW87wKfOVKW3Dkvcys9O6LKovw4KKpxIxrq IzN3aX/c18hrwVwVqTz3+QkOqY62/hEOOi17IeLOBu35j284+42QyX+7pbebFfZMm7e8 aSUK3un+Vdy5kPTN3sfXnZeDivXDVisAWN2cFOFVTWiM69d/WK6TJLO9jXkiOxDcTogF UUWtEz5G+KGi/pXKE7NOeW1Ilop28PCqH4sYQaw7CG4KzVhy90ZNOFsumWXu+441bdWB PIEHO8liBZXndjS03jlktEAUkILIbr2sypiAZWDSD/2xJ4k6pgDIGqda8wFlLyL6tWrR 9GAg== 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=DfAF+370M/hx5YibuOqrJQVz/6HFWdOrwIfIDcSM58M=; b=aeYgORJBRTWn/r2uhbV3mMCW3NUKsHEcSBln4jMKpZpcz0YfAD/lRwSOabAHHmVPMo /8tZee3JkdHiSENsQKORyioLkMQXyQcr7idQE9UNmktRHgNY5GLSGXaeVFj1ntMc5VJG xJCk84aCBbRdgLVQ3IimoyA0Csi9MIjZ2Rf+F/c4gLrxDBK+mvdYR/LaOUELlhWlkUwp 0B6tFf6YXv+hqKpDZ2Wb9GmW/w3d8NduHuqr4AYZpOpbCM4LzgL5J1iCfBouDn5iHLW6 kq5h5Zqz81UDSSaweJt/U7rCtHAflsIwfjG+89dqNXG/vtTOSI7fjhabrw0yXyZQRbpS Ee2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=IYAO1xLM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s2-20020a17090699c200b00718cc500533si5445585ejn.608.2022.06.23.09.54.42; Thu, 23 Jun 2022 09:55:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=IYAO1xLM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S232694AbiFWQrE (ORCPT + 99 others); Thu, 23 Jun 2022 12:47:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232554AbiFWQqa (ORCPT ); Thu, 23 Jun 2022 12:46:30 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6FFCF4993F; Thu, 23 Jun 2022 09:46:29 -0700 (PDT) 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 1CB8FB82486; Thu, 23 Jun 2022 16:46:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6FBD8C3411B; Thu, 23 Jun 2022 16:46:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656002786; bh=c/H6JMemIThUjrcPl0YgwG+2zX/iYSUhorGwS/jkui8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IYAO1xLMeqPZHZAnjK9MBLGIxppZKG7hbUNhijg8kg7YgB+ALHDlKH4sdjYemTRLT vYC2wiWfiWqP2TMOfZgdGisBMoc8u4XRR10Sffvi+4PZi7swikOigMvM2zUpc0dWwI PC+0Bme9WOt+LjcQqKyKXqCsBNrm3OSBOW0JEr30= 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.9 025/264] random: fix data race on crng_node_pool Date: Thu, 23 Jun 2022 18:40:18 +0200 Message-Id: <20220623164344.781654325@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164344.053938039@linuxfoundation.org> References: <20220623164344.053938039@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Biggers commit 5d73d1e320c3fd94ea15ba5f79301da9a8bcc7de upstream. extract_crng() and crng_backtrack_protect() load crng_node_pool with a plain load, which causes undefined behavior if do_numa_crng_init() modifies it concurrently. Fix this by using READ_ONCE(). Note: as per the previous discussion https://lore.kernel.org/lkml/20211219025139.31085-1-ebiggers@kernel.org/T/#u, READ_ONCE() is believed to be sufficient here, and it was requested that it be used here instead of smp_load_acquire(). Also change do_numa_crng_init() to set crng_node_pool using cmpxchg_release() instead of mb() + cmpxchg(), as the former is sufficient here but is more lightweight. Fixes: 1e7f583af67b ("random: make /dev/urandom scalable for silly userspace programs") 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 Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -814,8 +814,8 @@ static void do_numa_crng_init(struct wor crng_initialize(crng); pool[i] = crng; } - mb(); - if (cmpxchg(&crng_node_pool, NULL, pool)) { + /* pairs with READ_ONCE() in select_crng() */ + if (cmpxchg_release(&crng_node_pool, NULL, pool) != NULL) { for_each_node(i) kfree(pool[i]); kfree(pool); @@ -828,8 +828,26 @@ static void numa_crng_init(void) { schedule_work(&numa_crng_init_work); } + +static struct crng_state *select_crng(void) +{ + struct crng_state **pool; + int nid = numa_node_id(); + + /* pairs with cmpxchg_release() in do_numa_crng_init() */ + pool = READ_ONCE(crng_node_pool); + if (pool && pool[nid]) + return pool[nid]; + + return &primary_crng; +} #else static void numa_crng_init(void) {} + +static struct crng_state *select_crng(void) +{ + return &primary_crng; +} #endif /*