Skip to contentSkip to conversation

netfilter: check if the socket netns is correct.

PATCH Applied Guenter Roeck 3 participants 4 messages Sep 27, 2018 – Sep 28, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hi Flavio,

Quoted 12 lines · Flavio Leitner

Netfilter assumes that if the socket is present in the skb, then it can be used because that reference is cleaned up while the skb is crossing netns.

We want to change that to preserve the socket reference in a future patch, so this is a preparation updating netfilter to check if the socket netns matches before use it.

---

...

net/netfilter/xt_socket.c socket_match(const struct sk_buff *skb, struct xt_action_param *par, · line 61

Quoted 9 lines
net/netfilter/xt_socket.c +3 -0
@@ -56,8 +56,12 @@ socket_match(const struct sk_buff *skb, struct xt_action_param *par,
 	struct sk_buff *pskb = (struct sk_buff *)skb;
 	struct sock *sk = skb->sk;
 
+	if (!net_eq(xt_net(par), sock_net(sk)))
+		sk = NULL;
+

I am having trouble with this code. With CONFIG_NET_NS enabled, it crashes for me in read_pnet() because sk is NULL.

net/netfilter/xt_socket.c socket_match(const struct sk_buff *skb, struct xt_action_param *par, · line 63

Quoted 2 lines
net/netfilter/xt_socket.c +0 -0
 	if (!sk)
 		sk = nf_sk_lookup_slow_v4(xt_net(par), skb, xt_in(par));

The old code seems to suggest that sk == NULL was possible.

I see the problem with the Chrome OS kernel rebased to v4.19-rc5, so I can not guarantee that this really an upstream problem. The change seems odd, though. Are you sure that it is not (or, rather, no longer) necessary to check if sk == NULL before dereferencing it in sock_net() ?

net/netfilter/xt_socket.c socket_mt6_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par) · line 122

Quoted 11 lines
net/netfilter/xt_socket.c +4 -0
+
 	if (sk) {
 		bool wildcard;
 		bool transparent = true;
@@ -113,8 +117,12 @@ socket_mt6_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)
 	struct sk_buff *pskb = (struct sk_buff *)skb;
 	struct sock *sk = skb->sk;
 
+	if (!net_eq(xt_net(par), sock_net(sk)))
+		sk = NULL;
+

Same here.

net/netfilter/xt_socket.c socket_mt6_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par) · line 128

Quoted 6 lines
net/netfilter/xt_socket.c +1 -0
 	if (!sk)
 		sk = nf_sk_lookup_slow_v6(xt_net(par), skb, xt_in(par));
+
 	if (sk) {
 		bool wildcard;
 		bool transparent = true;

Thanks,
Guenter

#1 lore raw
Quoted 32 lines · Guenter Roeck

Hi Flavio,

Quoted 12 lines · Flavio Leitner

Netfilter assumes that if the socket is present in the skb, then it can be used because that reference is cleaned up while the skb is crossing netns.

We want to change that to preserve the socket reference in a future patch, so this is a preparation updating netfilter to check if the socket netns matches before use it.

---

...

net/netfilter/xt_socket.c socket_match(const struct sk_buff *skb, struct xt_action_param *par, · line 61

Quoted 9 lines
net/netfilter/xt_socket.c +3 -0
@@ -56,8 +56,12 @@ socket_match(const struct sk_buff *skb, struct xt_action_param *par,
 	struct sk_buff *pskb = (struct sk_buff *)skb;
 	struct sock *sk = skb->sk;
 
+	if (!net_eq(xt_net(par), sock_net(sk)))
+		sk = NULL;
+

I am having trouble with this code. With CONFIG_NET_NS enabled, it crashes for me in read_pnet() because sk is NULL.

net/netfilter/xt_socket.c socket_match(const struct sk_buff *skb, struct xt_action_param *par, · line 63

Quoted 2 lines
net/netfilter/xt_socket.c +0 -0
 	if (!sk)
 		sk = nf_sk_lookup_slow_v4(xt_net(par), skb, xt_in(par));
Quoted 6 lines

The old code seems to suggest that sk == NULL was possible.

I see the problem with the Chrome OS kernel rebased to v4.19-rc5, so I can not guarantee that this really an upstream problem. The change seems odd, though. Are you sure that it is not (or, rather, no longer) necessary to check if sk == NULL before dereferencing it in sock_net() ?

Oops, it is necessary but if it's not and the netns doesn't match, we need do the lookup. So, could you check if this fixes the problem for you?

Quoted 1 line

From a5f927e7f1368d753f87cb978d630d786d5adb62 Mon Sep 17 00:00:00 2001

From: Flavio Leitner <[email protected]>
Date: Thu, 27 Sep 2018 19:36:28 -0300
Subject: [PATCH] xt_socket: check sk before checking for netns.

Only check for the network namespace if the socket is available.

Fixes f564650106a6 ("netfilter: check if the socket netns is correct.")
---
 net/netfilter/xt_socket.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/xt_socket.c +2 -2
index 0472f3472842..ada144e5645b 100644
@@ -56,7 +56,7 @@ socket_match(const struct sk_buff *skb, struct xt_action_param *par,
 	struct sk_buff *pskb = (struct sk_buff *)skb;
 	struct sock *sk = skb->sk;
 
-	if (!net_eq(xt_net(par), sock_net(sk)))
+	if (sk && !net_eq(xt_net(par), sock_net(sk)))
 		sk = NULL;
 
 	if (!sk)
@@ -117,7 +117,7 @@ socket_mt6_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)
 	struct sk_buff *pskb = (struct sk_buff *)skb;
 	struct sock *sk = skb->sk;
 
-	if (!net_eq(xt_net(par), sock_net(sk)))
+	if (sk && !net_eq(xt_net(par), sock_net(sk)))
 		sk = NULL;
 
 	if (!sk)
Signature · 1 line
2.14.4
author Tested-by
#2 lore raw
Quoted 48 lines · Flavio Leitner
Quoted 32 lines · Guenter Roeck

Hi Flavio,

Quoted 12 lines · Flavio Leitner

Netfilter assumes that if the socket is present in the skb, then it can be used because that reference is cleaned up while the skb is crossing netns.

We want to change that to preserve the socket reference in a future patch, so this is a preparation updating netfilter to check if the socket netns matches before use it.

---

...

net/netfilter/xt_socket.c socket_match(const struct sk_buff *skb, struct xt_action_param *par, · line 61

Quoted 9 lines
net/netfilter/xt_socket.c +3 -0
@@ -56,8 +56,12 @@ socket_match(const struct sk_buff *skb, struct xt_action_param *par,
 	struct sk_buff *pskb = (struct sk_buff *)skb;
 	struct sock *sk = skb->sk;
 
+	if (!net_eq(xt_net(par), sock_net(sk)))
+		sk = NULL;
+

I am having trouble with this code. With CONFIG_NET_NS enabled, it crashes for me in read_pnet() because sk is NULL.

net/netfilter/xt_socket.c socket_match(const struct sk_buff *skb, struct xt_action_param *par, · line 63

Quoted 2 lines
net/netfilter/xt_socket.c +0 -0
 	if (!sk)
 		sk = nf_sk_lookup_slow_v4(xt_net(par), skb, xt_in(par));
Quoted 6 lines

The old code seems to suggest that sk == NULL was possible.

I see the problem with the Chrome OS kernel rebased to v4.19-rc5, so I can not guarantee that this really an upstream problem. The change seems odd, though. Are you sure that it is not (or, rather, no longer) necessary to check if sk == NULL before dereferencing it in sock_net() ?

Oops, it is necessary but if it's not and the netns doesn't match, we need do the lookup. So, could you check if this fixes the problem for you?

From a5f927e7f1368d753f87cb978d630d786d5adb62 Mon Sep 17 00:00:00 2001
From: Flavio Leitner <[email protected]>
Date: Thu, 27 Sep 2018 19:36:28 -0300
Subject: [PATCH] xt_socket: check sk before checking for netns.

Quoted 5 lines

Only check for the network namespace if the socket is available.

Fixes f564650106a6 ("netfilter: check if the socket netns is correct.")

This fixes the problem for me.

Thanks,
Guenter

Quoted 29 lines
---
 net/netfilter/xt_socket.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/xt_socket.c +2 -2
index 0472f3472842..ada144e5645b 100644
@@ -56,7 +56,7 @@ socket_match(const struct sk_buff *skb, struct xt_action_param *par,
 	struct sk_buff *pskb = (struct sk_buff *)skb;
 	struct sock *sk = skb->sk;
 
-	if (!net_eq(xt_net(par), sock_net(sk)))
+	if (sk && !net_eq(xt_net(par), sock_net(sk)))
 		sk = NULL;
 
 	if (!sk)
@@ -117,7 +117,7 @@ socket_mt6_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)
 	struct sk_buff *pskb = (struct sk_buff *)skb;
 	struct sock *sk = skb->sk;
 
-	if (!net_eq(xt_net(par), sock_net(sk)))
+	if (sk && !net_eq(xt_net(par), sock_net(sk)))
 		sk = NULL;
 
 	if (!sk)
Signature · 1 line
2.14.4
maintainer Applied
#3 lore raw
In reply to Flavio Leitner

On Thu, Sep 27, 2018 at 07:58:24PM -0300, Flavio Leitner wrote:
[...]

Quoted 6 lines

From a5f927e7f1368d753f87cb978d630d786d5adb62 Mon Sep 17 00:00:00 2001
From: Flavio Leitner <[email protected]>
Date: Thu, 27 Sep 2018 19:36:28 -0300
Subject: [PATCH] xt_socket: check sk before checking for netns.

Only check for the network namespace if the socket is available.

Applied.

Please, Cc [email protected] next time for your netfilter patches.

Thanks.