netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick McHardy <kaber@trash.net>
To: pablo@netfilter.org
Cc: arturo.borrero.glez@gmail.com, netfilter-devel@vger.kernel.org
Subject: [PATCH 1/2] expression: fix constant expression allocation on big endian
Date: Sat, 12 Apr 2014 11:09:58 +0200	[thread overview]
Message-ID: <1397293799-2600-2-git-send-email-kaber@trash.net> (raw)
In-Reply-To: <1397293799-2600-1-git-send-email-kaber@trash.net>

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


  reply	other threads:[~2014-04-12  9:10 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-12  9:09 [PATCH 0/2] nftables: fix big endian bugs Patrick McHardy
2014-04-12  9:09 ` Patrick McHardy [this message]
2014-04-12  9:09 ` [PATCH 2/2] gmputil: use MSF/LSF in import/export functions dependant on host byte order Patrick McHardy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1397293799-2600-2-git-send-email-kaber@trash.net \
    --to=kaber@trash.net \
    --cc=arturo.borrero.glez@gmail.com \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).