Skip to contentSkip to conversation

MPTCP fallback fixes

PATCH In review Mat Martineau 2 participants 6 messages Feb 13, 2020 – Feb 14, 2020

Subsystems (2)
lore
Share & tools
lore

Mat Martineau

author
#0 lore raw

v2 cover MPTCP fallback fixes

This is a follow-up to the "Protect subflow socket options before connection completes" patch I sent last week. It has two more patches to address race conditions where __mptcp_tcp_fallback() is used before calling TCP functions on a fallback subflow.

Self tests pass.

I plan to send this to netdev tomorrow, but a pre-review on this list would be great!

Mat Martineau (3):
mptcp: Keep TCP fallback subflow valid after lookup
mptcp: Protect subflow socket options before connection completes
mptcp: Use TCP sendmsg/recvmsg for fallback subflows

 net/mptcp/protocol.c | 78 +++++++++++++++++++++++++++-----------------
 1 file changed, 48 insertions(+), 30 deletions(-)

base-commit: b9287f2ac321ecac56eb51e6231f6579683dcdae

Signature · 1 line
2.25.0
author
#1 lore raw

PATCH v2 1/3 mptcp: Keep TCP fallback subflow valid after lookup

When __mptcp_tcp_fallback() returns a subflow socket pointer, that pointer needs to remain valid while normal TCP functions are called using that pointer. By making the __mptcp_tcp_fallback() caller responsible for releasing the MPTCP socket lock and making sure the function does not return a closed subflow, the subflow socket can be safely used.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
net/mptcp/protocol.c +9 -5
index 030dee668e0a..a8faf66c38d8 100644
@@ -55,10 +55,8 @@ static struct socket *__mptcp_tcp_fallback(struct mptcp_sock *msk)
 	if (likely(!__mptcp_needs_tcp_fallback(msk)))
 		return NULL;
 
-	if (msk->subflow) {
-		release_sock((struct sock *)msk);
+	if (msk->subflow)
 		return msk->subflow;
-	}
 
 	return NULL;
 }
@@ -282,6 +280,7 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 	if (unlikely(ssock)) {
 fallback:
 		pr_debug("fallback passthrough");
+		release_sock(sk);
 		ret = sock_sendmsg(ssock, msg);
 		return ret >= 0 ? ret + copied : (copied ? copied : ret);
 	}
@@ -391,6 +390,7 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 fallback:
 		pr_debug("fallback-read subflow=%p",
 			 mptcp_subflow_ctx(ssock->sk));
+		release_sock(sk);
 		copied = sock_recvmsg(ssock, msg, flags);
 		return copied;
 	}
@@ -599,6 +599,7 @@ static void mptcp_close(struct sock *sk, long timeout)
 	inet_sk_state_store(sk, TCP_CLOSE);
 
 	list_splice_init(&msk->conn_list, &conn_list);
+	msk->subflow = NULL;
 
 	release_sock(sk);
 
@@ -1084,8 +1085,11 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,
 	sock_poll_wait(file, sock, wait);
 	lock_sock(sk);
 	ssock = __mptcp_tcp_fallback(msk);
-	if (unlikely(ssock))
-		return ssock->ops->poll(file, ssock, NULL);
+	if (unlikely(ssock)) {
+		mask = ssock->ops->poll(file, ssock, NULL);
+		release_sock(sk);
+		return mask;
+	}
 
 	if (test_bit(MPTCP_DATA_READY, &msk->flags))
 		mask = EPOLLIN | EPOLLRDNORM;
Signature · 1 line
2.25.0
author
#2 lore raw

PATCH v2 2/3 mptcp: Protect subflow socket options before connection completes

In reply to Mat Martineau

Userspace should not be able to directly manipulate subflow socket options before a connection is established since it is not yet known if it will be an MPTCP subflow or a TCP fallback subflow. TCP fallback subflows can be more directly controlled by userspace because they are regular TCP connections, while MPTCP subflow sockets need to be configured for the specific needs of MPTCP. Use the same logic as sendmsg/recvmsg to ensure that socket option calls are only passed through to known TCP fallback subflows.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 50 ++++++++++++++++++++++++--------------------
 1 file changed, 27 insertions(+), 23 deletions(-)
net/mptcp/protocol.c +27 -23
index a8faf66c38d8..0169e7dfc2d1 100644
@@ -756,60 +756,64 @@ static int mptcp_setsockopt(struct sock *sk, int level, int optname,
 			    char __user *optval, unsigned int optlen)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
-	int ret = -EOPNOTSUPP;
 	struct socket *ssock;
-	struct sock *ssk;
 
 	pr_debug("msk=%p", msk);
 
 	/* @@ the meaning of setsockopt() when the socket is connected and
-	 * there are multiple subflows is not defined.
+	 * there are multiple subflows is not yet defined. It is up to the
+	 * MPTCP-level socket to configure the subflows until the subflow
+	 * is in TCP fallback, when TCP socket options are passed through
+	 * to the one remaining subflow.
 	 */
 	lock_sock(sk);
-	ssock = __mptcp_socket_create(msk, MPTCP_SAME_STATE);
-	if (IS_ERR(ssock)) {
+	ssock = __mptcp_tcp_fallback(msk);
+	if (ssock) {
+		struct sock *ssk = ssock->sk;
+		int ret;
+
+		sock_hold(ssk);
 		release_sock(sk);
+		ret = tcp_setsockopt(ssk, level, optname, optval, optlen);
+		sock_put(ssk);
 		return ret;
 	}
 
-	ssk = ssock->sk;
-	sock_hold(ssk);
 	release_sock(sk);
 
-	ret = tcp_setsockopt(ssk, level, optname, optval, optlen);
-	sock_put(ssk);
-
-	return ret;
+	return -EOPNOTSUPP;
 }
 
 static int mptcp_getsockopt(struct sock *sk, int level, int optname,
 			    char __user *optval, int __user *option)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
-	int ret = -EOPNOTSUPP;
 	struct socket *ssock;
-	struct sock *ssk;
 
 	pr_debug("msk=%p", msk);
 
-	/* @@ the meaning of getsockopt() when the socket is connected and
-	 * there are multiple subflows is not defined.
+	/* @@ the meaning of setsockopt() when the socket is connected and
+	 * there are multiple subflows is not yet defined. It is up to the
+	 * MPTCP-level socket to configure the subflows until the subflow
+	 * is in TCP fallback, when socket options are passed through
+	 * to the one remaining subflow.
 	 */
 	lock_sock(sk);
-	ssock = __mptcp_socket_create(msk, MPTCP_SAME_STATE);
-	if (IS_ERR(ssock)) {
+	ssock = __mptcp_tcp_fallback(msk);
+	if (ssock) {
+		struct sock *ssk = ssock->sk;
+		int ret;
+
+		sock_hold(ssk);
 		release_sock(sk);
+		ret = tcp_getsockopt(ssk, level, optname, optval, option);
+		sock_put(ssk);
 		return ret;
 	}
 
-	ssk = ssock->sk;
-	sock_hold(ssk);
 	release_sock(sk);
 
-	ret = tcp_getsockopt(ssk, level, optname, optval, option);
-	sock_put(ssk);
-
-	return ret;
+	return -EOPNOTSUPP;
 }
 
 static int mptcp_get_port(struct sock *sk, unsigned short snum)
Signature · 1 line
2.25.0
#3 lore raw

[MPTCP] Re: [PATCH net v2 2/3] mptcp: Protect subflow socket options before connection completes

Quoted 79 lines · Mat Martineau

Userspace should not be able to directly manipulate subflow socket options before a connection is established since it is not yet known if it will be an MPTCP subflow or a TCP fallback subflow. TCP fallback subflows can be more directly controlled by userspace because they are regular TCP connections, while MPTCP subflow sockets need to be configured for the specific needs of MPTCP. Use the same logic as sendmsg/recvmsg to ensure that socket option calls are only passed through to known TCP fallback subflows.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 50 ++++++++++++++++++++++++--------------------
 1 file changed, 27 insertions(+), 23 deletions(-)
net/mptcp/protocol.c +18 -15
index a8faf66c38d8..0169e7dfc2d1 100644
@@ -756,60 +756,64 @@ static int mptcp_setsockopt(struct sock *sk, int level, int optname,
 			    char __user *optval, unsigned int optlen)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
-	int ret = -EOPNOTSUPP;
 	struct socket *ssock;
-	struct sock *ssk;
 
 	pr_debug("msk=%p", msk);
 
 	/* @@ the meaning of setsockopt() when the socket is connected and
-	 * there are multiple subflows is not defined.
+	 * there are multiple subflows is not yet defined. It is up to the
+	 * MPTCP-level socket to configure the subflows until the subflow
+	 * is in TCP fallback, when TCP socket options are passed through
+	 * to the one remaining subflow.
 	 */
 	lock_sock(sk);
-	ssock = __mptcp_socket_create(msk, MPTCP_SAME_STATE);
-	if (IS_ERR(ssock)) {
+	ssock = __mptcp_tcp_fallback(msk);
+	if (ssock) {
+		struct sock *ssk = ssock->sk;
+		int ret;
+
+		sock_hold(ssk);
 		release_sock(sk);
+		ret = tcp_setsockopt(ssk, level, optname, optval, optlen);
+		sock_put(ssk);
 		return ret;
 	}
 
-	ssk = ssock->sk;
-	sock_hold(ssk);
 	release_sock(sk);
 
-	ret = tcp_setsockopt(ssk, level, optname, optval, optlen);
-	sock_put(ssk);
-
-	return ret;
+	return -EOPNOTSUPP;
 }
 
 static int mptcp_getsockopt(struct sock *sk, int level, int optname,
 			    char __user *optval, int __user *option)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
-	int ret = -EOPNOTSUPP;
 	struct socket *ssock;
-	struct sock *ssk;
 
 	pr_debug("msk=%p", msk);
 
-	/* @@ the meaning of getsockopt() when the socket is connected and
-	 * there are multiple subflows is not defined.
+	/* @@ the meaning of setsockopt() when the socket is connected and
+	 * there are multiple subflows is not yet defined. It is up to the
+	 * MPTCP-level socket to configure the subflows until the subflow
+	 * is in TCP fallback, when socket options are passed through
+	 * to the one remaining subflow.
 	 */

net/mptcp/protocol.c static int mptcp_setsockopt(struct sock *sk, int level, int optname, · line 808

Quoted 10 lines
net/mptcp/protocol.c +6 -2
 	lock_sock(sk);
-	ssock = __mptcp_socket_create(msk, MPTCP_SAME_STATE);
-	if (IS_ERR(ssock)) {
+	ssock = __mptcp_tcp_fallback(msk);
+	if (ssock) {
+		struct sock *ssk = ssock->sk;
+		int ret;
+
+		sock_hold(ssk);
 		release_sock(sk);

Since I can't wrap my head on it, I'm going to do ask dumb question:

do we need this refcont at all? (here, in poll and in {recv,send}msg) ?

AFAICS:
- subflows can be freed only by mptcp_close()
- mptcp_close() is called by vfs->release() when the last reference to the file descriptor associated to the msk socket is dropped.
- every syscall wraps vfs ops with fdget()/fdput() pairs so the file descriptor can't go away during the syscall execution.

Overall I think the race between mptcp_close() and sock_sendmsg() is not possible, WDYT ?!?

mptcp_close should need to care of possible cuncurrent BH/receive path processing.

Paolo

Note: the I this doubt since I was wondering why socket-releated inet_* operations that do not acquire the sock lock still do not have any issue racing with close().

author
#4 lore raw

[MPTCP] Re: [PATCH net v2 2/3] mptcp: Protect subflow socket options before connection completes

Quoted 84 lines · Paolo Abeni
Quoted 83 lines · Mat Martineau

Userspace should not be able to directly manipulate subflow socket options before a connection is established since it is not yet known if it will be an MPTCP subflow or a TCP fallback subflow. TCP fallback subflows can be more directly controlled by userspace because they are regular TCP connections, while MPTCP subflow sockets need to be configured for the specific needs of MPTCP. Use the same logic as sendmsg/recvmsg to ensure that socket option calls are only passed through to known TCP fallback subflows.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 50 ++++++++++++++++++++++++--------------------
 1 file changed, 27 insertions(+), 23 deletions(-)
net/mptcp/protocol.c +19 -17
index a8faf66c38d8..0169e7dfc2d1 100644
@@ -756,60 +756,64 @@ static int mptcp_setsockopt(struct sock *sk, int level, int optname,
 			    char __user *optval, unsigned int optlen)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
-	int ret = -EOPNOTSUPP;
 	struct socket *ssock;
-	struct sock *ssk;
 
 	pr_debug("msk=%p", msk);
 
 	/* @@ the meaning of setsockopt() when the socket is connected and
-	 * there are multiple subflows is not defined.
+	 * there are multiple subflows is not yet defined. It is up to the
+	 * MPTCP-level socket to configure the subflows until the subflow
+	 * is in TCP fallback, when TCP socket options are passed through
+	 * to the one remaining subflow.
 	 */
 	lock_sock(sk);
-	ssock = __mptcp_socket_create(msk, MPTCP_SAME_STATE);
-	if (IS_ERR(ssock)) {
+	ssock = __mptcp_tcp_fallback(msk);
+	if (ssock) {
+		struct sock *ssk = ssock->sk;
+		int ret;
+
+		sock_hold(ssk);
 		release_sock(sk);
+		ret = tcp_setsockopt(ssk, level, optname, optval, optlen);
+		sock_put(ssk);
 		return ret;
 	}
 
-	ssk = ssock->sk;
-	sock_hold(ssk);
 	release_sock(sk);
 
-	ret = tcp_setsockopt(ssk, level, optname, optval, optlen);
-	sock_put(ssk);
-
-	return ret;
+	return -EOPNOTSUPP;
 }
 
 static int mptcp_getsockopt(struct sock *sk, int level, int optname,
 			    char __user *optval, int __user *option)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
-	int ret = -EOPNOTSUPP;
 	struct socket *ssock;
-	struct sock *ssk;
 
 	pr_debug("msk=%p", msk);
 
-	/* @@ the meaning of getsockopt() when the socket is connected and
-	 * there are multiple subflows is not defined.
+	/* @@ the meaning of setsockopt() when the socket is connected and
+	 * there are multiple subflows is not yet defined. It is up to the
+	 * MPTCP-level socket to configure the subflows until the subflow
+	 * is in TCP fallback, when socket options are passed through
+	 * to the one remaining subflow.
 	 */
 	lock_sock(sk);
-	ssock = __mptcp_socket_create(msk, MPTCP_SAME_STATE);
-	if (IS_ERR(ssock)) {
+	ssock = __mptcp_tcp_fallback(msk);
Quoted 9 lines

net/mptcp/protocol.c static int mptcp_setsockopt(struct sock *sk, int level, int optname, · line 776

Quoted 6 lines
net/mptcp/protocol.c +5 -0
+	if (ssock) {
+		struct sock *ssk = ssock->sk;
+		int ret;
+
+		sock_hold(ssk);
 		release_sock(sk);

Since I can't wrap my head on it, I'm going to do ask dumb question:

I think it's a good question!

Quoted 4 lines

do we need this refcont at all? (here, in poll and in {recv,send}msg) ?

AFAICS:
- subflows can be freed only by mptcp_close()

This is true now, but Florian's patch that removed __mptcp_close() is fairly recent. I think the branch I was working from when I got concerned about the subflow lifetimes didn't have the change yet, so I the idea in my head that there were other paths to the subflow-freeing code that relied only on the MPTCP socket lock.

Quoted 4 lines

- mptcp_close() is called by vfs->release() when the last reference to the file descriptor associated to the msk socket is dropped.
- every syscall wraps vfs ops with fdget()/fdput() pairs so the file descriptor can't go away during the syscall execution.

While I saw that the MPTCP socket lifetime was managed with fdget/fdput, I couldn't track down such protections for the subflow socket structures because (as I now see) we're depending on the MPTCP-level socket reference count for that.

Quoted 3 lines

Overall I think the race between mptcp_close() and sock_sendmsg() is not possible, WDYT ?!?

For single subflow, it does seem safe to me now that it's clear when mptcp_close is called. I'll drop the fallback changes outside of the sockopt functions and send the one sockopt patch to netdev.

Quoted 4 lines

mptcp_close should need to care of possible cuncurrent BH/receive path processing.

Seems like the struct sock reference counts should handle that?

Quoted 4 lines

Note: the I this doubt since I was wondering why socket-releated inet_* operations that do not acquire the sock lock still do not have any issue racing with close().

I'm glad you looked in to it. I was wondering the same thing when I saw the lack of fdget/fdput on the "struct socket" for subflows but didn't see that those were only freed when the entire MPTCP socket was being torn down.

--
Mat Martineau
Intel

author
#5 lore raw

PATCH v2 3/3 mptcp: Use TCP sendmsg/recvmsg for fallback subflows

In reply to Mat Martineau

When a connection is in TCP fallback, the MPTCP send and recv functions are bypassed and calls are passed through to the subflow. The MPTCP socket is unlocked when this passthrough happens because the send or receive may block. Using sock_sendmsg() and sock_recvmsg() on the fallback subflows leads to a race condition because they take struct socket pointers that can be released by another thread. tcp_sendmsg() and tcp_recvmsg() use struct sock pointers that are reference counted and will remain valid after the MPTCP socket lock is released.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
net/mptcp/protocol.c +12 -2
index 0169e7dfc2d1..fc5d977549f6 100644
@@ -280,8 +280,13 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 	if (unlikely(ssock)) {
 fallback:
 		pr_debug("fallback passthrough");
+
+		ssk = ssock->sk;
+		sock_hold(ssk);
 		release_sock(sk);
-		ret = sock_sendmsg(ssock, msg);
+		ret = tcp_sendmsg(ssk, msg, msg_data_left(msg));
+		sock_put(ssk);
+
 		return ret >= 0 ? ret + copied : (copied ? copied : ret);
 	}
 
@@ -390,8 +395,13 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 fallback:
 		pr_debug("fallback-read subflow=%p",
 			 mptcp_subflow_ctx(ssock->sk));
+
+		ssk = ssock->sk;
+		sock_hold(ssk);
 		release_sock(sk);
-		copied = sock_recvmsg(ssock, msg, flags);
+		copied = tcp_recvmsg(ssk, msg, len, nonblock, flags, addr_len);
+		sock_put(ssk);
+
 		return copied;
 	}
 
Signature · 1 line
2.25.0