From: Pablo Neira Ayuso <pablo@xxxxxxxxxxxxx> [ Upstream commit d0009effa8862c20a13af4cb7475d9771b905693 ] Several expressions explicitly refer to NF_INET_* hook definitions from expr->ops->validate, however, family is not validated. Bail out with EOPNOTSUPP in case they are used from unsupported families. Fixes: 0ca743a55991 ("netfilter: nf_tables: add compatibility layer for x_tables") Fixes: a3c90f7a2323 ("netfilter: nf_tables: flow offload expression") Fixes: 2fa841938c64 ("netfilter: nf_tables: introduce routing expression") Fixes: 554ced0a6e29 ("netfilter: nf_tables: add support for native socket matching") Fixes: ad49d86e07a4 ("netfilter: nf_tables: Add synproxy support") Fixes: 4ed8eb6570a4 ("netfilter: nf_tables: Add native tproxy support") Fixes: 6c47260250fc ("netfilter: nf_tables: add xfrm expression") Signed-off-by: Pablo Neira Ayuso <pablo@xxxxxxxxxxxxx> Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- net/netfilter/nft_compat.c | 12 ++++++++++++ net/netfilter/nft_flow_offload.c | 5 +++++ net/netfilter/nft_nat.c | 5 +++++ net/netfilter/nft_rt.c | 5 +++++ net/netfilter/nft_socket.c | 5 +++++ net/netfilter/nft_tproxy.c | 5 +++++ 6 files changed, 37 insertions(+) --- a/net/netfilter/nft_compat.c +++ b/net/netfilter/nft_compat.c @@ -319,6 +319,12 @@ static int nft_target_validate(const str unsigned int hook_mask = 0; int ret; + if (ctx->family != NFPROTO_IPV4 && + ctx->family != NFPROTO_IPV6 && + ctx->family != NFPROTO_BRIDGE && + ctx->family != NFPROTO_ARP) + return -EOPNOTSUPP; + if (nft_is_base_chain(ctx->chain)) { const struct nft_base_chain *basechain = nft_base_chain(ctx->chain); @@ -560,6 +566,12 @@ static int nft_match_validate(const stru unsigned int hook_mask = 0; int ret; + if (ctx->family != NFPROTO_IPV4 && + ctx->family != NFPROTO_IPV6 && + ctx->family != NFPROTO_BRIDGE && + ctx->family != NFPROTO_ARP) + return -EOPNOTSUPP; + if (nft_is_base_chain(ctx->chain)) { const struct nft_base_chain *basechain = nft_base_chain(ctx->chain); --- a/net/netfilter/nft_flow_offload.c +++ b/net/netfilter/nft_flow_offload.c @@ -145,6 +145,11 @@ static int nft_flow_offload_validate(con { unsigned int hook_mask = (1 << NF_INET_FORWARD); + if (ctx->family != NFPROTO_IPV4 && + ctx->family != NFPROTO_IPV6 && + ctx->family != NFPROTO_INET) + return -EOPNOTSUPP; + return nft_chain_validate_hooks(ctx->chain, hook_mask); } --- a/net/netfilter/nft_nat.c +++ b/net/netfilter/nft_nat.c @@ -94,6 +94,11 @@ static int nft_nat_validate(const struct struct nft_nat *priv = nft_expr_priv(expr); int err; + if (ctx->family != NFPROTO_IPV4 && + ctx->family != NFPROTO_IPV6 && + ctx->family != NFPROTO_INET) + return -EOPNOTSUPP; + err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT); if (err < 0) return err; --- a/net/netfilter/nft_rt.c +++ b/net/netfilter/nft_rt.c @@ -159,6 +159,11 @@ static int nft_rt_validate(const struct const struct nft_rt *priv = nft_expr_priv(expr); unsigned int hooks; + if (ctx->family != NFPROTO_IPV4 && + ctx->family != NFPROTO_IPV6 && + ctx->family != NFPROTO_INET) + return -EOPNOTSUPP; + switch (priv->key) { case NFT_RT_NEXTHOP4: case NFT_RT_NEXTHOP6: --- a/net/netfilter/nft_socket.c +++ b/net/netfilter/nft_socket.c @@ -139,6 +139,11 @@ static int nft_socket_validate(const str const struct nft_expr *expr, const struct nft_data **data) { + if (ctx->family != NFPROTO_IPV4 && + ctx->family != NFPROTO_IPV6 && + ctx->family != NFPROTO_INET) + return -EOPNOTSUPP; + return nft_chain_validate_hooks(ctx->chain, (1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_IN) | --- a/net/netfilter/nft_tproxy.c +++ b/net/netfilter/nft_tproxy.c @@ -293,6 +293,11 @@ static int nft_tproxy_validate(const str const struct nft_expr *expr, const struct nft_data **data) { + if (ctx->family != NFPROTO_IPV4 && + ctx->family != NFPROTO_IPV6 && + ctx->family != NFPROTO_INET) + return -EOPNOTSUPP; + return nft_chain_validate_hooks(ctx->chain, 1 << NF_INET_PRE_ROUTING); } Patches currently in stable-queue which might be from kroah.com@xxxxxxxxxxxxxxx are queue-4.19/netfilter-nf_tables-bogus-ebusy-when-deleting-flowtable-after-flush-for-4.19.patch queue-4.19/netfilter-nft_set_rbtree-switch-to-node-list-walk-for-overlap-detection.patch queue-4.19/netfilter-nf_tables-validate-nfproto_-family.patch queue-4.19/netfilter-nf_tables-unregister-flowtable-hooks-on-netns-exit.patch queue-4.19/netfilter-nf_tables-fix-gc-transaction-races-with-netns-and-netlink-event-exit-path.patch queue-4.19/netfilter-nft_set_rbtree-use-read-spinlock-to-avoid-datapath-contention.patch queue-4.19/netfilter-nft_dynset-report-eopnotsupp-on-missing-set-feature.patch queue-4.19/netfilter-nf_tables-discard-table-flag-update-with-pending-basechain-deletion.patch queue-4.19/netfilter-nft_set_rbtree-skip-sync-gc-for-new-elements-in-this-transaction.patch queue-4.19/netfilter-nf_tables-mark-set-as-dead-when-unbinding-anonymous-set-with-timeout.patch queue-4.19/netfilter-nf_tables-reject-new-basechain-after-table-flag-update.patch queue-4.19/netfilter-nf_tables-gc-transaction-race-with-netns-dismantle.patch queue-4.19/netfilter-nftables-update-table-flags-from-the-commit-phase.patch queue-4.19/netfilter-nf_tables-allow-nfproto_inet-in-nft_-match-target-_validate.patch queue-4.19/netfilter-nft_set_rbtree-skip-end-interval-element-from-gc.patch queue-4.19/netfilter-nf_tables-drop-map-element-references-from-preparation-phase.patch queue-4.19/netfilter-nf_tables-gc-transaction-race-with-abort-path.patch queue-4.19/netfilter-nf_tables-fix-memleak-when-more-than-255-elements-expired.patch queue-4.19/netfilter-nft_set_rbtree-add-missing-expired-checks.patch queue-4.19/netfilter-nf_tables-double-hook-unregistration-in-netns-path.patch queue-4.19/netfilter-nft_dynset-relax-superfluous-check-on-set-updates.patch queue-4.19/netfilter-nf_tables-defer-gc-run-if-previous-batch-is-still-pending.patch queue-4.19/netfilter-nft_set_hash-try-later-when-gc-hits-eagain-on-iteration.patch queue-4.19/netfilter-nft_dynset-fix-timeouts-later-than-23-days.patch queue-4.19/netfilter-nf_tables-mark-newset-as-dead-on-transaction-abort.patch queue-4.19/netfilter-nf_tables-don-t-skip-expired-elements-during-walk.patch queue-4.19/netfilter-nftables-rename-set-element-data-activation-deactivation-functions.patch queue-4.19/netfilter-nf_tables-gc-transaction-api-to-avoid-race-with-control-plane.patch queue-4.19/netfilter-nf_tables-set-dormant-flag-on-hook-register-failure.patch queue-4.19/netfilter-nf_tables-remove-busy-mark-and-gc-batch-api.patch queue-4.19/netfilter-nf_tables-skip-dead-set-elements-in-netlink-dump.patch queue-4.19/netfilter-nf_tables-adapt-set-backend-to-use-gc-transaction-api.patch queue-4.19/netfilter-nft_set_rbtree-fix-overlap-expiration-walk.patch queue-4.19/netfilter-nf_tables-fix-table-flag-updates.patch queue-4.19/netfilter-nf_tables-do-not-compare-internal-table-flags-on-updates.patch queue-4.19/netfilter-nft_set_rbtree-allow-loose-matching-of-closing-element-in-interval.patch queue-4.19/netfilter-nftables-exthdr-fix-4-byte-stack-oob-write.patch queue-4.19/netfilter-nft_set_rbtree-fix-null-deref-on-element-insertion.patch queue-4.19/netfilter-nf_tables-pass-context-to-nft_set_destroy.patch queue-4.19/netfilter-nf_tables-disable-toggling-dormant-table-state-more-than-once.patch