Received: by 2002:a17:90a:9307:0:0:0:0 with SMTP id p7csp3949489pjo; Tue, 3 Mar 2020 09:57:55 -0800 (PST) X-Google-Smtp-Source: ADFU+vusNqDSq9XPSwqagbDtgyFg5EN4vaB46gFsa7OjoaaoA2wGa7+ao5ne3KT7kmQiUY8xBe6X X-Received: by 2002:a9d:5e9:: with SMTP id 96mr4303580otd.307.1583258274993; Tue, 03 Mar 2020 09:57:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583258274; cv=none; d=google.com; s=arc-20160816; b=C9ObRBFJfs5p8dx63zjKWIEzQgLhgIov+wT8BVWGijV/KPQw1NCesoXpOPzUZxiREl yZPbTsHxgelgpEVneViLmPQC5s239kTyvmjO/M/B6fZDDsBOXSLfb6BTyV4BeYvWudPN 81V4JgwMw0Nvw8Jiwlhatm23vUZ8irIFh5KB/NRsH1SizRgJ76dMt1R4EYG1xxPIF2lW obWGQM6CFtKFRxQWUhHqUexCZYlisXErFbVoDvq6rrEdla8MUICF+IZFPoRI5FzLVJA5 WCpkgafE8NxrYVb0uobadJ22H88tmloXKMKZS2IZRrSaA9veuwZo4SOM8me5YKGqIrg1 cCKg== 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=0LlaaZvUqlNRcVvZM4h+OCg4ZJORnkdPwp0NCNAv1bA525hmZEDNAivs0Xyv6LJ92v txtjlrFfE6c0ge4cAY8U452aJWzxXg455HdpUjip22ddTHQcpzf9hUBC8F2aisHQnND4 tqnuXGcb4w1QL30yfAOZvTWtkv1oCPplHyquB/zD2UJjHjO4uP/D7TskCIt5HUL5nUrB I2tRbrgOTFw7ZRqLuHxbsmdGfeqXNqbiWSOfspf70MCkHx4GdJPUCcGY43V/neohgccQ BuSxA/npqS7tJxi2ej8Bp8+JUFMWSJb6FkGq5/U0IcU6J5XoE/ZiUfDL9Rp5phTt7kv5 nZ7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Ta+EQeID; 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 o2si8907011otj.312.2020.03.03.09.57.43; Tue, 03 Mar 2020 09:57:54 -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=Ta+EQeID; 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 S1733021AbgCCR5L (ORCPT + 99 others); Tue, 3 Mar 2020 12:57:11 -0500 Received: from mail.kernel.org ([198.145.29.99]:39472 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733002AbgCCR5D (ORCPT ); Tue, 3 Mar 2020 12:57:03 -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 BF39420656; Tue, 3 Mar 2020 17:57:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1583258223; bh=eroNwEbXR2gs16J1JOIuR9JCnEkWWNCI256UXJQXHsI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ta+EQeID1Ea04OZ0GUuQx7SNHEn1vo6eXsfhwVtTEL+YY9dbQhiErjfUUb1kYyVu7 fm2QVQGsNhVQYiFMktbVuFcezR5oPZXRuyZs+7W3YgglwAqYWeH6GyL0XeFlJKJkt8 7JkJA7YI5oYP7GalT46vqpvTmISdDdB+rUhbG0w4= 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.4 091/152] netfilter: xt_hashlimit: reduce hashlimit_mutex scope for htable_put() Date: Tue, 3 Mar 2020 18:43:09 +0100 Message-Id: <20200303174312.916450565@linuxfoundation.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200303174302.523080016@linuxfoundation.org> References: <20200303174302.523080016@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)