Skip to contentSkip to conversation

netfilter: nf_tables: validate NFPROTO_{IPV4,IPV6,INET} family

PATCH Superseded Pablo Neira Ayuso 2 participants 2 messages Jan 23, 2024

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 netfilter: nf_tables: validate NFPROTO_{IPV4,IPV6,INET} family

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.

---
This is v1:

NF_INET_* and NF_NETDEV_* hook definitions overlap, and .validate refers to hooks only in several expressions, not families.

- synproxy refers to NFPROTO_BRIDGE from eval path, however, .validate does not refer to bridge hooks.
- rt and socket might be already used from the ingress hooks, another possibility would be to update .validate to refer to these hooks explicitly.
- nft_compat's xt_target_check() and xt_match_check() calls already validate the family, but better reject this unfront.

compile tested only.

 net/netfilter/nft_compat.c       | 10 ++++++++++
 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_synproxy.c     |  5 +++++
 net/netfilter/nft_tproxy.c       |  5 +++++
 7 files changed, 40 insertions(+)
net/netfilter/nft_compat.c +10 -0
index 5284cd2ad532..61f5498b73ef 100644
@@ -350,6 +350,11 @@ static int nft_target_validate(const struct nft_ctx *ctx,
 	unsigned int hook_mask = 0;
 	int ret;
 
+	if (ctx->family != NFPROTO_IPV4 &&
+	    ctx->family != NFPROTO_IPV6 &&
+	    ctx->family != NFPROTO_BRIDGE)
+		return -EOPNOTSUPP;
+
 	if (nft_is_base_chain(ctx->chain)) {
 		const struct nft_base_chain *basechain =
 						nft_base_chain(ctx->chain);
@@ -595,6 +600,11 @@ static int nft_match_validate(const struct nft_ctx *ctx,
 	unsigned int hook_mask = 0;
 	int ret;
 
+	if (ctx->family != NFPROTO_IPV4 &&
+	    ctx->family != NFPROTO_IPV6 &&
+	    ctx->family != NFPROTO_BRIDGE)
+		return -EOPNOTSUPP;
+
 	if (nft_is_base_chain(ctx->chain)) {
 		const struct nft_base_chain *basechain =
 						nft_base_chain(ctx->chain);
net/netfilter/nft_flow_offload.c +5 -0
index ab3362c483b4..397351fa4d5f 100644
@@ -384,6 +384,11 @@ static int nft_flow_offload_validate(const struct nft_ctx *ctx,
 {
 	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);
 }
 
net/netfilter/nft_nat.c +5 -0
index 583885ce7232..808f5802c270 100644
@@ -143,6 +143,11 @@ static int nft_nat_validate(const struct nft_ctx *ctx,
 	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;
net/netfilter/nft_rt.c +5 -0
index 35a2c28caa60..24d977138572 100644
@@ -166,6 +166,11 @@ static int nft_rt_validate(const struct nft_ctx *ctx, const struct nft_expr *exp
 	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:
net/netfilter/nft_socket.c +5 -0
index 9ed85be79452..f30163e2ca62 100644
@@ -242,6 +242,11 @@ static int nft_socket_validate(const struct nft_ctx *ctx,
 			       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) |
net/netfilter/nft_synproxy.c +5 -0
index 13da882669a4..ebdb81eaafb0 100644
@@ -253,6 +253,11 @@ static int nft_synproxy_validate(const struct nft_ctx *ctx,
 				 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_LOCAL_IN) |
 						    (1 << NF_INET_FORWARD));
 }
net/netfilter/nft_tproxy.c +5 -0
index ae15cd693f0e..71412adb73d4 100644
@@ -316,6 +316,11 @@ static int nft_tproxy_validate(const struct nft_ctx *ctx,
 			       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);
 }
 
Signature · 1 line
2.30.2
maintainer
#1 lore raw
Quoted 7 lines · Pablo Neira Ayuso

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.

Quoted 8 lines

---
This is v1:

NF_INET_* and NF_NETDEV_* hook definitions overlap, and .validate refers to hooks only in several expressions, not families.

- synproxy refers to NFPROTO_BRIDGE from eval path, however, .validate does not refer to bridge hooks.

Probably those NFPROTO_BRIDGE should be removed then.

Quoted 17 lines
net/netfilter/nft_compat.c +5 -0
index 5284cd2ad532..61f5498b73ef 100644
@@ -350,6 +350,11 @@ static int nft_target_validate(const struct nft_ctx *ctx,
 	unsigned int hook_mask = 0;
 	int ret;
 
+	if (ctx->family != NFPROTO_IPV4 &&
+	    ctx->family != NFPROTO_IPV6 &&
+	    ctx->family != NFPROTO_BRIDGE)
+		return -EOPNOTSUPP;
+
 	if (nft_is_base_chain(ctx->chain)) {
 		const struct nft_base_chain *basechain =
 						nft_base_chain(ctx->chain);

net/netfilter/nft_compat.c static int nft_match_validate(const struct nft_ctx *ctx, · line 607

Quoted 8 lines
net/netfilter/nft_compat.c +5 -0
@@ -595,6 +600,11 @@ static int nft_match_validate(const struct nft_ctx *ctx,
 	unsigned int hook_mask = 0;
 	int ret;
 
+	if (ctx->family != NFPROTO_IPV4 &&
+	    ctx->family != NFPROTO_IPV6 &&
+	    ctx->family != NFPROTO_BRIDGE)
+		return -EOPNOTSUPP;
+

Both need to permit NFPROTO_ARP too.

LGTM otherwise.