Skip to contentSkip to conversation

netfilter: nf_tables: fix big endian problems

PATCH In review Patrick McHardy 3 participants 4 messages Apr 12, 2014 – Apr 14, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover netfilter: nf_tables: fix big endian problems

This is the last patch required to fix the big endian problems observed by Arturo on Sparc. The changelog contains a detailed explanation of the problem.

This should also go to -stable.

#1 lore raw

PATCH 1/1 netfilter: nf_tables: fix nft_cmp_fast failure on big endian for size < 4

nft_cmp_fast is used for equality comparisions of size <= 4. For comparisions of size < 4 byte a mask is calculated that is applied to both the data from userspace (during initialization) and the register value (during runtime). Both values are stored using (in effect) memcpy to a memory area that is then interpreted as u32 by nft_cmp_fast.

This works fine on little endian since smaller types have the same base address, however on big endian this is not true and the smaller types are interpreted as a big number with trailing zero bytes.

The mask therefore must not include the lower bytes, but the higher bytes on big endian. Add a helper function that does a cpu_to_le32 to switch the bytes on big endian. Since we're dealing with a mask of just consequitive bits, this works out fine.

---
 include/net/netfilter/nf_tables_core.h | 10 ++++++++++
 net/netfilter/nf_tables_core.c         |  3 +--
 net/netfilter/nft_cmp.c                |  2 +-
 3 files changed, 12 insertions(+), 3 deletions(-)
include/net/netfilter/nf_tables_core.h +10 -0
index cf2b7ae..a75fc8e 100644
@@ -13,6 +13,16 @@ struct nft_cmp_fast_expr {
 	u8			len;
 };
 
+/* Calculate the mask for the nft_cmp_fast expression. On big endian the
+ * mask needs to include the *upper* bytes when interpreting that data as
+ * something smaller than the full u32, therefore a cpu_to_le32 is done.
+ */
+static inline u32 nft_cmp_fast_mask(unsigned int len)
+{
+	return cpu_to_le32(~0U >> (FIELD_SIZEOF(struct nft_cmp_fast_expr,
+						data) * BITS_PER_BYTE - len));
+}
+
 extern const struct nft_expr_ops nft_cmp_fast_ops;
 
 int nft_cmp_module_init(void);
net/netfilter/nf_tables_core.c +1 -2
index 90998a6..8041053 100644
@@ -25,9 +25,8 @@ static void nft_cmp_fast_eval(const struct nft_expr *expr,
 			      struct nft_data data[NFT_REG_MAX + 1])
 {
 	const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
-	u32 mask;
+	u32 mask = nft_cmp_fast_mask(priv->len);
 
-	mask = ~0U >> (sizeof(priv->data) * BITS_PER_BYTE - priv->len);
 	if ((data[priv->sreg].data[0] & mask) == priv->data)
 		return;
 	data[NFT_REG_VERDICT].verdict = NFT_BREAK;
net/netfilter/nft_cmp.c +1 -1
index 954925d..e2b3f51 100644
@@ -128,7 +128,7 @@ static int nft_cmp_fast_init(const struct nft_ctx *ctx,
 	BUG_ON(err < 0);
 	desc.len *= BITS_PER_BYTE;
 
-	mask = ~0U >> (sizeof(priv->data) * BITS_PER_BYTE - desc.len);
+	mask = nft_cmp_fast_mask(desc.len);
 	priv->data = data.data[0] & mask;
 	priv->len  = desc.len;
 	return 0;
Signature · 1 line
1.9.0
#2 lore raw

Re: [PATCH 1/1] netfilter: nf_tables: fix nft_cmp_fast failure on big endian for size < 4

Quoted 6 lines · Patrick McHardy

nft_cmp_fast is used for equality comparisions of size <= 4. For comparisions of size < 4 byte a mask is calculated that is applied to both the data from userspace (during initialization) and the register value (during runtime). Both values are stored using (in effect) memcpy to a memory area that is then interpreted as u32 by nft_cmp_fast.

Quoted 8 lines

This works fine on little endian since smaller types have the same base address, however on big endian this is not true and the smaller types are interpreted as a big number with trailing zero bytes.

The mask therefore must not include the lower bytes, but the higher bytes on big endian. Add a helper function that does a cpu_to_le32 to switch the bytes on big endian. Since we're dealing with a mask of just consequitive bits, this works out fine.

Applied and queue up to -stable. Thanks.

#3 lore raw
In reply to Patrick McHardy
Quoted 6 lines · Patrick McHardy

This is the last patch required to fix the big endian problems observed by Arturo on Sparc. The changelog contains a detailed explanation of the problem.

This should also go to -stable.

Thanks Patrick.

Best regards.

Signature · 5 lines
Arturo Borrero González
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html