Skip to contentSkip to conversation

netfilter: nft_bitwise: fix dst corruption in same register shifts

PATCH Superseded Fernando Fernandez Mancera 2 participants 3 messages Apr 21 – Apr 23

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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

For lshift and rshift, the shift operations are performed in a loop over 32-bit words. The loop calculates the shifted value and write it to dst, and then immediately reads from src to calculate the carry for the next iteration. Because src and dst could point to the same memory location, the carry is incorrectly calculated using the newly modified dst value instead of the original src value.

Adding a temporal local variable to cache the original value before writing to dst and using it for the carry calculation solves the problem. This was tested with the following payload:

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 0x44332211 0x88776655 ]
[ bitwise reg 1 = ( reg 1 << 0x08000000 ) ]
[ cmp eq reg 1 0x66443322 0x00887766 ]
[ counter pkts 0 bytes 0 ]
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 21794 bytes 1917798 ]

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

---
Note: I found this issue while digging into the lshift/rshift operation

---
 net/netfilter/nft_bitwise.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
net/netfilter/nft_bitwise.c +8 -4
index 13808e9cd999..136e8f3a71c5 100644
@@ -43,8 +43,10 @@ static void nft_bitwise_eval_lshift(u32 *dst, const u32 *src,
 	u32 carry = 0;
 
 	for (i = DIV_ROUND_UP(priv->len, sizeof(u32)); i > 0; i--) {
-		dst[i - 1] = (src[i - 1] << shift) | carry;
-		carry = src[i - 1] >> (BITS_PER_TYPE(u32) - shift);
+		u32 tmp_src = src[i - 1];
+
+		dst[i - 1] = (tmp_src << shift) | carry;
+		carry = tmp_src >> (BITS_PER_TYPE(u32) - shift);
 	}
 }
 
@@ -56,8 +58,10 @@ static void nft_bitwise_eval_rshift(u32 *dst, const u32 *src,
 	u32 carry = 0;
 
 	for (i = 0; i < DIV_ROUND_UP(priv->len, sizeof(u32)); i++) {
-		dst[i] = carry | (src[i] >> shift);
-		carry = src[i] << (BITS_PER_TYPE(u32) - shift);
+		u32 tmp_src = src[i];
+
+		dst[i] = carry | (tmp_src >> shift);
+		carry = tmp_src << (BITS_PER_TYPE(u32) - shift);
 	}
 }
 
Signature · 1 line
2.53.0
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Fernando Fernandez Mancera

For lshift and rshift, the shift operations are performed in a loop over 32-bit words. The loop calculates the shifted value and write it to dst, and then immediately reads from src to calculate the carry for the next iteration. Because src and dst could point to the same memory location, the carry is incorrectly calculated using the newly modified dst value instead of the original src value.

Adding a temporal local variable to cache the original value before

           ^^^^^^^^

"Temporary"?

Quoted 16 lines

writing to dst and using it for the carry calculation solves the problem. This was tested with the following payload:

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 0x44332211 0x88776655 ]
[ bitwise reg 1 = ( reg 1 << 0x08000000 ) ]
[ cmp eq reg 1 0x66443322 0x00887766 ]
[ counter pkts 0 bytes 0 ]
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 21794 bytes 1917798 ]

Quoted 2 lines
Fixes 567d746b55bc ("netfilter: bitwise: add support for shifts.")
Quoted 39 lines

---
Note: I found this issue while digging into the lshift/rshift operation

---
 net/netfilter/nft_bitwise.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
net/netfilter/nft_bitwise.c +8 -4
index 13808e9cd999..136e8f3a71c5 100644
@@ -43,8 +43,10 @@ static void nft_bitwise_eval_lshift(u32 *dst, const u32 *src,
 	u32 carry = 0;
 
 	for (i = DIV_ROUND_UP(priv->len, sizeof(u32)); i > 0; i--) {
-		dst[i - 1] = (src[i - 1] << shift) | carry;
-		carry = src[i - 1] >> (BITS_PER_TYPE(u32) - shift);
+		u32 tmp_src = src[i - 1];
+
+		dst[i - 1] = (tmp_src << shift) | carry;
+		carry = tmp_src >> (BITS_PER_TYPE(u32) - shift);
 	}
 }
 
@@ -56,8 +58,10 @@ static void nft_bitwise_eval_rshift(u32 *dst, const u32 *src,
 	u32 carry = 0;
 
 	for (i = 0; i < DIV_ROUND_UP(priv->len, sizeof(u32)); i++) {
-		dst[i] = carry | (src[i] >> shift);
-		carry = src[i] << (BITS_PER_TYPE(u32) - shift);
+		u32 tmp_src = src[i];
+
+		dst[i] = carry | (tmp_src >> shift);
+		carry = tmp_src << (BITS_PER_TYPE(u32) - shift);
 	}
 }
 
Signature · 1 line
2.53.0
#2 lore raw
Quoted 29 lines · Jeremy Sowden
Quoted 8 lines · Fernando Fernandez Mancera

For lshift and rshift, the shift operations are performed in a loop over 32-bit words. The loop calculates the shifted value and write it to dst, and then immediately reads from src to calculate the carry for the next iteration. Because src and dst could point to the same memory location, the carry is incorrectly calculated using the newly modified dst value instead of the original src value.

Adding a temporal local variable to cache the original value before

           ^^^^^^^^

"Temporary"?

Quoted 16 lines

writing to dst and using it for the carry calculation solves the problem. This was tested with the following payload:

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 0x44332211 0x88776655 ]
  [ bitwise reg 1 = ( reg 1 << 0x08000000 ) ]
  [ cmp eq reg 1 0x66443322 0x00887766 ]
  [ counter pkts 0 bytes 0 ]
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 21794 bytes 1917798 ]

Quoted 5 lines
Quoted 2 lines
Fixes 567d746b55bc ("netfilter: bitwise: add support for shifts.")

Thanks, I am preparing a v2 as sashiko spotted another problem and I have verified it is real. See:

"""
Does this leave the corruption issue unfixed for partial register
overlaps? If userspace provides overlapping registers where dreg is less than sreg, such as dreg=1 and sreg=2 with a len of 8, dst[1] aliases to src[0].

In the first iteration, this operation writes to dst[1]. In the second iteration, it reads from src[0], which would now contain the newly overwritten value instead of the original source data.
"""

I am testing the new fix.

Quoted 41 lines
Quoted 41 lines

---
Note: I found this issue while digging into the lshift/rshift operation
---
 net/netfilter/nft_bitwise.c | 12 ++++++++----

 1 file changed, 8 insertions(+), 4 deletions(-)
net/netfilter/nft_bitwise.c +8 -4
index 13808e9cd999..136e8f3a71c5 100644
@@ -43,8 +43,10 @@ static void nft_bitwise_eval_lshift(u32 *dst, const 
 u32 *src,
      u32 carry = 0;
 
      for (i = DIV_ROUND_UP(priv->len, sizeof(u32)); i > 0; i--) {
-        dst[i - 1] = (src[i - 1] << shift) | carry;
-        carry = src[i - 1] >> (BITS_PER_TYPE(u32) - shift);
+        u32 tmp_src = src[i - 1];
+
+        dst[i - 1] = (tmp_src << shift) | carry;
+        carry = tmp_src >> (BITS_PER_TYPE(u32) - shift);
      }
  }
@@ -56,8 +58,10 @@ static void nft_bitwise_eval_rshift(u32 *dst, const 
 u32 *src,
      u32 carry = 0;
 
      for (i = 0; i < DIV_ROUND_UP(priv->len, sizeof(u32)); i++) {
-        dst[i] = carry | (src[i] >> shift);
-        carry = src[i] << (BITS_PER_TYPE(u32) - shift);
+        u32 tmp_src = src[i];
+
+        dst[i] = carry | (tmp_src >> shift);
+        carry = tmp_src << (BITS_PER_TYPE(u32) - shift);
      }
  }
Signature · 2 lines
-- 
2.53.0