Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp1530209rdb; Mon, 2 Oct 2023 12:24:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGHJyJFcQStUuMM45EC6X4530ou/BCCP8HX1p1yEiJT713hnJLQhARgF7TeSqJgI3d4HGHP X-Received: by 2002:a17:90b:201:b0:26b:f67:9d1b with SMTP id fy1-20020a17090b020100b0026b0f679d1bmr9606934pjb.30.1696274678377; Mon, 02 Oct 2023 12:24:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696274678; cv=none; d=google.com; s=arc-20160816; b=ZLrPzgtrYB8LarataUl0l+e/cjiHfRRHZ1JOkE3DIOAl6pFFGC/62cGuRK/2R4F6BX rNRFLJlL5U32xpLXv45dnLzNPBtx3vEJrGvVJNZ7QZiNa8rZrIvlgPePQL1HVLAYfC3W MNmBHY/vltYbLNVMk++V1w9FwCWItJWyzAXpNdGPvrbVu5+0yCS8eZI/M2AKG6DAaVEp F96sNA2VYwPZYtNEdeCItvF4l5LbWceHgtGfsYBaA89YzwzUuzZDhR28LGOO6U5vBPiq bceCHBjfUO77xNXTq8rMSp5P7ZuEnlDBqXeymc+Vm6zbvCjxzOSw3nSDFF68nwCOjBoz 16GQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:message-id:in-reply-to :subject:cc:to:from:date; bh=jbkeV+gPUFDoBTbhWBElCOLad2wBOqbJLs3RpLBQCsQ=; fh=fW59Uxiw/7K3Gyt6XNdTx8eHfl7j8owpVbi26O2nAdw=; b=rsNZ/xlhn2/+2y6BK8kRWvkLbIdHT10sfUOasC09HcEpwqOtsudzUAbsdnVNdTndxl pOPBOCgJoRKmHNaUoUR20bsgQDwGb9I2ON6qI3HJBCbwljZBZDuG9F0Cf+yh3HU+wKSa 3nyi9wCmZ2dDbeh8J3H+IRMmpUe2n3YEgF4ZsETuB3HSg9r7tE7MT2V3mCXI0vXnzCPh VsLHFUpVlV32Hb4R+RSg+uSU5zeQtqMssCf99juahAE4xWe2XrJJWjjykM8s5lPl10TF /OhHeOPvO4zjHe/5Xc5OyjVGa0i+hT6TPklBVqezA2C7C06nqj1IxN+zMKpmlUHOBzmJ zVig== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id l191-20020a6388c8000000b0057884435a71si28301024pgd.396.2023.10.02.12.24.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:24:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 0EBC98225C85; Mon, 2 Oct 2023 12:14:11 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238519AbjJBTOK (ORCPT + 99 others); Mon, 2 Oct 2023 15:14:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229536AbjJBTOJ (ORCPT ); Mon, 2 Oct 2023 15:14:09 -0400 X-Greylist: delayed 449 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Mon, 02 Oct 2023 12:14:05 PDT Received: from smtp-out.kfki.hu (smtp-out.kfki.hu [148.6.0.48]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03B6BAC; Mon, 2 Oct 2023 12:14:04 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by smtp2.kfki.hu (Postfix) with ESMTP id 8E8BECC010C; Mon, 2 Oct 2023 21:06:28 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at smtp2.kfki.hu Received: from smtp2.kfki.hu ([127.0.0.1]) by localhost (smtp2.kfki.hu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP; Mon, 2 Oct 2023 21:06:26 +0200 (CEST) Received: from blackhole.kfki.hu (blackhole.szhk.kfki.hu [148.6.240.2]) by smtp2.kfki.hu (Postfix) with ESMTP id 3F575CC010A; Mon, 2 Oct 2023 21:06:23 +0200 (CEST) Received: by blackhole.kfki.hu (Postfix, from userid 1000) id E80B13431A9; Mon, 2 Oct 2023 21:06:23 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by blackhole.kfki.hu (Postfix) with ESMTP id E6836343155; Mon, 2 Oct 2023 21:06:23 +0200 (CEST) Date: Mon, 2 Oct 2023 21:06:23 +0200 (CEST) From: Jozsef Kadlecsik To: Simon Horman cc: xiaolinkui , Pablo Neira Ayuso , Florian Westphal , David Miller , edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, justinstitt@google.com, kuniyu@amazon.com, netfilter-devel@vger.kernel.org, coreteam@netfilter.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Linkui Xiao Subject: Re: [PATCH] netfilter: ipset: add ip_set lock to ip_set_test In-Reply-To: <20231002160651.GX92317@kernel.org> Message-ID: References: <20230927130309.30891-1-xiaolinkui@126.com> <20231002160651.GX92317@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 02 Oct 2023 12:14:11 -0700 (PDT) Hi, On Mon, 2 Oct 2023, Simon Horman wrote: > On Wed, Sep 27, 2023 at 09:03:09PM +0800, xiaolinkui wrote: > > From: Linkui Xiao > > > > If the ip_set is not locked during ip_set_test, the following situations > > may occur: > > > > CPU0 CPU1 > > ip_rcv-> > > ip_rcv_finish-> > > ip_local_deliver-> > > nf_hook_slow-> > > iptable_filter_hook-> > > ipt_do_table-> > > set_match_v4-> > > ip_set_test-> list_set_destroy-> > > hash_net4_kadt-> set->data = NULL > > I'm having a bit of trouble analysing this. > In particular, I'm concerned that in such a scenario set > itself will be also freed, which seems likely to lead to problems. > > Can you provide a more complete call stack for CPU1 ? ip_set_test() runs intentionally without holding a spinlock, it uses RCU. But I don't understand the scenario at all: CPU0: CPU1: hash_net4_kadt list_set_destroy so it's a hash:net type which works on a list of set type of sets only The list type of set can freely be destroyed (when not referenced), the destroy operation has no effect whatsoever on its possible hash:net type of member set. Moreover, kernel side add/del/test can only be performed when the set in question is referenced. Referenced sets cannot be deleted. So what is the scenario really in this case? Best regards, Jozsef > > h = set->data > > .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK) > > > > The set->data is empty, continuing to access set->data will result in a > > kernel NULL pointer. The call trace is as follows: > > > > [2350616.024418] Call trace: > > [2350616.024670] hash_net4_kadt+0x38/0x148 [ip_set_hash_net] > > [2350616.025147] ip_set_test+0xbc/0x230 [ip_set] > > [2350616.025549] set_match_v4+0xac/0xd0 [xt_set] > > [2350616.025951] ipt_do_table+0x32c/0x678 [ip_tables] > > [2350616.026391] iptable_filter_hook+0x30/0x40 [iptable_filter] > > [2350616.026905] nf_hook_slow+0x50/0x100 > > [2350616.027256] ip_local_deliver+0xd4/0xe8 > > [2350616.027616] ip_rcv_finish+0x90/0xb0 > > [2350616.027961] ip_rcv+0x50/0xb0 > > [2350616.028261] __netif_receive_skb_one_core+0x58/0x68 > > [2350616.028716] __netif_receive_skb+0x28/0x80 > > [2350616.029098] netif_receive_skb_internal+0x3c/0xa8 > > [2350616.029533] napi_gro_receive+0xf8/0x170 > > [2350616.029898] receive_buf+0xec/0xa08 [virtio_net] > > [2350616.030323] virtnet_poll+0x144/0x310 [virtio_net] > > [2350616.030761] net_rx_action+0x158/0x3a0 > > [2350616.031124] __do_softirq+0x11c/0x33c > > [2350616.031470] irq_exit+0x11c/0x128 > > [2350616.031793] __handle_domain_irq+0x6c/0xc0 > > [2350616.032172] gic_handle_irq+0x6c/0x170 > > [2350616.032528] el1_irq+0xb8/0x140 > > [2350616.032835] arch_cpu_idle+0x38/0x1c0 > > [2350616.033183] default_idle_call+0x24/0x58 > > [2350616.033549] do_idle+0x1a4/0x268 > > [2350616.033859] cpu_startup_entry+0x28/0x78 > > [2350616.034234] secondary_start_kernel+0x17c/0x1c8 > > > > Signed-off-by: Linkui Xiao > > --- > > net/netfilter/ipset/ip_set_core.c | 2 ++ > > 1 file changed, 2 insertions(+) > > > > diff --git a/net/netfilter/ipset/ip_set_core.c b/net/netfilter/ipset/ip_set_core.c > > index 35d2f9c9ada0..46f4f47e29e4 100644 > > --- a/net/netfilter/ipset/ip_set_core.c > > +++ b/net/netfilter/ipset/ip_set_core.c > > @@ -747,7 +747,9 @@ ip_set_test(ip_set_id_t index, const struct sk_buff *skb, > > !(opt->family == set->family || set->family == NFPROTO_UNSPEC)) > > return 0; > > > > + ip_set_lock(set); > > ret = set->variant->kadt(set, skb, par, IPSET_TEST, opt); > > + ip_set_unlock(set); > > > > if (ret == -EAGAIN) { > > /* Type requests element to be completed */ > > -- > > 2.17.1 > > > > > - E-mail : kadlec@blackhole.kfki.hu, kadlecsik.jozsef@wigner.hu PGP key : https://wigner.hu/~kadlec/pgp_public_key.txt Address : Wigner Research Centre for Physics H-1525 Budapest 114, POB. 49, Hungary