Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp2911630iog; Mon, 27 Jun 2022 05:36:35 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tSb5og9hn78o7yCzk5lPJeVKxY8bR8VjrJQpv7T1lCVz6+yL6eG8CBqvF2HKzVMeFiGVFh X-Received: by 2002:a05:6402:2711:b0:437:7404:2229 with SMTP id y17-20020a056402271100b0043774042229mr13168228edd.413.1656333395689; Mon, 27 Jun 2022 05:36:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656333395; cv=none; d=google.com; s=arc-20160816; b=ZlAqHoK/1OxnJ0bgytpl+8IrAp9DRN1wiet5F7DPB/WiJdncx9WQVs3ee3APnOtG1L VpFy3d16MuVGAQ0Evk79nlDyomr68DNLFo+5glV8lFiUChnDdAlAAs50fnaubjkFzU3g k64GduVXMLTTxjdb8XcrBf+maqKMUJMzdeoaRK7Y9ARhudMf5aRPEKbK6EteXAOt6Rw9 mdsg6+Y9L4cXfnZDkg4OefIMPsw3MHB02RVCNsa4gBVFt9eoJdxg404lxUAc7Laiw6kM kxATZY1ee/ET5RqLR9Fu7C4a43n41oSqKy87CAXqM+Jeg1F6T3kHlj/kd6bqegfIDNl3 J80Q== 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=OfViwELaPBHrpDNin6J7BSndLiGb025kjg4gF5sFUsE=; b=HlTm6zpWsUQvzkXhli4qJHQui/z/uDa4pt6+3vcO+ON9AJ52VTx2V5+mCc1DpQ1iNl pdxKWvYoTIulGo1li8F7H7pK5cpwCOQAMBa5mNtXf5e5gxuGoeQBoNkVFbUVKqonGZ3Y oGgetYRz4A60lufSsxRHjzcBWXIyHi9b+ddId6yWolmJBhoa+qJ5gLPPvrom/CDp/FJB MrvGSRv+m3K5TC0aPmHzwISCgwqdo0SRXszZYAJNwKY6GozE6P0r7CKC33VyimBNnx6J IizFB0pPmZAKhFm3UCGGXzOEaW0werIU5pYz6WkVh83XLKCi9uNiK3Iv1KqN0yA5F9k7 VLAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QL9qIqh8; 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 gf12-20020a170906e20c00b006f92ef03c83si10048173ejb.609.2022.06.27.05.36.06; Mon, 27 Jun 2022 05:36:35 -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=QL9qIqh8; 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 S238436AbiF0Lxj (ORCPT + 99 others); Mon, 27 Jun 2022 07:53:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238162AbiF0Lry (ORCPT ); Mon, 27 Jun 2022 07:47:54 -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 7B4D4BC89; Mon, 27 Jun 2022 04:39:52 -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 EF5EAB8111B; Mon, 27 Jun 2022 11:39:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 44445C3411D; Mon, 27 Jun 2022 11:39:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656329989; bh=XCFHaJCagRpq0gbPYvTBh6fQ/FDmtnX7FM0YjYEetag=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QL9qIqh8hMxjJ7WUieVwCoENyTrzf39psxFOPeDt3EqBTa5B5bnkWl6aQhm/2L2mH t2SY1pSy8bcnXycjalFod9UlwDT1jdO1Pg6cV8gd0SUJVpG4fwJWoXuwEYe9UXBdRa zJqPiFDCj50Su8JWskaKxLG+ehWh7qUINBaemWHc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Florian Westphal , Pablo Neira Ayuso , Sasha Levin Subject: [PATCH 5.18 044/181] netfilter: use get_random_u32 instead of prandom Date: Mon, 27 Jun 2022 13:20:17 +0200 Message-Id: <20220627111945.839956837@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220627111944.553492442@linuxfoundation.org> References: <20220627111944.553492442@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.5 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: Florian Westphal [ Upstream commit b1fd94e704571f98b21027340eecf821b2bdffba ] bh might occur while updating per-cpu rnd_state from user context, ie. local_out path. BUG: using smp_processor_id() in preemptible [00000000] code: nginx/2725 caller is nft_ng_random_eval+0x24/0x54 [nft_numgen] Call Trace: check_preemption_disabled+0xde/0xe0 nft_ng_random_eval+0x24/0x54 [nft_numgen] Use the random driver instead, this also avoids need for local prandom state. Moreover, prandom now uses the random driver since d4150779e60f ("random32: use real rng for non-deterministic randomness"). Based on earlier patch from Pablo Neira. Fixes: 6b2faee0ca91 ("netfilter: nft_meta: place prandom handling in a helper") Fixes: 978d8f9055c3 ("netfilter: nft_numgen: add map lookups for numgen random operations") Signed-off-by: Florian Westphal Signed-off-by: Pablo Neira Ayuso Signed-off-by: Sasha Levin --- net/netfilter/nft_meta.c | 13 ++----------- net/netfilter/nft_numgen.c | 12 +++--------- 2 files changed, 5 insertions(+), 20 deletions(-) diff --git a/net/netfilter/nft_meta.c b/net/netfilter/nft_meta.c index ac4859241e17..55d2d49c3425 100644 --- a/net/netfilter/nft_meta.c +++ b/net/netfilter/nft_meta.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -32,8 +33,6 @@ #define NFT_META_SECS_PER_DAY 86400 #define NFT_META_DAYS_PER_WEEK 7 -static DEFINE_PER_CPU(struct rnd_state, nft_prandom_state); - static u8 nft_meta_weekday(void) { time64_t secs = ktime_get_real_seconds(); @@ -271,13 +270,6 @@ static bool nft_meta_get_eval_ifname(enum nft_meta_keys key, u32 *dest, return true; } -static noinline u32 nft_prandom_u32(void) -{ - struct rnd_state *state = this_cpu_ptr(&nft_prandom_state); - - return prandom_u32_state(state); -} - #ifdef CONFIG_IP_ROUTE_CLASSID static noinline bool nft_meta_get_eval_rtclassid(const struct sk_buff *skb, u32 *dest) @@ -389,7 +381,7 @@ void nft_meta_get_eval(const struct nft_expr *expr, break; #endif case NFT_META_PRANDOM: - *dest = nft_prandom_u32(); + *dest = get_random_u32(); break; #ifdef CONFIG_XFRM case NFT_META_SECPATH: @@ -518,7 +510,6 @@ int nft_meta_get_init(const struct nft_ctx *ctx, len = IFNAMSIZ; break; case NFT_META_PRANDOM: - prandom_init_once(&nft_prandom_state); len = sizeof(u32); break; #ifdef CONFIG_XFRM diff --git a/net/netfilter/nft_numgen.c b/net/netfilter/nft_numgen.c index 81b40c663d86..45d3dc9e96f2 100644 --- a/net/netfilter/nft_numgen.c +++ b/net/netfilter/nft_numgen.c @@ -9,12 +9,11 @@ #include #include #include +#include #include #include #include -static DEFINE_PER_CPU(struct rnd_state, nft_numgen_prandom_state); - struct nft_ng_inc { u8 dreg; u32 modulus; @@ -135,12 +134,9 @@ struct nft_ng_random { u32 offset; }; -static u32 nft_ng_random_gen(struct nft_ng_random *priv) +static u32 nft_ng_random_gen(const struct nft_ng_random *priv) { - struct rnd_state *state = this_cpu_ptr(&nft_numgen_prandom_state); - - return reciprocal_scale(prandom_u32_state(state), priv->modulus) + - priv->offset; + return reciprocal_scale(get_random_u32(), priv->modulus) + priv->offset; } static void nft_ng_random_eval(const struct nft_expr *expr, @@ -168,8 +164,6 @@ static int nft_ng_random_init(const struct nft_ctx *ctx, if (priv->offset + priv->modulus - 1 < priv->offset) return -EOVERFLOW; - prandom_init_once(&nft_numgen_prandom_state); - return nft_parse_register_store(ctx, tb[NFTA_NG_DREG], &priv->dreg, NULL, NFT_DATA_VALUE, sizeof(u32)); } -- 2.35.1