]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
src: Don't parse string as verdict in map
authorXiao Liang <shaw.leon@gmail.com>
Fri, 19 Aug 2022 02:40:23 +0000 (10:40 +0800)
committerFlorian Westphal <fw@strlen.de>
Fri, 19 Aug 2022 06:10:06 +0000 (08:10 +0200)
In verdict map, string values are accidentally treated as verdicts.

For example:

table t {
    map foo {
        type ipv4_addr : verdict
        elements = {
            192.168.0.1 : bar
        }
    }
    chain output {
        type filter hook output priority mangle;
        ip daddr vmap @foo
    }
}

Though "bar" is not a valid verdict (should be "jump bar" or something),
the string is taken as the element value. Then NFTA_DATA_VALUE is sent
to the kernel instead of NFTA_DATA_VERDICT. This would be rejected by
recent kernels. On older ones (e.g. v5.4.x) that don't validate the
type, a warning can be seen when the rule is hit, because of the
corrupted verdict value:

[5120263.467627] WARNING: CPU: 12 PID: 303303 at net/netfilter/nf_tables_core.c:229 nft_do_chain+0x394/0x500 [nf_tables]

Indeed, we don't parse verdicts during evaluation, but only chain names,
which is of type string rather than verdict. For example, "jump $var" is
a verdict while "$var" is a string.

Fixes: c64457cff967 ("src: Allow goto and jump to a variable")
Signed-off-by: Xiao Liang <shaw.leon@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
src/datatype.c
src/evaluate.c
tests/shell/testcases/nft-f/0031vmap_string_0 [new file with mode: 0755]

index 2e31c8585bdc011fee9e826884a394b3352112a4..002ed46a99316341ac80520c43bf62da5f898b38 100644 (file)
@@ -321,23 +321,11 @@ static void verdict_type_print(const struct expr *expr, struct output_ctx *octx)
        }
 }
 
-static struct error_record *verdict_type_parse(struct parse_ctx *ctx,
-                                              const struct expr *sym,
-                                              struct expr **res)
-{
-       *res = constant_expr_alloc(&sym->location, &string_type,
-                                  BYTEORDER_HOST_ENDIAN,
-                                  (strlen(sym->identifier) + 1) * BITS_PER_BYTE,
-                                  sym->identifier);
-       return NULL;
-}
-
 const struct datatype verdict_type = {
        .type           = TYPE_VERDICT,
        .name           = "verdict",
        .desc           = "netfilter verdict",
        .print          = verdict_type_print,
-       .parse          = verdict_type_parse,
 };
 
 static const struct symbol_table nfproto_tbl = {
index 919c38c5604ee5b83aebc6fac118d230fb20c38d..d9c9ca28a53a9c782886c3e107f838b941dbee1c 100644 (file)
@@ -2575,7 +2575,8 @@ static int stmt_evaluate_verdict(struct eval_ctx *ctx, struct stmt *stmt)
                if (stmt->expr->verdict != NFT_CONTINUE)
                        stmt->flags |= STMT_F_TERMINAL;
                if (stmt->expr->chain != NULL) {
-                       if (expr_evaluate(ctx, &stmt->expr->chain) < 0)
+                       if (stmt_evaluate_arg(ctx, stmt, &string_type, 0, 0,
+                                             &stmt->expr->chain) < 0)
                                return -1;
                        if (stmt->expr->chain->etype != EXPR_VALUE) {
                                return expr_error(ctx->msgs, stmt->expr->chain,
diff --git a/tests/shell/testcases/nft-f/0031vmap_string_0 b/tests/shell/testcases/nft-f/0031vmap_string_0
new file mode 100755 (executable)
index 0000000..2af846a
--- /dev/null
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+# Tests parse of corrupted verdicts
+
+set -e
+
+RULESET="
+table ip foo {
+       map bar {
+               type ipv4_addr : verdict
+               elements = {
+                       192.168.0.1 : ber
+               }
+       }
+
+       chain ber {
+       }
+}"
+
+$NFT -f - <<< "$RULESET" && exit 1
+exit 0