Skip to contentSkip to conversation

mptcp: close poll() races

PATCH In review Paolo Abeni 3 participants 3 messages Jun 10, 2020 – Jun 11, 2020

Subsystems (2)
lore
Share & tools
lore

Paolo Abeni

author
#0 lore raw

PATCH 1/1 mptcp: close poll() races

mptcp_poll always return POLLOUT for unblocking connect(), ensure that the socket is a suitable state.
The MPTCP_DATA_READY bit is never cleared on accept:
ensure we don't leave mptcp_accept() with an empty accept queue and such bit set.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>

---
This can be:
- keep as is
- split in 2 patches (listen fix and connect fix)
- squashed into "mptcp: fallback in case of simultaneous connect",
- partially squashed (listen fix) and partially keeped as separate patch

I'll personally vote for option 1 or 3 but I'm open to other alternatives ;)

---
 net/mptcp/protocol.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
net/mptcp/protocol.c +21 -5
index 0a832c84655a..b6d129489204 100644
@@ -1913,6 +1913,7 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
 	if (!ssock)
 		goto unlock_fail;
 
+	clear_bit(MPTCP_DATA_READY, &msk->flags);
 	sock_hold(ssock->sk);
 	release_sock(sock->sk);
 
@@ -1933,6 +1934,8 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
 		}
 	}
 
+	if (inet_csk_listen_poll(ssock->sk))
+		set_bit(MPTCP_DATA_READY, &msk->flags);
 	sock_put(ssock->sk);
 	return err;
 
@@ -1941,21 +1944,34 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
 	return -EINVAL;
 }
 
+
+static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
+{
+	return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM :
+	       0;
+}
+
 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
 			   struct poll_table_struct *wait)
 {
 	struct sock *sk = sock->sk;
 	struct mptcp_sock *msk;
 	__poll_t mask = 0;
+	int state;
 
 	msk = mptcp_sk(sk);
 	sock_poll_wait(file, sock, wait);
 
-	if (test_bit(MPTCP_DATA_READY, &msk->flags))
-		mask = EPOLLIN | EPOLLRDNORM;
-	if (sk_stream_is_writeable(sk) &&
-	    test_bit(MPTCP_SEND_SPACE, &msk->flags))
-		mask |= EPOLLOUT | EPOLLWRNORM;
+	state = inet_sk_state_load(sk);
+	if (state == TCP_LISTEN)
+		return mptcp_check_readable(msk);
+
+	if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
+		mask |= mptcp_check_readable(msk);
+		if (sk_stream_is_writeable(sk) &&
+		    test_bit(MPTCP_SEND_SPACE, &msk->flags))
+			mask |= EPOLLOUT | EPOLLWRNORM;
+	}
 	if (sk->sk_shutdown & RCV_SHUTDOWN)
 		mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
 
Signature · 1 line
2.21.3
#1 lore raw
Quoted 15 lines · Paolo Abeni

mptcp_poll always return POLLOUT for unblocking connect(), ensure that the socket is a suitable state.
The MPTCP_DATA_READY bit is never cleared on accept:
ensure we don't leave mptcp_accept() with an empty accept queue and such bit set.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>

---
This can be:
- keep as is
- split in 2 patches (listen fix and connect fix)
- squashed into "mptcp: fallback in case of simultaneous connect",
- partially squashed (listen fix) and partially keeped as separate patch

Quoted 2 lines

I'll personally vote for option 1 or 3 but I'm open to other alternatives ;)

Option 1 sounds good, but I don't object to #3

Quoted 26 lines

---
net/mptcp/protocol.c | 26 +++++++++++++++++++++-----

1 file changed, 21 insertions(+), 5 deletions(-)
net/mptcp/protocol.c +3 -0
index 0a832c84655a..b6d129489204 100644
@@ -1913,6 +1913,7 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
 	if (!ssock)
 		goto unlock_fail;
 
+	clear_bit(MPTCP_DATA_READY, &msk->flags);
 	sock_hold(ssock->sk);
 	release_sock(sock->sk);
 
@@ -1933,6 +1934,8 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
 		}
 	}
 
+	if (inet_csk_listen_poll(ssock->sk))
+		set_bit(MPTCP_DATA_READY, &msk->flags);
 	sock_put(ssock->sk);
 	return err;
 

net/mptcp/protocol.c static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, · line 1951

Quoted 8 lines
net/mptcp/protocol.c +5 -0
@@ -1941,21 +1944,34 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
 	return -EINVAL;
 }
 
+
+static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
+{
+	return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM :
+	       0;

You haven't set your editor for 100 character lines? :)

Quoted 33 lines
net/mptcp/protocol.c +13 -5
+}
+
 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
 			   struct poll_table_struct *wait)
 {
 	struct sock *sk = sock->sk;
 	struct mptcp_sock *msk;
 	__poll_t mask = 0;
+	int state;
 
 	msk = mptcp_sk(sk);
 	sock_poll_wait(file, sock, wait);
 
-	if (test_bit(MPTCP_DATA_READY, &msk->flags))
-		mask = EPOLLIN | EPOLLRDNORM;
-	if (sk_stream_is_writeable(sk) &&
-	    test_bit(MPTCP_SEND_SPACE, &msk->flags))
-		mask |= EPOLLOUT | EPOLLWRNORM;
+	state = inet_sk_state_load(sk);
+	if (state == TCP_LISTEN)
+		return mptcp_check_readable(msk);
+
+	if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
+		mask |= mptcp_check_readable(msk);
+		if (sk_stream_is_writeable(sk) &&
+		    test_bit(MPTCP_SEND_SPACE, &msk->flags))
+			mask |= EPOLLOUT | EPOLLWRNORM;
+	}
 	if (sk->sk_shutdown & RCV_SHUTDOWN)
 		mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
 
Signature · 1 line
2.21.3

Now that I've taken a closer look at tcp_poll, it seems clear that the socket state needs to be taken in to account like you've done. Thanks for the fix.

--
Mat Martineau
Intel

#2 lore raw

Hi Paolo, Mat,

Quoted 9 lines · Mat Martineau
Quoted 6 lines · Paolo Abeni

mptcp_poll always return POLLOUT for unblocking connect(), ensure that the socket is a suitable state.
The MPTCP_DATA_READY bit is never cleared on accept:
ensure we don't leave mptcp_accept() with an empty accept queue and such bit set.

Thank you for the patch and the review!

Quoted 8 lines
Quoted 8 lines
Signed-off-by Paolo Abeni <pabeni(a)redhat.com>

---
This can be:
- keep as is
- split in 2 patches (listen fix and connect fix)
- squashed into "mptcp: fallback in case of simultaneous connect",
- partially squashed (listen fix) and partially keeped as separate patch

Quoted 4 lines
Quoted 2 lines

I'll personally vote for option 1 or 3 but I'm open to other alternatives ;)

Option 1 sounds good, but I don't object to #3

I picked the option 1 because the "mptcp: fallback in case of simultaneous connect" is currently waiting for review :)

So this patch is now at the end of the series.

- 76c6304b26cb..e914fa85bdc4: result

The "export" branch has been updated. Tests are still OK!

Cheers,
Matt

Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net