Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3446682imw; Thu, 7 Jul 2022 01:47:02 -0700 (PDT) X-Google-Smtp-Source: AGRyM1txS1n3veNEqJXtIhXJuXSlpzWbAzl13hFC4lZCFuW+dEY+ib7N3Wcdly1glpX6MOmEBh4J X-Received: by 2002:a17:907:3f0a:b0:726:324c:5bc2 with SMTP id hq10-20020a1709073f0a00b00726324c5bc2mr46005378ejc.32.1657183621876; Thu, 07 Jul 2022 01:47:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657183621; cv=none; d=google.com; s=arc-20160816; b=Ggyfo3OnnHVZ5ZKQPtZw9K9c8ga/jnM5RUhmfhdItvdMs/sIwHoV84Skr4wEH9jax7 NX8jjqMkr6oPr1/3Ljlb1r/1CXMW495h6i7Z6IRJIdgbHy4ao7/Vwk/8tUnGrmSpIeHt PIkHlGT1R1k/hTFtvM/oePzrj123LhFX8nKOQED6hD7EWeHzDn3wd6bn0/BYdk0gCqRw EdAk1fqz08gkFIYTTKUOni9c4IJHEmmQXbXGgTq9AmwGAp40dfDxzhBCTr6NNCNiCzSi 93hSQJh441lzap08HKhtPbcGuljRweO0gDDVIIeFsKiJ46cvNxNoZeJkIUazCP6TLRwE MjmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=fbF7XN+PSCjR2wd1XqHQhHktnV7QnxiWHEKRFy9Hp9o=; b=D8W1geGf0HgZ2GZ6yg+cX83Q//fHtsQM8ArutHy4pe4sDO3fMIAU5gwFXwKlXoWFDK 7uTaj5GbCiOI7fWsCQ8T6YcMPz+2RIAHN6n1OT+alssRC9tIYQkN7I14HbcsMMFpykdk fiYXwf/2R3VnOWKdoIEW8H3zRku9eCeCEw7Npzwuy0x6JrCYFYr4LBC+fPywyVnTnyQe 8ELKytnUMNkxnrjLyHaPDILjTpQSodwPKuCcSRZQG356oCMfTeaDPZak2cGWWhte9vqG 0XIOTMT896J3LYiHrUI9xFdwqg3v8nfd3w7KEiOy8pcDKNGoSg90zJ83GYsBqa5tpaSR Bgsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QduiOdNu; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hj1-20020a1709069fc100b0072846e4d504si9316789ejc.447.2022.07.07.01.46.36; Thu, 07 Jul 2022 01:47:01 -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=@kernel.org header.s=k20201202 header.b=QduiOdNu; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235516AbiGGIVq (ORCPT + 99 others); Thu, 7 Jul 2022 04:21:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235344AbiGGIVR (ORCPT ); Thu, 7 Jul 2022 04:21:17 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0AF114083; Thu, 7 Jul 2022 01:19:57 -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 916B4B81F1F; Thu, 7 Jul 2022 08:19:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DF964C3411E; Thu, 7 Jul 2022 08:19:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1657181995; bh=dlKZYg79yRfs31l7B5/WgBfVM2TrLooFz8zeZbYFV+4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=QduiOdNuFqVXHcTMs4SxGJaq1lG+dELES08mPqbFMLL+6gFYNszYUar1eRo36Xa/P zRR2GK4qKXGnMFxhZkB/NvKKJrUZnrWol/+RglB/4VzeLlat9aSaoUOZ91/WgeG51M qL144onb9+qe7IM+R2qxO7HIyf46LshybFDDUpbReLkFDsbi3MFWvDDSQNF7m4Qdvw Eh2Dx0cYfx0zcJQ6/xhiVwqpNzxFlfO1ySiDnAPz9b24rU89VWau/xxA3npTPYTu4p WL0By6eEwEJVzB9y219Cqw5xRtHnMbSpvZkbNWgc6AHxQV4b6sncPD+IuzEOg/x7wL zS/NWgN1c7a8Q== Date: Thu, 7 Jul 2022 09:19:50 +0100 From: Will Deacon To: Florian Westphal Cc: netfilter-devel@vger.kernel.org, regressions@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Zijlstra , Kajetan Puchalski Subject: Re: [PATCH nf v3] netfilter: conntrack: fix crash due to confirmed bit load reordering Message-ID: <20220707081949.GA4057@willie-the-truck> References: <20220706124007.GB7996@breakpoint.cc> <20220706145004.22355-1-fw@strlen.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220706145004.22355-1-fw@strlen.de> User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-7.8 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 On Wed, Jul 06, 2022 at 04:50:04PM +0200, Florian Westphal wrote: > Kajetan Puchalski reports crash on ARM, with backtrace of: > > __nf_ct_delete_from_lists > nf_ct_delete > early_drop > __nf_conntrack_alloc > > Unlike atomic_inc_not_zero, refcount_inc_not_zero is not a full barrier. > conntrack uses SLAB_TYPESAFE_BY_RCU, i.e. it is possible that a 'newly' > allocated object is still in use on another CPU: > > CPU1 CPU2 > encounter 'ct' during hlist walk > delete_from_lists > refcount drops to 0 > kmem_cache_free(ct); > __nf_conntrack_alloc() // returns same object > refcount_inc_not_zero(ct); /* might fail */ > > /* If set, ct is public/in the hash table */ > test_bit(IPS_CONFIRMED_BIT, &ct->status); > > In case CPU1 already set refcount back to 1, refcount_inc_not_zero() > will succeed. > > The expected possibilities for a CPU that obtained the object 'ct' > (but no reference so far) are: > > 1. refcount_inc_not_zero() fails. CPU2 ignores the object and moves to > the next entry in the list. This happens for objects that are about > to be free'd, that have been free'd, or that have been reallocated > by __nf_conntrack_alloc(), but where the refcount has not been > increased back to 1 yet. > > 2. refcount_inc_not_zero() succeeds. CPU2 checks the CONFIRMED bit > in ct->status. If set, the object is public/in the table. > > If not, the object must be skipped; CPU2 calls nf_ct_put() to > un-do the refcount increment and moves to the next object. > > Parallel deletion from the hlists is prevented by a > 'test_and_set_bit(IPS_DYING_BIT, &ct->status);' check, i.e. only one > cpu will do the unlink, the other one will only drop its reference count. > > Because refcount_inc_not_zero is not a full barrier, CPU2 may try to > delete an object that is not on any list: > > 1. refcount_inc_not_zero() successful (refcount inited to 1 on other CPU) > 2. CONFIRMED test also successful (load was reordered or zeroing > of ct->status not yet visible) > 3. delete_from_lists unlinks entry not on the hlist, because > IPS_DYING_BIT is 0 (already cleared). > > 2) is already wrong: CPU2 will handle a partially initited object > that is supposed to be private to CPU1. > > Add needed barriers when refcount_inc_not_zero() is successful. > > It also inserts a smp_wmb() before the refcount is set to 1 during > allocation. > > Because other CPU might still 'see' the object, refcount_set(1) > "resurrects" the object, so we need to make sure that other CPUs will > also observe the right contents. In particular, the CONFIRMED bit test > must only pass once the object is fully initialised and either in the > hash or about to be inserted (with locks held to delay possible unlink from > early_drop or gc worker). > > I did not change flow_offload_alloc(), as far as I can see it should call > refcount_inc(), not refcount_inc_not_zero(): the ct object is attached to > the skb so its refcount should be >= 1 in all cases. > > v2: prefer smp_acquire__after_ctrl_dep to smp_rmb (Will Deacon). > v3: keep smp_acquire__after_ctrl_dep close to refcount_inc_not_zero call > add comment in nf_conntrack_netlink, no control dependency there > due to locks. > > Cc: Peter Zijlstra > Reported-by: Kajetan Puchalski > Diagnosed-by: Will Deacon > Fixes: 719774377622 ("netfilter: conntrack: convert to refcount_t api") > Signed-off-by: Florian Westphal > --- > net/netfilter/nf_conntrack_core.c | 22 ++++++++++++++++++++++ > net/netfilter/nf_conntrack_netlink.c | 1 + > net/netfilter/nf_conntrack_standalone.c | 3 +++ > 3 files changed, 26 insertions(+) Acked-by: Will Deacon Will