Skip to contentSkip to conversation

mptcp: fix splats and crashes

PATCH In review Florian Westphal 2 participants 11 messages Apr 15, 2020 – Apr 16, 2020

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author
#0 lore raw

cover mptcp: fix splats and crashes

This series resolves a number of crashes in MPTCP.
I'm not 100% sure yet that I managed to fix all of them (I will run the selftest again over night), but I wanted to send it out anyway because its unlikely that any of the fixes is flat out wrong. If anything, there is a different/better solution that I missed so far or another fix needs to be placed on top of this pile.

Florian Westphal (7):
mptcp: fix splat when incoming connection is never accepted before exit/close
mptcp: fix 'Attempt to release TCP socket in state' warnings
mptcp: handle mptcp listener destruction via rcu
mptcp: avoid callback invocation when mptcp parent socket doesn't exist
mptcp: use rcu helpers to fetch ulp subflow context
mptcp: reverse order of sk_state_change and is_mptcp check
mptcp: prevent null deref crash on normal tcp sockets

 protocol.c |   29 +++++++++++++----
 protocol.h |   14 +-------
 subflow.c  |  103 +++++++++++++++++++++++++++++++++++++++++++++++--------------
 3 files changed, 105 insertions(+), 41 deletions(-)
author
#1 lore raw

PATCH 1/7 mptcp: fix splat when incoming connection is never accepted before exit/close

Following tool (replicated from syzkaller generated code) generates
warning: "IPv4: Attempt to release TCP socket in state 1".

int main(void) {
        struct sockaddr_in sin1 = { .sin_family = 2, .sin_port = 0x4e20, .sin_addr.s_addr = 0x010000e0, };
        struct sockaddr_in sin2 = { .sin_family = 2, .sin_port = 0x0, .sin_addr.s_addr = 0x0100007f, };
        struct sockaddr_in sin3 = { .sin_family = 2, .sin_port = 0x4e20, .sin_addr.s_addr = 0x0100007f, };
        int r0 = socket(0x2, 0x1, 0x106);
        int r1 = socket(0x2, 0x1, 0x106);

        bind(r1, (void *)&sin1, sizeof(sin1));
        connect(r1, (void *)&sin2, sizeof(sin2));
        listen(r1, 3);
        return connect(r0, (void *)&sin3, 0x4d);
}

Reason is that the newly generated mptcp socket is closed via the ulp release of the tcp socket, via the listeners accept backlog purge.
To fix this, delay setting the ESTABLISHED state until after userspace calls accept and via mptcp specific destructor.

Fixes 58b09919626bf ("mptcp: create msk early")
Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c |  1 +
 net/mptcp/subflow.c  | 25 ++++++++++++++++++++++++-
 2 files changed, 25 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +1 -0
index 457655ea875b..2118b51afd60 100644
@@ -1440,6 +1440,7 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 		newsk = new_mptcp_sock;
 		mptcp_copy_inaddrs(newsk, ssk);
 		list_add(&subflow->node, &msk->conn_list);
+		inet_sk_state_store(newsk, TCP_ESTABLISHED);
 
 		bh_unlock_sock(new_mptcp_sock);
 
net/mptcp/subflow.c +24 -1
index 47f901b712f9..936f748075a7 100644
@@ -347,6 +347,29 @@ static bool subflow_hmac_valid(const struct request_sock *req,
 	return ret;
 }
 
+static void mptcp_sock_destruct(struct sock *sk)
+{
+	/* if new mptcp socket isn't accepted, it is free'd
+	 * from the tcp listener sockets request queue, linked
+	 * from req->sk.  The tcp socket is released.
+	 * This calls the ULP release function which will
+	 * also remove the mptcp socket, via
+	 * sock_put(ctx->conn).
+	 *
+	 * Problem is that the mptcp socket will not be in
+	 * SYN_RECV state and doesn't have SOCK_DEAD flag.
+	 * Both result in warnings from inet_sock_destruct.
+	 */
+
+	if (sk->sk_state == TCP_SYN_RECV) {
+		sk->sk_state = TCP_CLOSE;
+		WARN_ON_ONCE(sk->sk_socket);
+		sock_orphan(sk);
+	}
+
+	inet_sock_destruct(sk);
+}
+
 static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 					  struct sk_buff *skb,
 					  struct request_sock *req,
@@ -422,7 +445,7 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 			/* new mpc subflow takes ownership of the newly
 			 * created mptcp socket
 			 */
-			inet_sk_state_store(new_msk, TCP_ESTABLISHED);
+			new_msk->sk_destruct = mptcp_sock_destruct;
 			mptcp_pm_new_connection(mptcp_sk(new_msk), 1);
 			ctx->conn = new_msk;
 			new_msk = NULL;
Signature · 1 line
2.24.1
author
#2 lore raw

PATCH 2/7 mptcp: fix 'Attempt to release TCP socket in state' warnings

In reply to Florian Westphal

We need to set sk_state to CLOSED, else we will get following:

IPv4: Attempt to release TCP socket in state 3 00000000b95f109e
IPv4: Attempt to release TCP socket in state 10 00000000b95f109e

First one is from inet_sock_destruct(), second one from mptcp_sk_clone failure handling. Setting sk_state to CLOSED isn't enough, we also need to orphan sk so it has DEAD flag set. Otherwise, a very similar warning is printed from inet_sock_destruct().

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c | 7 +++++--
 net/mptcp/subflow.c  | 8 +++++++-
 2 files changed, 12 insertions(+), 3 deletions(-)
net/mptcp/protocol.c +5 -2
index 2118b51afd60..2bdfe2ea35bb 100644
@@ -1364,12 +1364,15 @@ struct sock *mptcp_sk_clone(const struct sock *sk, struct request_sock *req)
 	msk->subflow = NULL;
 
 	if (unlikely(mptcp_token_new_accept(subflow_req->token, nsk))) {
+		nsk->sk_state = TCP_CLOSE;
 		bh_unlock_sock(nsk);
 
 		/* we can't call into mptcp_close() here - possible BH context
-		 * free the sock directly
+		 * free the sock directly.
+		 * sk_clone_lock() sets nsk refcnt to two, hence call sk_free()
+		 * too.
 		 */
-		nsk->sk_prot->destroy(nsk);
+		sk_common_release(nsk);
 		sk_free(nsk);
 		return NULL;
 	}
net/mptcp/subflow.c +7 -1
index 936f748075a7..3a94f859347a 100644
@@ -370,6 +370,12 @@ static void mptcp_sock_destruct(struct sock *sk)
 	inet_sock_destruct(sk);
 }
 
+static void mptcp_force_close(struct sock *sk)
+{
+	inet_sk_state_store(sk, TCP_CLOSE);
+	sk_common_release(sk);
+}
+
 static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 					  struct sk_buff *skb,
 					  struct request_sock *req,
@@ -467,7 +473,7 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 out:
 	/* dispose of the left over mptcp master, if any */
 	if (unlikely(new_msk))
-		sock_put(new_msk);
+		mptcp_force_close(new_msk);
 	return child;
 
 close_child:
Signature · 1 line
2.24.1
author
#3 lore raw

PATCH 3/7 mptcp: handle mptcp listener destruction via rcu

In reply to Florian Westphal

Following splat can occur during self test:

 BUG: KASAN: use-after-free in subflow_data_ready+0x156/0x160
 Read of size 8 at addr ffff888100c35c28 by task mptcp_connect/4808

  subflow_data_ready+0x156/0x160
  tcp_child_process+0x6a3/0xb30
  tcp_v4_rcv+0x2231/0x3730
  ip_protocol_deliver_rcu+0x5c/0x860
  ip_local_deliver_finish+0x220/0x360
  ip_local_deliver+0x1c8/0x4e0
  ip_rcv_finish+0x1da/0x2f0
  ip_rcv+0xd0/0x3c0
  __netif_receive_skb_one_core+0xf5/0x160
  __netif_receive_skb+0x27/0x1c0
  process_backlog+0x21e/0x780
  net_rx_action+0x35f/0xe90
  do_softirq+0x4c/0x50
  [..]

This occurs when accessing subflow_ctx->conn.

Problem is that tcp_child_process() calls listen sockets' sk_data_ready() notification, but it doesn't hold the listener lock. Another cpu calling close() on the listener will then cause transition of refcount to 0.

Fixes 58b09919626bf ("mptcp: create msk early")
Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c | 3 +++
 1 file changed, 3 insertions(+)
net/mptcp/protocol.c +3 -0
index 2bdfe2ea35bb..955b3d99bc21 100644
@@ -1387,6 +1387,7 @@ struct sock *mptcp_sk_clone(const struct sock *sk, struct request_sock *req)
 		msk->ack_seq = ack_seq;
 	}
 
+	sock_reset_flag(nsk, SOCK_RCU_FREE);
 	/* will be fully established after successful MPC subflow creation */
 	inet_sk_state_store(nsk, TCP_SYN_RECV);
 	bh_unlock_sock(nsk);
@@ -1788,6 +1789,8 @@ static int mptcp_listen(struct socket *sock, int backlog)
 		goto unlock;
 	}
 
+	sock_set_flag(sock->sk, SOCK_RCU_FREE);
+
 	err = ssock->ops->listen(ssock, backlog);
 	inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
 	if (!err)
Signature · 1 line
2.24.1
author
#4 lore raw

PATCH 4/7 mptcp: avoid callback invocation when mptcp parent socket doesn't exist

In reply to Florian Westphal

We can crash with a NULL dereference in case a packet arrives on the new socket before ctx->conn has been initialized.

When the new socket is cloned, it inherits the subflow callbacks (sk_data_ready/state_change/write_space) from the listener socket.

To fix this, we either need to add ctx->conn == NULL checks in several places, or we need to reset the callbacks to the tcp callbacks until ctx->conn is initialized.

Do the same on close: The parent MPTCP socket is not subject to RCU freeing, so when ctx->conn reference is put, reset the sk callbacks for the affected subflow to detach it from the mptcp layer.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.h | 10 ----------
 net/mptcp/subflow.c  | 37 ++++++++++++++++++++++++++++++++++---
 2 files changed, 34 insertions(+), 13 deletions(-)
net/mptcp/protocol.h +0 -10
index 67448002a2d7..305e703e1fb3 100644
@@ -312,16 +312,6 @@ int __mptcp_subflow_connect(struct sock *sk, int ifindex,
 			    const struct mptcp_addr_info *remote);
 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock);
 
-static inline void mptcp_subflow_tcp_fallback(struct sock *sk,
-					      struct mptcp_subflow_context *ctx)
-{
-	sk->sk_data_ready = ctx->tcp_data_ready;
-	sk->sk_state_change = ctx->tcp_state_change;
-	sk->sk_write_space = ctx->tcp_write_space;
-
-	inet_csk(sk)->icsk_af_ops = ctx->icsk_af_ops;
-}
-
 extern const struct inet_connection_sock_af_ops ipv4_specific;
 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
 extern const struct inet_connection_sock_af_ops ipv6_specific;
net/mptcp/subflow.c +34 -3
index 3a94f859347a..0bc1f5dd9999 100644
@@ -22,6 +22,10 @@
 #include "protocol.h"
 #include "mib.h"
 
+static void subflow_state_change(struct sock *sk);
+static void subflow_write_space(struct sock *sk);
+static void subflow_data_ready(struct sock *sk);
+
 static void SUBFLOW_REQ_INC_STATS(struct request_sock *req,
 				  enum linux_mptcp_mib_field field)
 {
@@ -219,6 +223,29 @@ static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow)
 	return thmac == subflow->thmac;
 }
 
+static void mptcp_subflow_set_callbacks(struct sock *sk)
+{
+	smp_wmb();	/* callbacks rely on subflow->conn != NULL */
+	sk->sk_data_ready = subflow_data_ready;
+	sk->sk_write_space = subflow_write_space;
+	sk->sk_state_change = subflow_state_change;
+}
+
+static void mptcp_subflow_reset_callbacks(struct sock *sk,
+					  const struct mptcp_subflow_context *ctx)
+{
+	sk->sk_data_ready = ctx->tcp_data_ready;
+	sk->sk_state_change = ctx->tcp_state_change;
+	sk->sk_write_space = ctx->tcp_write_space;
+}
+
+static void mptcp_subflow_tcp_fallback(struct sock *sk,
+				       const struct mptcp_subflow_context *ctx)
+{
+	mptcp_subflow_reset_callbacks(sk, ctx);
+	inet_csk(sk)->icsk_af_ops = ctx->icsk_af_ops;
+}
+
 static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
 {
 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
@@ -454,6 +481,7 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 			new_msk->sk_destruct = mptcp_sock_destruct;
 			mptcp_pm_new_connection(mptcp_sk(new_msk), 1);
 			ctx->conn = new_msk;
+			mptcp_subflow_set_callbacks(child);
 			new_msk = NULL;
 		} else if (ctx->mp_join) {
 			struct mptcp_sock *owner;
@@ -463,6 +491,7 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 				goto close_child;
 
 			ctx->conn = (struct sock *)owner;
+			mptcp_subflow_set_callbacks(child);
 			if (!mptcp_finish_join(child))
 				goto close_child;
 
@@ -984,6 +1013,7 @@ int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock)
 	*new_sock = sf;
 	sock_hold(sk);
 	subflow->conn = sk;
+	mptcp_subflow_set_callbacks(sf->sk);
 
 	return 0;
 }
@@ -1074,9 +1104,6 @@ static int subflow_ulp_init(struct sock *sk)
 	ctx->tcp_data_ready = sk->sk_data_ready;
 	ctx->tcp_state_change = sk->sk_state_change;
 	ctx->tcp_write_space = sk->sk_write_space;
-	sk->sk_data_ready = subflow_data_ready;
-	sk->sk_write_space = subflow_write_space;
-	sk->sk_state_change = subflow_state_change;
 out:
 	return err;
 }
@@ -1088,6 +1115,7 @@ static void subflow_ulp_release(struct sock *sk)
 	if (!ctx)
 		return;
 
+	mptcp_subflow_tcp_fallback(sk, ctx);
 	if (ctx->conn)
 		sock_put(ctx->conn);
 
@@ -1125,6 +1153,9 @@ static void subflow_ulp_clone(const struct request_sock *req,
 		return;
 	}
 
+	/* don't call subflow callbacks before ctx->conn is set */
+	mptcp_subflow_reset_callbacks(newsk, old_ctx);
+
 	new_ctx->conn_finished = 1;
 	new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
 	new_ctx->tcp_data_ready = old_ctx->tcp_data_ready;
Signature · 1 line
2.24.1
#5 lore raw

[MPTCP] Re: [PATCH mptcp 4/7] mptcp: avoid callback invocation when mptcp parent socket doesn't exist

Quoted 2 lines · Florian Westphal

We can crash with a NULL dereference in case a packet arrives on the new socket before ctx->conn has been initialized.

I don't understand this race. I thought tcp_v4_syn_recv_sock() creates the new socket and acquires the socket lock atomically, and ctx->conn = new_msk happens in the same critical section ?!?

So no packets should reach the newly created socket while conn is NULL !?!

but I do see a similar crash even if ctx->conn is not NULL, is a low, invalid value.

I'll try to give this patch a spin in my testbed.

Thanks!

/P

author
#6 lore raw

[MPTCP] Re: [PATCH mptcp 4/7] mptcp: avoid callback invocation when mptcp parent socket doesn't exist

Quoted 10 lines · Paolo Abeni
Quoted 2 lines · Florian Westphal

We can crash with a NULL dereference in case a packet arrives on the new socket before ctx->conn has been initialized.

I don't understand this race. I thought tcp_v4_syn_recv_sock() creates the new socket and acquires the socket lock atomically, and ctx->conn = new_msk happens in the same critical section ?!?

So no packets should reach the newly created socket while conn is NULL !?!

Might be related to the last patch, i.e. this patch is bogus because the NULL was because of lack of is_mptcp = 0 assignment?

I will test again with this patch removed from the series.

sorry.

author
#7 lore raw

[MPTCP] Re: [PATCH mptcp 4/7] mptcp: avoid callback invocation when mptcp parent socket doesn't exist

Quoted 9 lines · Florian Westphal
Quoted 8 lines · Paolo Abeni
Quoted 2 lines · Florian Westphal

We can crash with a NULL dereference in case a packet arrives on the new socket before ctx->conn has been initialized.

I don't understand this race. I thought tcp_v4_syn_recv_sock() creates the new socket and acquires the socket lock atomically, and ctx->conn = new_msk happens in the same critical section ?!?

Quoted 7 lines
Quoted 2 lines

So no packets should reach the newly created socket while conn is NULL !?!

Might be related to the last patch, i.e. this patch is bogus because the NULL was because of lack of is_mptcp = 0 assignment?

I will test again with this patch removed from the series.

I powered off my VM after ~8h of continuos tests; but without this patch I get crash after ~30 minutes.

I think its related to the case where we return child with subflow but with ctx->conn == NULL.

I plan to mangle the last patch with this one and reset the sk callbacks in that case.

Alternative is to keep this patch as-is, squash in the last one and amdend the commit message.

author
#8 lore raw

PATCH 5/7 mptcp: use rcu helpers to fetch ulp subflow context

In reply to Florian Westphal

mptcp_sublow_ctx does:

    return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data;

One caller of the function is subflow_v4_conn_request.

In that case 'sk' is listener socket, but tcp does not hold the socket lock.

Using rcu_dereference_check triggers a few warnings where don't hold the socket lock, so fix up those places by adding either a rcu_read_lock or the sk lock.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c | 18 +++++++++++++-----
 net/mptcp/protocol.h |  4 ++--
 net/mptcp/subflow.c  | 21 ++++++++-------------
 3 files changed, 23 insertions(+), 20 deletions(-)
net/mptcp/protocol.c +13 -5
index 955b3d99bc21..761e258699b8 100644
@@ -117,8 +117,12 @@ static struct socket *__mptcp_socket_create(struct mptcp_sock *msk, int state)
 		return ssock;
 
 	ssock = __mptcp_nmpc_socket(msk);
-	if (ssock)
-		goto set_state;
+	if (ssock) {
+		if (state != MPTCP_SAME_STATE)
+			inet_sk_state_store(sk, state);
+
+		return ssock;
+	}
 
 	if (!__mptcp_can_create_subflow(msk))
 		return ERR_PTR(-EINVAL);
@@ -129,13 +133,15 @@ static struct socket *__mptcp_socket_create(struct mptcp_sock *msk, int state)
 
 	msk->first = ssock->sk;
 	msk->subflow = ssock;
+
+	lock_sock(ssock->sk);
 	subflow = mptcp_subflow_ctx(ssock->sk);
 	list_add(&subflow->node, &msk->conn_list);
 	subflow->request_mptcp = 1;
-
-set_state:
 	if (state != MPTCP_SAME_STATE)
 		inet_sk_state_store(sk, state);
+	release_sock(ssock->sk);
+
 	return ssock;
 }
 
@@ -1410,7 +1416,6 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 		return NULL;
 	}
 
-	pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
 	newsk = inet_csk_accept(listener->sk, flags, err, kern);
 	if (!newsk)
 		return NULL;
@@ -1422,6 +1427,7 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 		struct sock *new_mptcp_sock;
 		struct sock *ssk = newsk;
 
+		rcu_read_lock();
 		subflow = mptcp_subflow_ctx(newsk);
 		new_mptcp_sock = subflow->conn;
 
@@ -1429,6 +1435,7 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 		 * subflow_syn_recv_sock()
 		 */
 		if (WARN_ON_ONCE(!new_mptcp_sock)) {
+			rcu_read_unlock();
 			tcp_sk(newsk)->is_mptcp = 0;
 			return newsk;
 		}
@@ -1446,6 +1453,7 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 		list_add(&subflow->node, &msk->conn_list);
 		inet_sk_state_store(newsk, TCP_ESTABLISHED);
 
+		rcu_read_unlock();
 		bh_unlock_sock(new_mptcp_sock);
 
 		__MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
net/mptcp/protocol.h +2 -2
index 305e703e1fb3..a013f1c1045b 100644
@@ -278,8 +278,8 @@ mptcp_subflow_ctx(const struct sock *sk)
 {
 	struct inet_connection_sock *icsk = inet_csk(sk);
 
-	/* Use RCU on icsk_ulp_data only for sock diag code */
-	return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data;
+	return rcu_dereference_check(icsk->icsk_ulp_data,
+				     lockdep_sock_is_held(sk));
 }
 
 static inline struct sock *
net/mptcp/subflow.c +8 -13
index 0bc1f5dd9999..8b17f8ad8aeb 100644
@@ -126,12 +126,10 @@ static void subflow_init_req(struct request_sock *req,
 			     const struct sock *sk_listener,
 			     struct sk_buff *skb)
 {
-	struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
+	const struct mptcp_subflow_context *listener;
 	struct tcp_options_received rx_opt;
 
-	pr_debug("subflow_req=%p, listener=%p", subflow_req, listener);
-
 	memset(&rx_opt.mptcp, 0, sizeof(rx_opt.mptcp));
 	mptcp_get_options(skb, &rx_opt);
 
@@ -156,6 +154,9 @@ static void subflow_init_req(struct request_sock *req,
 		SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNRX);
 	}
 
+	rcu_read_lock();
+
+	listener = mptcp_subflow_ctx(sk_listener);
 	if (rx_opt.mptcp.mp_capable && listener->request_mptcp) {
 		int err;
 
@@ -178,6 +179,7 @@ static void subflow_init_req(struct request_sock *req,
 			// @@ need to trigger RST
 		}
 	}
+	rcu_read_unlock();
 }
 
 static void subflow_v4_init_req(struct request_sock *req,
@@ -306,10 +308,6 @@ static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops;
 
 static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb)
 {
-	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
-
-	pr_debug("subflow=%p", subflow);
-
 	/* Never answer to SYNs sent to broadcast or multicast */
 	if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
 		goto drop;
@@ -329,10 +327,6 @@ static struct inet_connection_sock_af_ops subflow_v6m_specific;
 
 static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb)
 {
-	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
-
-	pr_debug("subflow=%p", subflow);
-
 	if (skb->protocol == htons(ETH_P_IP))
 		return subflow_v4_conn_request(sk, skb);
 
@@ -940,7 +934,8 @@ int __mptcp_subflow_connect(struct sock *sk, int ifindex,
 	if (err)
 		return err;
 
-	subflow = mptcp_subflow_ctx(sf->sk);
+	/* socket is still private */
+	subflow = rcu_access_pointer(inet_csk(sf->sk)->icsk_ulp_data);
 	subflow->remote_key = msk->remote_key;
 	subflow->local_key = msk->local_key;
 	subflow->token = msk->token;
@@ -1002,12 +997,12 @@ int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock)
 	this_cpu_add(*net->core.sock_inuse, 1);
 #endif
 	err = tcp_set_ulp(sf->sk, "mptcp");
+	subflow = mptcp_subflow_ctx(sf->sk);
 	release_sock(sf->sk);
 
 	if (err)
 		return err;
 
-	subflow = mptcp_subflow_ctx(sf->sk);
 	pr_debug("subflow=%p", subflow);
 
 	*new_sock = sf;
Signature · 1 line
2.24.1
author
#9 lore raw

PATCH 6/7 mptcp: reverse order of sk_state_change and is_mptcp check

In reply to Florian Westphal

There is a small race where we can end up in subflow_finish_connect() with tp->is_mptcp == 0 and parent == NULL, resulting in a crash.

Just move the is_mptcp check to avoid this.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/subflow.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/mptcp/subflow.c +3 -3
index 8b17f8ad8aeb..7ad9413cf9d9 100644
@@ -255,14 +255,14 @@ static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
 
 	subflow->icsk_af_ops->sk_rx_dst_set(sk, skb);
 
+	if (subflow->conn_finished || !tcp_sk(sk)->is_mptcp)
+		return;
+
 	if (inet_sk_state_load(parent) != TCP_ESTABLISHED) {
 		inet_sk_state_store(parent, TCP_ESTABLISHED);
 		parent->sk_state_change(parent);
 	}
 
-	if (subflow->conn_finished || !tcp_sk(sk)->is_mptcp)
-		return;
-
 	if (subflow->mp_capable) {
 		pr_debug("subflow=%p, remote_key=%llu", mptcp_subflow_ctx(sk),
 			 subflow->remote_key);
Signature · 1 line
2.24.1
author
#10 lore raw

PATCH 7/7 mptcp: prevent null deref crash on normal tcp sockets

In reply to Florian Westphal

Best case we get a WARN in mptcp_accept().

Worst case is a null pointer dereference when we attempt to add mptcp options to the tcp flow: it has 'is_mptcp' set to 1, but a subflow context with a NULL mptcp master socket (ctx->conn).

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/subflow.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
net/mptcp/subflow.c +5 -1
index 7ad9413cf9d9..f9fe59febf6a 100644
@@ -495,8 +495,12 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 
 out:
 	/* dispose of the left over mptcp master, if any */
-	if (unlikely(new_msk))
+	if (unlikely(new_msk)) {
+		if (child)
+			tcp_sk(child)->is_mptcp = 0;
 		mptcp_force_close(new_msk);
+	}
+
 	return child;
 
 close_child:
Signature · 1 line
2.24.1