Skip to contentSkip to conversation

netfilter: nftables: fix incorrect increment of loop counter

PATCH Applied Colin King 2 participants 4 messages Dec 14, 2020 – Dec 16, 2020

Subsystems (4)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 netfilter: nftables: fix incorrect increment of loop counter

From: Colin Ian King <[email protected]>

The intention of the err_expr cleanup path is to iterate over the allocated expr_array objects and free them, starting from i - 1 and working down to the start of the array. Currently the loop counter is being incremented instead of decremented and also the index i is being used instead of k, repeatedly destroying the same expr_array element. Fix this by decrementing k and using k as the index into expr_array.

Addresses-Coverity: ("Infinite loop")

Fixes 8cfd9b0f8515 ("netfilter: nftables: generalize set expressions support")
---
 net/netfilter/nf_tables_api.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/nf_tables_api.c +2 -2
index 8d5aa0ac45f4..4186b1e52d58 100644
@@ -5254,8 +5254,8 @@ static int nft_set_elem_expr_clone(const struct nft_ctx *ctx,
 	return 0;
 
 err_expr:
-	for (k = i - 1; k >= 0; k++)
-		nft_expr_destroy(ctx, expr_array[i]);
+	for (k = i - 1; k >= 0; k--)
+		nft_expr_destroy(ctx, expr_array[k]);
 
 	return -ENOMEM;
 }
Signature · 1 line
2.29.2
maintainer Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 10 lines · Colin King

From: Colin Ian King <[email protected]>

The intention of the err_expr cleanup path is to iterate over the allocated expr_array objects and free them, starting from i - 1 and working down to the start of the array. Currently the loop counter is being incremented instead of decremented and also the index i is being used instead of k, repeatedly destroying the same expr_array element. Fix this by decrementing k and using k as the index into expr_array.

Quoted 3 lines

Addresses-Coverity: ("Infinite loop")

Fixes 8cfd9b0f8515 ("netfilter: nftables: generalize set expressions support")

@Jakub: Would you please take this one into net-next? Thanks!

Quoted 22 lines
---
 net/netfilter/nf_tables_api.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/nf_tables_api.c +2 -2
index 8d5aa0ac45f4..4186b1e52d58 100644
@@ -5254,8 +5254,8 @@ static int nft_set_elem_expr_clone(const struct nft_ctx *ctx,
 	return 0;
 
 err_expr:
-	for (k = i - 1; k >= 0; k++)
-		nft_expr_destroy(ctx, expr_array[i]);
+	for (k = i - 1; k >= 0; k--)
+		nft_expr_destroy(ctx, expr_array[k]);
 
 	return -ENOMEM;
 }
Signature · 1 line
2.29.2
#2 lore raw
Quoted 13 lines · Pablo Neira Ayuso

Hi,

Quoted 10 lines · Colin King

From: Colin Ian King <[email protected]>

The intention of the err_expr cleanup path is to iterate over the allocated expr_array objects and free them, starting from i - 1 and working down to the start of the array. Currently the loop counter is being incremented instead of decremented and also the index i is being used instead of k, repeatedly destroying the same expr_array element. Fix this by decrementing k and using k as the index into expr_array.

Quoted 7 lines
Quoted 3 lines

Addresses-Coverity: ("Infinite loop")

Fixes 8cfd9b0f8515 ("netfilter: nftables: generalize set expressions support")

@Jakub: Would you please take this one into net-next? Thanks!

You marked as "Awaiting Upstream", I'll take care of it.

Thanks.

maintainer Applied
#3 lore raw
In reply to Colin King
Quoted 9 lines · Colin King

From: Colin Ian King <[email protected]>

The intention of the err_expr cleanup path is to iterate over the allocated expr_array objects and free them, starting from i - 1 and working down to the start of the array. Currently the loop counter is being incremented instead of decremented and also the index i is being used instead of k, repeatedly destroying the same expr_array element. Fix this by decrementing k and using k as the index into expr_array.

Applied to nf.git, thanks.