Skip to contentSkip to conversation

netfilter: nf_tables: fix use counter for rule

PATCH In review [email protected] 2 participants 10 messages Dec 18, 2019 – Dec 20, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover netfilter: nf_tables: fix use counter for rule

From: wenxu <[email protected]>

wenxu (3):
netfilter: nf_tables: fix rule release in err path
netfilter: nf_tables: fix miss activate operation in the
netfilter: nf_tables: fix miss dec set use counter in the
nf_tables_destroy_set

 net/netfilter/nf_tables_api.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/3 netfilter: nf_tables: fix rule release in err path

From: wenxu <[email protected]>

The err2 failed path in nf_tables_newrule fail err2 should only destory this new rule without deactivate it. Because the rule is not been activated.

---
 net/netfilter/nf_tables_api.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +1 -1
index a8caf73..27e6a6f 100644
@@ -3130,7 +3130,7 @@ static int nf_tables_newrule(struct net *net, struct sock *nlsk,
 
 	return 0;
 err2:
-	nf_tables_rule_release(&ctx, rule);
+	nf_tables_rule_destroy(&ctx, rule);
 err1:
 	for (i = 0; i < n; i++) {
 		if (info[i].ops) {
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [PATCH nf 1/3] netfilter: nf_tables: fix rule release in err path

Quoted 15 lines · On Wed, Dec 18, 2019 at 10:59:11PM +0800, [email protected] wrote:

From: wenxu <[email protected]>

The err2 failed path in nf_tables_newrule fail err2 should only destory this new rule without deactivate it. Because the rule is not been activated.

---
 net/netfilter/nf_tables_api.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +0 -0
index a8caf73..27e6a6f 100644

net/netfilter/nf_tables_api.c static int nf_tables_newrule(struct net *net, struct sock *nlsk, · line 3133

Quoted 5 lines
net/netfilter/nf_tables_api.c +1 -1
@@ -3130,7 +3130,7 @@ static int nf_tables_newrule(struct net *net, struct sock *nlsk,
 
 	return 0;
 err2:
-	nf_tables_rule_release(&ctx, rule);
+	nf_tables_rule_destroy(&ctx, rule);

This is not correct, the rule might have a reference to a chain jump, nft_data_release() needs to be called in that case.

Quoted 6 lines
net/netfilter/nf_tables_api.c +0 -0
 err1:
 	for (i = 0; i < n; i++) {
 		if (info[i].ops) {
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 2/3 netfilter: nf_tables: fix miss activate operation in the

In reply to [email protected]

From: wenxu <[email protected]>

nf_tables_commit for NFT_MSG_NEWRULE

The new create rule should be activated in the nf_tables_commit.

create a flowtable:
nft add table firewall
nft add flowtable firewall fb1 { hook ingress priority 2 \; devices = { tun1, mlx_pf0vf0 } \; } nft add chain firewall ftb-all {type filter hook forward priority 0 \; policy accept \; } nft add rule firewall ftb-all ct zone 1 ip protocol tcp flow offload @fb1
nft add rule firewall ftb-all ct zone 1 ip protocol udp flow offload @fb1

delete the related rule:
nft delete chain firewall ftb-all

The flowtable can be deleted
nft delete flowtable firewall fb1

But failed with: Device is busy

The nf_flowtable->use is not zero for no activated operation.

---
 net/netfilter/nf_tables_api.c | 1 +
 1 file changed, 1 insertion(+)
net/netfilter/nf_tables_api.c +1 -0
index 27e6a6f..174b362 100644
@@ -7101,6 +7101,7 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)
 			nf_tables_rule_notify(&trans->ctx,
 					      nft_trans_rule(trans),
 					      NFT_MSG_NEWRULE);
+			nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans));
 			nft_trans_destroy(trans);
 			break;
 		case NFT_MSG_DELRULE:
Signature · 1 line
1.8.3.1
#4 lore raw

Re: [PATCH nf 2/3] netfilter: nf_tables: fix miss activate operation in the

Quoted 16 lines · On Wed, Dec 18, 2019 at 10:59:12PM +0800, [email protected] wrote:

From: wenxu <[email protected]>

nf_tables_commit for NFT_MSG_NEWRULE

The new create rule should be activated in the nf_tables_commit.

create a flowtable:
nft add table firewall
nft add flowtable firewall fb1 { hook ingress priority 2 \; devices = { tun1, mlx_pf0vf0 } \; } nft add chain firewall ftb-all {type filter hook forward priority 0 \; policy accept \; } nft add rule firewall ftb-all ct zone 1 ip protocol tcp flow offload @fb1
nft add rule firewall ftb-all ct zone 1 ip protocol udp flow offload @fb1

delete the related rule:
nft delete chain firewall ftb-all

Quoted 6 lines

The flowtable can be deleted
nft delete flowtable firewall fb1

But failed with: Device is busy

The nf_flowtable->use is not zero for no activated operation.

This is correct.

Quoted 10 lines
---
 net/netfilter/nf_tables_api.c | 1 +
 1 file changed, 1 insertion(+)
net/netfilter/nf_tables_api.c +0 -0
index 27e6a6f..174b362 100644

net/netfilter/nf_tables_api.c static int nf_tables_commit(struct net *net, struct sk_buff *skb) · line 7104

Quoted 4 lines
net/netfilter/nf_tables_api.c +1 -0
@@ -7101,6 +7101,7 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)
 			nf_tables_rule_notify(&trans->ctx,
 					      nft_trans_rule(trans),
 					      NFT_MSG_NEWRULE);
+			nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans));

I don't think this fix is correct, probably this patch?

net/netfilter/nft_flow_offload.c +3 -0
index dd82ff2ee19f..f1280321b129 100644
@@ -186,6 +186,9 @@ static void nft_flow_offload_deactivate(const struct nft_ctx *ctx,
 {
 	struct nft_flow_offload *priv = nft_expr_priv(expr);
 
+	if (phase == NFT_TRANS_COMMIT)
+		return;
+
 	nf_tables_deactivate_flowtable(ctx, priv->flowtable, phase);
 }
 
author
#5 lore raw

Re: [PATCH nf 2/3] netfilter: nf_tables: fix miss activate operation in the

Quoted 30 lines · Pablo Neira Ayuso
Quoted 16 lines · On Wed, Dec 18, 2019 at 10:59:12PM +0800, [email protected] wrote:

From: wenxu <[email protected]>

nf_tables_commit for NFT_MSG_NEWRULE

The new create rule should be activated in the nf_tables_commit.

create a flowtable:
nft add table firewall
nft add flowtable firewall fb1 { hook ingress priority 2 \; devices = { tun1, mlx_pf0vf0 } \; } nft add chain firewall ftb-all {type filter hook forward priority 0 \; policy accept \; } nft add rule firewall ftb-all ct zone 1 ip protocol tcp flow offload @fb1
nft add rule firewall ftb-all ct zone 1 ip protocol udp flow offload @fb1

delete the related rule:
nft delete chain firewall ftb-all

Quoted 6 lines

The flowtable can be deleted
nft delete flowtable firewall fb1

But failed with: Device is busy

The nf_flowtable->use is not zero for no activated operation.

This is correct.

Quoted 5 lines
---
 net/netfilter/nf_tables_api.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 10 lines

net/netfilter/nf_tables_api.c static int nf_tables_commit(struct net *net, struct sk_buff *skb) · line 7104

Quoted 9 lines
net/netfilter/nf_tables_api.c +1 -0
index 27e6a6f..174b362 100644
@@ -7101,6 +7101,7 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)
 			nf_tables_rule_notify(&trans->ctx,
 					      nft_trans_rule(trans),
 					      NFT_MSG_NEWRULE);
+			nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans));

I don't think this fix is correct, probably this patch?

Maybe your patch is also not correct.    The  nf_tables_deactivate_flowtable already ignore

the NFT_TRANS_COMMIT.

void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx, struct nft_flowtable *flowtable, enum nft_trans_phase phase) {
        switch (phase) {
        case NFT_TRANS_PREPARE:
        case NFT_TRANS_ABORT:
        case NFT_TRANS_RELEASE:
                flowtable->use--;
                /* fall through */
        default:
                return;
        }
}

Nft_flow_offload  inc the use counter , when delete the rule and dec it in deactivate with phase NFT_TRANS_PREPARE.

So the nft_flow_offload_destroy should not dec the use?

So the patch should be as following.

static void nft_flow_offload_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) {
        struct nft_flow_offload *priv = nft_expr_priv(expr);

-        priv->flowtable->use--;
        nf_ct_netns_put(ctx->net, ctx->family);
}

The rule should be like the following?

Create rule nft_xx_init   inc the use counter,  If the rule create failed just deactivate it

Delete the rule  deactivate dec the use counter, If the rule delete failed just activate it

BR

wenxu

#6 lore raw

Re: [PATCH nf 2/3] netfilter: nf_tables: fix miss activate operation in the

Quoted 27 lines · wenxu
Quoted 26 lines · Pablo Neira Ayuso
Quoted 16 lines · On Wed, Dec 18, 2019 at 10:59:12PM +0800, [email protected] wrote:

From: wenxu <[email protected]>

nf_tables_commit for NFT_MSG_NEWRULE

The new create rule should be activated in the nf_tables_commit.

create a flowtable:
nft add table firewall
nft add flowtable firewall fb1 { hook ingress priority 2 \; devices = { tun1, mlx_pf0vf0 } \; } nft add chain firewall ftb-all {type filter hook forward priority 0 \; policy accept \; } nft add rule firewall ftb-all ct zone 1 ip protocol tcp flow offload @fb1
nft add rule firewall ftb-all ct zone 1 ip protocol udp flow offload @fb1

delete the related rule:
nft delete chain firewall ftb-all

Quoted 6 lines

The flowtable can be deleted
nft delete flowtable firewall fb1

But failed with: Device is busy

The nf_flowtable->use is not zero for no activated operation.

This is correct.

Quoted 1 line

[...]

Quoted 12 lines

So the patch should be as following.

static void nft_flow_offload_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) {
        struct nft_flow_offload *priv = nft_expr_priv(expr);

-        priv->flowtable->use--;
        nf_ct_netns_put(ctx->net, ctx->family);
}

Quoted 8 lines

The rule should be like the following?

Create rule nft_xx_init   inc the use counter,  If the rule create failed just deactivate it

Delete the rule  deactivate dec the use counter, If the rule delete failed just activate it

That looks like the right fix.

Thanks.

#7 lore raw

PATCH 3/3 netfilter: nf_tables: fix miss dec set use counter in the nf_tables_destroy_set

In reply to [email protected]

From: wenxu <[email protected]>

In the create rule path nf_tables_bind_set the set->use will inc, and with the activate operatoion also inc it. In the delete rule patch deactivate will dec it. So the destroy opertion should also deactivate it.

---
 net/netfilter/nf_tables_api.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +3 -1
index 174b362..d71793e 100644
@@ -4147,8 +4147,10 @@ void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
 
 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set)
 {
-	if (list_empty(&set->bindings) && nft_set_is_anonymous(set))
+	if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) {
+		set->use--;
 		nft_set_destroy(set);
+	}
 }
 EXPORT_SYMBOL_GPL(nf_tables_destroy_set);
 
Signature · 1 line
1.8.3.1
#8 lore raw

Re: [PATCH nf 3/3] netfilter: nf_tables: fix miss dec set use counter in the nf_tables_destroy_set

Quoted 6 lines · On Wed, Dec 18, 2019 at 10:59:13PM +0800, [email protected] wrote:

From: wenxu <[email protected]>

In the create rule path nf_tables_bind_set the set->use will inc, and with the activate operatoion also inc it. In the delete rule patch deactivate will dec it. So the destroy opertion should also deactivate it.

[...]

Is this a theoretical issue? Thanks.

[...]

Quoted 19 lines
net/netfilter/nf_tables_api.c +3 -1
index 174b362..d71793e 100644
@@ -4147,8 +4147,10 @@ void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
 
 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set)
 {
-	if (list_empty(&set->bindings) && nft_set_is_anonymous(set))
+	if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) {
+		set->use--;
 		nft_set_destroy(set);
+	}
 }
 EXPORT_SYMBOL_GPL(nf_tables_destroy_set);
 
Signature · 1 line
1.8.3.1
author
#9 lore raw

Re: [PATCH nf 3/3] netfilter: nf_tables: fix miss dec set use counter in the nf_tables_destroy_set

Quoted 10 lines · Pablo Neira Ayuso
Quoted 6 lines · On Wed, Dec 18, 2019 at 10:59:13PM +0800, [email protected] wrote:

From: wenxu <[email protected]>

In the create rule path nf_tables_bind_set the set->use will inc, and with the activate operatoion also inc it. In the delete rule patch deactivate will dec it. So the destroy opertion should also deactivate it.

[...]

Is this a theoretical issue? Thanks.

As we talked in patch2.  Destroy the rule don't need  dec the use counter. So just drop this series. Thx!

Quoted 20 lines

[...]

Quoted 19 lines
net/netfilter/nf_tables_api.c +3 -1
index 174b362..d71793e 100644
@@ -4147,8 +4147,10 @@ void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
 
 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set)
 {
-	if (list_empty(&set->bindings) && nft_set_is_anonymous(set))
+	if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) {
+		set->use--;
 		nft_set_destroy(set);
+	}
 }
 EXPORT_SYMBOL_GPL(nf_tables_destroy_set);
 
Signature · 1 line
1.8.3.1