Blob Blame History Raw
From ed405d0eafc7b1f71013cf42f9ed550d64ec56c5 Mon Sep 17 00:00:00 2001
From: Phil Sutter <psutter@redhat.com>
Date: Wed, 6 Jun 2018 10:44:43 +0200
Subject: [PATCH] src: avoid errouneous assert with map+concat

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1540917
Upstream Status: nftables commit 483e5ea7167e1

commit 483e5ea7167e1537accf4cb083b88a8beea8f834
Author: Florian Westphal <fw@strlen.de>
Date:   Tue Mar 27 09:29:54 2018 +0200

    src: avoid errouneous assert with map+concat

    Phil reported following assert:

    add rule ip6 f o mark set ip6 saddr . ip6 daddr . tcp dport \
                            map { dead::beef . f00::. 22 : 1 }
    nft: netlink_linearize.c:655: netlink_gen_expr: Assertion `dreg < ctx->reg_low' failed.

    This happens because "mark set" will allocate one register (the dreg),
    but netlink_gen_concat_expr will populate a lot more register space if
    the concat expression strings a lot of expressions together.

    As the assert is useful pseudo-reserve the register space as per
    concat->len and undo after generating the expressions.

    Reported-by: Phil Sutter <phil@nwl.cc>
    Signed-off-by: Florian Westphal <fw@strlen.de>
---
 src/netlink_linearize.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/netlink_linearize.c b/src/netlink_linearize.c
index a268dcc..e9a4515 100644
--- a/src/netlink_linearize.c
+++ b/src/netlink_linearize.c
@@ -243,6 +243,7 @@ static void netlink_gen_map(struct netlink_linearize_ctx *ctx,
 {
 	struct nftnl_expr *nle;
 	enum nft_registers sreg;
+	int regspace = 0;
 
 	assert(expr->mappings->ops->type == EXPR_SET_REF);
 
@@ -251,7 +252,14 @@ static void netlink_gen_map(struct netlink_linearize_ctx *ctx,
 	else
 		sreg = dreg;
 
+	/* suppress assert in netlink_gen_expr */
+	if (expr->map->ops->type == EXPR_CONCAT) {
+		regspace = netlink_register_space(expr->map->len);
+		ctx->reg_low += regspace;
+	}
+
 	netlink_gen_expr(ctx, expr->map, sreg);
+	ctx->reg_low -= regspace;
 
 	nle = alloc_nft_expr("lookup");
 	netlink_put_register(nle, NFTNL_EXPR_LOOKUP_SREG, sreg);
-- 
1.8.3.1