Skip to contentSkip to conversation

netfilter: nft_bitwise: fix dst corruption in same register shifts

PATCH Superseded Fernando Fernandez Mancera 3 participants 6 messages Apr 23

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 netfilter: nft_bitwise: fix dst corruption in same register shifts

In nft_bitwise_eval_lshift() and nft_bitwise_eval_rshift(), shift operations are performed in a loop over 32-bit words. The loop calculates the shifted value, writes it to dst, and calculates the carry from src for the next iteration.

If the source and destination registers overlap either exactly (sreg == dreg) or partially (e.g., dreg is offset from sreg by 4 bytes) the loop overwrites src data before it can be read by subsequent iterations. This causes the carry or the shifted data itself to be incorrectly calculated using the newly modified dst value instead of the original src payload.

Fix this by separating the calculation and assignment phases. Evaluate the shift into a temporary local array and then copy the final results into dst. This completely decouples the memory reads from the memory writes, safely supporting any register overlap configuration without corrupting the payload.

This was tested for both partial and exact overlaps with the following bytecode:

table test_table ip flags 0 use 1 handle 1
ip test_table test_chain use 3 type filter hook input prio 0 policy accept packets 0 bytes 0 flags 1 ip test_table test_chain 2
[ immediate reg 1 0xaaaaaaaa 0x44332211 0x88776655 ]
[ bitwise reg 1 = ( reg 9 << 0x08000000 ) ]
[ cmp eq reg 1 0x55008877 0x00887766 ]
[ counter pkts 0 bytes 0 ]
ip test_table test_chain 3 2
[ immediate reg 1 0xaaaaaaaa 0x44332211 0x88776655 ]
[ bitwise reg 1 = ( reg 9 << 0x08000000 ) ]
[ cmp eq reg 1 0x55443322 0x00887766 ]
[ counter pkts 1511 bytes 133128 ]
ip test_table test_chain 4 3
[ immediate reg 1 0x44332211 0x88776655 ]
[ bitwise reg 1 = ( reg 1 << 0x08000000 ) ]
[ cmp eq reg 1 0x55443322 0x00887766 ]
[ counter pkts 109 bytes 9548 ]
ip test_table test_chain 5 4
[ immediate reg 1 0x44332211 0x88776655 ]
[ bitwise reg 1 = ( reg 1 << 0x08000000 ) ]
[ cmp eq reg 1 0x66443322 0x00887766 ]
[ counter pkts 0 bytes 0 ]

Fixes 567d746b55bc ("netfilter: bitwise: add support for shifts.")

---
v2: handle partially register overlap too

---
 net/netfilter/nft_bitwise.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
net/netfilter/nft_bitwise.c +14 -6
index 13808e9cd999..27fc09ec0e11 100644
@@ -38,27 +38,35 @@ static void nft_bitwise_eval_mask_xor(u32 *dst, const u32 *src,
 static void nft_bitwise_eval_lshift(u32 *dst, const u32 *src,
 				    const struct nft_bitwise *priv)
 {
+	unsigned int i, n = DIV_ROUND_UP(priv->len, sizeof(u32));
 	u32 shift = priv->data.data[0];
-	unsigned int i;
+	u32 res[NFT_REG_SIZE];
 	u32 carry = 0;
 
-	for (i = DIV_ROUND_UP(priv->len, sizeof(u32)); i > 0; i--) {
-		dst[i - 1] = (src[i - 1] << shift) | carry;
+	for (i = n; i > 0; i--) {
+		res[i - 1] = (src[i - 1] << shift) | carry;
 		carry = src[i - 1] >> (BITS_PER_TYPE(u32) - shift);
 	}
+
+	for (i = 0; i < n; i++)
+		dst[i] = res[i];
 }
 
 static void nft_bitwise_eval_rshift(u32 *dst, const u32 *src,
 				    const struct nft_bitwise *priv)
 {
+	unsigned int i, n = DIV_ROUND_UP(priv->len, sizeof(u32));
 	u32 shift = priv->data.data[0];
-	unsigned int i;
+	u32 res[NFT_REG_SIZE];
 	u32 carry = 0;
 
-	for (i = 0; i < DIV_ROUND_UP(priv->len, sizeof(u32)); i++) {
-		dst[i] = carry | (src[i] >> shift);
+	for (i = 0; i < n; i++) {
+		res[i] = carry | (src[i] >> shift);
 		carry = src[i] << (BITS_PER_TYPE(u32) - shift);
 	}
+
+	for (i = 0; i < n; i++)
+		dst[i] = res[i];
 }
 
 static void nft_bitwise_eval_and(u32 *dst, const u32 *src, const u32 *src2,
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 10 lines · Fernando Fernandez Mancera

In nft_bitwise_eval_lshift() and nft_bitwise_eval_rshift(), shift operations are performed in a loop over 32-bit words. The loop calculates the shifted value, writes it to dst, and calculates the carry from src for the next iteration.

If the source and destination registers overlap either exactly (sreg == dreg) or partially (e.g., dreg is offset from sreg by 4 bytes) the loop overwrites src data before it can be read by subsequent iterations. This causes the carry or the shifted data itself to be incorrectly calculated using the newly modified dst value instead of the original src payload.

We should support overlaps (src == dst). But partial overlaps? Does nft generate such byte code?

I think we should reject it. Either userspace has no more use for src (and can clobber it with the shifted result), or it has to keep it for later reuse. But in what case is a partially clobbered register useful?

#2 lore raw
Quoted 6 lines · Florian Westphal
Quoted 5 lines · Fernando Fernandez Mancera

In nft_bitwise_eval_lshift() and nft_bitwise_eval_rshift(), shift operations are performed in a loop over 32-bit words. The loop calculates the shifted value, writes it to dst, and calculates the carry from src for the next iteration.

Quoted 9 lines
Quoted 5 lines

If the source and destination registers overlap either exactly (sreg == dreg) or partially (e.g., dreg is offset from sreg by 4 bytes) the loop overwrites src data before it can be read by subsequent iterations. This causes the carry or the shifted data itself to be incorrectly calculated using the newly modified dst value instead of the original src payload.

We should support overlaps (src == dst). But partial overlaps? Does nft generate such byte code?

No, not at all.

Quoted 5 lines

I think we should reject it. Either userspace has no more use for src (and can clobber it with the shifted result), or it has to keep it for later reuse. But in what case is a partially clobbered register useful?

Fine for me. I could not find any situation where this could be useful but I was afraid of breaking some weird setups. If we have agreement that this isn't useful I am fine rejecting it from control plane with something like (not tested):

unsigned int n = DIV_ROUND_UP(priv->len, sizeof(u32));

if (priv->sreg != priv->dreg &&

    priv->dreg < priv->sreg + n &&
    priv->sreg < priv->dreg + n)
	return -EINVAL;

Thanks,
Fernando.

maintainer
#3 lore raw
Quoted 6 lines · Fernando Fernandez Mancera
Quoted 3 lines · Florian Westphal

We should support overlaps (src == dst). But partial overlaps? Does nft generate such byte code?

No, not at all.

Phew. :-)

Quoted 4 lines

Fine for me. I could not find any situation where this could be useful but I was afraid of breaking some weird setups. If we have agreement that this isn't useful I am fine rejecting it from control plane with something like (not tested):

Lets wait for Pablos take on this, but I would prefer reject from control plane.

#4 lore raw
Quoted 10 lines · Florian Westphal
Quoted 6 lines · Fernando Fernandez Mancera
Quoted 3 lines · Florian Westphal

We should support overlaps (src == dst). But partial overlaps? Does nft generate such byte code?

No, not at all.

Phew. :-)

Quoted 7 lines
Quoted 4 lines

Fine for me. I could not find any situation where this could be useful but I was afraid of breaking some weird setups. If we have agreement that this isn't useful I am fine rejecting it from control plane with something like (not tested):

Lets wait for Pablos take on this, but I would prefer reject from control plane.

I'd rather reject this from control plane too, thanks!

#5 lore raw
Quoted 16 lines · Pablo Neira Ayuso
Quoted 15 lines · Florian Westphal
Quoted 6 lines · Fernando Fernandez Mancera
Quoted 3 lines · Florian Westphal

We should support overlaps (src == dst). But partial overlaps? Does nft generate such byte code?

No, not at all.

Phew. :-)

Quoted 4 lines

Fine for me. I could not find any situation where this could be useful but I was afraid of breaking some weird setups. If we have agreement that this isn't useful I am fine rejecting it from control plane with something like (not tested):

Quoted 4 lines
Quoted 2 lines

Lets wait for Pablos take on this, but I would prefer reject from control plane.

I'd rather reject this from control plane too, thanks!

Got it, then I will take the v1 and add the control plane check. Thanks!