Skip to contentSkip to conversation

netlink_linearize: size nat register allocation by address expression

PATCH Applied Adrian Moisey 2 participants 2 messages Aug 6 – Aug 20 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netlink_linearize: size nat register allocation by address expression

nat statements with a literal concatenation of address and port, e.g.

dnat ip6 addr . port to fd00:10:244::4 . 6443

allocated a fixed 128-bit register block for the address, but the ip6 address . port concatenation is 144 bits (5 registers), so netlink_gen_concat overran into unallocated registers and hit the assertion 'dreg < ctx->reg_low' in netlink_gen_expr. IPv4 escaped by luck since its 48-bit concatenation fits into the padded block.

Allocate the register based on the address expression instead, which sizes by expr->len for concatenations, and add the matching release_register() call. Extend the nat_addr_port test with literal concatenation rules for ip and ip6.

---
 src/netlink_linearize.c                       |  6 ++-
 .../maps/dumps/nat_addr_port.json-nft         | 54 +++++++++++++++++++
 .../testcases/maps/dumps/nat_addr_port.nft    |  2 +
 tests/shell/testcases/maps/nat_addr_port      |  2 +
 4 files changed, 62 insertions(+), 2 deletions(-)
src/netlink_linearize.c +4 -2
index ac0eaff9..dfa841c6 100644
@@ -1292,8 +1292,7 @@ static void netlink_gen_nat_stmt(struct netlink_linearize_ctx *ctx,
 		nftnl_expr_set_u32(nle, nftnl_flag_attr, stmt->nat.flags);
 
 	if (stmt->nat.addr) {
-		amin_reg = get_register(ctx, NULL);
-		registers++;
+		amin_reg = get_register(ctx, stmt->nat.addr);
 
 		if (stmt->nat.addr->etype == EXPR_RANGE) {
 			amax_reg = get_register(ctx, NULL);
@@ -1374,6 +1373,9 @@ static void netlink_gen_nat_stmt(struct netlink_linearize_ctx *ctx,
 		registers--;
 	}
 
+	if (stmt->nat.addr)
+		release_register(ctx, stmt->nat.addr);
+
 	nft_rule_add_expr(ctx, nle, &stmt->location);
 }
 
tests/shell/testcases/maps/dumps/nat_addr_port.json-nft +54 -0

66 lines not on this page · raw message

tests/shell/testcases/maps/dumps/nat_addr_port.nft +2 -0
index c8493b3a..fb1c190a 100644
@@ -29,6 +29,7 @@ table ip ipfoo {
 		ip saddr 10.1.1.2 tcp dport 42 dnat to 10.2.3.4:4242
 		meta l4proto tcp dnat ip to ip saddr map @y
 		dnat ip to ip saddr . tcp dport map @z
+		meta l4proto tcp dnat to 10.2.3.4:4242
 		dnat to numgen inc mod 2 map @t1
 		meta l4proto tcp dnat ip to numgen inc mod 2 map @t2
 	}
@@ -62,6 +63,7 @@ table ip6 ip6foo {
 		ip6 saddr dead::2 tcp dport 42 dnat to [c0::1a]:4242
 		meta l4proto tcp dnat ip6 to ip6 saddr map @y
 		dnat ip6 to ip6 saddr . tcp dport map @z
+		meta l4proto tcp dnat to [fd00:10:244::4]:6443
 		dnat to numgen inc mod 2 map @t1
 		meta l4proto tcp dnat ip6 to numgen inc mod 2 map @t2
 	}
tests/shell/testcases/maps/nat_addr_port +2 -0
index 703a2ad9..5f6ff1c4 100755
@@ -31,6 +31,7 @@ table ip ipfoo {
 		ip saddr 10.1.1.2 tcp dport 42 dnat to 10.2.3.4:4242
 		meta l4proto tcp dnat ip addr . port to ip saddr map @y
 		meta l4proto tcp dnat ip addr . port to ip saddr . tcp dport map @z
+		meta l4proto tcp dnat ip addr . port to 10.2.3.4 . 4242
 		dnat ip to numgen inc mod 2 map @t1
 		meta l4proto tcp dnat ip addr . port to numgen inc mod 2 map @t2
 	}
@@ -72,6 +73,7 @@ table ip6 ip6foo {
 		ip6 saddr dead::2 tcp dport 42 dnat to [c0::1a]:4242
 		meta l4proto tcp dnat ip6 addr . port to ip6 saddr map @y
 		meta l4proto tcp dnat ip6 addr . port to ip6 saddr . tcp dport map @z
+		meta l4proto tcp dnat ip6 addr . port to fd00:10:244::4 . 6443
 		dnat ip6 to numgen inc mod 2 map @t1
 		meta l4proto tcp dnat ip6 addr . port to numgen inc mod 2 map @t2
 	}
Signature · 1 line
2.55.0
maintainer Applied
#1 lore raw
Quoted 10 lines · Adrian Moisey

nat statements with a literal concatenation of address and port, e.g.

dnat ip6 addr . port to fd00:10:244::4 . 6443

allocated a fixed 128-bit register block for the address, but the ip6 address . port concatenation is 144 bits (5 registers), so netlink_gen_concat overran into unallocated registers and hit the assertion 'dreg < ctx->reg_low' in netlink_gen_expr. IPv4 escaped by luck since its 48-bit concatenation fits into the padded block.

Quoted 4 lines

Allocate the register based on the address expression instead, which sizes by expr->len for concatenations, and add the matching release_register() call. Extend the nat_addr_port test with literal concatenation rules for ip and ip6.

Applied, thanks