Skip to contentSkip to conversation

netfilter: shift-out-of-bounds in nft_bitwise

PATCH In review Ren Wei 2 participants 2 messages Apr 21 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Ren Wei

author
#1 lore raw

PATCH 1/1 netfilter: shift-out-of-bounds in nft_bitwise

From: Kai Ma <[email protected]>

Handle zero shift operands explicitly in nft_bitwise_eval_lshift() and nft_bitwise_eval_rshift().

Shift expressions accept values in the range [0, 31], but the carry propagation code assumes a non-zero shift and computes the carry from the adjacent 32-bit word unconditionally. For a zero shift operand, the expected result is to leave the value unchanged.

Treat zero shift as a no-op before entering the carry propagation loops. This preserves the existing behaviour for non-zero shifts and matches the expected semantics of shifting by zero.

Fixes 567d746b55bc ("netfilter: bitwise: add support for shifts.")
---
 net/netfilter/nft_bitwise.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
net/netfilter/nft_bitwise.c +16 -4
index d550910aabec..f74774b176af 100644
@@ -39,10 +39,16 @@ static void nft_bitwise_eval_lshift(u32 *dst, const u32 *src,
 				    const struct nft_bitwise *priv)
 {
 	u32 shift = priv->data.data[0];
-	unsigned int i;
+	unsigned int i, n = DIV_ROUND_UP(priv->len, sizeof(u32));
 	u32 carry = 0;
 
-	for (i = DIV_ROUND_UP(priv->len, sizeof(u32)); i > 0; i--) {
+	if (!shift) {
+		for (i = 0; i < n; i++)
+			dst[i] = src[i];
+		return;
+	}
+
+	for (i = n; i > 0; i--) {
 		dst[i - 1] = (src[i - 1] << shift) | carry;
 		carry = src[i - 1] >> (BITS_PER_TYPE(u32) - shift);
 	}
@@ -52,10 +58,16 @@ static void nft_bitwise_eval_rshift(u32 *dst, const u32 *src,
 				    const struct nft_bitwise *priv)
 {
 	u32 shift = priv->data.data[0];
-	unsigned int i;
+	unsigned int i, n = DIV_ROUND_UP(priv->len, sizeof(u32));
 	u32 carry = 0;
 
-	for (i = 0; i < DIV_ROUND_UP(priv->len, sizeof(u32)); i++) {
+	if (!shift) {
+		for (i = 0; i < n; i++)
+			dst[i] = src[i];
+		return;
+	}
+
+	for (i = 0; i < n; i++) {
 		dst[i] = carry | (src[i] >> shift);
 		carry = src[i] << (BITS_PER_TYPE(u32) - shift);
 	}
Signature · 1 line
2.43.0
#2 lore raw
Quoted 5 lines · Ren Wei

From: Kai Ma <[email protected]>

Handle zero shift operands explicitly in nft_bitwise_eval_lshift() and nft_bitwise_eval_rshift().

Quoted 9 lines

Shift expressions accept values in the range [0, 31], but the carry propagation code assumes a non-zero shift and computes the carry from the adjacent 32-bit word unconditionally. For a zero shift operand, the expected result is to leave the value unchanged.

Treat zero shift as a no-op before entering the carry propagation loops. This preserves the existing behaviour for non-zero shifts and matches the expected semantics of shifting by zero.

I think the issue here is an Undefined Behavior actually, AFAICS when shifting by the size of the type is UB and depending on the architecture used it can lead to shift-out-of-bounds due to carry being equals to .

Shouldn't this be rejected in control plane during validation? As a 0 shift operation is pointless, we can reject it right away.

In addition, please use nf target for v2.

Thanks,
Fernando.

Quoted 54 lines
Fixes 567d746b55bc ("netfilter: bitwise: add support for shifts.")
---
  net/netfilter/nft_bitwise.c | 20 ++++++++++++++++----
  1 file changed, 16 insertions(+), 4 deletions(-)
net/netfilter/nft_bitwise.c +16 -4
index d550910aabec..f74774b176af 100644
@@ -39,10 +39,16 @@ static void nft_bitwise_eval_lshift(u32 *dst, const u32 *src,
  				    const struct nft_bitwise *priv)
  {
  	u32 shift = priv->data.data[0];
-	unsigned int i;
+	unsigned int i, n = DIV_ROUND_UP(priv->len, sizeof(u32));
  	u32 carry = 0;
 
-	for (i = DIV_ROUND_UP(priv->len, sizeof(u32)); i > 0; i--) {
+	if (!shift) {
+		for (i = 0; i < n; i++)
+			dst[i] = src[i];
+		return;
+	}
+
+	for (i = n; i > 0; i--) {
  		dst[i - 1] = (src[i - 1] << shift) | carry;
  		carry = src[i - 1] >> (BITS_PER_TYPE(u32) - shift);
  	}
@@ -52,10 +58,16 @@ static void nft_bitwise_eval_rshift(u32 *dst, const u32 *src,
  				    const struct nft_bitwise *priv)
  {
  	u32 shift = priv->data.data[0];
-	unsigned int i;
+	unsigned int i, n = DIV_ROUND_UP(priv->len, sizeof(u32));
  	u32 carry = 0;
 
-	for (i = 0; i < DIV_ROUND_UP(priv->len, sizeof(u32)); i++) {
+	if (!shift) {
+		for (i = 0; i < n; i++)
+			dst[i] = src[i];
+		return;
+	}
+
+	for (i = 0; i < n; i++) {
  		dst[i] = carry | (src[i] >> shift);
  		carry = src[i] << (BITS_PER_TYPE(u32) - shift);
  	}