Skip to contentSkip to conversation

Forward-ports from mptcp_trunk

PATCH In review Christoph Paasch 3 participants 6 messages Aug 20, 2018 – Aug 21, 2018

Subsystems (3)
lore
Share & tools
lore

Christoph Paasch

author
#0 lore raw

cover Forward-ports from mptcp_trunk

Two forward-ports coming from mptcp_trunk.

Gregory Detal (1):
mptcp: correct refcnt for sk in tcp_v{4,6}_rcv

Matthieu Baerts (1):
mptcp:minisocks: fix refcnt when adding to backlog

 include/net/mptcp.h      |  2 ++
 net/ipv4/tcp_ipv4.c      |  2 +-
 net/ipv4/tcp_minisocks.c |  2 +-
 net/ipv6/tcp_ipv6.c      |  2 +-
 net/mptcp/mptcp_ctrl.c   | 18 ++++++++++++++++--
 5 files changed, 21 insertions(+), 5 deletions(-)
Signature · 1 line
2.16.2
author
#1 lore raw

PATCH 1/2 mptcp: correct refcnt for sk in tcp_v{4, 6}_rcv

From: Gregory Detal <gregory.detal(a)tessares.net>

When we receive an skb in tcp_vX_rcv if the sock is hold by user we copy the sk inside the skb before putting the skb inside the backlog, the sk will be used after from mptcp_backlog_rcv. If the sk refcnt is not done properly the sk read from mptcp_backlog_rcv could point to another sk because it may have been freed and reused in the mean time.

In mptcp_backlog_rcv, the call to skb_oprhan will call the destructor that we set for the skb and decrement the sk refcnt. Note that we call skb_oprhan after the increase of sk_refcnt. This ensures that we never free the sk too quickly.

Adaptation of commit 952bd28cc5be (mptcp: correct refcnt for sk in tcp_v{4,6}_rcv) for mptcp_trunk.

Fixes Zero-day-bug
Signed-off-by Gregory Detal <gregory.detal(a)tessares.net>
Signed-off-by Benjamin Hesmans <benjamin.hesmans(a)tessares.net>
Signed-off-by Matthieu Baerts <matthieu.baerts(a)tessares.net>
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
(cherry picked from commit 2e4ce385ada1bd7c3b91145dcd67b95968fe7c3a)
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
 include/net/mptcp.h    |  2 ++
 net/ipv4/tcp_ipv4.c    |  2 +-
 net/ipv6/tcp_ipv6.c    |  2 +-
 net/mptcp/mptcp_ctrl.c | 18 ++++++++++++++++--
 4 files changed, 20 insertions(+), 4 deletions(-)
include/net/mptcp.h +2 -0
index 2b5418d808bf..62266a6e0223 100644
@@ -831,6 +831,7 @@ void mptcp_sub_close_wq(struct work_struct *work);
 void mptcp_sub_close(struct sock *sk, unsigned long delay);
 struct sock *mptcp_select_ack_sock(const struct sock *meta_sk);
 void mptcp_fallback_meta_sk(struct sock *meta_sk);
+void mptcp_prepare_for_backlog(struct sock *sk, struct sk_buff *skb);
 int mptcp_backlog_rcv(struct sock *meta_sk, struct sk_buff *skb);
 void mptcp_ack_handler(struct timer_list *t);
 bool mptcp_check_rtt(const struct tcp_sock *tp, int time);
@@ -1408,6 +1409,7 @@ static inline bool mptcp_fallback_infinite(const struct sock *sk, int flag)
 	return false;
 }
 static inline void mptcp_init_mp_opt(const struct mptcp_options_received *mopt) {}
+static inline void mptcp_prepare_for_backlog(struct sock *sk, struct sk_buff *skb) {}
 static inline bool mptcp_check_rtt(const struct tcp_sock *tp, int time)
 {
 	return false;
net/ipv4/tcp_ipv4.c +1 -1
index 12c577d170f3..e6b1e256eb12 100644
@@ -1906,7 +1906,7 @@ int tcp_v4_rcv(struct sk_buff *skb)
 
 		bh_lock_sock_nested(meta_sk);
 		if (sock_owned_by_user(meta_sk))
-			skb->sk = sk;
+			mptcp_prepare_for_backlog(sk, skb);
 	} else {
 		meta_sk = sk;
 		bh_lock_sock_nested(sk);
net/ipv6/tcp_ipv6.c +1 -1
index 95614a2f6e25..b051b60e345a 100644
@@ -1650,7 +1650,7 @@ static int tcp_v6_rcv(struct sk_buff *skb)
 
 		bh_lock_sock_nested(meta_sk);
 		if (sock_owned_by_user(meta_sk))
-			skb->sk = sk;
+			mptcp_prepare_for_backlog(sk, skb);
 	} else {
 		meta_sk = sk;
 		bh_lock_sock_nested(sk);
net/mptcp/mptcp_ctrl.c +16 -2
index f5d46c688279..1e7b38911083 100644
@@ -984,6 +984,16 @@ static void mptcp_sub_inherit_sockopts(const struct sock *meta_sk, struct sock *
 	inet_sk(sub_sk)->recverr = 0;
 }
 
+void mptcp_prepare_for_backlog(struct sock *sk, struct sk_buff *skb)
+{
+	/* In case of success (in mptcp_backlog_rcv) and error (in kfree_skb) of
+	 * sk_add_backlog, we will decrement the sk refcount.
+	 */
+	sock_hold(sk);
+	skb->sk = sk;
+	skb->destructor = sock_efree;
+}
+
 int mptcp_backlog_rcv(struct sock *meta_sk, struct sk_buff *skb)
 {
 	/* skb-sk may be NULL if we receive a packet immediatly after the
@@ -992,13 +1002,17 @@ int mptcp_backlog_rcv(struct sock *meta_sk, struct sk_buff *skb)
 	struct sock *sk = skb->sk ? skb->sk : meta_sk;
 	int ret = 0;
 
-	skb->sk = NULL;
-
 	if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt))) {
 		kfree_skb(skb);
 		return 0;
 	}
 
+	/* Decrement sk refcnt when calling the skb destructor.
+	 * Refcnt is incremented and skb destructor is set in tcp_v{4,6}_rcv via
+	 * mptcp_prepare_for_backlog() here above.
+	 */
+	skb_orphan(skb);
+
 	if (sk->sk_family == AF_INET)
 		ret = tcp_v4_do_rcv(sk, skb);
 #if IS_ENABLED(CONFIG_IPV6)
Signature · 1 line
2.16.2
author
#2 lore raw

PATCH 2/2 mptcp:minisocks: fix refcnt when adding to backlog

In reply to Christoph Paasch

From: Matthieu Baerts <matthieu.baerts(a)tessares.net>

It is the same as what we did in the parent commit: if the sock is hold by user we copy the sk inside the skb before putting the skb inside the backlog, the sk will be used after from mptcp_backlog_rcv. If the sk refcnt is not done properly the sk read from mptcp_backlog_rcv could point to another sk because it may have been freed and reused in the mean time.

It seems to be very unlikely to arrive in this situation but in theory it is.

Fixes 952bd28cc5be (mptcp: correct refcnt for sk in tcp_v{4,6}_rcv)
CC Gregory Detal <gregory.detal(a)tessares.net>
CC Benjamin Hesmans <benjamin.hesmans(a)tessares.net>
Signed-off-by Matthieu Baerts <matthieu.baerts(a)tessares.net>
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
(cherry picked from commit 98209f874fd93e95fd73e03e68be93ee157306dd)
Signed-off-by Christoph Paasch <cpaasch(a)apple.com>
---
 net/ipv4/tcp_minisocks.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/tcp_minisocks.c +1 -1
index fafa4479d45f..781c68848f6e 100644
@@ -913,7 +913,7 @@ int tcp_child_process(struct sock *parent, struct sock *child,
 		 * socket does not protect us more.
 		 */
 		if (mptcp(tcp_sk(child)))
-			skb->sk = child;
+			mptcp_prepare_for_backlog(child, skb);
 		__sk_add_backlog(meta_sk, skb);
 	}
 
Signature · 1 line
2.16.2
#3 lore raw
In reply to Christoph Paasch
Quoted 8 lines · Christoph Paasch

Two forward-ports coming from mptcp_trunk.

Gregory Detal (1):
mptcp: correct refcnt for sk in tcp_v{4,6}_rcv

Matthieu Baerts (1):
mptcp:minisocks: fix refcnt when adding to backlog

Quoted 6 lines
include/net/mptcp.h      |  2 ++
net/ipv4/tcp_ipv4.c      |  2 +-
net/ipv4/tcp_minisocks.c |  2 +-
net/ipv6/tcp_ipv6.c      |  2 +-
net/mptcp/mptcp_ctrl.c   | 18 ++++++++++++++++--
5 files changed, 21 insertions(+), 5 deletions(-)

Both applied.

Is
https://github.com/multipath-tcp/mptcp_net-next/blob/master/net/ipv4/tcp_ipv4.c#L1822
another place that mptcp_prepare_for_backlog() should be used?

--
Mat Martineau
Intel OTC

author
#4 lore raw
Quoted 11 lines · Mat Martineau
Quoted 8 lines · Christoph Paasch

Two forward-ports coming from mptcp_trunk.

Gregory Detal (1):
mptcp: correct refcnt for sk in tcp_v{4,6}_rcv

Matthieu Baerts (1):
mptcp:minisocks: fix refcnt when adding to backlog

Quoted 8 lines
Quoted 6 lines
include/net/mptcp.h      |  2 ++
net/ipv4/tcp_ipv4.c      |  2 +-
net/ipv4/tcp_minisocks.c |  2 +-
net/ipv6/tcp_ipv6.c      |  2 +-
net/mptcp/mptcp_ctrl.c   | 18 ++++++++++++++++--
5 files changed, 21 insertions(+), 5 deletions(-)

Both applied.

Cool!

Quoted 3 lines

Is https://github.com/multipath-tcp/mptcp_net-next/blob/master/net/ipv4/tcp_ipv4.c#L1822
another place that mptcp_prepare_for_backlog() should be used?

In principle yes, but we didn't put this in mptcp_trunk, because this has already the lockless subflow establishment patches that I still need to forward-port to the net-next branch.

Christoph

#5 lore raw
In reply to Christoph Paasch

Hello,

Quoted 17 lines · Mat Martineau
Quoted 8 lines · Christoph Paasch

Two forward-ports coming from mptcp_trunk.

Gregory Detal (1):
mptcp: correct refcnt for sk in tcp_v{4,6}_rcv

Matthieu Baerts (1):
mptcp:minisocks: fix refcnt when adding to backlog

Quoted 6 lines

include/net/mptcp.h | 2 ++
net/ipv4/tcp_ipv4.c | 2 +-
net/ipv4/tcp_minisocks.c | 2 +-
net/ipv6/tcp_ipv6.c | 2 +-
net/mptcp/mptcp_ctrl.c | 18 ++++++++++++++++--

5 files changed, 21 insertions(+), 5 deletions(-) 
Quoted 5 lines

Both applied.

Is
https://github.com/multipath-tcp/mptcp_net-next/blob/master/net/ipv4/tcp_ipv4.c#L1822
another place that mptcp_prepare_for_backlog() should be used?

Very good point. This part of the code is not in mptcp_trunk. Is it normal? Maybe some commits are missing?

https://github.com/multipath-tcp/mptcp/blob/2e4ce385ada1bd7c3b91145dcd67b95968fe7c3a/net/ipv4/tcp_ipv4.c#L1750