Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3026831rwb; Mon, 15 Aug 2022 16:25:01 -0700 (PDT) X-Google-Smtp-Source: AA6agR6vJrGD92S+w1eKyn0Gq36/6S982lVxaK/e06/MHchUryQWbPrylnBrflZkPOxuiWdgNn4H X-Received: by 2002:a17:907:11c1:b0:731:5c53:d912 with SMTP id va1-20020a17090711c100b007315c53d912mr11557184ejb.354.1660605792595; Mon, 15 Aug 2022 16:23:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660605792; cv=none; d=google.com; s=arc-20160816; b=1Gg2GMs1DCnKW9Q5z5IvazVpuZQE6418nzmAq+VHR3XPE+6o54hsHj1EZhAvv8AFwI 7dY8mvYfMQbW5rjbCsDp121BsOfXmoNdwUrGivr2PmkIfRqjBIecoamdlbvtkgtZB6oV sPIB/JF9MenxkJIWD66O779uS/lZwaR/ThLEo68B39KZvx9fEY+wHdFfL5BAOBjlV5pi vpCBe1dy1PuFarHlrwLBowq4A6F/DLMzj7rwz73aB6JJMtg01CgwEz2A/A4cRCiR35ii 1jRkmRDKt4924x3/PRffQbcG911KwGONQIViSwjDWurbVcpmpEamlYXsHOWIlJQaLgXk CRmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=LwRLPDplQFlqw/JsuUS0GJxoZgfUfEkuwyDrZUboiv4=; b=N7XEFSPI/fjlnB4jkpvhF6XOfspkn/cS1RrKWsdpRj7mcAnK0ZDQIKZl2hIhMB/r4p +vYgm1Fn5qwKQnJ1+IvZyQxkEKHzOJPNzdSrXD0LxC1/nxbfC5Y5UboHC9vBeVvaF9qw d+kqsQfqnbYHJSpYWT2u97V6zatDcpaW9cWCasgv9kmg2KPx2TbRLIjouSuYrJc1agCn dCzX/TD/Ql6wk2WS+jcT0gacglgFGTSmWRJgYT1sF3GakP2KrnXBrbkfdkEc7fHfMNBK ebn1xYRXVIFBpiSLjvxQIFXPTAZkOhYvcQN06KE0S5ZvMYZ9ZO8bUMCBZhjPh3fDbSe0 qbiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=B7DVEEFR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ds15-20020a170907724f00b0072f43ed6e7dsi9250915ejc.351.2022.08.15.16.22.47; Mon, 15 Aug 2022 16:23:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=B7DVEEFR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348853AbiHOW33 (ORCPT + 99 others); Mon, 15 Aug 2022 18:29:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348357AbiHOWXI (ORCPT ); Mon, 15 Aug 2022 18:23:08 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD9951256DE; Mon, 15 Aug 2022 12:44:06 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id AA6636068D; Mon, 15 Aug 2022 19:44:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 964A2C433C1; Mon, 15 Aug 2022 19:44:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660592645; bh=dTvCjq362IS86Z1I6lrrYgBBtQVKVYdvi1jWP7T5AoU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B7DVEEFRB3I1HvmzNmmJtO4NXPhdO8ZY3bZL65ajEhYheHQono9WuYK8Vg5tLUL+H 36kqA3iYOFCCLRBVaJQABwr7A9sNw4XWZHJnNEz2TNQ1WCV5xhrBT4ucZPKw+LFDh2 Y8S/hWB5nFHMaffPZ6urQu9us/dFRTkfE36teH1w= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Thadeu Lima de Souza Cascardo , Pablo Neira Ayuso Subject: [PATCH 5.19 0142/1157] netfilter: nf_tables: do not allow RULE_ID to refer to another chain Date: Mon, 15 Aug 2022 19:51:38 +0200 Message-Id: <20220815180445.317714795@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180439.416659447@linuxfoundation.org> References: <20220815180439.416659447@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thadeu Lima de Souza Cascardo commit 36d5b2913219ac853908b0f1c664345e04313856 upstream. When doing lookups for rules on the same batch by using its ID, a rule from a different chain can be used. If a rule is added to a chain but tries to be positioned next to a rule from a different chain, it will be linked to chain2, but the use counter on chain1 would be the one to be incremented. When looking for rules by ID, use the chain that was used for the lookup by name. The chain used in the context copied to the transaction needs to match that same chain. That way, struct nft_rule does not need to get enlarged with another member. Fixes: 1a94e38d254b ("netfilter: nf_tables: add NFTA_RULE_ID attribute") Fixes: 75dd48e2e420 ("netfilter: nf_tables: Support RULE_ID reference in new rule") Signed-off-by: Thadeu Lima de Souza Cascardo Cc: Signed-off-by: Pablo Neira Ayuso Signed-off-by: Greg Kroah-Hartman --- net/netfilter/nf_tables_api.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) --- a/net/netfilter/nf_tables_api.c +++ b/net/netfilter/nf_tables_api.c @@ -3373,6 +3373,7 @@ static int nft_table_validate(struct net } static struct nft_rule *nft_rule_lookup_byid(const struct net *net, + const struct nft_chain *chain, const struct nlattr *nla); #define NFT_RULE_MAXEXPRS 128 @@ -3461,7 +3462,7 @@ static int nf_tables_newrule(struct sk_b return PTR_ERR(old_rule); } } else if (nla[NFTA_RULE_POSITION_ID]) { - old_rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_POSITION_ID]); + old_rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_POSITION_ID]); if (IS_ERR(old_rule)) { NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]); return PTR_ERR(old_rule); @@ -3606,6 +3607,7 @@ err_release_expr: } static struct nft_rule *nft_rule_lookup_byid(const struct net *net, + const struct nft_chain *chain, const struct nlattr *nla) { struct nftables_pernet *nft_net = nft_pernet(net); @@ -3616,6 +3618,7 @@ static struct nft_rule *nft_rule_lookup_ struct nft_rule *rule = nft_trans_rule(trans); if (trans->msg_type == NFT_MSG_NEWRULE && + trans->ctx.chain == chain && id == nft_trans_rule_id(trans)) return rule; } @@ -3665,7 +3668,7 @@ static int nf_tables_delrule(struct sk_b err = nft_delrule(&ctx, rule); } else if (nla[NFTA_RULE_ID]) { - rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_ID]); + rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_ID]); if (IS_ERR(rule)) { NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]); return PTR_ERR(rule);