All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] nftables: fix big endian bugs
@ 2014-04-12  9:09 Patrick McHardy
  2014-04-12  9:09 ` [PATCH 1/2] expression: fix constant expression allocation on big endian Patrick McHardy
  2014-04-12  9:09 ` [PATCH 2/2] gmputil: use MSF/LSF in import/export functions dependant on host byte order Patrick McHardy
  0 siblings, 2 replies; 3+ messages in thread
From: Patrick McHardy @ 2014-04-12  9:09 UTC (permalink / raw)
  To: pablo; +Cc: arturo.borrero.glez, netfilter-devel

The following two patches fix the remaining big endian bugs in nftables
I could find during my testing.

Patch 1 addresses the problem that a constant expression is allocated with
data from a variable that is larger than the data size. On big endian we
can't simply use &var but need to calculate the address of the data
dependant on the size of the variable and the size of the data we want.

Patch 2 fixes the GMP import/export functions for BYTEORDER_HOST_ENDIAN
to use the proper word order dependant on the host endianess. This is needed
because we always use a word size of 1 and the word order basically
determines the byte order.

With these patches in place, everything appears to work fine. A single
fix for the kernel is also needed, this will be sent seperately.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] expression: fix constant expression allocation on big endian
  2014-04-12  9:09 [PATCH 0/2] nftables: fix big endian bugs Patrick McHardy
@ 2014-04-12  9:09 ` Patrick McHardy
  2014-04-12  9:09 ` [PATCH 2/2] gmputil: use MSF/LSF in import/export functions dependant on host byte order Patrick McHardy
  1 sibling, 0 replies; 3+ messages in thread
From: Patrick McHardy @ 2014-04-12  9:09 UTC (permalink / raw)
  To: pablo; +Cc: arturo.borrero.glez, netfilter-devel

From: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>

When allocating a constant expression, a pointer to the data is passed
to the allocation function. When the variable used to store the data
is larger than the size of the data type, this fails on big endian since
the most significant bytes (being zero) come first.

Add a helper function to calculate the proper address for the cases
where this is needed.

This currently affects symbolic tables for values < u64 and payload
dependency generation for protocol values < u32.

Signed-off-by: Patrick McHardy <kaber@trash.net>
---
 include/utils.h | 14 ++++++++++++++
 src/datatype.c  |  2 +-
 src/payload.c   |  3 ++-
 3 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/include/utils.h b/include/utils.h
index 88ee0c9..cc5948c 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -1,6 +1,7 @@
 #ifndef NFTABLES_UTILS_H
 #define NFTABLES_UTILS_H
 
+#include <asm/byteorder.h>
 #include <stdint.h>
 #include <stdbool.h>
 #include <stdarg.h>
@@ -46,6 +47,19 @@
 	typeof( ((type *)0)->member ) *__mptr = (ptr);		\
 	(type *)( (void *)__mptr - offsetof(type,member) );})
 
+/**
+ * Return a pointer to a constant variable of a size smaller than the variable.
+ */
+#ifdef __LITTLE_ENDIAN_BITFIELD
+#define constant_data_ptr(val, len) \
+	((void *)&(val))
+#elif defined(__BIG_ENDIAN_BITFIELD)
+#define constant_data_ptr(val, len) \
+	((void *)&(val) + sizeof(val) - (len) / BITS_PER_BYTE)
+#else
+#error "byteorder undefined"
+#endif
+
 #define field_sizeof(t, f)	(sizeof(((t *)NULL)->f))
 #define array_size(arr)		(sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
 #define div_round_up(n, d)	(((n) + (d) - 1) / (d))
diff --git a/src/datatype.c b/src/datatype.c
index ac42faa..331f235 100644
--- a/src/datatype.c
+++ b/src/datatype.c
@@ -124,7 +124,7 @@ struct error_record *symbolic_constant_parse(const struct expr *sym,
 
 	*res = constant_expr_alloc(&sym->location, dtype,
 				   dtype->byteorder, dtype->size,
-				   &s->value);
+				   constant_data_ptr(s->value, dtype->size));
 	return NULL;
 }
 
diff --git a/src/payload.c b/src/payload.c
index 427080c..a1785a5 100644
--- a/src/payload.c
+++ b/src/payload.c
@@ -209,7 +209,8 @@ int payload_gen_dependency(struct eval_ctx *ctx, const struct expr *expr,
 
 	right = constant_expr_alloc(&expr->location, tmpl->dtype,
 				    BYTEORDER_HOST_ENDIAN,
-				    tmpl->len, &protocol);
+				    tmpl->len,
+				    constant_data_ptr(protocol, tmpl->len));
 
 	dep = relational_expr_alloc(&expr->location, OP_EQ, left, right);
 	left->ops->pctx_update(&ctx->pctx, dep);
-- 
1.9.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] gmputil: use MSF/LSF in import/export functions dependant on host byte order
  2014-04-12  9:09 [PATCH 0/2] nftables: fix big endian bugs Patrick McHardy
  2014-04-12  9:09 ` [PATCH 1/2] expression: fix constant expression allocation on big endian Patrick McHardy
@ 2014-04-12  9:09 ` Patrick McHardy
  1 sibling, 0 replies; 3+ messages in thread
From: Patrick McHardy @ 2014-04-12  9:09 UTC (permalink / raw)
  To: pablo; +Cc: arturo.borrero.glez, netfilter-devel

For data of byteorder BYTEORDER_HOST_ENDIAN we need to set the word order
dependant on the host byte order.

Signed-off-by: Patrick McHardy <kaber@trash.net>
---
 include/gmputil.h | 9 +++++++++
 src/gmputil.c     | 7 +++++--
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/include/gmputil.h b/include/gmputil.h
index 88be697..63eb0ba 100644
--- a/include/gmputil.h
+++ b/include/gmputil.h
@@ -2,12 +2,21 @@
 #define NFTABLES_GMPUTIL_H
 
 #include <gmp.h>
+#include <asm/byteorder.h>
 
 enum mpz_word_order {
 	MPZ_MSWF		= 1,
 	MPZ_LSWF		= -1,
 };
 
+#ifdef __LITTLE_ENDIAN_BITFIELD
+#define MPZ_HWO	MPZ_LSWF
+#elif defined(__BIG_ENDIAN_BITFIELD)
+#define MPZ_HWO MPZ_MSWF
+#else
+#error "byteorder undefined"
+#endif
+
 enum mpz_byte_order {
 	MPZ_BIG_ENDIAN		= 1,
 	MPZ_HOST_ENDIAN		= 0,
diff --git a/src/gmputil.c b/src/gmputil.c
index f34c077..cb46445 100644
--- a/src/gmputil.c
+++ b/src/gmputil.c
@@ -98,20 +98,23 @@ void *mpz_export_data(void *data, const mpz_t op,
 		      enum byteorder byteorder,
 		      unsigned int len)
 {
+	enum mpz_word_order order;
 	enum mpz_byte_order endian;
 
 	switch (byteorder) {
 	case BYTEORDER_BIG_ENDIAN:
 	default:
+		order = MPZ_MSWF;
 		endian = MPZ_BIG_ENDIAN;
 		break;
 	case BYTEORDER_HOST_ENDIAN:
+		order = MPZ_HWO;
 		endian = MPZ_HOST_ENDIAN;
 		break;
 	}
 
 	memset(data, 0, len);
-	mpz_export(data, NULL, MPZ_MSWF, len, endian, 0, op);
+	mpz_export(data, NULL, order, len, endian, 0, op);
 	return data;
 }
 
@@ -129,7 +132,7 @@ void mpz_import_data(mpz_t rop, const void *data,
 		endian = MPZ_BIG_ENDIAN;
 		break;
 	case BYTEORDER_HOST_ENDIAN:
-		order  = MPZ_LSWF;
+		order  = MPZ_HWO;
 		endian = MPZ_HOST_ENDIAN;
 		break;
 	}
-- 
1.9.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-04-12  9:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-12  9:09 [PATCH 0/2] nftables: fix big endian bugs Patrick McHardy
2014-04-12  9:09 ` [PATCH 1/2] expression: fix constant expression allocation on big endian Patrick McHardy
2014-04-12  9:09 ` [PATCH 2/2] gmputil: use MSF/LSF in import/export functions dependant on host byte order Patrick McHardy

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.