Skip to contentSkip to conversation

Fix listing of sets containing unclosed address prefix intervals

PATCH Applied Jeremy Sowden 2 participants 7 messages Sep 18, 2022 – Sep 21, 2022 netfilter

lore
Share & tools
lore
#0 lore raw

cover Fix listing of sets containing unclosed address prefix intervals

The code which decomposes unclosed intervals in sets doesn't check for prefixes. This means that a set containing such a prefix (e.g., ff00::/8 or 192.0.0.0/2) is incorrectly listed:

  # nft list table ip6 t
  table ip6 t {
    chain c {
      ip6 saddr ff00::/8 drop
      ip6 saddr fe80::/10 drop
      ip6 saddr { fe80::/10, ff00::-ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff } drop
    }
  }
  # nft list table ip t
  table ip t {
    chain c {
      ip saddr 192.0.0.0/2 drop
      ip saddr 10.0.0.0/8 drop
      ip saddr { 10.0.0.0/8, 192.0.0.0-255.255.255.255 } drop
    }
  }

This patch-set refactors interval_map_decompose to use the same code to handle unclosed intervals that is used for closed ones.

Jeremy Sowden (2):
segtree: refactor decomposition of closed intervals
segtree: fix decomposition of unclosed intervals containing address
prefixes

 src/segtree.c                                 | 90 +++++++++----------
 .../sets/0071unclosed_prefix_interval_0       | 23 +++++
 .../dumps/0071unclosed_prefix_interval_0.nft  | 19 ++++
 3 files changed, 85 insertions(+), 47 deletions(-)
 create mode 100755 tests/shell/testcases/sets/0071unclosed_prefix_interval_0
 create mode 100644 tests/shell/testcases/sets/dumps/0071unclosed_prefix_interval_0.nft
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/2 segtree: refactor decomposition of closed intervals

Move the code in interval_map_decompose which adds a new closed interval to the set into a separate function. In addition to the moving of the code, there is one other change: compound_expr_add is called once, after the main conditional, instead of being called in each branch.

---
 src/segtree.c | 71 +++++++++++++++++++++++++++------------------------
 1 file changed, 38 insertions(+), 33 deletions(-)
src/segtree.c +38 -33
index c36497ce6253..d15c39f31f3a 100644
@@ -493,12 +493,48 @@ static struct expr *interval_to_range(struct expr *low, struct expr *i, mpz_t ra
 	return __expr_to_set_elem(low, tmp);
 }
 
+static void
+add_interval(struct expr *set, struct expr *low, struct expr *i)
+{
+	struct expr *expr;
+	mpz_t range, p;
+
+	mpz_init(range);
+	mpz_init(p);
+
+	mpz_sub(range, expr_value(i)->value, expr_value(low)->value);
+	mpz_sub_ui(range, range, 1);
+
+	mpz_and(p, expr_value(low)->value, range);
+
+	if (!mpz_cmp_ui(range, 0)) {
+		if (expr_basetype(low)->type == TYPE_STRING)
+			mpz_switch_byteorder(expr_value(low)->value,
+					     expr_value(low)->len / BITS_PER_BYTE);
+		low->flags |= EXPR_F_KERNEL;
+		expr = expr_get(low);
+	} else if (range_is_prefix(range) && !mpz_cmp_ui(p, 0)) {
+
+		if (i->dtype->flags & DTYPE_F_PREFIX)
+			expr = interval_to_prefix(low, i, range);
+		else if (expr_basetype(i)->type == TYPE_STRING)
+			expr = interval_to_string(low, i, range);
+		else
+			expr = interval_to_range(low, i, range);
+	} else
+		expr = interval_to_range(low, i, range);
+
+	compound_expr_add(set, expr);
+
+	mpz_clear(range);
+	mpz_clear(p);
+}
+
 void interval_map_decompose(struct expr *set)
 {
 	struct expr *i, *next, *low = NULL, *end, *catchall = NULL, *key;
 	struct expr **elements, **ranges;
 	unsigned int n, m, size;
-	mpz_t range, p;
 	bool interval;
 
 	if (set->size == 0)
@@ -507,9 +543,6 @@ void interval_map_decompose(struct expr *set)
 	elements = xmalloc_array(set->size, sizeof(struct expr *));
 	ranges = xmalloc_array(set->size * 2, sizeof(struct expr *));
 
-	mpz_init(range);
-	mpz_init(p);
-
 	/* Sort elements */
 	n = 0;
 	list_for_each_entry_safe(i, next, &set->expressions, list) {
@@ -568,32 +601,7 @@ void interval_map_decompose(struct expr *set)
 			}
 		}
 
-		mpz_sub(range, expr_value(i)->value, expr_value(low)->value);
-		mpz_sub_ui(range, range, 1);
-
-		mpz_and(p, expr_value(low)->value, range);
-
-		if (!mpz_cmp_ui(range, 0)) {
-			if (expr_basetype(low)->type == TYPE_STRING)
-				mpz_switch_byteorder(expr_value(low)->value, expr_value(low)->len / BITS_PER_BYTE);
-			low->flags |= EXPR_F_KERNEL;
-			compound_expr_add(set, expr_get(low));
-		} else if (range_is_prefix(range) && !mpz_cmp_ui(p, 0)) {
-			struct expr *expr;
-
-			if (i->dtype->flags & DTYPE_F_PREFIX)
-				expr = interval_to_prefix(low, i, range);
-			else if (expr_basetype(i)->type == TYPE_STRING)
-				expr = interval_to_string(low, i, range);
-			else
-				expr = interval_to_range(low, i, range);
-
-			compound_expr_add(set, expr);
-		} else {
-			struct expr *expr = interval_to_range(low, i, range);
-
-			compound_expr_add(set, expr);
-		}
+		add_interval(set, low, i);
 
 		if (i->flags & EXPR_F_INTERVAL_END) {
 			expr_free(low);
@@ -633,9 +641,6 @@ out:
 	if (catchall)
 		compound_expr_add(set, catchall);
 
-	mpz_clear(range);
-	mpz_clear(p);
-
 	xfree(ranges);
 	xfree(elements);
 }
Signature · 1 line
2.35.1
#2 lore raw

PATCH 2/2 segtree: fix decomposition of unclosed intervals containing address prefixes

In reply to Jeremy Sowden

The code which decomposes unclosed intervals doesn't check for prefixes. This leads to incorrect output for sets which contain these. For example,

  # nft -f - <<END
  table ip t {
    chain c {
      ip saddr 192.0.0.0/2 drop
      ip saddr 10.0.0.0/8 drop
      ip saddr { 192.0.0.0/2, 10.0.0.0/8 } drop
    }
  }
  table ip6 t {
    chain c {
      ip6 saddr ff00::/8 drop
      ip6 saddr fe80::/10 drop
      ip6 saddr { ff00::/8, fe80::/10 } drop
    }
  }
  END
  # nft list table ip6 t
  table ip6 t {
    chain c {
      ip6 saddr ff00::/8 drop
      ip6 saddr fe80::/10 drop
      ip6 saddr { fe80::/10, ff00::-ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff } drop
    }
  }
  # nft list table ip t
  table ip t {
    chain c {
      ip saddr 192.0.0.0/2 drop
      ip saddr 10.0.0.0/8 drop
      ip saddr { 10.0.0.0/8, 192.0.0.0-255.255.255.255 } drop
    }
  }

Instead of treating the final unclosed interval as a special case, reuse the code which correctly handles closed intervals.

Add a shell test-case.

Fixes 86b965bdab8d ("segtree: fix decomposition of unclosed intervals")

tests: shell: add case to test unclosed prefix intervals

---
 src/segtree.c                                 | 21 +++++------------
 .../sets/0071unclosed_prefix_interval_0       | 23 +++++++++++++++++++
 .../dumps/0071unclosed_prefix_interval_0.nft  | 19 +++++++++++++++
 3 files changed, 48 insertions(+), 15 deletions(-)
 create mode 100755 tests/shell/testcases/sets/0071unclosed_prefix_interval_0
 create mode 100644 tests/shell/testcases/sets/dumps/0071unclosed_prefix_interval_0.nft
src/segtree.c +6 -15
index d15c39f31f3a..ad3821376dae 100644
@@ -158,6 +158,8 @@ static struct expr *expr_value(struct expr *expr)
 		return expr->left->key;
 	case EXPR_SET_ELEM:
 		return expr->key;
+	case EXPR_VALUE:
+		return expr;
 	default:
 		BUG("invalid expression type %s\n", expr_name(expr));
 	}
@@ -503,7 +505,8 @@ add_interval(struct expr *set, struct expr *low, struct expr *i)
 	mpz_init(p);
 
 	mpz_sub(range, expr_value(i)->value, expr_value(low)->value);
-	mpz_sub_ui(range, range, 1);
+	if (i->etype != EXPR_VALUE)
+		mpz_sub_ui(range, range, 1);
 
 	mpz_and(p, expr_value(low)->value, range);
 
@@ -619,24 +622,12 @@ void interval_map_decompose(struct expr *set)
 
 	if (!mpz_cmp(i->value, expr_value(low)->value)) {
 		expr_free(i);
-		i = low;
+		compound_expr_add(set, low);
 	} else {
-		i = range_expr_alloc(&low->location,
-				     expr_clone(expr_value(low)), i);
-		i = set_elem_expr_alloc(&low->location, i);
-		if (low->etype == EXPR_MAPPING) {
-			i = mapping_expr_alloc(&i->location, i,
-					       expr_clone(low->right));
-			interval_expr_copy(i->left, low->left);
-		} else {
-			interval_expr_copy(i, low);
-		}
-		i->flags |= EXPR_F_KERNEL;
-
+		add_interval(set, low, i);
 		expr_free(low);
 	}
 
-	compound_expr_add(set, i);
 out:
 	if (catchall)
 		compound_expr_add(set, catchall);
tests/shell/testcases/sets/0071unclosed_prefix_interval_0 new file +23 -0
new file mode 100755
index 000000000000..79e3ca7da743
@@ -0,0 +1,23 @@
+#!/bin/bash
+
+set -e
+
+RULESET="
+table inet t {
+	set s1 {
+		type ipv4_addr
+		flags interval
+		elements = { 192.0.0.0/2, 10.0.0.0/8 }
+	}
+	set s2 {
+		type ipv6_addr
+		flags interval
+		elements = { ff00::/8, fe80::/10 }
+	}
+	chain c {
+		ip saddr @s1 accept
+		ip6 daddr @s2 accept
+	}
+}"
+
+$NFT -f - <<< "$RULESET"
tests/shell/testcases/sets/dumps/0071unclosed_prefix_interval_0.nft new file +19 -0
new file mode 100644
index 000000000000..4eed94c2c884
@@ -0,0 +1,19 @@
+table inet t {
+	set s1 {
+		type ipv4_addr
+		flags interval
+		elements = { 10.0.0.0/8, 192.0.0.0/2 }
+	}
+
+	set s2 {
+		type ipv6_addr
+		flags interval
+		elements = { fe80::/10,
+			     ff00::/8 }
+	}
+
+	chain c {
+		ip saddr @s1 accept
+		ip6 daddr @s2 accept
+	}
+}
Signature · 1 line
2.35.1
maintainer
#3 lore raw

Re: [PATCH nft 2/2] segtree: fix decomposition of unclosed intervals containing address prefixes

Quoted 14 lines · Jeremy Sowden
(quoted diff) +1 -8
@@ -619,24 +622,12 @@ void interval_map_decompose(struct expr *set)
 
 	if (!mpz_cmp(i->value, expr_value(low)->value)) {
 		expr_free(i);
-		i = low;
+		compound_expr_add(set, low);
 	} else {
-		i = range_expr_alloc(&low->location,
-				     expr_clone(expr_value(low)), i);
-		i = set_elem_expr_alloc(&low->location, i);
-		if (low->etype == EXPR_MAPPING) {
-			i = mapping_expr_alloc(&i->location, i,
-					       expr_clone(low->right));
-			interval_expr_copy(i->left, low->left);

Review void interval_map_decompose(struct expr *set) · line 639

Quoted 10 lines
(quoted diff) +1 -6
-		} else {
-			interval_expr_copy(i, low);
-		}
-		i->flags |= EXPR_F_KERNEL;
-
+		add_interval(set, low, i);
 		expr_free(low);
 	}
 
-	compound_expr_add(set, i);

This results in a memory leak:

__interceptor_malloc libsanitizer/asan/asan_malloc_linux.cpp:145
xmalloc src/utils.c:36
xzalloc src/utils.c:75
expr_alloc src/expression.c:46
constant_expr_alloc src/expression.c:420
interval_map_decompose src/segtree.c:619

Before, 'i' was assigned to the compund expr, but thats no longer the case.

Does this look good to you? If so, I will sqash this before applying:

src/segtree.c +2 -1
@@ -621,13 +621,14 @@ void interval_map_decompose(struct expr *set)
 	mpz_bitmask(i->value, i->len);
 
 	if (!mpz_cmp(i->value, expr_value(low)->value)) {
-		expr_free(i);
 		compound_expr_add(set, low);
 	} else {
 		add_interval(set, low, i);
 		expr_free(low);
 	}
 
+	expr_free(i);
+
 out:
 	if (catchall)
 		compound_expr_add(set, catchall);
#4 lore raw

Re: [PATCH nft 2/2] segtree: fix decomposition of unclosed intervals containing address prefixes

Quoted 28 lines · Florian Westphal
Quoted 14 lines · Jeremy Sowden
(quoted diff) +1 -8
@@ -619,24 +622,12 @@ void interval_map_decompose(struct expr *set)
 
 	if (!mpz_cmp(i->value, expr_value(low)->value)) {
 		expr_free(i);
-		i = low;
+		compound_expr_add(set, low);
 	} else {
-		i = range_expr_alloc(&low->location,
-				     expr_clone(expr_value(low)), i);
-		i = set_elem_expr_alloc(&low->location, i);
-		if (low->etype == EXPR_MAPPING) {
-			i = mapping_expr_alloc(&i->location, i,
-					       expr_clone(low->right));
-			interval_expr_copy(i->left, low->left);

Review void interval_map_decompose(struct expr *set) · line 639

Quoted 10 lines
(quoted diff) +1 -6
-		} else {
-			interval_expr_copy(i, low);
-		}
-		i->flags |= EXPR_F_KERNEL;
-
+		add_interval(set, low, i);
 		expr_free(low);
 	}
 
-	compound_expr_add(set, i);

This results in a memory leak:

Quoted 6 lines
__interceptor_malloc libsanitizer/asan/asan_malloc_linux.cpp:145
xmalloc src/utils.c:36
xzalloc src/utils.c:75
expr_alloc src/expression.c:46
constant_expr_alloc src/expression.c:420
interval_map_decompose src/segtree.c:619

I did try running the new shell test under valgrind: lots of noise, not a lot of signal. :)

Quoted 4 lines

Before, 'i' was assigned to the compund expr, but thats no longer the case.

Does this look good to you?

Yes, LTGM.

Quoted 12 lines

If so, I will sqash this before applying:

src/segtree.c +0 -1
@@ -621,13 +621,14 @@ void interval_map_decompose(struct expr *set)
 	mpz_bitmask(i->value, i->len);
 
 	if (!mpz_cmp(i->value, expr_value(low)->value)) {
-		expr_free(i);
 		compound_expr_add(set, low);

src/segtree.c void interval_map_decompose(struct expr *set) · line 634

Quoted 10 lines
src/segtree.c +2 -0
 	} else {
 		add_interval(set, low, i);
 		expr_free(low);
 	}
 
+	expr_free(i);
+
 out:
 	if (catchall)
 		compound_expr_add(set, catchall);

Thanks,

J.

maintainer Applied
#5 lore raw

Re: [PATCH nft 2/2] segtree: fix decomposition of unclosed intervals containing address prefixes

Quoted 9 lines · Jeremy Sowden
Quoted 6 lines
__interceptor_malloc libsanitizer/asan/asan_malloc_linux.cpp:145
xmalloc src/utils.c:36
xzalloc src/utils.c:75
expr_alloc src/expression.c:46
constant_expr_alloc src/expression.c:420
interval_map_decompose src/segtree.c:619

I did try running the new shell test under valgrind: lots of noise, not a lot of signal. :)

Yes. You can use -fsanitize=leak as alternative, that has a much better signal/noise ratio.

Quoted 6 lines
Quoted 4 lines

Before, 'i' was assigned to the compund expr, but thats no longer the case.

Does this look good to you?

Yes, LTGM.

Applied, thanks!

#6 lore raw
In reply to Jeremy Sowden
Quoted 3 lines · Jeremy Sowden

The code which decomposes unclosed intervals in sets doesn't check for prefixes. This means that a set containing such a prefix (e.g., ff00::/8 or 192.0.0.0/2) is incorrectly listed:

The original Debian bug-report only covers the IPv6 case:

Quoted 8 lines
  # nft list table ip6 t
  table ip6 t {
    chain c {
      ip6 saddr ff00::/8 drop
      ip6 saddr fe80::/10 drop
      ip6 saddr { fe80::/10, ff00::-ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff } drop
    }
  }

To the reporter that range looked like a garbled address with a negative hex number embedded in it, and when I read the report it looked like that to me too. Inevitably, it was only after I sent this patch-set that I finally parsed it correctly as the range ff00:: to ffff:ffff:...:ffff:ffff, largely because of the IPv4 case:

Quoted 8 lines
  # nft list table ip t
  table ip t {
    chain c {
      ip saddr 192.0.0.0/2 drop
      ip saddr 10.0.0.0/8 drop
      ip saddr { 10.0.0.0/8, 192.0.0.0-255.255.255.255 } drop
    }
  }

which, to me at least, is easier to read.

The reason that I bring this up is that I should probably have phrased the commit messages differently and avoided the use of "correct" and "incorrect" if I hadn't misparsed the IPv6 range, like the original reporter, since the ranges currently output are unexpected (and arguably confusing), rather than wrong. I'm happy to reword the commits if you would like.

J.

Quoted 19 lines

This patch-set refactors interval_map_decompose to use the same code to handle unclosed intervals that is used for closed ones.

Jeremy Sowden (2):
segtree: refactor decomposition of closed intervals
segtree: fix decomposition of unclosed intervals containing address
prefixes

 src/segtree.c                                 | 90 +++++++++----------
 .../sets/0071unclosed_prefix_interval_0       | 23 +++++
 .../dumps/0071unclosed_prefix_interval_0.nft  | 19 ++++
 3 files changed, 85 insertions(+), 47 deletions(-)
 create mode 100755 tests/shell/testcases/sets/0071unclosed_prefix_interval_0
 create mode 100644 tests/shell/testcases/sets/dumps/0071unclosed_prefix_interval_0.nft
Signature · 1 line
2.35.1