]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
src: do not allow to chain more than 16 binops
authorFlorian Westphal <fw@strlen.de>
Thu, 21 Dec 2023 10:25:14 +0000 (11:25 +0100)
committerFlorian Westphal <fw@strlen.de>
Fri, 22 Dec 2023 10:57:46 +0000 (11:57 +0100)
netlink_linearize.c has never supported more than 16 chained binops.
Adding more is possible but overwrites the stack in
netlink_gen_bitwise().

Add a recursion counter to catch this at eval stage.

Its not enough to just abort once the counter hits
NFT_MAX_EXPR_RECURSION.

This is because there are valid test cases that exceed this.
For example, evaluation of 1 | 2 will merge the constans, so even
if there are a dozen recursive eval calls this will not end up
with large binop chain post-evaluation.

v2: allow more than 16 binops iff the evaluation function
    did constant-merging.

Signed-off-by: Florian Westphal <fw@strlen.de>
include/expression.h
include/rule.h
src/evaluate.c
src/netlink_linearize.c
tests/shell/testcases/bogons/nft-f/huge_binop_expr_chain_crash [new file with mode: 0644]

index 809089c899741d5aee3058028151646ad2d66355..f5d7e160a9aad43db1aea44dd5719fc18531916f 100644 (file)
@@ -13,6 +13,7 @@
 
 #define NFT_MAX_EXPR_LEN_BYTES (NFT_REG32_COUNT * sizeof(uint32_t))
 #define NFT_MAX_EXPR_LEN_BITS  (NFT_MAX_EXPR_LEN_BYTES * BITS_PER_BYTE)
+#define NFT_MAX_EXPR_RECURSION 16
 
 /**
  * enum expr_types
index 6236d2927c0a938005dd853ee1a24efc79511db9..6835fe0691653a7820b64b3c88a7ff313d9b0f57 100644 (file)
@@ -753,10 +753,13 @@ extern void cmd_free(struct cmd *cmd);
  * @rule:      current rule
  * @set:       current set
  * @stmt:      current statement
+ * @stmt_len:  current statement template length
+ * @recursion:  expr evaluation recursion counter
  * @cache:     cache context
  * @debug_mask: debugging bitmask
  * @ectx:      expression context
- * @pctx:      payload context
+ * @_pctx:     payload contexts
+ * @inner_desc: inner header description
  */
 struct eval_ctx {
        struct nft_ctx          *nft;
@@ -767,6 +770,7 @@ struct eval_ctx {
        struct set              *set;
        struct stmt             *stmt;
        uint32_t                stmt_len;
+       uint32_t                recursion;
        struct expr_ctx         ectx;
        struct proto_ctx        _pctx[2];
        const struct proto_desc *inner_desc;
index 26f0110f66ea5dbc4ce11c544248f2032a1688cc..41524eef12b7cffd0028c1e3e8a6a232a3194a88 100644 (file)
@@ -1418,6 +1418,13 @@ static int expr_evaluate_binop(struct eval_ctx *ctx, struct expr **expr)
        struct expr *op = *expr, *left, *right;
        const char *sym = expr_op_symbols[op->op];
        unsigned int max_shift_len = ctx->ectx.len;
+       int ret = -1;
+
+       if (ctx->recursion >= USHRT_MAX)
+               return expr_binary_error(ctx->msgs, op, NULL,
+                                        "Binary operation limit %u reached ",
+                                        ctx->recursion);
+       ctx->recursion++;
 
        if (expr_evaluate(ctx, &op->left) < 0)
                return -1;
@@ -1472,14 +1479,42 @@ static int expr_evaluate_binop(struct eval_ctx *ctx, struct expr **expr)
        switch (op->op) {
        case OP_LSHIFT:
        case OP_RSHIFT:
-               return expr_evaluate_shift(ctx, expr);
+               ret = expr_evaluate_shift(ctx, expr);
+               break;
        case OP_AND:
        case OP_XOR:
        case OP_OR:
-               return expr_evaluate_bitwise(ctx, expr);
+               ret = expr_evaluate_bitwise(ctx, expr);
+               break;
        default:
                BUG("invalid binary operation %u\n", op->op);
        }
+
+
+       if (ctx->recursion == 0)
+               BUG("recursion counter underflow");
+
+       /* can't check earlier: evaluate functions might do constant-merging + expr_free.
+        *
+        * So once we've evaluate everything check for remaining length of the
+        * binop chain.
+        */
+       if (--ctx->recursion == 0) {
+               unsigned int to_linearize = 0;
+
+               op = *expr;
+               while (op && op->etype == EXPR_BINOP && op->left != NULL) {
+                       to_linearize++;
+                       op = op->left;
+
+                       if (to_linearize >= NFT_MAX_EXPR_RECURSION)
+                               return expr_binary_error(ctx->msgs, op, NULL,
+                                                        "Binary operation limit %u reached ",
+                                                        NFT_MAX_EXPR_RECURSION);
+               }
+       }
+
+       return ret;
 }
 
 static int list_member_evaluate(struct eval_ctx *ctx, struct expr **expr)
index d8b41a0889482c2eb53935604ff78f7d71e3f32a..50dbd36c1b8ea1ed6c895f6028e77c43971bc933 100644 (file)
@@ -695,10 +695,10 @@ static void netlink_gen_bitwise(struct netlink_linearize_ctx *ctx,
                                const struct expr *expr,
                                enum nft_registers dreg)
 {
+       struct expr *binops[NFT_MAX_EXPR_RECURSION];
        struct nftnl_expr *nle;
        struct nft_data_linearize nld;
        struct expr *left, *i;
-       struct expr *binops[16];
        mpz_t mask, xor, val, tmp;
        unsigned int len;
        int n = 0;
@@ -710,8 +710,11 @@ static void netlink_gen_bitwise(struct netlink_linearize_ctx *ctx,
 
        binops[n++] = left = (struct expr *) expr;
        while (left->etype == EXPR_BINOP && left->left != NULL &&
-              (left->op == OP_AND || left->op == OP_OR || left->op == OP_XOR))
+              (left->op == OP_AND || left->op == OP_OR || left->op == OP_XOR)) {
+               if (n == array_size(binops))
+                       BUG("NFT_MAX_EXPR_RECURSION limit reached");
                binops[n++] = left = left->left;
+       }
 
        netlink_gen_expr(ctx, binops[--n], dreg);
 
diff --git a/tests/shell/testcases/bogons/nft-f/huge_binop_expr_chain_crash b/tests/shell/testcases/bogons/nft-f/huge_binop_expr_chain_crash
new file mode 100644 (file)
index 0000000..8d1da72
--- /dev/null
@@ -0,0 +1,5 @@
+table t {
+       chain c {
+               meta oifname^a^b^c^d^e^f^g^h^i^j^k^l^m^n^o^p^q^r^s^t^u^v^w^x^y^z^A^B^C^D^E^F^G^H^I^J^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^0^1^2^3^4^5^6^7^8^9 bar
+       }
+}