nftables: fix big endian bugs
cover nftables: fix big endian bugs
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.
PATCH 1/2 expression: fix constant expression allocation on big endian
From: Arturo Borrero Gonzalez <[email protected]>
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.
--- include/utils.h | 14 ++++++++++++++ src/datatype.c | 2 +- src/payload.c | 3 ++- 3 files changed, 17 insertions(+), 2 deletions(-)
include/utils.h +14 -0
@@ -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))
src/datatype.c +1 -1
@@ -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 ; }
src/payload.c +2 -1
@@ -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);
Signature · 1 line
1.9.0
PATCH 2/2 gmputil: use MSF/LSF in import/export functions dependant on host byte order
In reply to Patrick McHardyFor data of byteorder BYTEORDER_HOST_ENDIAN we need to set the word order dependant on the host byte order.
--- include/gmputil.h | 9 +++++++++ src/gmputil.c | 7 +++++-- 2 files changed, 14 insertions(+), 2 deletions(-)
include/gmputil.h +9 -0
@@ -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 ,
src/gmputil.c +5 -2
@@ -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 ; }
Signature · 1 line
1.9.0