]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
netfilter: nft_fib: reject fib expression on the netdev egress hook
authorTheodor Arsenij Larionov-Trichkine <theodorlarionov@gmail.com>
Mon, 29 Jun 2026 10:53:11 +0000 (12:53 +0200)
committerFlorian Westphal <fw@strlen.de>
Tue, 30 Jun 2026 04:37:12 +0000 (06:37 +0200)
A fib expression in a netdev egress base chain dereferences nft_in(pkt),
NULL on the transmit path, causing a NULL pointer dereference at eval.
nft_fib_validate() masks the hook with NF_INET_* values, but netdev hook
numbers are a separate enum that aliases them (NF_NETDEV_EGRESS ==
NF_INET_LOCAL_IN), so an egress chain passes validation and then faults.

Add nft_fib_netdev_validate() that limits each result/flag to the netdev
hook where the device it reads exists: the input-device cases (OIF,
OIFNAME, ADDRTYPE with F_IIF) to ingress, the output-device case (ADDRTYPE
with F_OIF) to egress, ADDRTYPE with no device flag to both. Also restrict
nft_fib_validate() to NFPROTO_IPV4/IPV6/INET so its NF_INET_* masks are
not applied to another family's hooks.

Fixes: 42df6e1d221d ("netfilter: Introduce egress hook")
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/netfilter-devel/ajxsjcDOnwllMfoR@strlen.de/
Signed-off-by: Theodor Arsenij Larionov-Trichkine <theodorlarionov@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
net/netfilter/nft_fib.c
net/netfilter/nft_fib_netdev.c

index e048f05694cdddc844aabe659bfb60da5486a265..89555380f1c5a04fb2f82e64ba4587bd4a4bad57 100644 (file)
@@ -31,6 +31,15 @@ int nft_fib_validate(const struct nft_ctx *ctx, const struct nft_expr *expr)
        const struct nft_fib *priv = nft_expr_priv(expr);
        unsigned int hooks;
 
+       switch (ctx->family) {
+       case NFPROTO_IPV4:
+       case NFPROTO_IPV6:
+       case NFPROTO_INET:
+               break;
+       default:
+               return -EOPNOTSUPP;
+       }
+
        switch (priv->result) {
        case NFT_FIB_RESULT_OIF:
        case NFT_FIB_RESULT_OIFNAME:
index 3f3478abd8454606b16f1674abb3bb83f35704b7..5774a75440278027925a3736f698324a4a56a034 100644 (file)
@@ -50,6 +50,33 @@ static void nft_fib_netdev_eval(const struct nft_expr *expr,
        regs->verdict.code = NFT_BREAK;
 }
 
+static int nft_fib_netdev_validate(const struct nft_ctx *ctx,
+                                  const struct nft_expr *expr)
+{
+       const struct nft_fib *priv = nft_expr_priv(expr);
+       unsigned int hooks;
+
+       switch (priv->result) {
+       case NFT_FIB_RESULT_OIF:
+       case NFT_FIB_RESULT_OIFNAME:
+               hooks = (1 << NF_NETDEV_INGRESS);
+               break;
+       case NFT_FIB_RESULT_ADDRTYPE:
+               if (priv->flags & NFTA_FIB_F_IIF)
+                       hooks = (1 << NF_NETDEV_INGRESS);
+               else if (priv->flags & NFTA_FIB_F_OIF)
+                       hooks = (1 << NF_NETDEV_EGRESS);
+               else
+                       hooks = (1 << NF_NETDEV_INGRESS) |
+                               (1 << NF_NETDEV_EGRESS);
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       return nft_chain_validate_hooks(ctx->chain, hooks);
+}
+
 static struct nft_expr_type nft_fib_netdev_type;
 static const struct nft_expr_ops nft_fib_netdev_ops = {
        .type           = &nft_fib_netdev_type,
@@ -57,7 +84,7 @@ static const struct nft_expr_ops nft_fib_netdev_ops = {
        .eval           = nft_fib_netdev_eval,
        .init           = nft_fib_init,
        .dump           = nft_fib_dump,
-       .validate       = nft_fib_validate,
+       .validate       = nft_fib_netdev_validate,
 };
 
 static struct nft_expr_type nft_fib_netdev_type __read_mostly = {