Received: by 2002:a17:90a:9307:0:0:0:0 with SMTP id p7csp3964708pjo; Tue, 3 Mar 2020 10:11:46 -0800 (PST) X-Google-Smtp-Source: ADFU+vtBDskxq0ZgssqfkPjZV9bN65eDE7bM/CoU6RLIWe2B6Gaehp4mX8EN5N/tTKqkYATTk1K7 X-Received: by 2002:aca:bfc2:: with SMTP id p185mr3401220oif.57.1583259106536; Tue, 03 Mar 2020 10:11:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583259106; cv=none; d=google.com; s=arc-20160816; b=OLtXTYd3UJTNHsBI/o2Vdueq8oDuxr0ibg6nE+53t9tBm8gLiELxzMyrucfqRYzqjZ CvJYUDl+WToYuAUQ4O05OXrNjZuiBoL4bhFFMk44prPzFThQ+qPKoE2C9Io7HIhGtYQK IxazfaGw+d3PbXYTFF6d5wuLLrgoV+ptlXUCA14W9oBIkvy5gAOHFsjHDTXidkdoRDMK OBu/rtuAGpsnRHwBJcnB52lefwaJ962jDpz/OR0BMnxsS+traCJDmB4V2RcAeBh+KEx1 viGW+uagHmvLDxfcnCF5A0xKl/dwhNLX7WtUuUIcgmvyndutjPMARtfhHu6nKHqJ3BE7 NxUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=riOuLlZ4QOaDHDvAlauzOD4LkNnijvekPFP8/cu4P6w=; b=Iv6/3fv/i5ObPQoqtL22F3GgkNchMPWOc6y3Qnv7NuhM+5H9gFYGrRyxxLs20ZXFxT nWtmBA2M2rfjNqLlNFNLzr1DWSvlBBtGmybc7IM9EPXGWj981M9Gp2AjMVj5R4r81seL dhtS3EhZ8eNerwQzePMX7UX5kGOEzreSN6sKn3xfRJyFEvcq7EFomMyxGEVDEeGODQTz euC3/ToGfxCHRmD4G+XQWKHyeS5fqWdJe9PqOKePjJcmqlfGy85vDiJ1GopNLTHhrjRo kvZ5GzBjZQHuf/X5CfNCpnsqqgWZbasdLeUW2z08JG4WWTAjPCfRmDmdU+phZvR0Sv8i SQ4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=RvJ68nfb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d185si7383008oig.189.2020.03.03.10.11.34; Tue, 03 Mar 2020 10:11:46 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=RvJ68nfb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731866AbgCCRuO (ORCPT + 99 others); Tue, 3 Mar 2020 12:50:14 -0500 Received: from mail.kernel.org ([198.145.29.99]:57516 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731118AbgCCRuF (ORCPT ); Tue, 3 Mar 2020 12:50:05 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0E4F220870; Tue, 3 Mar 2020 17:50:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1583257804; bh=eroNwEbXR2gs16J1JOIuR9JCnEkWWNCI256UXJQXHsI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RvJ68nfbCFIZXkpZMCUNFzexEsfUSHrMH99qVtWGKHmit7ZqQAEK6JXuMly8KKeps i3AUTiRN0m6q9i3ojVF3uuB0pWz0cLT7MRjetZkxxFelyMO3Dez1X3SsbU6q2lCUZW /Sns9tXmcWk4bFd4jK4SHeZamIa/2uclc1cHiUIw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Florian Westphal , Cong Wang , Pablo Neira Ayuso , syzbot+adf6c6c2be1c3a718121@syzkaller.appspotmail.com Subject: [PATCH 5.5 106/176] netfilter: xt_hashlimit: reduce hashlimit_mutex scope for htable_put() Date: Tue, 3 Mar 2020 18:42:50 +0100 Message-Id: <20200303174317.107924611@linuxfoundation.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200303174304.593872177@linuxfoundation.org> References: <20200303174304.593872177@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Cong Wang commit c4a3922d2d20c710f827d3a115ee338e8d0467df upstream. It is unnecessary to hold hashlimit_mutex for htable_destroy() as it is already removed from the global hashtable and its refcount is already zero. Also, switch hinfo->use to refcount_t so that we don't have to hold the mutex until it reaches zero in htable_put(). Reported-and-tested-by: syzbot+adf6c6c2be1c3a718121@syzkaller.appspotmail.com Acked-by: Florian Westphal Signed-off-by: Cong Wang Signed-off-by: Pablo Neira Ayuso Signed-off-by: Greg Kroah-Hartman --- net/netfilter/xt_hashlimit.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) --- a/net/netfilter/xt_hashlimit.c +++ b/net/netfilter/xt_hashlimit.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #define XT_HASHLIMIT_ALL (XT_HASHLIMIT_HASH_DIP | XT_HASHLIMIT_HASH_DPT | \ @@ -114,7 +115,7 @@ struct dsthash_ent { struct xt_hashlimit_htable { struct hlist_node node; /* global list of all htables */ - int use; + refcount_t use; u_int8_t family; bool rnd_initialized; @@ -315,7 +316,7 @@ static int htable_create(struct net *net for (i = 0; i < hinfo->cfg.size; i++) INIT_HLIST_HEAD(&hinfo->hash[i]); - hinfo->use = 1; + refcount_set(&hinfo->use, 1); hinfo->count = 0; hinfo->family = family; hinfo->rnd_initialized = false; @@ -434,7 +435,7 @@ static struct xt_hashlimit_htable *htabl hlist_for_each_entry(hinfo, &hashlimit_net->htables, node) { if (!strcmp(name, hinfo->name) && hinfo->family == family) { - hinfo->use++; + refcount_inc(&hinfo->use); return hinfo; } } @@ -443,12 +444,11 @@ static struct xt_hashlimit_htable *htabl static void htable_put(struct xt_hashlimit_htable *hinfo) { - mutex_lock(&hashlimit_mutex); - if (--hinfo->use == 0) { + if (refcount_dec_and_mutex_lock(&hinfo->use, &hashlimit_mutex)) { hlist_del(&hinfo->node); + mutex_unlock(&hashlimit_mutex); htable_destroy(hinfo); } - mutex_unlock(&hashlimit_mutex); } /* The algorithm used is the Simple Token Bucket Filter (TBF)