Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp40402pxx; Mon, 26 Oct 2020 02:46:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyGCdrWwicp1pgRRf4rH1vqPbyaKSype+FKY20LmlQFtzS7gYX9zKIP4BCAFMuNWEKpvOFK X-Received: by 2002:a17:906:abce:: with SMTP id kq14mr15484222ejb.411.1603705609060; Mon, 26 Oct 2020 02:46:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603705609; cv=none; d=google.com; s=arc-20160816; b=iLetg52wLsr9UvcJMUKmWln4IJiz8Y874Ag8+sP9rqMGwD6BTzGYUX5qQM2BU7o1t4 Ecax+edXN8g6gbDN733Y28mQvUVXVQs3YWYAiZAx7iSqZj0rowGceKk+dWTF7E+TzauC 85jKYT9Uj7RBd8+TJJFj8aY3ZpaywD6i5B0FSGEPa41XTvnn1IZppQ0IoX2F5nqmAB0F aOoe6R8iFKbhhRnS4YK9CAaLHT9bmf6m+SeAjWdep8D6Io3pc7ozhF+8Btu6HHdxqDbQ 3GChJ+CMBAlOaaA4HGh7o+T/j2WNrzXfuJH5QetZ6eEpyb0G7EKDaHgx/nMf5jgxwziu KpcQ== 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=8ImwGHeAjlYta0cj5nMnO0LmXFk6/O5wbNMkJ97e8Tk=; b=WF3bg4XynoisYu1154P+YYrb2AqAMfD7VouZcpqqQsLIG1yM7kem3AjkL+8+/+bQ6W 7GqWOdYFqwUuHrVKGuflfkixLN956JQs8+/uuS0XzGO55Lker3dotGcs/lLWHggtpiVp 8/ZEyDZRS95lMcVdH0YYig1NSOKyWbT380l3n1fy9sywL6SrUjo378cynx2dxIofgM2a bsOxgbH6ZkLx4aHS9bGEQLWQq/s5ug9A1xqiGJD+j5Iq6ZTLJqsgqilDk/6Xk3gbP07U ELZkEu8+ThLcVxhe+G9u73T98+EsZY+49jYX2k/SgGCje3kClXZM2V+UtlvtV7X5gOgW TEkg== 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 p16si6373933edy.344.2020.10.26.02.46.24; Mon, 26 Oct 2020 02:46:49 -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 S1771577AbgJZIXm (ORCPT + 99 others); Mon, 26 Oct 2020 04:23:42 -0400 Received: from a.mx.secunet.com ([62.96.220.36]:39586 "EHLO a.mx.secunet.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1771568AbgJZIXm (ORCPT ); Mon, 26 Oct 2020 04:23:42 -0400 Received: from localhost (localhost [127.0.0.1]) by a.mx.secunet.com (Postfix) with ESMTP id 59A8020501; Mon, 26 Oct 2020 09:23:40 +0100 (CET) X-Virus-Scanned: by secunet Received: from a.mx.secunet.com ([127.0.0.1]) by localhost (a.mx.secunet.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id bBK94TIIxWU1; Mon, 26 Oct 2020 09:23:39 +0100 (CET) Received: from mail-essen-01.secunet.de (unknown [10.53.40.204]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by a.mx.secunet.com (Postfix) with ESMTPS id CBB11200A3; Mon, 26 Oct 2020 09:23:39 +0100 (CET) Received: from mbx-essen-01.secunet.de (10.53.40.197) by mail-essen-01.secunet.de (10.53.40.204) with Microsoft SMTP Server (TLS) id 14.3.487.0; Mon, 26 Oct 2020 09:23:39 +0100 Received: from gauss2.secunet.de (10.182.7.193) by mbx-essen-01.secunet.de (10.53.40.197) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2044.4; Mon, 26 Oct 2020 09:23:39 +0100 Received: by gauss2.secunet.de (Postfix, from userid 1000) id ABD2B3184555; Mon, 26 Oct 2020 09:23:38 +0100 (CET) Date: Mon, 26 Oct 2020 09:23:38 +0100 From: Steffen Klassert To: Zhuoliang Zhang CC: Herbert Xu , "David S . Miller" , Jakub Kicinski , Matthias Brugger , , , , , Subject: Re: [PATCH v2] net: xfrm: fix a race condition during allocing spi Message-ID: <20201026082338.GS31157@gauss3.secunet.de> 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.9.4 (2018-02-28) X-ClientProxiedBy: cas-essen-01.secunet.de (10.53.40.201) To mbx-essen-01.secunet.de (10.53.40.197) X-EXCLAIMER-MD-CONFIG: 2c86f778-e09b-4440-8b15-867914633a10 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 Applied, thanks a lot! One remark, please don't use base64 encoding when you send patches. I had to hand edit your patch to get it applied.