Skip to contentSkip to conversation

ruleparse: arp: Fix for all-zero mask on Big Endian

PATCH Superseded Phil Sutter 2 participants 4 messages Jan 28 – Jan 29 netfilter

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/1 ruleparse: arp: Fix for all-zero mask on Big Endian

With 16bit mask values, the first two bytes of bitwise.mask in struct nft_xt_ctx_reg are significant. Reading the first 32bit-sized field works only on Little Endian, on Big Endian the mask appears in the upper two bytes which are discarded when assigning to a 16bit variable.

Fixes ab2d5f8c7bbee ("nft-arp: add missing mask support")
---
 iptables/nft-ruleparse-arp.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
iptables/nft-ruleparse-arp.c +4 -4
index b0671cb0dfe8f..0648b2748931f 100644
@@ -90,7 +90,7 @@ static void nft_arp_parse_payload(struct nft_xt_ctx *ctx,
 		if (inv)
 			fw->arp.invflags |= IPT_INV_ARPHRD;
 		if (reg->bitwise.set)
-			fw->arp.arhrd_mask = reg->bitwise.mask[0];
+			fw->arp.arhrd_mask = ((uint16_t *)reg->bitwise.mask)[0];
 		break;
 	case offsetof(struct arphdr, ar_pro):
 		get_cmp_data(e, &ar_pro, sizeof(ar_pro), &inv);
@@ -99,7 +99,7 @@ static void nft_arp_parse_payload(struct nft_xt_ctx *ctx,
 		if (inv)
 			fw->arp.invflags |= IPT_INV_PROTO;
 		if (reg->bitwise.set)
-			fw->arp.arpro_mask = reg->bitwise.mask[0];
+			fw->arp.arpro_mask = ((uint16_t *)reg->bitwise.mask)[0];
 		break;
 	case offsetof(struct arphdr, ar_op):
 		get_cmp_data(e, &ar_op, sizeof(ar_op), &inv);
@@ -108,7 +108,7 @@ static void nft_arp_parse_payload(struct nft_xt_ctx *ctx,
 		if (inv)
 			fw->arp.invflags |= IPT_INV_ARPOP;
 		if (reg->bitwise.set)
-			fw->arp.arpop_mask = reg->bitwise.mask[0];
+			fw->arp.arpop_mask = ((uint16_t *)reg->bitwise.mask)[0];
 		break;
 	case offsetof(struct arphdr, ar_hln):
 		get_cmp_data(e, &ar_hln, sizeof(ar_hln), &inv);
@@ -117,7 +117,7 @@ static void nft_arp_parse_payload(struct nft_xt_ctx *ctx,
 		if (inv)
 			fw->arp.invflags |= IPT_INV_ARPHLN;
 		if (reg->bitwise.set)
-			fw->arp.arhln_mask = reg->bitwise.mask[0];
+			fw->arp.arhln_mask = ((uint8_t *)reg->bitwise.mask)[0];
 		break;
 	case offsetof(struct arphdr, ar_pln):
 		get_cmp_data(e, &ar_pln, sizeof(ar_pln), &inv);
Signature · 1 line
2.51.0
maintainer
#1 lore raw
Quoted 4 lines · Phil Sutter

With 16bit mask values, the first two bytes of bitwise.mask in struct nft_xt_ctx_reg are significant. Reading the first 32bit-sized field works only on Little Endian, on Big Endian the mask appears in the upper two bytes which are discarded when assigning to a 16bit variable.

nft-ruleparse-arp.c: In function 'nft_arp_parse_payload':
nft-ruleparse-arp.c:93:77: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
93 | fw->arp.arhrd_mask = ((uint16_t *)reg->bitwise.mask)[0];
| ^
nft-ruleparse-arp.c:102:77: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
102 | fw->arp.arpro_mask = ((uint16_t *)reg->bitwise.mask)[0];
| ^
nft-ruleparse-arp.c:111:77: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
111 | fw->arp.arpop_mask = ((uint16_t *)reg->bitwise.mask)[0];
| ^

author reviewer
#2 lore raw
Quoted 6 lines · Florian Westphal
Quoted 4 lines · Phil Sutter

With 16bit mask values, the first two bytes of bitwise.mask in struct nft_xt_ctx_reg are significant. Reading the first 32bit-sized field works only on Little Endian, on Big Endian the mask appears in the upper two bytes which are discarded when assigning to a 16bit variable.

Quoted 10 lines

nft-ruleparse-arp.c: In function 'nft_arp_parse_payload':
nft-ruleparse-arp.c:93:77: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
93 | fw->arp.arhrd_mask = ((uint16_t *)reg->bitwise.mask)[0];
| ^
nft-ruleparse-arp.c:102:77: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
102 | fw->arp.arpro_mask = ((uint16_t *)reg->bitwise.mask)[0];
| ^
nft-ruleparse-arp.c:111:77: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
111 | fw->arp.arpop_mask = ((uint16_t *)reg->bitwise.mask)[0];
| ^

Oops! I didn't notice this because my build script passed -O0 in CFLAGS, probably from code coverage analysis. So back to the previous approach involving a union which I had deemed too much for just those four cases:

iptables/nft-ruleparse.h +5 -1
index 0377e4ae17a6e..dc755b361ec72 100644
@@ -36,7 +36,11 @@ struct nft_xt_ctx_reg {
        };
 
        struct {
-               uint32_t mask[4];
+               union {
+                       uint32_t mask[4];
+                       uint16_t mask16[];
+                       uint8_t  mask8[];
+               };
                uint32_t xor[4];
                bool set;
        } bitwise;

Or simply use mempcy() instead of the assignment?

Cheers, Phil

maintainer
#3 lore raw
Quoted 40 lines · Phil Sutter
Quoted 6 lines · Florian Westphal
Quoted 4 lines · Phil Sutter

With 16bit mask values, the first two bytes of bitwise.mask in struct nft_xt_ctx_reg are significant. Reading the first 32bit-sized field works only on Little Endian, on Big Endian the mask appears in the upper two bytes which are discarded when assigning to a 16bit variable.

Quoted 10 lines

nft-ruleparse-arp.c: In function 'nft_arp_parse_payload':
nft-ruleparse-arp.c:93:77: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
93 | fw->arp.arhrd_mask = ((uint16_t *)reg->bitwise.mask)[0];
| ^
nft-ruleparse-arp.c:102:77: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
102 | fw->arp.arpro_mask = ((uint16_t *)reg->bitwise.mask)[0];
| ^
nft-ruleparse-arp.c:111:77: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
111 | fw->arp.arpop_mask = ((uint16_t *)reg->bitwise.mask)[0];
| ^

Oops! I didn't notice this because my build script passed -O0 in CFLAGS, probably from code coverage analysis. So back to the previous approach involving a union which I had deemed too much for just those four cases:

iptables/nft-ruleparse.h +5 -1
index 0377e4ae17a6e..dc755b361ec72 100644
@@ -36,7 +36,11 @@ struct nft_xt_ctx_reg {
        };
 
        struct {
-               uint32_t mask[4];
+               union {
+                       uint32_t mask[4];
+                       uint16_t mask16[];
+                       uint8_t  mask8[];
+               };
                uint32_t xor[4];
                bool set;
        } bitwise;
Quoted 1 line

Or simply use mempcy() instead of the assignment?

Or add:

static uint16_t get_u16p(uint32_t *t) { const uint16_t *p = (uint16_t *)t; return *p; } static uint8_t get_u8p(uint32_t *t) { const uint8_t *p = (uint8_t *)t; return *p; }

... and use that. But up to you.