From a2c55e04d5a1187914cba2c02810db94de499ace Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Sat, 9 Dec 2017 16:52:29 +0100 Subject: src: fix protocol context update on big-endian systems There is an obscure bug on big-endian systems when trying to list a rule containing the expression 'ct helper tftp' which triggers the assert() call in mpz_get_type(). Florian identified the cause: ct_expr_pctx_update() is called for the relational expression which calls mpz_get_uint32() to get RHS value (assuming it is a protocol number). On big-endian systems, the misinterpreted value exceeds UINT_MAX. Expressions' pctx_update() callback should only be called for protocol matches, so ct_meta_common_postprocess() lacked a check for 'left->flags & EXPR_F_PROTOCOL' like the one already present in payload_expr_pctx_update(). In order to fix this in a clean way, this patch introduces a wrapper relational_expr_pctx_update() to be used instead of directly calling LHS's pctx_update() callback which unifies the necessary checks (and adds one more assert): - assert(expr->ops->type == EXPR_RELATIONAL) -> This is new, just to ensure the wrapper is called properly. - assert(expr->op == OP_EQ) -> This was moved from {ct,meta,payload}_expr_pctx_update(). - left->ops->pctx_update != NULL -> This was taken from expr_evaluate_relational(), a necessary requirement for the introduced wrapper to function at all. - (left->flags & EXPR_F_PROTOCOL) != 0 -> The crucial missing check which led to the problem. Suggested-by: Florian Westphal Signed-off-by: Phil Sutter Signed-off-by: Florian Westphal --- src/expression.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/expression.c') diff --git a/src/expression.c b/src/expression.c index 273038e6..393c1b2b 100644 --- a/src/expression.c +++ b/src/expression.c @@ -600,6 +600,19 @@ struct expr *relational_expr_alloc(const struct location *loc, enum ops op, return expr; } +void relational_expr_pctx_update(struct proto_ctx *ctx, + const struct expr *expr) +{ + const struct expr *left = expr->left; + + assert(expr->ops->type == EXPR_RELATIONAL); + assert(expr->op == OP_EQ); + + if (left->ops->pctx_update && + (left->flags & EXPR_F_PROTOCOL)) + left->ops->pctx_update(ctx, expr); +} + static void range_expr_print(const struct expr *expr, struct output_ctx *octx) { octx->numeric += NFT_NUMERIC_ALL + 1; -- cgit v1.2.3