Netfilter fixes for net
Subsystems (3)
Florian Westphal
maintainercover Netfilter fixes for net
Hi,
This PR has three patches for your *net* tree:
1. Silence warning about unused variable when CONFIG_NF_NAT=n, from Tom Rix.
2. nftables: Fix possible out-of-bounds access, from myself.
3. nftables: fix null deref+UAF during element insertion into rbtree, also from myself.
The following changes since commit ab87603b251134441a67385ecc9d3371be17b7a7:
net: wwan: t7xx: Ensure init is completed before system sleep (2023-05-17 13:02:25 +0100)
are available in the Git repository at:
https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git tags/nf-23-05-17
for you to fetch changes up to 61ae320a29b0540c16931816299eb86bf2b66c08:
netfilter: nft_set_rbtree: fix null deref on element insertion (2023-05-17 14:18:28 +0200)
----------------------------------------------------------------
Florian Westphal (2):
netfilter: nf_tables: fix nft_trans type confusion
netfilter: nft_set_rbtree: fix null deref on element insertion
Tom Rix (1):
netfilter: conntrack: define variables exp_nat_nla_policy and any_addr with CONFIG_NF_NAT
net/netfilter/nf_conntrack_netlink.c | 4 ++++ net/netfilter/nf_tables_api.c | 4 +--- net/netfilter/nft_set_rbtree.c | 20 +++++++++++++-------
Signature · 1 line
2.39.3
Florian Westphal
maintainerPATCH 1/3 netfilter: conntrack: define variables exp_nat_nla_policy and any_addr with CONFIG_NF_NAT
From: Tom Rix <[email protected]>
gcc with W=1 and ! CONFIG_NF_NAT net/netfilter/nf_conntrack_netlink.c:3463:32: error:
‘exp_nat_nla_policy’ defined but not used [-Werror=unused-const-variable=]
3463 | static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
| ^~~~~~~~~~~~~~~~~~net/netfilter/nf_conntrack_netlink.c:2979:33: error:
‘any_addr’ defined but not used [-Werror=unused-const-variable=]
2979 | static const union nf_inet_addr any_addr;
| ^~~~~~~~
These variables use is controlled by CONFIG_NF_NAT, so should their definitions.
--- net/netfilter/nf_conntrack_netlink.c | 4 ++++ 1 file changed, 4 insertions(+)
net/netfilter/nf_conntrack_netlink.c +4 -0
@@ -2976,7 +2976,9 @@ static int ctnetlink_exp_dump_mask(struct sk_buff *skb,return -1 ; } +#if IS_ENABLED (CONFIG_NF_NAT )static const union nf_inet_addr any_addr; +#endif static __be32 nf_expect_get_id (const struct nf_conntrack_expect *exp) {
@@ -3460,10 +3462,12 @@ ctnetlink_change_expect(struct nf_conntrack_expect *x,return 0 ; } +#if IS_ENABLED (CONFIG_NF_NAT )static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX +1 ] = { [CTA_EXPECT_NAT_DIR ] = { .type =NLA_U32 }, [CTA_EXPECT_NAT_TUPLE ] = { .type =NLA_NESTED }, }; +#endif static int ctnetlink_parse_expect_nat(const struct nlattr *attr,
Signature · 1 line
2.39.3
Florian Westphal
maintainerPATCH 2/3 netfilter: nf_tables: fix nft_trans type confusion
In reply to Florian Westphalnft_trans_FOO objects all share a common nft_trans base structure, but trailing fields depend on the real object size. Access is only safe after trans->msg_type check.
Check for rule type first. Found by code inspection.
--- net/netfilter/nf_tables_api.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
net/netfilter/nf_tables_api.c +1 -3
@@ -3865,12 +3865,10 @@ static struct nft_rule *nft_rule_lookup_byid(const struct net *net,struct nft_trans *trans;list_for_each_entry (trans, &nft_net->commit_list, list) { -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; +return nft_trans_rule (trans); }return ERR_PTR (-ENOENT ); }
Signature · 1 line
2.39.3
Florian Westphal
maintainerPATCH 3/3 netfilter: nft_set_rbtree: fix null deref on element insertion
In reply to Florian WestphalThere is no guarantee that rb_prev() will not return NULL in nft_rbtree_gc_elem():
general protection fault, probably for non-canonical address 0xdffffc0000000003: 0000 [#1] PREEMPT SMP KASAN KASAN: null-ptr-deref in range [0x0000000000000018-0x000000000000001f] nft_add_set_elem+0x14b0/0x2990 nf_tables_newsetelem+0x528/0xb30
Furthermore, there is a possible use-after-free while iterating, 'node' can be free'd so we need to cache the next value to use.
--- net/netfilter/nft_set_rbtree.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
net/netfilter/nft_set_rbtree.c +13 -7
@@ -221,7 +221,7 @@ static int nft_rbtree_gc_elem(const struct nft_set *__set, {struct nft_set *set = (struct nft_set *)__set;struct rb_node *prev =rb_prev (&rbe->node); -struct nft_rbtree_elem *rbe_prev; +struct nft_rbtree_elem *rbe_prev =NULL ;struct nft_set_gc_batch *gcb; gcb =nft_set_gc_batch_check (set,NULL ,GFP_ATOMIC );
@@ -229,17 +229,21 @@ static int nft_rbtree_gc_elem(const struct nft_set *__set,return -ENOMEM ;/* search for expired end interval coming before this element. */ -do { +while (prev) { rbe_prev = rb_entry(prev, struct nft_rbtree_elem, node);if (nft_rbtree_interval_end (rbe_prev))break ; prev =rb_prev (prev); - }while (prev !=NULL ); + } + +if (rbe_prev) { +rb_erase (&rbe_prev->node, &priv->root); +atomic_dec (&set->nelems); + } -rb_erase (&rbe_prev->node, &priv->root);rb_erase (&rbe->node, &priv->root); -atomic_sub (2 , &set->nelems); +atomic_dec (&set->nelems);nft_set_gc_batch_add (gcb, rbe);nft_set_gc_batch_complete (gcb);
@@ -268,7 +272,7 @@ static int __nft_rbtree_insert(const struct net *net, const struct nft_set *set, struct nft_set_ext **ext) {struct nft_rbtree_elem *rbe, *rbe_le =NULL , *rbe_ge =NULL ; -struct rb_node *node, *parent, **p, *first =NULL ; +struct rb_node *node, *next, *parent, **p, *first =NULL ;struct nft_rbtree *priv =nft_set_priv (set);u8 genmask =nft_genmask_next (net);int d, err;
@@ -307,7 +311,9 @@ static int __nft_rbtree_insert(const struct net *net, const struct nft_set *set,* Values stored in the tree are in reversed order, starting from * highest to lowest value. */ -for (node = first; node !=NULL ; node =rb_next (node)) { +for (node = first; node !=NULL ; node = next) { + next =rb_next (node); + rbe = rb_entry(node, struct nft_rbtree_elem, node);if (!nft_set_elem_active (&rbe->ext, genmask))
Signature · 1 line
2.39.3
Re: [PATCH net 1/3] netfilter: conntrack: define variables exp_nat_nla_policy and any_addr with CONFIG_NF_NAT
In reply to Florian WestphalThis series was applied to netdev/net.git (main) by Florian Westphal <[email protected]>:
Hello:
This series was applied to netdev/net.git (main)
by Florian Westphal <[email protected]>:
Quoted 5 lines · On Wed, 17 May 2023 14:37:54 +0200 you wrote:
From: Tom Rix <[email protected]>
gcc with W=1 and !
CONFIG_NF_NATnet/netfilter/nf_conntrack_netlink.c:3463:32: error:
‘exp_nat_nla_policy’ defined but not used [-Werror=unused-const-variable=]
Quoted 8 lines
3463 | static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = { | ^~~~~~~~~~~~~~~~~~
net/netfilter/nf_conntrack_netlink.c:2979:33: error:
‘any_addr’ defined but not used [-Werror=unused-const-variable=]
2979 | static const unionnf_inet_addrany_addr;
| ^~~~~~~~[...]
Here is the summary with links:
- [net,1/3] netfilter: conntrack: define variables exp_nat_nla_policy and any_addr with CONFIG_NF_NAT
https://git.kernel.org/netdev/net/c/224a876e3754 - [net,2/3] netfilter: nf_tables: fix nft_trans type confusion https://git.kernel.org/netdev/net/c/e3c361b8acd6 - [net,3/3] netfilter: nft_set_rbtree: fix null deref on element insertion https://git.kernel.org/netdev/net/c/61ae320a29b0
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html