Skip to contentSkip to conversation

netfilter: x_tables: reject missing '-p' flag and check for INV flag

PATCH In review Florian Westphal 2 participants 8 messages Aug 21 – Aug 24

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 netfilter: x_tables: reject missing '-p' flag and check for INV flag

ip6tables traverser doesn't walk ipv6 extension headers unless the PROTO flag is set, so direct sockopt-users can bypass the check (ip6tables always sets it if '-p' flag is given. This is a cleanup patch, it doesn't fix any real bug.

Fixes 74f77a6b2b1c ("netfilter: introduce l2tp match extension")
Fixes af0d29cd2a73 ("netfilter: xtables: add an IPv6 capable version of the ECN match")
Fixes a89ecb6a2ef7 ("[NETFILTER]: x_tables: unify IPv4/IPv6 multiport match")

---
Can also be applied to nf, but I don't think this one is urgent.

 net/netfilter/xt_ecn.c       |  3 ++-
 net/netfilter/xt_l2tp.c      | 11 +++++++----
 net/netfilter/xt_multiport.c |  3 +++
 3 files changed, 12 insertions(+), 5 deletions(-)
net/netfilter/xt_ecn.c +2 -1
index a8503f5d26bf..008e7fcbeb22 100644
@@ -139,7 +139,8 @@ static int ecn_mt_check6(const struct xt_mtchk_param *par)
 		return -EINVAL;
 
 	if (info->operation & (XT_ECN_OP_MATCH_ECE | XT_ECN_OP_MATCH_CWR) &&
-	    (ip->proto != IPPROTO_TCP || ip->invflags & IP6T_INV_PROTO)) {
+	    (!(ip->flags & IP6T_F_PROTO) ||
+	     ip->proto != IPPROTO_TCP || ip->invflags & IP6T_INV_PROTO)) {
 		pr_info_ratelimited("cannot match TCP bits for non-tcp packets\n");
 		return -EINVAL;
 	}
net/netfilter/xt_l2tp.c +7 -4
index a61eb81e9f49..ad2d559caa99 100644
@@ -267,8 +267,9 @@ static int l2tp_mt_check4(const struct xt_mtchk_param *par)
 	if (ret != 0)
 		return ret;
 
-	if ((ip->proto != IPPROTO_UDP) &&
-	    (ip->proto != IPPROTO_L2TP)) {
+	if (ip->invflags & IPT_INV_PROTO ||
+	    (ip->proto != IPPROTO_UDP &&
+	    ip->proto != IPPROTO_L2TP)) {
 		pr_info_ratelimited("missing protocol rule (udp|l2tpip)\n");
 		return -EINVAL;
 	}
@@ -294,8 +295,10 @@ static int l2tp_mt_check6(const struct xt_mtchk_param *par)
 	if (ret != 0)
 		return ret;
 
-	if ((ip->proto != IPPROTO_UDP) &&
-	    (ip->proto != IPPROTO_L2TP)) {
+	if (((ip->flags & IP6T_F_PROTO) == 0 ||
+	     ip->invflags & IP6T_INV_PROTO ||
+	   (ip->proto != IPPROTO_UDP &&
+	    ip->proto != IPPROTO_L2TP))) {
 		pr_info_ratelimited("missing protocol rule (udp|l2tpip)\n");
 		return -EINVAL;
 	}
net/netfilter/xt_multiport.c +3 -0
index a1691ff405d3..48aa25749063 100644
@@ -160,6 +160,9 @@ static int multiport_mt6_check(const struct xt_mtchk_param *par)
 	const struct ip6t_ip6 *ip = par->entryinfo;
 	const struct xt_multiport_v1 *multiinfo = par->matchinfo;
 
+	if (!(ip->flags & IP6T_F_PROTO))
+		return -EINVAL;
+
 	if (!check(ip->proto, ip->invflags, multiinfo->flags, multiinfo->count))
 		return -EINVAL;
 
Signature · 1 line
2.54.0
#1 lore raw

Hi,

Quoted 4 lines · Florian Westphal

ip6tables traverser doesn't walk ipv6 extension headers unless the PROTO flag is set, so direct sockopt-users can bypass the check (ip6tables always sets it if '-p' flag is given. This is a cleanup patch, it doesn't fix any real bug.

I really think it is better to squash the TPROXY fix into this one too, it is the same logical fix.

Sashiko also hinting to fix ip6_tables core check for this protocol flag:

https://sashiko.dev/#/patchset/20260821134136.18191-1-fw%40strlen.de

Thanks!

Quoted 74 lines
Fixes 74f77a6b2b1c ("netfilter: introduce l2tp match extension")
Fixes af0d29cd2a73 ("netfilter: xtables: add an IPv6 capable version of the ECN match")
Fixes a89ecb6a2ef7 ("[NETFILTER]: x_tables: unify IPv4/IPv6 multiport match")

---
Can also be applied to nf, but I don't think this one is urgent.

 net/netfilter/xt_ecn.c       |  3 ++-
 net/netfilter/xt_l2tp.c      | 11 +++++++----
 net/netfilter/xt_multiport.c |  3 +++
 3 files changed, 12 insertions(+), 5 deletions(-)
net/netfilter/xt_ecn.c +2 -1
index a8503f5d26bf..008e7fcbeb22 100644
@@ -139,7 +139,8 @@ static int ecn_mt_check6(const struct xt_mtchk_param *par)
 		return -EINVAL;
 
 	if (info->operation & (XT_ECN_OP_MATCH_ECE | XT_ECN_OP_MATCH_CWR) &&
-	    (ip->proto != IPPROTO_TCP || ip->invflags & IP6T_INV_PROTO)) {
+	    (!(ip->flags & IP6T_F_PROTO) ||
+	     ip->proto != IPPROTO_TCP || ip->invflags & IP6T_INV_PROTO)) {
 		pr_info_ratelimited("cannot match TCP bits for non-tcp packets\n");
 		return -EINVAL;
 	}
net/netfilter/xt_l2tp.c +7 -4
index a61eb81e9f49..ad2d559caa99 100644
@@ -267,8 +267,9 @@ static int l2tp_mt_check4(const struct xt_mtchk_param *par)
 	if (ret != 0)
 		return ret;
 
-	if ((ip->proto != IPPROTO_UDP) &&
-	    (ip->proto != IPPROTO_L2TP)) {
+	if (ip->invflags & IPT_INV_PROTO ||
+	    (ip->proto != IPPROTO_UDP &&
+	    ip->proto != IPPROTO_L2TP)) {
 		pr_info_ratelimited("missing protocol rule (udp|l2tpip)\n");
 		return -EINVAL;
 	}
@@ -294,8 +295,10 @@ static int l2tp_mt_check6(const struct xt_mtchk_param *par)
 	if (ret != 0)
 		return ret;
 
-	if ((ip->proto != IPPROTO_UDP) &&
-	    (ip->proto != IPPROTO_L2TP)) {
+	if (((ip->flags & IP6T_F_PROTO) == 0 ||
+	     ip->invflags & IP6T_INV_PROTO ||
+	   (ip->proto != IPPROTO_UDP &&
+	    ip->proto != IPPROTO_L2TP))) {
 		pr_info_ratelimited("missing protocol rule (udp|l2tpip)\n");
 		return -EINVAL;
 	}
net/netfilter/xt_multiport.c +3 -0
index a1691ff405d3..48aa25749063 100644
@@ -160,6 +160,9 @@ static int multiport_mt6_check(const struct xt_mtchk_param *par)
 	const struct ip6t_ip6 *ip = par->entryinfo;
 	const struct xt_multiport_v1 *multiinfo = par->matchinfo;
 
+	if (!(ip->flags & IP6T_F_PROTO))
+		return -EINVAL;
+
 	if (!check(ip->proto, ip->invflags, multiinfo->flags, multiinfo->count))
 		return -EINVAL;
 
Signature · 1 line
2.54.0
author maintainer
#2 lore raw
Quoted 8 lines · Pablo Neira Ayuso
Quoted 4 lines · Florian Westphal

ip6tables traverser doesn't walk ipv6 extension headers unless the PROTO flag is set, so direct sockopt-users can bypass the check (ip6tables always sets it if '-p' flag is given. This is a cleanup patch, it doesn't fix any real bug.

I really think it is better to squash the TPROXY fix into this one too, it is the same logical fix.

I don't mind either way. I did not do it because unlike TPROXY these are benign.

Quoted 4 lines

Sashiko also hinting to fix ip6_tables core check for this protocol flag:

https://sashiko.dev/#/patchset/20260821134136.18191-1-fw%40strlen.de

Not so sure. Maybe just do this? I don't think proto > 0 and flag unset makes any sense.

net/ipv6/netfilter/ip6_tables.c +7 -0
@@ -647,6 +647,13 @@ check_entry_size_and_hooks(struct ip6t_entry *e,
 	/* Clear counters and comefrom */
 	e->counters = ((struct xt_counters) { 0, 0 });
 	e->comefrom = 0;
+
+	/* Make sure F_PROTO is set, else ip6_packet_match
+	 * won't do the right thing.
+	 */
+	if (e->ipv6.proto)
+		e->ipv6.flags |= IP6T_F_PROTO;
+
 	return 0;
 }
 
#3 lore raw
Quoted 25 lines · Florian Westphal
Quoted 8 lines · Pablo Neira Ayuso
Quoted 4 lines · Florian Westphal

ip6tables traverser doesn't walk ipv6 extension headers unless the PROTO flag is set, so direct sockopt-users can bypass the check (ip6tables always sets it if '-p' flag is given. This is a cleanup patch, it doesn't fix any real bug.

I really think it is better to squash the TPROXY fix into this one too, it is the same logical fix.

I don't mind either way. I did not do it because unlike TPROXY these are benign.

Quoted 4 lines

Sashiko also hinting to fix ip6_tables core check for this protocol flag:

https://sashiko.dev/#/patchset/20260821134136.18191-1-fw%40strlen.de

Not so sure. Maybe just do this? I don't think proto > 0 and flag unset makes any sense.

net/ipv6/netfilter/ip6_tables.c check_entry_size_and_hooks(struct ip6t_entry *e, · line 655

Quoted 9 lines
net/ipv6/netfilter/ip6_tables.c +6 -0
@@ -647,6 +647,13 @@ check_entry_size_and_hooks(struct ip6t_entry *e,
 	/* Clear counters and comefrom */
 	e->counters = ((struct xt_counters) { 0, 0 });
 	e->comefrom = 0;
+
+	/* Make sure F_PROTO is set, else ip6_packet_match
+	 * won't do the right thing.
+	 */
+	if (e->ipv6.proto)
+		e->ipv6.flags |= IP6T_F_PROTO;

Userspace always provides a zeroed e->ipv6.proto when unused, correct?

If so, LGTM.

Quoted 4 lines
net/ipv6/netfilter/ip6_tables.c +1 -0
+
 	return 0;
 }
 
author maintainer
#4 lore raw
Quoted 8 lines · Pablo Neira Ayuso
Quoted 8 lines
Quoted 4 lines

Sashiko also hinting to fix ip6_tables core check for this protocol flag:

https://sashiko.dev/#/patchset/20260821134136.18191-1-fw%40strlen.de

Not so sure. Maybe just do this? I don't think proto > 0 and flag unset makes any sense.

Quoted 15 lines

net/ipv6/netfilter/ip6_tables.c check_entry_size_and_hooks(struct ip6t_entry *e, · line 655

Quoted 13 lines
net/ipv6/netfilter/ip6_tables.c +6 -0
@@ -647,6 +647,13 @@ check_entry_size_and_hooks(struct ip6t_entry *e,
 	/* Clear counters and comefrom */
 	e->counters = ((struct xt_counters) { 0, 0 });
 	e->comefrom = 0;
+
+	/* Make sure F_PROTO is set, else ip6_packet_match
+	 * won't do the right thing.
+	 */
+	if (e->ipv6.proto)
+		e->ipv6.flags |= IP6T_F_PROTO;

Userspace always provides a zeroed e->ipv6.proto when unused, correct?

Yes, ip6tables raises it when it parses '-p' option.

Quoted 1 line

If so, LGTM.

Alright, I will respin tomorrow, thanks.

Do you want me to include the TPROXY fix too?

author maintainer
#5 lore raw
Quoted 9 lines · Florian Westphal
Quoted 1 line

Userspace always provides a zeroed e->ipv6.proto when unused, correct?

Yes, ip6tables raises it when it parses '-p' option.

Quoted 1 line

If so, LGTM.

Alright, I will respin tomorrow, thanks.

Do you want me to include the TPROXY fix too?

Actually nft_compat.c already does:

if (proto)
entry->e6.ipv6.flags |= IP6T_F_PROTO;

so the only other necessary fix is the check for 'INV' in xt_l2tp.c.

#6 lore raw
Quoted 7 lines · Florian Westphal
Quoted 6 lines · Florian Westphal
Quoted 1 line

Userspace always provides a zeroed e->ipv6.proto when unused, correct?

Yes, ip6tables raises it when it parses '-p' option.

Quoted 1 line

If so, LGTM.

Quoted 8 lines
Quoted 3 lines

Alright, I will respin tomorrow, thanks.

Do you want me to include the TPROXY fix too?

Actually nft_compat.c already does:

if (proto)
entry->e6.ipv6.flags |= IP6T_F_PROTO;

So it is only x_tables legacy that has this issue.

Quoted 1 line

so the only other necessary fix is the check for 'INV' in xt_l2tp.c.

Agreed.

Thanks!

#7 lore raw
In reply to Florian Westphal
Quoted 25 lines · Florian Westphal
Quoted 8 lines · Pablo Neira Ayuso
Quoted 8 lines
Quoted 4 lines

Sashiko also hinting to fix ip6_tables core check for this protocol flag:

https://sashiko.dev/#/patchset/20260821134136.18191-1-fw%40strlen.de

Not so sure. Maybe just do this? I don't think proto > 0 and flag unset makes any sense.

Quoted 15 lines

net/ipv6/netfilter/ip6_tables.c check_entry_size_and_hooks(struct ip6t_entry *e, · line 655

Quoted 13 lines
net/ipv6/netfilter/ip6_tables.c +6 -0
@@ -647,6 +647,13 @@ check_entry_size_and_hooks(struct ip6t_entry *e,
 	/* Clear counters and comefrom */
 	e->counters = ((struct xt_counters) { 0, 0 });
 	e->comefrom = 0;
+
+	/* Make sure F_PROTO is set, else ip6_packet_match
+	 * won't do the right thing.
+	 */
+	if (e->ipv6.proto)
+		e->ipv6.flags |= IP6T_F_PROTO;

Userspace always provides a zeroed e->ipv6.proto when unused, correct?

Quoted 7 lines

Yes, ip6tables raises it when it parses '-p' option.

Quoted 1 line

If so, LGTM.

Alright, I will respin tomorrow, thanks.

Do you want me to include the TPROXY fix too?

I think that is nf-next material, for correctness. Not urgent.

Thanks!