Skip to contentSkip to conversation

src: Check range bounds before converting to prefix

PATCH Applied Xiao Liang 2 participants 4 messages Sep 6, 2021 netfilter

lore
Share & tools
lore

Xiao Liang

author
#0 lore raw

PATCH 1/1 src: Check range bounds before converting to prefix

The lower bound must be the first value of the prefix to be coverted. For example, range "10.0.0.15-10.0.0.240" can not be converted to "10.0.0.15/24". Validate it by checking if the lower bound value has enough trailing zeros.

---
 src/netlink.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
src/netlink.c +9 -6
index cbf9d436..0fd0b664 100644
@@ -1079,12 +1079,15 @@ struct expr *range_expr_to_prefix(struct expr *range)
 
 	if (mpz_bitmask_is_prefix(bitmask, len)) {
 		prefix_len = mpz_bitmask_to_prefix(bitmask, len);
-		prefix = prefix_expr_alloc(&range->location, expr_get(left),
-					   prefix_len);
-		mpz_clear(bitmask);
-		expr_free(range);
-
-		return prefix;
+		if (mpz_scan1(left->value, 0) >= len - prefix_len) {
+			prefix = prefix_expr_alloc(&range->location,
+						   expr_get(left),
+						   prefix_len);
+			mpz_clear(bitmask);
+			expr_free(range);
+
+			return prefix;
+		}
 	}
 	mpz_clear(bitmask);
 
Signature · 1 line
2.33.0
#1 lore raw

Hi,

Quoted 4 lines · Xiao Liang

The lower bound must be the first value of the prefix to be coverted. For example, range "10.0.0.15-10.0.0.240" can not be converted to "10.0.0.15/24". Validate it by checking if the lower bound value has enough trailing zeros.

# nft add rule x y ip saddr 10.0.0.15-10.0.0.240
# nft list ruleset
...
        ip saddr 10.0.0.15-10.0.0.240

Is a different range that triggers the problem?

author
#2 lore raw
Quoted 9 lines · Pablo Neira Ayuso

Hi,

Quoted 4 lines · Xiao Liang

The lower bound must be the first value of the prefix to be coverted. For example, range "10.0.0.15-10.0.0.240" can not be converted to "10.0.0.15/24". Validate it by checking if the lower bound value has enough trailing zeros.

Quoted 6 lines
# nft add rule x y ip saddr 10.0.0.15-10.0.0.240
# nft list ruleset
...
        ip saddr 10.0.0.15-10.0.0.240

Is a different range that triggers the problem?

Hi,

Please try

# nft add rule x y snat to 10.0.0.15-10.0.0.240
maintainer Applied
#3 lore raw
In reply to Xiao Liang
Quoted 4 lines · Xiao Liang

The lower bound must be the first value of the prefix to be coverted. For example, range "10.0.0.15-10.0.0.240" can not be converted to "10.0.0.15/24". Validate it by checking if the lower bound value has enough trailing zeros.

Applied, thanks.