Skip to contentSkip to conversation

fib: Fix for existence check on Big Endian

PATCH In review Phil Sutter 2 participants 7 messages Sep 9, 2025 – Sep 11, 2025 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/1 fib: Fix for existence check on Big Endian

Adjust the expression size to 1B so cmp expression value is correct. Without this, the rule 'fib saddr . iif check exists' generates following byte code on BE:

|  [ fib saddr . iif oif present => reg 1 ]
|  [ cmp eq reg 1 0x00000001 ]

Though with NFTA_FIB_F_PRESENT flag set, nft_fib.ko writes to the first byte of reg 1 only (using nft_reg_store8()). With this patch in place, byte code is correct:

|  [ fib saddr . iif oif present => reg 1 ]
|  [ cmp eq reg 1 0x01000000 ]
Fixes f686a17eafa0b ("fib: Support existence check")
Cc Yi Chen <[email protected]>
---
 src/evaluate.c | 1 +
 src/fib.c      | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)
src/evaluate.c +1 -0
index 8cecbe09de01c..6a1aa4963bceb 100644
@@ -3002,6 +3002,7 @@ static int expr_evaluate_fib(struct eval_ctx *ctx, struct expr **exprp)
 	if (expr->flags & EXPR_F_BOOLEAN) {
 		expr->fib.flags |= NFTA_FIB_F_PRESENT;
 		datatype_set(expr, &boolean_type);
+		expr->len = BITS_PER_BYTE;
 	}
 	return expr_evaluate_primary(ctx, exprp);
 }
src/fib.c +3 -1
index 5383613292a5e..4db7cd2bbc9c3 100644
@@ -198,8 +198,10 @@ struct expr *fib_expr_alloc(const struct location *loc,
 		BUG("Unknown result %d\n", result);
 	}
 
-	if (flags & NFTA_FIB_F_PRESENT)
+	if (flags & NFTA_FIB_F_PRESENT) {
 		type = &boolean_type;
+		len = BITS_PER_BYTE;
+	}
 
 	expr = expr_alloc(loc, EXPR_FIB, type,
 			  BYTEORDER_HOST_ENDIAN, len);
Signature · 1 line
2.51.0
#1 lore raw

Hi Phil,

Quoted 7 lines · Phil Sutter

Adjust the expression size to 1B so cmp expression value is correct. Without this, the rule 'fib saddr . iif check exists' generates following byte code on BE:

|  [ fib saddr . iif oif present => reg 1 ]
|  [ cmp eq reg 1 0x00000001 ]
Quoted 6 lines

Though with NFTA_FIB_F_PRESENT flag set, nft_fib.ko writes to the first byte of reg 1 only (using nft_reg_store8()). With this patch in place, byte code is correct:

|  [ fib saddr . iif oif present => reg 1 ]
|  [ cmp eq reg 1 0x01000000 ]

Is this a generic issue of boolean that is using 1 bit?

const struct datatype boolean_type = {

        .type           = TYPE_BOOLEAN,
        .name           = "boolean",
        .desc           = "boolean type",
        .size           = 1,
Quoted 39 lines
Fixes f686a17eafa0b ("fib: Support existence check")
Cc Yi Chen <[email protected]>
---
 src/evaluate.c | 1 +
 src/fib.c      | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)
src/evaluate.c +1 -0
index 8cecbe09de01c..6a1aa4963bceb 100644
@@ -3002,6 +3002,7 @@ static int expr_evaluate_fib(struct eval_ctx *ctx, struct expr **exprp)
 	if (expr->flags & EXPR_F_BOOLEAN) {
 		expr->fib.flags |= NFTA_FIB_F_PRESENT;
 		datatype_set(expr, &boolean_type);
+		expr->len = BITS_PER_BYTE;
 	}
 	return expr_evaluate_primary(ctx, exprp);
 }
src/fib.c +3 -1
index 5383613292a5e..4db7cd2bbc9c3 100644
@@ -198,8 +198,10 @@ struct expr *fib_expr_alloc(const struct location *loc,
 		BUG("Unknown result %d\n", result);
 	}
 
-	if (flags & NFTA_FIB_F_PRESENT)
+	if (flags & NFTA_FIB_F_PRESENT) {
 		type = &boolean_type;
+		len = BITS_PER_BYTE;
+	}
 
 	expr = expr_alloc(loc, EXPR_FIB, type,
 			  BYTEORDER_HOST_ENDIAN, len);
Signature · 1 line
2.51.0
author reviewer
#2 lore raw

Hi Pablo,

Quoted 15 lines · Pablo Neira Ayuso
Quoted 7 lines · Phil Sutter

Adjust the expression size to 1B so cmp expression value is correct. Without this, the rule 'fib saddr . iif check exists' generates following byte code on BE:

|  [ fib saddr . iif oif present => reg 1 ]
|  [ cmp eq reg 1 0x00000001 ]
Quoted 6 lines

Though with NFTA_FIB_F_PRESENT flag set, nft_fib.ko writes to the first byte of reg 1 only (using nft_reg_store8()). With this patch in place, byte code is correct:

|  [ fib saddr . iif oif present => reg 1 ]
|  [ cmp eq reg 1 0x01000000 ]
Quoted 7 lines

Is this a generic issue of boolean that is using 1 bit?

const struct datatype boolean_type = {

        .type           = TYPE_BOOLEAN,
        .name           = "boolean",
        .desc           = "boolean type",
        .size           = 1,

Maybe, yes: I compared fib existence checks to exthdr ones in order to find the bug. With exthdr, we know in parser already that it is an existence check (see exthdr_exists_expr rule in parser_bison.y). If so, exthdr expression is allocated with type 1 which is (assumed to be) the NEXTHDR field in all extension headers. This field has inet_protocol_type, which is size 8b.

Via expr_ctx::len, RHS will then be adjusted to 8b size (see 'expr->len = masklen' in expr_evaluate_integer()).

IIRC, LHS defines the RHS size in relationals. I am not sure if we may sanely reverse this rule if RHS is a boolean_type.

Cheers, Phil

#3 lore raw
Quoted 33 lines · Phil Sutter

Hi Pablo,

Quoted 15 lines · Pablo Neira Ayuso
Quoted 7 lines · Phil Sutter

Adjust the expression size to 1B so cmp expression value is correct. Without this, the rule 'fib saddr . iif check exists' generates following byte code on BE:

|  [ fib saddr . iif oif present => reg 1 ]
|  [ cmp eq reg 1 0x00000001 ]
Quoted 6 lines

Though with NFTA_FIB_F_PRESENT flag set, nft_fib.ko writes to the first byte of reg 1 only (using nft_reg_store8()). With this patch in place, byte code is correct:

|  [ fib saddr . iif oif present => reg 1 ]
|  [ cmp eq reg 1 0x01000000 ]
Quoted 7 lines

Is this a generic issue of boolean that is using 1 bit?

const struct datatype boolean_type = {

        .type           = TYPE_BOOLEAN,
        .name           = "boolean",
        .desc           = "boolean type",
        .size           = 1,

Maybe, yes: I compared fib existence checks to exthdr ones in order to find the bug. With exthdr, we know in parser already that it is an existence check (see exthdr_exists_expr rule in parser_bison.y). If so, exthdr expression is allocated with type 1 which is (assumed to be) the NEXTHDR field in all extension headers. This field has inet_protocol_type, which is size 8b.

Quoted 5 lines

Via expr_ctx::len, RHS will then be adjusted to 8b size (see 'expr->len = masklen' in expr_evaluate_integer()).

IIRC, LHS defines the RHS size in relationals. I am not sure if we may sanely reverse this rule if RHS is a boolean_type.

Probably this fix is more generic, see untested patch.

src/datatype.c +1 -1
index f347010f4a1a..2d39239316a6 100644
@@ -1581,7 +1581,7 @@ const struct datatype boolean_type = {
 	.type		= TYPE_BOOLEAN,
 	.name		= "boolean",
 	.desc		= "boolean type",
-	.size		= 1,
+	.size		= BITS_PER_BYTE,
 	.parse		= boolean_type_parse,
 	.basetype	= &integer_type,
 	.sym_tbl	= &boolean_tbl,
author reviewer
#4 lore raw
Quoted 47 lines · Pablo Neira Ayuso
Quoted 33 lines · Phil Sutter

Hi Pablo,

Quoted 15 lines · Pablo Neira Ayuso
Quoted 7 lines · Phil Sutter

Adjust the expression size to 1B so cmp expression value is correct. Without this, the rule 'fib saddr . iif check exists' generates following byte code on BE:

|  [ fib saddr . iif oif present => reg 1 ]
|  [ cmp eq reg 1 0x00000001 ]
Quoted 6 lines

Though with NFTA_FIB_F_PRESENT flag set, nft_fib.ko writes to the first byte of reg 1 only (using nft_reg_store8()). With this patch in place, byte code is correct:

|  [ fib saddr . iif oif present => reg 1 ]
|  [ cmp eq reg 1 0x01000000 ]
Quoted 7 lines

Is this a generic issue of boolean that is using 1 bit?

const struct datatype boolean_type = {

        .type           = TYPE_BOOLEAN,
        .name           = "boolean",
        .desc           = "boolean type",
        .size           = 1,

Maybe, yes: I compared fib existence checks to exthdr ones in order to find the bug. With exthdr, we know in parser already that it is an existence check (see exthdr_exists_expr rule in parser_bison.y). If so, exthdr expression is allocated with type 1 which is (assumed to be) the NEXTHDR field in all extension headers. This field has inet_protocol_type, which is size 8b.

Quoted 5 lines

Via expr_ctx::len, RHS will then be adjusted to 8b size (see 'expr->len = masklen' in expr_evaluate_integer()).

IIRC, LHS defines the RHS size in relationals. I am not sure if we may sanely reverse this rule if RHS is a boolean_type.

Probably this fix is more generic, see untested patch.

src/datatype.c +0 -0
index f347010f4a1a..2d39239316a6 100644

src/datatype.c const struct datatype boolean_type = { · line 1587

Quoted 8 lines
src/datatype.c +1 -1
@@ -1581,7 +1581,7 @@ const struct datatype boolean_type = {
 	.type		= TYPE_BOOLEAN,
 	.name		= "boolean",
 	.desc		= "boolean type",
-	.size		= 1,
+	.size		= BITS_PER_BYTE,
 	.parse		= boolean_type_parse,
 	.basetype	= &integer_type,
 	.sym_tbl	= &boolean_tbl,

This does not make a difference. Since the fib expr::len remains at value 32, the cmp payload will be 32 bits as well with the first three bytes being zero on BE irrespective of RHS value.

Cheers, Phil

maintainer Reviewed-by on v1 1/1
#5 lore raw
In reply to Phil Sutter
Quoted 11 lines · Phil Sutter

Adjust the expression size to 1B so cmp expression value is correct. Without this, the rule 'fib saddr . iif check exists' generates following byte code on BE:

|  [ fib saddr . iif oif present => reg 1 ]
|  [ cmp eq reg 1 0x00000001 ]

Though with NFTA_FIB_F_PRESENT flag set, nft_fib.ko writes to the first byte of reg 1 only (using nft_reg_store8()). With this patch in place, byte code is correct:

Quoted 6 lines
|  [ fib saddr . iif oif present => reg 1 ]
|  [ cmp eq reg 1 0x01000000 ]
Fixes f686a17eafa0b ("fib: Support existence check")
Cc Yi Chen <[email protected]>

Thanks.

author reviewer
#6 lore raw
Quoted 12 lines · Pablo Neira Ayuso
Quoted 11 lines · Phil Sutter

Adjust the expression size to 1B so cmp expression value is correct. Without this, the rule 'fib saddr . iif check exists' generates following byte code on BE:

|  [ fib saddr . iif oif present => reg 1 ]
|  [ cmp eq reg 1 0x00000001 ]

Though with NFTA_FIB_F_PRESENT flag set, nft_fib.ko writes to the first byte of reg 1 only (using nft_reg_store8()). With this patch in place, byte code is correct:

Quoted 8 lines
Quoted 6 lines
|  [ fib saddr . iif oif present => reg 1 ]
|  [ cmp eq reg 1 0x01000000 ]
Fixes f686a17eafa0b ("fib: Support existence check")
Cc Yi Chen <[email protected]>

Patch applied, thanks!