Skip to contentSkip to conversation

Netfilter fixes for net

PATCH Applied Pablo Neira Ayuso 3 participants 13 messages Mar 6, 2021 – Mar 7, 2021

Subsystems (4)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover Netfilter fixes for net

Hi,

The following patchset contains Netfilter fixes for net:

1) Fix incorrect enum type definition in nfnetlink_cthelper UAPI, from Dmitry V. Levin.

2) Remove extra space in deprecated automatic helper assignment notice, from Klemen Košir.

3) Drop early socket demux socket after NAT mangling, from Florian Westphal. Add a test to exercise this bug.

4) Fix bogus invalid packet report in the conntrack TCP tracker, also from Florian.

5) Fix access to xt[NFPROTO_UNSPEC] list with no mutex in target/match_revfn(), from Vasily Averin.

6) Disallow updates on the table ownership flag.

7) Fix double hook unregistration of tables with owner.

8) Remove bogus check on the table owner in __nft_release_tables().

Please, pull these changes from:

git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git

Thanks!

----------------------------------------------------------------

The following changes since commit eee7ede695cfbb19fefdeb14992535b605448f35:

Merge branch 'bnxt_en-error-recovery-bug-fixes' (2021-02-26 15:50:25 -0800)

are available in the Git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git HEAD

for you to fetch changes up to bd1777b3a88f98e223392221b330668458aac7f1:

netfilter: nftables: bogus check for netlink portID with table owner (2021-03-04 04:02:54 +0100)

----------------------------------------------------------------
Dmitry V. Levin (1):
uapi: nfnetlink_cthelper.h: fix userspace compilation error

Florian Westphal (3):
netfilter: nf_nat: undo erroneous tcp edemux lookup
netfilter: conntrack: avoid misleading 'invalid' in log message
selftests: netfilter: test nat port clash resolution interaction with tcp early demux

Klemen Košir (1):
netfilter: conntrack: Remove a double space in a log message

Pablo Neira Ayuso (3):
netfilter: nftables: disallow updates on table ownership
netfilter: nftables: fix possible double hook unregistration with table owner
netfilter: nftables: bogus check for netlink portID with table owner

Vasily Averin (1):
netfilter: x_tables: gpf inside xt_find_revision()

 include/uapi/linux/netfilter/nfnetlink_cthelper.h  |  2 +-
 net/netfilter/nf_conntrack_helper.c                |  3 +-
 net/netfilter/nf_conntrack_proto_tcp.c             |  6 +-
 net/netfilter/nf_nat_proto.c                       | 25 +++++-
 net/netfilter/nf_tables_api.c                      | 19 +++--
 net/netfilter/x_tables.c                           |  6 +-
 tools/testing/selftests/netfilter/Makefile         |  2 +-
 tools/testing/selftests/netfilter/nf_nat_edemux.sh | 99 ++++++++++++++++++++++
 8 files changed, 145 insertions(+), 17 deletions(-)
 create mode 100755 tools/testing/selftests/netfilter/nf_nat_edemux.sh
author maintainer
#1 lore raw

PATCH 1/9 uapi: nfnetlink_cthelper.h: fix userspace compilation error

From: "Dmitry V. Levin" <[email protected]>

Apparently, <linux/netfilter/nfnetlink_cthelper.h> and <linux/netfilter/nfnetlink_acct.h> could not be included into the same compilation unit because of a cut-and-paste typo in the former header.

Fixes 12f7a505331e6 ("netfilter: add user-space connection tracking helper infrastructure")
Cc <[email protected]> # v3.6
---
 include/uapi/linux/netfilter/nfnetlink_cthelper.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/uapi/linux/netfilter/nfnetlink_cthelper.h +1 -1
index a13137afc429..70af02092d16 100644
@@ -5,7 +5,7 @@
 #define NFCT_HELPER_STATUS_DISABLED	0
 #define NFCT_HELPER_STATUS_ENABLED	1
 
-enum nfnl_acct_msg_types {
+enum nfnl_cthelper_msg_types {
 	NFNL_MSG_CTHELPER_NEW,
 	NFNL_MSG_CTHELPER_GET,
 	NFNL_MSG_CTHELPER_DEL,
Signature · 1 line
2.20.1
#2 lore raw

Re: [PATCH net 1/9] uapi: nfnetlink_cthelper.h: fix userspace compilation error

This series was applied to netdev/net.git (refs/heads/master):

Hello:

This series was applied to netdev/net.git (refs/heads/master):

Quoted 6 lines · On Sat, 6 Mar 2021 13:12:15 +0100 you wrote:

From: "Dmitry V. Levin" <[email protected]>

Apparently, <linux/netfilter/nfnetlink_cthelper.h> and <linux/netfilter/nfnetlink_acct.h> could not be included into the same compilation unit because of a cut-and-paste typo in the former header.

Quoted 6 lines
Fixes 12f7a505331e6 ("netfilter: add user-space connection tracking helper infrastructure")
Cc <[email protected]> # v3.6

[...]

Here is the summary with links:
- [net,1/9] uapi: nfnetlink_cthelper.h: fix userspace compilation error

    https://git.kernel.org/netdev/net/c/c33cb0020ee6
  - [net,2/9] netfilter: conntrack: Remove a double space in a log message
    https://git.kernel.org/netdev/net/c/c57ea2d7d81f
  - [net,3/9] netfilter: nf_nat: undo erroneous tcp edemux lookup
    https://git.kernel.org/netdev/net/c/03a3ca37e4c6
  - [net,4/9] netfilter: conntrack: avoid misleading 'invalid' in log message
    https://git.kernel.org/netdev/net/c/07b5a76e1892
  - [net,5/9] selftests: netfilter: test nat port clash resolution interaction with tcp early demux
    https://git.kernel.org/netdev/net/c/c2c16ccba2f5
  - [net,6/9] netfilter: x_tables: gpf inside xt_find_revision()
    https://git.kernel.org/netdev/net/c/8e24edddad15
  - [net,7/9] netfilter: nftables: disallow updates on table ownership
    https://git.kernel.org/netdev/net/c/9cc0001a18b4
  - [net,8/9] netfilter: nftables: fix possible double hook unregistration with table owner
    https://git.kernel.org/netdev/net/c/2888b080d05c
  - [net,9/9] netfilter: nftables: bogus check for netlink portID with table owner
    https://git.kernel.org/netdev/net/c/bd1777b3a88f

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html

author maintainer
#3 lore raw

PATCH 2/9 netfilter: conntrack: Remove a double space in a log message

In reply to Pablo Neira Ayuso

From: Klemen Košir <[email protected]>

Removed an extra space in a log message and an extra blank line in code.

---
 net/netfilter/nf_conntrack_helper.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/netfilter/nf_conntrack_helper.c +1 -2
index 118f415928ae..b055187235f8 100644
@@ -219,7 +219,7 @@ nf_ct_lookup_helper(struct nf_conn *ct, struct net *net)
 			return NULL;
 		pr_info("nf_conntrack: default automatic helper assignment "
 			"has been turned off for security reasons and CT-based "
-			" firewall rule not found. Use the iptables CT target "
+			"firewall rule not found. Use the iptables CT target "
 			"to attach helpers instead.\n");
 		net->ct.auto_assign_helper_warned = 1;
 		return NULL;
@@ -228,7 +228,6 @@ nf_ct_lookup_helper(struct nf_conn *ct, struct net *net)
 	return __nf_ct_helper_find(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
 }
 
-
 int __nf_ct_try_assign_helper(struct nf_conn *ct, struct nf_conn *tmpl,
 			      gfp_t flags)
 {
Signature · 1 line
2.20.1
author maintainer
#4 lore raw

PATCH 3/9 netfilter: nf_nat: undo erroneous tcp edemux lookup

In reply to Pablo Neira Ayuso

From: Florian Westphal <[email protected]>

Under extremely rare conditions TCP early demux will retrieve the wrong socket.

1. local machine establishes a connection to a remote server, S, on port p.

This gives:
laddr:lport -> S:p
... both in tcp and conntrack.

2. local machine establishes a connection to host H, on port p2. 2a. TCP stack choses same laddr:lport, so we have laddr:lport -> H:p2 from TCP point of view.
2b). There is a destination NAT rewrite in place, translating
H:p2 to S:p. This results in following conntrack entries:

I) laddr:lport -> S:p (origin) S:p -> laddr:lport (reply) II) laddr:lport -> H:p2 (origin) S:p -> laddr:lport2 (reply)

NAT engine has rewritten laddr:lport to laddr:lport2 to map the reply packet to the correct origin.

When server sends SYN/ACK to laddr:lport2, the PREROUTING hook will undo-the SNAT transformation, rewriting IP header to S:p -> laddr:lport

This causes TCP early demux to associate the skb with the TCP socket of the first connection.

The INPUT hook will then reverse the DNAT transformation, rewriting the IP header to H:p2 -> laddr:lport.

Because packet ends up with the wrong socket, the new connection never completes: originator stays in SYN_SENT and conntrack entry remains in SYN_RECV until timeout, and responder retransmits SYN/ACK until it gives up.

To resolve this, orphan the skb after the input rewrite:
Because the source IP address changed, the socket must be incorrect. We can't move the DNAT undo to prerouting due to backwards compatibility, doing so will make iptables/nftables rules to no longer match the way they did.

After orphan, the packet will be handed to the next protocol layer (tcp, udp, ...) and that will repeat the socket lookup just like as if early demux was disabled.

Fixes 41063e9dd1195 ("ipv4: Early TCP socket demux.")
---
 net/netfilter/nf_nat_proto.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
net/netfilter/nf_nat_proto.c +21 -4
index e87b6bd6b3cd..4731d21fc3ad 100644
@@ -646,8 +646,8 @@ nf_nat_ipv4_fn(void *priv, struct sk_buff *skb,
 }
 
 static unsigned int
-nf_nat_ipv4_in(void *priv, struct sk_buff *skb,
-	       const struct nf_hook_state *state)
+nf_nat_ipv4_pre_routing(void *priv, struct sk_buff *skb,
+			const struct nf_hook_state *state)
 {
 	unsigned int ret;
 	__be32 daddr = ip_hdr(skb)->daddr;
@@ -659,6 +659,23 @@ nf_nat_ipv4_in(void *priv, struct sk_buff *skb,
 	return ret;
 }
 
+static unsigned int
+nf_nat_ipv4_local_in(void *priv, struct sk_buff *skb,
+		     const struct nf_hook_state *state)
+{
+	__be32 saddr = ip_hdr(skb)->saddr;
+	struct sock *sk = skb->sk;
+	unsigned int ret;
+
+	ret = nf_nat_ipv4_fn(priv, skb, state);
+
+	if (ret == NF_ACCEPT && sk && saddr != ip_hdr(skb)->saddr &&
+	    !inet_sk_transparent(sk))
+		skb_orphan(skb); /* TCP edemux obtained wrong socket */
+
+	return ret;
+}
+
 static unsigned int
 nf_nat_ipv4_out(void *priv, struct sk_buff *skb,
 		const struct nf_hook_state *state)
@@ -736,7 +753,7 @@ nf_nat_ipv4_local_fn(void *priv, struct sk_buff *skb,
 static const struct nf_hook_ops nf_nat_ipv4_ops[] = {
 	/* Before packet filtering, change destination */
 	{
-		.hook		= nf_nat_ipv4_in,
+		.hook		= nf_nat_ipv4_pre_routing,
 		.pf		= NFPROTO_IPV4,
 		.hooknum	= NF_INET_PRE_ROUTING,
 		.priority	= NF_IP_PRI_NAT_DST,
@@ -757,7 +774,7 @@ static const struct nf_hook_ops nf_nat_ipv4_ops[] = {
 	},
 	/* After packet filtering, change source */
 	{
-		.hook		= nf_nat_ipv4_fn,
+		.hook		= nf_nat_ipv4_local_in,
 		.pf		= NFPROTO_IPV4,
 		.hooknum	= NF_INET_LOCAL_IN,
 		.priority	= NF_IP_PRI_NAT_SRC,
Signature · 1 line
2.20.1
#5 lore raw

Re: [PATCH net 3/9] netfilter: nf_nat: undo erroneous tcp edemux lookup

Quoted 18 lines · Pablo Neira Ayuso

From: Florian Westphal <[email protected]>

Under extremely rare conditions TCP early demux will retrieve the wrong socket.

1. local machine establishes a connection to a remote server, S, on port

    p.

    This gives:
    laddr:lport -> S:p
    ... both in tcp and conntrack.

2. local machine establishes a connection to host H, on port p2. 2a. TCP stack choses same laddr:lport, so we have laddr:lport -> H:p2 from TCP point of view.
2b). There is a destination NAT rewrite in place, translating
H:p2 to S:p. This results in following conntrack entries:

Quoted 5 lines

I) laddr:lport -> S:p (origin) S:p -> laddr:lport (reply) II) laddr:lport -> H:p2 (origin) S:p -> laddr:lport2 (reply)

NAT engine has rewritten laddr:lport to laddr:lport2 to map the reply packet to the correct origin.

Could you eloborate where and how linux nat engine is doing the

laddr:lport to laddr:lport2

rewrite? There's only DST nat and there should be conflict (for reply) in tuple establishment afaik....

Quoted 91 lines

When server sends SYN/ACK to laddr:lport2, the PREROUTING hook will undo-the SNAT transformation, rewriting IP header to S:p -> laddr:lport

This causes TCP early demux to associate the skb with the TCP socket of the first connection.

The INPUT hook will then reverse the DNAT transformation, rewriting the IP header to H:p2 -> laddr:lport.

Because packet ends up with the wrong socket, the new connection never completes: originator stays in SYN_SENT and conntrack entry remains in SYN_RECV until timeout, and responder retransmits SYN/ACK until it gives up.

To resolve this, orphan the skb after the input rewrite:
Because the source IP address changed, the socket must be incorrect. We can't move the DNAT undo to prerouting due to backwards compatibility, doing so will make iptables/nftables rules to no longer match the way they did.

After orphan, the packet will be handed to the next protocol layer (tcp, udp, ...) and that will repeat the socket lookup just like as if early demux was disabled.

Fixes 41063e9dd1195 ("ipv4: Early TCP socket demux.")
---
  net/netfilter/nf_nat_proto.c | 25 +++++++++++++++++++++----
  1 file changed, 21 insertions(+), 4 deletions(-)
net/netfilter/nf_nat_proto.c +21 -4
index e87b6bd6b3cd..4731d21fc3ad 100644
@@ -646,8 +646,8 @@ nf_nat_ipv4_fn(void *priv, struct sk_buff *skb,
  }
 
  static unsigned int
-nf_nat_ipv4_in(void *priv, struct sk_buff *skb,
-	       const struct nf_hook_state *state)
+nf_nat_ipv4_pre_routing(void *priv, struct sk_buff *skb,
+			const struct nf_hook_state *state)
  {
  	unsigned int ret;
  	__be32 daddr = ip_hdr(skb)->daddr;
@@ -659,6 +659,23 @@ nf_nat_ipv4_in(void *priv, struct sk_buff *skb,
  	return ret;
  }
 
+static unsigned int
+nf_nat_ipv4_local_in(void *priv, struct sk_buff *skb,
+		     const struct nf_hook_state *state)
+{
+	__be32 saddr = ip_hdr(skb)->saddr;
+	struct sock *sk = skb->sk;
+	unsigned int ret;
+
+	ret = nf_nat_ipv4_fn(priv, skb, state);
+
+	if (ret == NF_ACCEPT && sk && saddr != ip_hdr(skb)->saddr &&
+	    !inet_sk_transparent(sk))
+		skb_orphan(skb); /* TCP edemux obtained wrong socket */
+
+	return ret;
+}
+
  static unsigned int
  nf_nat_ipv4_out(void *priv, struct sk_buff *skb,
  		const struct nf_hook_state *state)
@@ -736,7 +753,7 @@ nf_nat_ipv4_local_fn(void *priv, struct sk_buff *skb,
  static const struct nf_hook_ops nf_nat_ipv4_ops[] = {
  	/* Before packet filtering, change destination */
  	{
-		.hook		= nf_nat_ipv4_in,
+		.hook		= nf_nat_ipv4_pre_routing,
  		.pf		= NFPROTO_IPV4,
  		.hooknum	= NF_INET_PRE_ROUTING,
  		.priority	= NF_IP_PRI_NAT_DST,
@@ -757,7 +774,7 @@ static const struct nf_hook_ops nf_nat_ipv4_ops[] = {
  	},
  	/* After packet filtering, change source */
  	{
-		.hook		= nf_nat_ipv4_fn,
+		.hook		= nf_nat_ipv4_local_in,
  		.pf		= NFPROTO_IPV4,
  		.hooknum	= NF_INET_LOCAL_IN,
  		.priority	= NF_IP_PRI_NAT_SRC,
#6 lore raw

Re: [PATCH net 3/9] netfilter: nf_nat: undo erroneous tcp edemux lookup

Quoted 24 lines · Mika Penttilä
Quoted 21 lines · Pablo Neira Ayuso

From: Florian Westphal <[email protected]>

Under extremely rare conditions TCP early demux will retrieve the wrong socket.

1. local machine establishes a connection to a remote server, S, on port p.

    This gives:
    laddr:lport -> S:p
    ... both in tcp and conntrack.

2. local machine establishes a connection to host H, on port p2. 2a. TCP stack choses same laddr:lport, so we have laddr:lport -> H:p2 from TCP point of view.
    2b). There is a destination NAT rewrite in place, translating H:p2 to S:p.  This results in following conntrack entries:

    I)  laddr:lport -> S:p  (origin)  S:p -> laddr:lport (reply) II) laddr:lport -> H:p2 (origin)  S:p -> laddr:lport2 (reply)

Quoted 7 lines
Quoted 2 lines

    NAT engine has rewritten laddr:lport to laddr:lport2 to map the reply packet to the correct origin.

Could you eloborate where and how linux nat engine is doing the

laddr:lport to laddr:lport2

rewrite? There's only DST nat and there should be conflict (for reply) in tuple establishment afaik....

Ah I see it is the nat null binding for src to make it unique

Quoted 94 lines
Quoted 93 lines

    When server sends SYN/ACK to laddr:lport2, the PREROUTING hook will undo-the SNAT transformation, rewriting IP header to S:p -> laddr:lport

    This causes TCP early demux to associate the skb with the TCP socket of the first connection.

    The INPUT hook will then reverse the DNAT transformation, rewriting the IP header to H:p2 -> laddr:lport.

Because packet ends up with the wrong socket, the new connection never completes: originator stays in SYN_SENT and conntrack entry remains in SYN_RECV until timeout, and responder retransmits SYN/ACK until it gives up.

To resolve this, orphan the skb after the input rewrite:
Because the source IP address changed, the socket must be incorrect. We can't move the DNAT undo to prerouting due to backwards compatibility, doing so will make iptables/nftables rules to no longer match the way they did.

After orphan, the packet will be handed to the next protocol layer (tcp, udp, ...) and that will repeat the socket lookup just like as if early demux was disabled.

Fixes 41063e9dd1195 ("ipv4: Early TCP socket demux.")

---
  net/netfilter/nf_nat_proto.c | 25 +++++++++++++++++++++----

  1 file changed, 21 insertions(+), 4 deletions(-)
net/netfilter/nf_nat_proto.c +0 -0
index e87b6bd6b3cd..4731d21fc3ad 100644
@@ -646,8 +646,8 @@ nf_nat_ipv4_fn(void *priv, struct sk_buff *skb,
   }

    static unsigned int

net/netfilter/nf_nat_proto.c +2 -2
-nf_nat_ipv4_in(void *priv, struct sk_buff *skb,
-           const struct nf_hook_state *state)
+nf_nat_ipv4_pre_routing(void *priv, struct sk_buff *skb,
+            const struct nf_hook_state *state)

  {
      unsigned int ret;
      __be32 daddr = ip_hdr(skb)->daddr;

net/netfilter/nf_nat_proto.c +0 -0
@@ -659,6 +659,23 @@ nf_nat_ipv4_in(void *priv, struct sk_buff *skb,
       return ret;
   }

  +static unsigned int

net/netfilter/nf_nat_proto.c +16 -0
+nf_nat_ipv4_local_in(void *priv, struct sk_buff *skb,
+             const struct nf_hook_state *state)
+{
+    __be32 saddr = ip_hdr(skb)->saddr;
+    struct sock *sk = skb->sk;
+    unsigned int ret;
+
+    ret = nf_nat_ipv4_fn(priv, skb, state);
+
+    if (ret == NF_ACCEPT && sk && saddr != ip_hdr(skb)->saddr &&
+        !inet_sk_transparent(sk))
+        skb_orphan(skb); /* TCP edemux obtained wrong socket */
+
+    return ret;
+}
+

  static unsigned int
  nf_nat_ipv4_out(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)

net/netfilter/nf_nat_proto.c +1 -1
@@ -736,7 +753,7 @@ nf_nat_ipv4_local_fn(void *priv, struct sk_buff 
 *skb,
   static const struct nf_hook_ops nf_nat_ipv4_ops[] = {
       /* Before packet filtering, change destination */
       {
-        .hook        = nf_nat_ipv4_in,
+        .hook        = nf_nat_ipv4_pre_routing,
           .pf        = NFPROTO_IPV4,
           .hooknum    = NF_INET_PRE_ROUTING,

          .priority    = NF_IP_PRI_NAT_DST,

net/netfilter/nf_nat_proto.c +1 -1
@@ -757,7 +774,7 @@ static const struct nf_hook_ops nf_nat_ipv4_ops[] 
 = {
       },
       /* After packet filtering, change source */
       {
-        .hook        = nf_nat_ipv4_fn,
+        .hook        = nf_nat_ipv4_local_in,
           .pf        = NFPROTO_IPV4,
           .hooknum    = NF_INET_LOCAL_IN,

          .priority    = NF_IP_PRI_NAT_SRC,

author maintainer
#7 lore raw

PATCH 4/9 netfilter: conntrack: avoid misleading 'invalid' in log message

In reply to Pablo Neira Ayuso

From: Florian Westphal <[email protected]>

The packet is not flagged as invalid: conntrack will accept it and its associated with the conntrack entry.

This happens e.g. when receiving a retransmitted SYN in SYN_RECV state.

---
 net/netfilter/nf_conntrack_proto_tcp.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/netfilter/nf_conntrack_proto_tcp.c +4 -2
index 1d7e1c595546..ec23330687a5 100644
@@ -982,8 +982,10 @@ int nf_conntrack_tcp_packet(struct nf_conn *ct,
 					IP_CT_EXP_CHALLENGE_ACK;
 		}
 		spin_unlock_bh(&ct->lock);
-		nf_ct_l4proto_log_invalid(skb, ct, "invalid packet ignored in "
-					  "state %s ", tcp_conntrack_names[old_state]);
+		nf_ct_l4proto_log_invalid(skb, ct,
+					  "packet (index %d) in dir %d ignored, state %s",
+					  index, dir,
+					  tcp_conntrack_names[old_state]);
 		return NF_ACCEPT;
 	case TCP_CONNTRACK_MAX:
 		/* Special case for SYN proxy: when the SYN to the server or
Signature · 1 line
2.20.1
author maintainer
#8 lore raw

PATCH 5/9 selftests: netfilter: test nat port clash resolution interaction with tcp early demux

In reply to Pablo Neira Ayuso

From: Florian Westphal <[email protected]>

Convert Antonio Ojeas bug reproducer to a kselftest.

---
 tools/testing/selftests/netfilter/Makefile    |  2 +-
 .../selftests/netfilter/nf_nat_edemux.sh      | 99 +++++++++++++++++++
 2 files changed, 100 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/netfilter/nf_nat_edemux.sh
tools/testing/selftests/netfilter/Makefile +1 -1
index 3006a8e5b41a..3171069a6b46 100644
@@ -4,7 +4,7 @@
 TEST_PROGS := nft_trans_stress.sh nft_nat.sh bridge_brouter.sh \
 	conntrack_icmp_related.sh nft_flowtable.sh ipvs.sh \
 	nft_concat_range.sh nft_conntrack_helper.sh \
-	nft_queue.sh nft_meta.sh \
+	nft_queue.sh nft_meta.sh nf_nat_edemux.sh \
 	ipip-conntrack-mtu.sh
 
 LDLIBS = -lmnl
tools/testing/selftests/netfilter/nf_nat_edemux.sh new file +99 -0

99 lines not on this page · raw message

Signature · 1 line
2.20.1
author maintainer
#9 lore raw

PATCH 6/9 netfilter: x_tables: gpf inside xt_find_revision()

In reply to Pablo Neira Ayuso

From: Vasily Averin <[email protected]>

nested target/match_revfn() calls work with xt[NFPROTO_UNSPEC] lists without taking xt[NFPROTO_UNSPEC].mutex. This can race with module unload and cause host to crash:

general protection fault: 0000 [#1]
Modules linked in: ... [last unloaded: xt_cluster]
CPU: 0 PID: 542455 Comm: iptables
RIP: 0010:[<ffffffff8ffbd518>]  [<ffffffff8ffbd518>] strcmp+0x18/0x40
RDX: 0000000000000003 RSI: ffff9a5a5d9abe10 RDI: dead000000000111
R13: ffff9a5a5d9abe10 R14: ffff9a5a5d9abd8c R15: dead000000000100
(VvS: %R15 -- &xt_match,  %RDI -- &xt_match.name,
xt_cluster unregister match in xt[NFPROTO_UNSPEC].match list)
Call Trace:
 [<ffffffff902ccf44>] match_revfn+0x54/0xc0
 [<ffffffff902ccf9f>] match_revfn+0xaf/0xc0
 [<ffffffff902cd01e>] xt_find_revision+0x6e/0xf0
 [<ffffffffc05a5be0>] do_ipt_get_ctl+0x100/0x420 [ip_tables]
 [<ffffffff902cc6bf>] nf_getsockopt+0x4f/0x70
 [<ffffffff902dd99e>] ip_getsockopt+0xde/0x100
 [<ffffffff903039b5>] raw_getsockopt+0x25/0x50
 [<ffffffff9026c5da>] sock_common_getsockopt+0x1a/0x20
 [<ffffffff9026b89d>] SyS_getsockopt+0x7d/0xf0
 [<ffffffff903cbf92>] system_call_fastpath+0x25/0x2a
Fixes 656caff20e1 ("netfilter 04/09: x_tables: fix match/target revision lookup")
---
 net/netfilter/x_tables.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/netfilter/x_tables.c +4 -2
index acce622582e3..bce6ca203d46 100644
@@ -330,6 +330,7 @@ static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
 	const struct xt_match *m;
 	int have_rev = 0;
 
+	mutex_lock(&xt[af].mutex);
 	list_for_each_entry(m, &xt[af].match, list) {
 		if (strcmp(m->name, name) == 0) {
 			if (m->revision > *bestp)
@@ -338,6 +339,7 @@ static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
 				have_rev = 1;
 		}
 	}
+	mutex_unlock(&xt[af].mutex);
 
 	if (af != NFPROTO_UNSPEC && !have_rev)
 		return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
@@ -350,6 +352,7 @@ static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
 	const struct xt_target *t;
 	int have_rev = 0;
 
+	mutex_lock(&xt[af].mutex);
 	list_for_each_entry(t, &xt[af].target, list) {
 		if (strcmp(t->name, name) == 0) {
 			if (t->revision > *bestp)
@@ -358,6 +361,7 @@ static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
 				have_rev = 1;
 		}
 	}
+	mutex_unlock(&xt[af].mutex);
 
 	if (af != NFPROTO_UNSPEC && !have_rev)
 		return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
@@ -371,12 +375,10 @@ int xt_find_revision(u8 af, const char *name, u8 revision, int target,
 {
 	int have_rev, best = -1;
 
-	mutex_lock(&xt[af].mutex);
 	if (target == 1)
 		have_rev = target_revfn(af, name, revision, &best);
 	else
 		have_rev = match_revfn(af, name, revision, &best);
-	mutex_unlock(&xt[af].mutex);
 
 	/* Nothing at all?  Return 0 to try loading module. */
 	if (best == -1) {
Signature · 1 line
2.20.1
author maintainer
#10 lore raw

PATCH 7/9 netfilter: nftables: disallow updates on table ownership

In reply to Pablo Neira Ayuso

Disallow updating the ownership bit on an existing table: Do not allow to grab ownership on an existing table. Do not allow to drop ownership on an existing table.

Fixes 6001a930ce03 ("netfilter: nftables: introduce table ownership")
---
 net/netfilter/nf_tables_api.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/netfilter/nf_tables_api.c +6 -0
index c1eb5cdb3033..b07703e19108 100644
@@ -916,6 +916,12 @@ static int nf_tables_updtable(struct nft_ctx *ctx)
 	if (flags == ctx->table->flags)
 		return 0;
 
+	if ((nft_table_has_owner(ctx->table) &&
+	     !(flags & NFT_TABLE_F_OWNER)) ||
+	    (!nft_table_has_owner(ctx->table) &&
+	     flags & NFT_TABLE_F_OWNER))
+		return -EOPNOTSUPP;
+
 	trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
 				sizeof(struct nft_trans_table));
 	if (trans == NULL)
Signature · 1 line
2.20.1
author maintainer
#11 lore raw

PATCH 8/9 netfilter: nftables: fix possible double hook unregistration with table owner

In reply to Pablo Neira Ayuso

Skip hook unregistration of owner tables from the netns exit path, nft_rcv_nl_event() unregisters the table hooks before tearing down the table content.

Fixes 6001a930ce03 ("netfilter: nftables: introduce table ownership")
---
 net/netfilter/nf_tables_api.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +5 -1
index b07703e19108..796ce86ef7eb 100644
@@ -9028,8 +9028,12 @@ static void __nft_release_hooks(struct net *net)
 {
 	struct nft_table *table;
 
-	list_for_each_entry(table, &net->nft.tables, list)
+	list_for_each_entry(table, &net->nft.tables, list) {
+		if (nft_table_has_owner(table))
+			continue;
+
 		__nft_release_hook(net, table);
+	}
 }
 
 static void __nft_release_table(struct net *net, struct nft_table *table)
Signature · 1 line
2.20.1
author maintainer
#12 lore raw

PATCH 9/9 netfilter: nftables: bogus check for netlink portID with table owner

In reply to Pablo Neira Ayuso

The existing branch checks for 0 != table->nlpid which always evaluates true for tables that have an owner.

Fixes 6001a930ce03 ("netfilter: nftables: introduce table ownership")
---
 net/netfilter/nf_tables_api.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
net/netfilter/nf_tables_api.c +3 -4
index 796ce86ef7eb..224c8e537cb3 100644
@@ -9083,13 +9083,12 @@ static void __nft_release_table(struct net *net, struct nft_table *table)
 	nf_tables_table_destroy(&ctx);
 }
 
-static void __nft_release_tables(struct net *net, u32 nlpid)
+static void __nft_release_tables(struct net *net)
 {
 	struct nft_table *table, *nt;
 
 	list_for_each_entry_safe(table, nt, &net->nft.tables, list) {
-		if (nft_table_has_owner(table) &&
-		    nlpid != table->nlpid)
+		if (nft_table_has_owner(table))
 			continue;
 
 		__nft_release_table(net, table);
@@ -9155,7 +9154,7 @@ static void __net_exit nf_tables_exit_net(struct net *net)
 	mutex_lock(&net->nft.commit_mutex);
 	if (!list_empty(&net->nft.commit_list))
 		__nf_tables_abort(net, NFNL_ABORT_NONE);
-	__nft_release_tables(net, 0);
+	__nft_release_tables(net);
 	mutex_unlock(&net->nft.commit_mutex);
 	WARN_ON_ONCE(!list_empty(&net->nft.tables));
 	WARN_ON_ONCE(!list_empty(&net->nft.module_list));
Signature · 1 line
2.20.1