Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp2266522imc; Tue, 12 Mar 2019 10:13:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqwZ/mIgZoseCOFr5YktPaGiibv7U5AVhlDK2gd91fYlD4wOjeZc3dHN9XS/E0tmPWC4fw36 X-Received: by 2002:a65:518b:: with SMTP id h11mr25937174pgq.41.1552410780683; Tue, 12 Mar 2019 10:13:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552410780; cv=none; d=google.com; s=arc-20160816; b=HEa3dQLr1LUoZ44xwKe2KNSLk6eIu/u5Tvx7ykqDBrgatIftk4C7lh09nLj92tuCg7 PgC1VX6S2gYvWdI02QBK8FgQw0PJt0H7eyE4EZ52RpNChLY49aIDLz0LE7FD8OWUgK45 78fzSQYYl60Iuz3H9jet5zd5xNRvY5YXIeYCit3TXO3zp23Xy5kwJ7qht0rqvPriXdK0 0MabV5Gj3rLb8SGzZDF014R3BYjc3rSPCvs6q+s16EQPpYEB9VfrOOaDnXpU8yT0Pb++ GhzUIcaWMCYq2GzaG1TbjUwcOeyf9lTmR3LCE94OEqQ/2w4b++koqKXE76ovKQ+oDvlV oC3w== 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=m0gGWngeSZnHqLNWvl9UgroyQtVCK80Uzs9yFFlitfw=; b=aulnnAIyNbTRzqiRnA1SFEELc9E38Hi0hwZf9rl32zonvae4SugNZdD4lfKlljwXnH Bc0VZKZ4Vh1A7brZUmreI6I0zpbeRBGclAW6D7HlTOfL8AtQs87OQsJSiHgILNH/Xmqr z0uxcYg+k4LdsT3KoikRQYWnFOt0cbQRXCxq4hknVEvxEi/Qa11dCQyc6QrxBgwfWLmf Rr0UWqEk9DqRC23uIITw5THjiNSS+kdSJUG14xc7Dvp7Wlaf8OAOkvbxnz2Y45jlEBvI qrNOFaitH9646HpZQMWOLJ58yMYASSU76sncUGN9Es6da6BQPiXDuNZqwPllHYmt6PNT 3Kgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=cHoMsPxt; 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 t18si7923583pgh.89.2019.03.12.10.12.44; Tue, 12 Mar 2019 10:13:00 -0700 (PDT) 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=cHoMsPxt; 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 S1727210AbfCLRL4 (ORCPT + 99 others); Tue, 12 Mar 2019 13:11:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:45664 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727100AbfCLRLq (ORCPT ); Tue, 12 Mar 2019 13:11:46 -0400 Received: from localhost (unknown [104.133.8.98]) (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 ACAA92173C; Tue, 12 Mar 2019 17:11:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1552410705; bh=3H3ebCvKD9TytECa5/AZ1ZJHGJXPZFwkvUltvkkytUI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cHoMsPxtjlJrnI9s+6pcbpXN8mc9Tlge/Pf5jpf1htToDipzY/UUH0AKdGHIsEnm1 WbczMN/aUkLN+Hqk8qW4Il9vw7GTfhVQHDXm5yWFdCl2YBF/LeM0nYx9723YW8UQB4 BtTxxQzaeX8xBl/9oq583jpaIOyxfgF4HbjRnEf0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Taehee Yoo , Florian Westphal , Pablo Neira Ayuso , Sasha Levin Subject: [PATCH 4.20 004/171] netfilter: nft_compat: use refcnt_t type for nft_xt reference count Date: Tue, 12 Mar 2019 10:06:24 -0700 Message-Id: <20190312170348.240062961@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190312170347.868927101@linuxfoundation.org> References: <20190312170347.868927101@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 4.20-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 12c44aba6618b7f6c437076e5722237190f6cd5f ] Using standard integer type was fine while all operations on it were guarded by the nftnl subsys mutex. This isn't true anymore: 1. transactions are guarded only by a pernet mutex, so concurrent rule manipulation in different netns is racy 2. the ->destroy hook runs from a work queue after the transaction mutex has been released already. cpu0 cpu1 (net 1) cpu2 (net 2) kworker nft_compat->destroy nft_compat->init nft_compat->init if (--nft_xt->ref == 0) nft_xt->ref++ nft_xt->ref++ Switch to refcount_t. Doing this however only fixes a minor aspect, nft_compat also performs linked-list operations in an unsafe way. This is addressed in the next two patches. Fixes: f102d66b335a ("netfilter: nf_tables: use dedicated mutex to guard transactions") Fixes: 0935d5588400 ("netfilter: nf_tables: asynchronous release") Reported-by: Taehee Yoo Signed-off-by: Florian Westphal Signed-off-by: Pablo Neira Ayuso Signed-off-by: Sasha Levin --- net/netfilter/nft_compat.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/net/netfilter/nft_compat.c b/net/netfilter/nft_compat.c index c90a4640723f..791f3e693e3d 100644 --- a/net/netfilter/nft_compat.c +++ b/net/netfilter/nft_compat.c @@ -26,7 +26,7 @@ struct nft_xt { struct list_head head; struct nft_expr_ops ops; - unsigned int refcnt; + refcount_t refcnt; /* Unlike other expressions, ops doesn't have static storage duration. * nft core assumes they do. We use kfree_rcu so that nft core can @@ -45,7 +45,7 @@ struct nft_xt_match_priv { static bool nft_xt_put(struct nft_xt *xt) { - if (--xt->refcnt == 0) { + if (refcount_dec_and_test(&xt->refcnt)) { list_del(&xt->head); kfree_rcu(xt, rcu_head); return true; @@ -273,7 +273,7 @@ nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr, return -EINVAL; nft_xt = container_of(expr->ops, struct nft_xt, ops); - nft_xt->refcnt++; + refcount_inc(&nft_xt->refcnt); return 0; } @@ -487,7 +487,7 @@ __nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr, return ret; nft_xt = container_of(expr->ops, struct nft_xt, ops); - nft_xt->refcnt++; + refcount_inc(&nft_xt->refcnt); return 0; } @@ -790,7 +790,7 @@ nft_match_select_ops(const struct nft_ctx *ctx, goto err; } - nft_match->refcnt = 0; + refcount_set(&nft_match->refcnt, 0); nft_match->ops.type = &nft_match_type; nft_match->ops.eval = nft_match_eval; nft_match->ops.init = nft_match_init; @@ -894,7 +894,7 @@ nft_target_select_ops(const struct nft_ctx *ctx, goto err; } - nft_target->refcnt = 0; + refcount_set(&nft_target->refcnt, 0); nft_target->ops.type = &nft_target_type; nft_target->ops.size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize)); nft_target->ops.init = nft_target_init; @@ -965,7 +965,7 @@ static void __exit nft_compat_module_exit(void) list_for_each_entry_safe(xt, next, &nft_target_list, head) { struct xt_target *target = xt->ops.data; - if (WARN_ON_ONCE(xt->refcnt)) + if (WARN_ON_ONCE(refcount_read(&xt->refcnt))) continue; module_put(target->me); kfree(xt); @@ -974,7 +974,7 @@ static void __exit nft_compat_module_exit(void) list_for_each_entry_safe(xt, next, &nft_match_list, head) { struct xt_match *match = xt->ops.data; - if (WARN_ON_ONCE(xt->refcnt)) + if (WARN_ON_ONCE(refcount_read(&xt->refcnt))) continue; module_put(match->me); kfree(xt); -- 2.19.1