Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp5740625pxu; Thu, 22 Oct 2020 09:51:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJykY/WdLyzYwo2S2JrzcBYxi0CLSH+CuEW0mfj2oWV1aHRAyQ3ke8+BWIsfkvWEtyPKju2i X-Received: by 2002:a17:906:580e:: with SMTP id m14mr3020145ejq.237.1603385487886; Thu, 22 Oct 2020 09:51:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603385487; cv=none; d=google.com; s=arc-20160816; b=Fe8IRv8juVcTPV1lnGHjpDa4jSrlimd+lOrNX74UPWFUi2XdiS40gYfO2pGnLHkBFP Uj7FCsFjymXkeIrLzIvoGtA1uznKS77NgcZ3CzG0WgsjtpvRzmr2uBJ8fdt7q3IM7Xna f3RB0B5zKwGEDsWv3A9ZM7QI+bxoc6yLnxo0YtIkVTfBSuQgOsyRljztSrl0sgsaOi5F tXGFRxgWJWEC27rUj0q16KWrIIkzMes+aq+mkIATnZ/viRtb6sO+8o0GHiO1XcSxvzG2 LoayhQb6+wRZmmkeuzR247473cGOZG5NkROqQNKDkP6hocSs+nO/86aekhNM4eN+p1cU f65w== 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; bh=5lvN3kDf7sdKCdp37znY7MGEWfytAK3zs6GNQ524WW4=; b=CYUOYTF/gZExzIp5S0Sk+ikzF5pUDJpYuf+fdG5P/E/U6w9fPzCQX4Fm6s17Y7y4Pw llUWboFG/cUHiEXCI2uT3NtW77m6eB7kHt97XVWsqKbMuH1ySjgFn5GeMpI+QPVVZLYo 7qvsjZaHCNWUTmIw15ZXmYbQgebzwnaKGWUnqW80VEMBQLYlSmMTSvjC5h1lRfyUbOQ1 b048bR83FSMb8/chQpGNUeLbgZBKMUHCFe8oCz2fD1u8u5GdwshzVN2smmg5Bdk/P0Y5 wRcnTvzhCGR1k+xQaz5poCbwTJ5620NcIs0rHx3WtQI0Ay/lcOkaOSOUI40b2muAeYZm Sr2g== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id by20si1629757ejc.528.2020.10.22.09.51.05; Thu, 22 Oct 2020 09:51:27 -0700 (PDT) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2898218AbgJVM3m (ORCPT + 99 others); Thu, 22 Oct 2020 08:29:42 -0400 Received: from helcar.hmeau.com ([216.24.177.18]:37662 "EHLO fornost.hmeau.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2898209AbgJVM3k (ORCPT ); Thu, 22 Oct 2020 08:29:40 -0400 Received: from gwarestrin.arnor.me.apana.org.au ([192.168.0.7]) by fornost.hmeau.com with smtp (Exim 4.92 #5 (Debian)) id 1kVZid-00048U-5g; Thu, 22 Oct 2020 23:29:24 +1100 Received: by gwarestrin.arnor.me.apana.org.au (sSMTP sendmail emulation); Thu, 22 Oct 2020 23:29:23 +1100 Date: Thu, 22 Oct 2020 23:29:23 +1100 From: Herbert Xu To: Zhuoliang Zhang Cc: Steffen Klassert , "David S . Miller" , Jakub Kicinski , Matthias Brugger , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, wsd_upstream@mediatek.com Subject: Re: [PATCH v2] net: xfrm: fix a race condition during allocing spi Message-ID: <20201022122923.GA16706@gondor.apana.org.au> References: <20201022100126.19565-1-zhuoliang.zhang@mediatek.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201022100126.19565-1-zhuoliang.zhang@mediatek.com> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 22, 2020 at 06:01:27PM +0800, Zhuoliang Zhang wrote: > From: zhuoliang zhang > > we found that the following race condition exists in > xfrm_alloc_userspi flow: > > user thread state_hash_work thread > ---- ---- > xfrm_alloc_userspi() > __find_acq_core() > /*alloc new xfrm_state:x*/ > xfrm_state_alloc() > /*schedule state_hash_work thread*/ > xfrm_hash_grow_check() xfrm_hash_resize() > xfrm_alloc_spi /*hold lock*/ > x->id.spi = htonl(spi) spin_lock_bh(&net->xfrm.xfrm_state_lock) > /*waiting lock release*/ xfrm_hash_transfer() > spin_lock_bh(&net->xfrm.xfrm_state_lock) /*add x into hlist:net->xfrm.state_byspi*/ > hlist_add_head_rcu(&x->byspi) > spin_unlock_bh(&net->xfrm.xfrm_state_lock) > > /*add x into hlist:net->xfrm.state_byspi 2 times*/ > hlist_add_head_rcu(&x->byspi) > > 1. a new state x is alloced in xfrm_state_alloc() and added into the bydst hlist > in __find_acq_core() on the LHS; > 2. on the RHS, state_hash_work thread travels the old bydst and tranfers every xfrm_state > (include x) into the new bydst hlist and new byspi hlist; > 3. user thread on the LHS gets the lock and adds x into the new byspi hlist again. > > So the same xfrm_state (x) is added into the same list_hash > (net->xfrm.state_byspi) 2 times that makes the list_hash become > an inifite loop. > > To fix the race, x->id.spi = htonl(spi) in the xfrm_alloc_spi() is moved > to the back of spin_lock_bh, sothat state_hash_work thread no longer add x > which id.spi is zero into the hash_list. > > Fixes: f034b5d4efdf ("[XFRM]: Dynamic xfrm_state hash table sizing.") > Signed-off-by: zhuoliang zhang > --- > net/xfrm/xfrm_state.c | 8 +++++--- > 1 file changed, 5 insertions(+), 3 deletions(-) Acked-by: Herbert Xu -- Email: Herbert Xu Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt