Skip to contentSkip to conversation

net: nf_tables: Make nft_meta expression more robust

PATCH Superseded Phil Sutter 2 participants 7 messages Jul 19, 2019 – Jul 23, 2019

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH v2 1/2 net: nf_tables: Make nft_meta expression more robust

nft_meta_get_eval()'s tendency to bail out setting NFT_BREAK verdict in situations where required data is missing breaks inverted checks like e.g.:

| meta iifname != eth0 accept

This rule will never match if there is no input interface (or it is not known) which is not intuitive and, what's worse, breaks consistency of iptables-nft with iptables-legacy.

Fix this by falling back to placing a value in dreg which never matches (avoiding accidental matches):

{I,O}IF:
Use invalid ifindex value zero.

{BRI_,}{I,O}IFNAME, {I,O}IFKIND:
Use an empty string which is neither a valid interface name nor kind.

{I,O}IFTYPE:

	Use ARPHRD_VOID (0xFFFF).

---
Changes since v1:
- Apply same fix to net/bridge/netfilter/nft_meta_bridge.c as well.

---
 net/bridge/netfilter/nft_meta_bridge.c |  6 +---
 net/netfilter/nft_meta.c               | 45 +++++++++++---------------
 2 files changed, 20 insertions(+), 31 deletions(-)
net/bridge/netfilter/nft_meta_bridge.c +1 -5
index bed66f536b345..a98dec2cf0cfd 100644
@@ -30,13 +30,9 @@ static void nft_meta_bridge_get_eval(const struct nft_expr *expr,
 	switch (priv->key) {
 	case NFT_META_BRI_IIFNAME:
 		br_dev = nft_meta_get_bridge(in);
-		if (!br_dev)
-			goto err;
 		break;
 	case NFT_META_BRI_OIFNAME:
 		br_dev = nft_meta_get_bridge(out);
-		if (!br_dev)
-			goto err;
 		break;
 	case NFT_META_BRI_IIFPVID: {
 		u16 p_pvid;
@@ -64,7 +60,7 @@ static void nft_meta_bridge_get_eval(const struct nft_expr *expr,
 		goto out;
 	}
 
-	strncpy((char *)dest, br_dev->name, IFNAMSIZ);
+	strncpy((char *)dest, br_dev ? br_dev->name : "", IFNAMSIZ);
 	return;
 out:
 	return nft_meta_get_eval(expr, regs, pkt);
net/netfilter/nft_meta.c +19 -26

75 lines not on this page · raw message

Signature · 1 line
2.22.0
author reviewer
#1 lore raw

PATCH 2/2 net: netfilter: nft_meta_bridge: Eliminate 'out' label

The label is used just once and the code it points at is not reused, no point in keeping it.

---
 net/bridge/netfilter/nft_meta_bridge.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
net/bridge/netfilter/nft_meta_bridge.c +1 -3
index a98dec2cf0cfd..1804e867f7151 100644
@@ -57,13 +57,11 @@ static void nft_meta_bridge_get_eval(const struct nft_expr *expr,
 		return;
 	}
 	default:
-		goto out;
+		return nft_meta_get_eval(expr, regs, pkt);
 	}
 
 	strncpy((char *)dest, br_dev ? br_dev->name : "", IFNAMSIZ);
 	return;
-out:
-	return nft_meta_get_eval(expr, regs, pkt);
 err:
 	regs->verdict.code = NFT_BREAK;
 }
Signature · 1 line
2.22.0
#2 lore raw
In reply to Phil Sutter
Quoted 16 lines · Phil Sutter

nft_meta_get_eval()'s tendency to bail out setting NFT_BREAK verdict in situations where required data is missing breaks inverted checks like e.g.:

| meta iifname != eth0 accept

This rule will never match if there is no input interface (or it is not known) which is not intuitive and, what's worse, breaks consistency of iptables-nft with iptables-legacy.

Fix this by falling back to placing a value in dreg which never matches (avoiding accidental matches):

{I,O}IF:
Use invalid ifindex value zero.

Quoted 6 lines

{BRI_,}{I,O}IFNAME, {I,O}IFKIND:
Use an empty string which is neither a valid interface name nor kind.

{I,O}IFTYPE:

	Use ARPHRD_VOID (0xFFFF).

What could it be done with?

NFT_META_BRI_IIFPVID
NFT_META_BRI_IIFPVPROTO

Those will still not work for

        meta ibrpvid != 40

if interface is not available.

For VPROTO probably it's possible. I don't have a solution for IIFPVID.

author reviewer
#3 lore raw

Hi,

Quoted 29 lines · Pablo Neira Ayuso
Quoted 16 lines · Phil Sutter

nft_meta_get_eval()'s tendency to bail out setting NFT_BREAK verdict in situations where required data is missing breaks inverted checks like e.g.:

| meta iifname != eth0 accept

This rule will never match if there is no input interface (or it is not known) which is not intuitive and, what's worse, breaks consistency of iptables-nft with iptables-legacy.

Fix this by falling back to placing a value in dreg which never matches (avoiding accidental matches):

{I,O}IF:
Use invalid ifindex value zero.

Quoted 6 lines

{BRI_,}{I,O}IFNAME, {I,O}IFKIND:
Use an empty string which is neither a valid interface name nor kind.

{I,O}IFTYPE:

	Use ARPHRD_VOID (0xFFFF).

What could it be done with?

NFT_META_BRI_IIFPVID
NFT_META_BRI_IIFPVPROTO

Quoted 8 lines

Those will still not work for

        meta ibrpvid != 40

if interface is not available.

For VPROTO probably it's possible. I don't have a solution for IIFPVID.

VLAN IDs 0 and 4095 are reserved, we could use those. I refrained from changing bridge VLAN matches because of IIFPVPROTO, no idea if there's an illegal value we could use for that. If you have an idea, I'm all for it. :)

Thanks, Phil

#4 lore raw
Quoted 38 lines · Phil Sutter

Hi,

Quoted 35 lines · Pablo Neira Ayuso
Quoted 16 lines · Phil Sutter

nft_meta_get_eval()'s tendency to bail out setting NFT_BREAK verdict in situations where required data is missing breaks inverted checks like e.g.:

| meta iifname != eth0 accept

This rule will never match if there is no input interface (or it is not known) which is not intuitive and, what's worse, breaks consistency of iptables-nft with iptables-legacy.

Fix this by falling back to placing a value in dreg which never matches (avoiding accidental matches):

{I,O}IF:
Use invalid ifindex value zero.

Quoted 6 lines

{BRI_,}{I,O}IFNAME, {I,O}IFKIND:
Use an empty string which is neither a valid interface name nor kind.

{I,O}IFTYPE:

	Use ARPHRD_VOID (0xFFFF).

What could it be done with?

NFT_META_BRI_IIFPVID
NFT_META_BRI_IIFPVPROTO

Those will still not work for

        meta ibrpvid != 40

if interface is not available.

Quoted 7 lines
Quoted 2 lines

For VPROTO probably it's possible. I don't have a solution for IIFPVID.

VLAN IDs 0 and 4095 are reserved, we could use those. I refrained from changing bridge VLAN matches because of IIFPVPROTO, no idea if there's an illegal value we could use for that. If you have an idea, I'm all for it. :)

I think we can add something like:

NFT_META_BRI_IIFVLAN

just to check for br_vlan_enabled(), from userspace we can check for exists/missing as a boolean, so we don't have to worry on assuming an unused value for things like this. This can be added in the next release cycle.

Regarding IIFTYPE / OIFTYPE, if there is no in-trees interfaces using "", I think we are fine, probably send a patch to propose to disallow this to net.

These ones are missing:

NFT_META_IIFGROUP
NFT_META_OIFGROUP

For these two, the default group (0) should be fine since every interface is falling under this category by default.

I can squash this small patch to this one and push it one.

Thanks.

net/netfilter/nft_meta.c +2 -6
index d8d04ec1face..e2c6857c0f68 100644
@@ -177,14 +177,10 @@ void nft_meta_get_eval(const struct nft_expr *expr,
 		*dest = raw_smp_processor_id();
 		break;
 	case NFT_META_IIFGROUP:
-		if (in == NULL)
-			goto err;
-		*dest = in->group;
+		*dest = in ? in->group : 0;
 		break;
 	case NFT_META_OIFGROUP:
-		if (out == NULL)
-			goto err;
-		*dest = out->group;
+		*dest = out ? out->group : 0;
 		break;
 #ifdef CONFIG_CGROUP_NET_CLASSID
 	case NFT_META_CGROUP:
author reviewer
#5 lore raw

Hi Pablo,

Quoted 47 lines · Pablo Neira Ayuso
Quoted 38 lines · Phil Sutter

Hi,

Quoted 35 lines · Pablo Neira Ayuso
Quoted 16 lines · Phil Sutter

nft_meta_get_eval()'s tendency to bail out setting NFT_BREAK verdict in situations where required data is missing breaks inverted checks like e.g.:

| meta iifname != eth0 accept

This rule will never match if there is no input interface (or it is not known) which is not intuitive and, what's worse, breaks consistency of iptables-nft with iptables-legacy.

Fix this by falling back to placing a value in dreg which never matches (avoiding accidental matches):

{I,O}IF:
Use invalid ifindex value zero.

Quoted 6 lines

{BRI_,}{I,O}IFNAME, {I,O}IFKIND:
Use an empty string which is neither a valid interface name nor kind.

{I,O}IFTYPE:

	Use ARPHRD_VOID (0xFFFF).

What could it be done with?

NFT_META_BRI_IIFPVID
NFT_META_BRI_IIFPVPROTO

Those will still not work for

        meta ibrpvid != 40

if interface is not available.

Quoted 7 lines
Quoted 2 lines

For VPROTO probably it's possible. I don't have a solution for IIFPVID.

VLAN IDs 0 and 4095 are reserved, we could use those. I refrained from changing bridge VLAN matches because of IIFPVPROTO, no idea if there's an illegal value we could use for that. If you have an idea, I'm all for it. :)

Quoted 8 lines

I think we can add something like:

NFT_META_BRI_IIFVLAN

just to check for br_vlan_enabled(), from userspace we can check for exists/missing as a boolean, so we don't have to worry on assuming an unused value for things like this. This can be added in the next release cycle.

Adding existence checks where missing is indeed a good idea, but doesn't quite solve the problem we're facing here. :)

[...]

Quoted 9 lines

These ones are missing:

NFT_META_IIFGROUP
NFT_META_OIFGROUP

For these two, the default group (0) should be fine since every interface is falling under this category by default.

I can squash this small patch to this one and push it one.

My problem with these "sane defaults" is that we may cause inconsistent behaviour in rulesets: In prerouting, 'meta oifgroup 0' will match no matter which interface the packet will be routed to. Yes, prerouting implies there is no output interface (yet), but I consider this an implementation detail and there will likely be cases where it is not as easy to spot why something can't work.

Cheers, Phil

#6 lore raw
Quoted 54 lines · Phil Sutter

Hi Pablo,

Quoted 51 lines · Pablo Neira Ayuso
Quoted 38 lines · Phil Sutter

Hi,

Quoted 35 lines · Pablo Neira Ayuso
Quoted 16 lines · Phil Sutter

nft_meta_get_eval()'s tendency to bail out setting NFT_BREAK verdict in situations where required data is missing breaks inverted checks like e.g.:

| meta iifname != eth0 accept

This rule will never match if there is no input interface (or it is not known) which is not intuitive and, what's worse, breaks consistency of iptables-nft with iptables-legacy.

Fix this by falling back to placing a value in dreg which never matches (avoiding accidental matches):

{I,O}IF:
Use invalid ifindex value zero.

Quoted 6 lines

{BRI_,}{I,O}IFNAME, {I,O}IFKIND:
Use an empty string which is neither a valid interface name nor kind.

{I,O}IFTYPE:

	Use ARPHRD_VOID (0xFFFF).

What could it be done with?

NFT_META_BRI_IIFPVID
NFT_META_BRI_IIFPVPROTO

Those will still not work for

        meta ibrpvid != 40

if interface is not available.

Quoted 7 lines
Quoted 2 lines

For VPROTO probably it's possible. I don't have a solution for IIFPVID.

VLAN IDs 0 and 4095 are reserved, we could use those. I refrained from changing bridge VLAN matches because of IIFPVPROTO, no idea if there's an illegal value we could use for that. If you have an idea, I'm all for it. :)

I think we can add something like:

NFT_META_BRI_IIFVLAN

Quoted 7 lines
Quoted 4 lines

just to check for br_vlan_enabled(), from userspace we can check for exists/missing as a boolean, so we don't have to worry on assuming an unused value for things like this. This can be added in the next release cycle.

Adding existence checks where missing is indeed a good idea, but doesn't quite solve the problem we're facing here. :)

I'm not proposing to use this approach for _every key_. For this one specifically I think it's meaningful to have a NFT_META_BRI_IIFVLAN since vlan is an optional configuration.

Quoted 9 lines

[...]

Quoted 8 lines

These ones are missing:

NFT_META_IIFGROUP
NFT_META_OIFGROUP

For these two, the default group (0) should be fine since every interface is falling under this category by default.

Quoted 8 lines
Quoted 1 line

I can squash this small patch to this one and push it one.

My problem with these "sane defaults" is that we may cause inconsistent behaviour in rulesets: In prerouting, 'meta oifgroup 0' will match no matter which interface the packet will be routed to. Yes, prerouting implies there is no output interface (yet), but I consider this an implementation detail and there will likely be cases where it is not as easy to spot why something can't work.

I think disallowing 'meta oifgroup 0' from prerouting (and the input path in general) is fine from the control plane configuration, but you know Florian disagrees with this. Probably this can be left as is for IIFNAME and OIFNAME, which as used by iptables-nft for compatibility with legacy. For new keys, check for invalid configuration I would argue that is fine.

But let's wait for Florian to say what he thinks :-)