Skip to contentSkip to conversation

evaluate: Reject set stmt refs to sets without dynamic flag

PATCH In review Phil Sutter 2 participants 6 messages May 3, 2023 – May 18, 2023 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/1 evaluate: Reject set stmt refs to sets without dynamic flag

This is a revert of commit 8d443adfcc8c1 ("evaluate: attempt to set_eval flag if dynamic updates requested"), implementing the alternative mentioned in the comment it added.

Reason is the inconsistent behaviour when applying the same ruleset
twice: In the first call, the set lacking 'dynamic' flag does not exist and is therefore added to the cache. Consequently, both the 'add set' command and the set statement point at the same set object. In the second call, a set with same name exists already, so the object created for 'add set' command is not added to cache and consequently not updated with the missing flag. The kernel thus rejects the NEWSET request as the existing set differs from the new one.

Fixes 8d443adfcc8c1 ("evaluate: attempt to set_eval flag if dynamic updates requested")

---
*Note*: This is the best I could come up with. If you see a better way to fix this (i.e., fix up cmd->set from stmt_evaluate_set()), please speak up. :)

---
 src/evaluate.c                                | 13 ++------
 .../testcases/sets/dumps/dynset_missing.nft   | 12 -------
 tests/shell/testcases/sets/dynset_missing     | 32 -------------------
 3 files changed, 3 insertions(+), 54 deletions(-)
 delete mode 100644 tests/shell/testcases/sets/dumps/dynset_missing.nft
 delete mode 100755 tests/shell/testcases/sets/dynset_missing
src/evaluate.c +3 -10
index fe15d7ace5dde..9fb768cbd4c9f 100644
@@ -4139,7 +4139,6 @@ static int stmt_evaluate_log(struct eval_ctx *ctx, struct stmt *stmt)
 
 static int stmt_evaluate_set(struct eval_ctx *ctx, struct stmt *stmt)
 {
-	struct set *this_set;
 	struct stmt *this;
 
 	expr_set_context(&ctx->ectx, NULL, 0);
@@ -4148,6 +4147,9 @@ static int stmt_evaluate_set(struct eval_ctx *ctx, struct stmt *stmt)
 	if (stmt->set.set->etype != EXPR_SET_REF)
 		return expr_error(ctx->msgs, stmt->set.set,
 				  "Expression does not refer to a set");
+	if (!(stmt->set.set->set->flags & NFT_SET_EVAL))
+		return expr_error(ctx->msgs, stmt->set.set,
+				  "Referenced set lacks \"dynamic\" flag");
 
 	if (stmt_evaluate_key(ctx, stmt,
 			      stmt->set.set->set->key->dtype,
@@ -4169,15 +4171,6 @@ static int stmt_evaluate_set(struct eval_ctx *ctx, struct stmt *stmt)
 					  "statement must be stateful");
 	}
 
-	this_set = stmt->set.set->set;
-
-	/* Make sure EVAL flag is set on set definition so that kernel
-	 * picks a set that allows updates from the packet path.
-	 *
-	 * Alternatively we could error out in case 'flags dynamic' was
-	 * not given, but we can repair this here.
-	 */
-	this_set->flags |= NFT_SET_EVAL;
 	return 0;
 }
 
tests/shell/testcases/sets/dumps/dynset_missing.nft deleted +0 -12
deleted file mode 100644
index 6c8ed323bdc94..0000000000000
@@ -1,12 +0,0 @@
-table ip test {
-	set dlist {
-		type ipv4_addr
-		size 65535
-		flags dynamic
-	}
-
-	chain output {
-		type filter hook output priority filter; policy accept;
-		udp dport 1234 update @dlist { ip daddr } counter packets 0 bytes 0
-	}
-}
tests/shell/testcases/sets/dynset_missing deleted +0 -32
deleted file mode 100755
index fdf5f49edb9c6..0000000000000
@@ -1,32 +0,0 @@
-#!/bin/bash
-
-set -e
-
-$NFT -f /dev/stdin <<EOF
-table ip test {
-	chain output { type filter hook output priority 0;
-	}
-}
-EOF
-
-# misses 'flags dynamic'
-$NFT 'add set ip test dlist {type ipv4_addr; }'
-
-# picks rhash backend because 'size' was also missing.
-$NFT 'add rule ip test output udp dport 1234 update @dlist { ip daddr } counter'
-
-tmpfile=$(mktemp)
-
-trap "rm -rf $tmpfile" EXIT
-
-# kernel has forced an 64k upper size, i.e. this restore file
-# has 'size 65536' but no 'flags dynamic'.
-$NFT list ruleset > $tmpfile
-
-# this restore works, because set is still the rhash backend.
-$NFT -f $tmpfile # success
-$NFT flush ruleset
-
-# fails without commit 'attempt to set_eval flag if dynamic updates requested',
-# because set in $tmpfile has 'size x' but no 'flags dynamic'.
-$NFT -f $tmpfile
Signature · 1 line
2.40.0
#1 lore raw
Quoted 4 lines · Phil Sutter

This is a revert of commit 8d443adfcc8c1 ("evaluate: attempt to set_eval flag if dynamic updates requested"), implementing the alternative mentioned in the comment it added.

Quoted 8 lines

Reason is the inconsistent behaviour when applying the same ruleset
twice: In the first call, the set lacking 'dynamic' flag does not exist and is therefore added to the cache. Consequently, both the 'add set' command and the set statement point at the same set object. In the second call, a set with same name exists already, so the object created for 'add set' command is not added to cache and consequently not updated with the missing flag. The kernel thus rejects the NEWSET request as the existing set differs from the new one.

# cat test.nft
flush ruleset
table ip test {
        set dlist {
                type ipv4_addr
                size 65535
        }

        chain output {
                type filter hook output priority filter; policy accept;
                udp dport 1234 update @dlist { ip daddr } counter packets 0 bytes 0
        }
}
# nft -f test.nft
# nft -f test.nft
# nft list ruleset
table ip test {
        set dlist {
                type ipv4_addr
                size 65535
                flags dynamic
        }
        chain output {
                type filter hook output priority filter; policy accept;
                udp dport 1234 update @dlist { ip daddr } counter packets 0 bytes 0
        }
}

What are the steps to reproduce this? Did I misunderstand?

author reviewer
#2 lore raw

Hi Pablo,

Quoted 14 lines · Pablo Neira Ayuso
Quoted 4 lines · Phil Sutter

This is a revert of commit 8d443adfcc8c1 ("evaluate: attempt to set_eval flag if dynamic updates requested"), implementing the alternative mentioned in the comment it added.

Quoted 8 lines

Reason is the inconsistent behaviour when applying the same ruleset
twice: In the first call, the set lacking 'dynamic' flag does not exist and is therefore added to the cache. Consequently, both the 'add set' command and the set statement point at the same set object. In the second call, a set with same name exists already, so the object created for 'add set' command is not added to cache and consequently not updated with the missing flag. The kernel thus rejects the NEWSET request as the existing set differs from the new one.

Quoted 2 lines
# cat test.nft
flush ruleset

Just remove this 'flush ruleset' call, then it should trigger.

Cheers, Phil

#3 lore raw
Quoted 17 lines · Phil Sutter

Hi Pablo,

Quoted 14 lines · Pablo Neira Ayuso
Quoted 12 lines · Phil Sutter

This is a revert of commit 8d443adfcc8c1 ("evaluate: attempt to set_eval flag if dynamic updates requested"), implementing the alternative mentioned in the comment it added.

Reason is the inconsistent behaviour when applying the same ruleset
twice: In the first call, the set lacking 'dynamic' flag does not exist and is therefore added to the cache. Consequently, both the 'add set' command and the set statement point at the same set object. In the second call, a set with same name exists already, so the object created for 'add set' command is not added to cache and consequently not updated with the missing flag. The kernel thus rejects the NEWSET request as the existing set differs from the new one.

Quoted 4 lines
Quoted 2 lines
# cat test.nft
flush ruleset

Just remove this 'flush ruleset' call, then it should trigger.

I cannot reproduce it yet :(

# cat test.nft
table ip test {
        set dlist {
                type ipv4_addr
                size 65535
        }
        chain output {
                type filter hook output priority filter; policy accept;
                udp dport 1234 update @dlist { ip daddr } counter packets 0 bytes 0
        }
}
# nft -f test.nft
# nft -f test.nft
# nft list ruleset
table ip test {
        set dlist {
                type ipv4_addr
                size 65535
                flags dynamic
        }
        chain output {
                type filter hook output priority filter; policy accept;
                udp dport 1234 update @dlist { ip daddr } counter packets 0 bytes 0
                udp dport 1234 update @dlist { ip daddr } counter packets 0 bytes 0
        }
}

Maybe I need a specific kernel? I am trying with latest.

author reviewer
#4 lore raw

Hi Pablo,

Quoted 18 lines · Pablo Neira Ayuso
Quoted 17 lines · Phil Sutter

Hi Pablo,

Quoted 14 lines · Pablo Neira Ayuso
Quoted 12 lines · Phil Sutter

This is a revert of commit 8d443adfcc8c1 ("evaluate: attempt to set_eval flag if dynamic updates requested"), implementing the alternative mentioned in the comment it added.

Reason is the inconsistent behaviour when applying the same ruleset
twice: In the first call, the set lacking 'dynamic' flag does not exist and is therefore added to the cache. Consequently, both the 'add set' command and the set statement point at the same set object. In the second call, a set with same name exists already, so the object created for 'add set' command is not added to cache and consequently not updated with the missing flag. The kernel thus rejects the NEWSET request as the existing set differs from the new one.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
# cat test.nft
flush ruleset

Just remove this 'flush ruleset' call, then it should trigger.

I cannot reproduce it yet :(

This is very odd.

Quoted 7 lines
# cat test.nft
table ip test {
        set dlist {
                type ipv4_addr
                size 65535
        }
Quoted 7 lines
        chain output {
                type filter hook output priority filter; policy accept;
                udp dport 1234 update @dlist { ip daddr } counter packets 0 bytes 0
        }
}
# nft -f test.nft
# nft -f test.nft

On my side, this second call emits:

| /tmp/test.nft:2:6-10: Error: Could not process rule: File exists
|         set dlist {
|             ^^^^^
Quoted 1 line

Maybe I need a specific kernel? I am trying with latest.

IIUC, it is not kernel-related. I can reproduce with:

nftables @ d486c9e626405 ("datatype: add hint error handler") libnftnl @ libnftnl-1.2.5
linux @ b50a8b0d57ab1 ("net: openvswitch: Use struct_size()")

Cheers, Phil

#5 lore raw
In reply to Pablo Neira Ayuso
Quoted 18 lines · Pablo Neira Ayuso
Quoted 17 lines · Phil Sutter

Hi Pablo,

Quoted 14 lines · Pablo Neira Ayuso
Quoted 12 lines · Phil Sutter

This is a revert of commit 8d443adfcc8c1 ("evaluate: attempt to set_eval flag if dynamic updates requested"), implementing the alternative mentioned in the comment it added.

Reason is the inconsistent behaviour when applying the same ruleset
twice: In the first call, the set lacking 'dynamic' flag does not exist and is therefore added to the cache. Consequently, both the 'add set' command and the set statement point at the same set object. In the second call, a set with same name exists already, so the object created for 'add set' command is not added to cache and consequently not updated with the missing flag. The kernel thus rejects the NEWSET request as the existing set differs from the new one.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
# cat test.nft
flush ruleset

Just remove this 'flush ruleset' call, then it should trigger.

I cannot reproduce it yet :(

I can reproduce it. For the record, I have applied this alternate fix:

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/