Skip to contentSkip to conversation

mptcp: more token follow-up

PATCH In review Paolo Abeni 4 participants 8 messages Jun 8, 2020 – Jun 10, 2020

Subsystems (2)
lore
Share & tools
lore

Paolo Abeni

author
#0 lore raw

v2 cover mptcp: more token follow-up

The patch 1 addesses a race on close which may possibly fix issues/36 && issues/29, the 2nd patch is resolve conflicts and the 3rd one is a follow-up cleanup.

Patch 3 should be inserted just after "mptcp: refactor token container."

v1 -> v2:
- hopefully includes the correct patches

 protocol.c |    9 ++++---
 subflow.c  |   69 ++++++++++++++++++++++---------------------------------------
 2 files changed, 30 insertions(+), 48 deletions(-)
author
#1 lore raw

PATCH v2 1/3 Squash-to: "mptcp: refactor token container."

Move token creation into stream_connect(). This clean the code a bit reducing the test in critical path and plug a race: at shutdown time, subflow_rebuild_header() can be called after the msk token is removed, leaving a refecen to the to-be destroyed socket into the token container with later UaF.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/protocol.c |  6 +++++-
 net/mptcp/subflow.c  | 10 ++--------
 2 files changed, 7 insertions(+), 9 deletions(-)
net/mptcp/protocol.c +5 -1
index 99c019879833..be09fd525f8f 100644
@@ -1800,6 +1800,7 @@ static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
 				int addr_len, int flags)
 {
 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
+	struct mptcp_subflow_context *subflow;
 	struct socket *ssock;
 	int err;
 
@@ -1819,13 +1820,16 @@ static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
 		goto unlock;
 	}
 
+	subflow = mptcp_subflow_ctx(ssock->sk);
 #ifdef CONFIG_TCP_MD5SIG
 	/* no MPTCP if MD5SIG is enabled on this socket or we may run out of
 	 * TCP option space.
 	 */
 	if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
-		mptcp_subflow_ctx(ssock->sk)->request_mptcp = 0;
+		subflow->request_mptcp = 0;
 #endif
+	if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk))
+		subflow->request_mptcp = 0;
 
 do_connect:
 	err = ssock->ops->connect(ssock, uaddr, addr_len, flags);
net/mptcp/subflow.c +2 -8
index fcb9ca9a9dce..2d30fafa1aed 100644
@@ -34,13 +34,7 @@ static int subflow_rebuild_header(struct sock *sk)
 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 	int local_id;
 
-	if (subflow->request_mptcp && sk_unhashed(subflow->conn)) {
-		pr_debug("subflow=%p", sk);
-		if (mptcp_token_new_connect(sk)) {
-			subflow->mp_capable = 0;
-			goto out;
-		}
-	} else if (subflow->request_join && !subflow->local_nonce) {
+	if (subflow->request_join && !subflow->local_nonce) {
 		struct mptcp_sock *msk = (struct mptcp_sock *)subflow->conn;
 
 		pr_debug("subflow=%p", sk);
@@ -251,7 +245,7 @@ static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
 		subflow->remote_nonce = mp_opt.nonce;
 		pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u", subflow,
 			 subflow->thmac, subflow->remote_nonce);
-	} else if (subflow->request_mptcp) {
+	} else {
 		tp->is_mptcp = 0;
 	}
 
Signature · 1 line
2.21.3
#2 lore raw

[MPTCP] Re: [PATCH v2 1/3] Squash-to: "mptcp: refactor token container."

Quoted 8 lines · Paolo Abeni

Move token creation into stream_connect(). This clean the code a bit reducing the test in critical path and plug a race: at shutdown time, subflow_rebuild_header() can be called after the msk token is removed, leaving a refecen to the to-be destroyed socket into the token container with later UaF.

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

hello Paolo,

this patch fixes the kasan UAF and the GPF crash I saw yesterday on my test environments. See below:

Quoted 4 lines
---
 net/mptcp/protocol.c |  6 +++++-
 net/mptcp/subflow.c  | 10 ++--------
 2 files changed, 7 insertions(+), 9 deletions(-)

[...]

Quoted 4 lines
net/mptcp/subflow.c +0 -0
index fcb9ca9a9dce..2d30fafa1aed 100644

[...]

net/mptcp/subflow.c static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb) · line 250

Quoted 8 lines
net/mptcp/subflow.c +1 -1
@@ -251,7 +245,7 @@ static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
 		subflow->remote_nonce = mp_opt.nonce;
 		pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u", subflow,
 			 subflow->thmac, subflow->remote_nonce);
-	} else if (subflow->request_mptcp) {
+	} else {
 		tp->is_mptcp = 0;
 	}

the above hunk does not apply because the lower context is different (it has been changed wityh the fallback rework). I reworked it like this:

------------------------ >8 ------------------------
@@ -252,7 +246,7 @@ static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)

                subflow->remote_nonce = mp_opt.nonce;
                pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u", subflow,
                         subflow->thmac, subflow->remote_nonce);
-       } else if (subflow->request_mptcp) {
+       } else {
                mptcp_do_fallback(sk);
                pr_fallback(mptcp_sk(subflow->conn));
                MPTCP_INC_STATS(sock_net(sk),

------------------------ >8 ------------------------

During tests, I saw a sporadic packetdrill failure (one occurrence) here:

/root/packetdrill/gtests/net/mptcp/mp_capable/v1_connect_tcpfallback_flagB.pkt

and here:

/root/packetdrill/gtests/net/mptcp/mp_capable/v1_bind_tcpfallback_wrongver.pkt

but I'm unsure if this is really a problem related to this patch. I'm quite sure that it's something recent, because tests in the 'mp_capable' subfolder have been passing wothout problems since a long time.

Signature · 1 line
davide
author
#3 lore raw

PATCH v2 2/3 Squash-to: "net: mptcp: improve fallback to TCP"

In reply to Paolo Abeni

Drop socket lock on mptcp poll: we don't need it anymore, as we only use atomic ops on msk flags

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/protocol.c | 3 ---
 net/mptcp/subflow.c  | 5 +++--
 2 files changed, 3 insertions(+), 5 deletions(-)
net/mptcp/protocol.c +0 -3
index 817472998c37..d6e786f20681 100644
@@ -1945,7 +1945,6 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,
 
 	msk = mptcp_sk(sk);
 	sock_poll_wait(file, sock, wait);
-	lock_sock(sk);
 
 	if (test_bit(MPTCP_DATA_READY, &msk->flags))
 		mask = EPOLLIN | EPOLLRDNORM;
@@ -1955,8 +1954,6 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,
 	if (sk->sk_shutdown & RCV_SHUTDOWN)
 		mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
 
-	release_sock(sk);
-
 	return mask;
 }
 
net/mptcp/subflow.c +3 -2
index cd0f8c14be40..db066a43925f 100644
@@ -247,10 +247,11 @@ static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
 		pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u", subflow,
 			 subflow->thmac, subflow->remote_nonce);
 	} else  {
+		if (subflow->request_mptcp)
+			MPTCP_INC_STATS(sock_net(sk),
+					MPTCP_MIB_MPCAPABLEACTIVEFALLBACK);
 		mptcp_do_fallback(sk);
 		pr_fallback(mptcp_sk(subflow->conn));
-		MPTCP_INC_STATS(sock_net(sk),
-				MPTCP_MIB_MPCAPABLEACTIVEFALLBACK);
 	}
 
 	if (mptcp_check_fallback(sk))
Signature · 1 line
2.21.3
author
#4 lore raw

PATCH v2 3/3 mptcp: do nonce initialization at subflow creation time

In reply to Paolo Abeni

This clean-up the code a bit, reduces the number of used hooks and indirect call requested, and allow better error reporting from __mptcp_subflow_connect()

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/subflow.c | 54 +++++++++++++++++----------------------------
 1 file changed, 20 insertions(+), 34 deletions(-)
net/mptcp/subflow.c +20 -34
index db066a43925f..d5935f3bfbd1 100644
@@ -29,34 +29,6 @@ static void SUBFLOW_REQ_INC_STATS(struct request_sock *req,
 	MPTCP_INC_STATS(sock_net(req_to_sk(req)), field);
 }
 
-static int subflow_rebuild_header(struct sock *sk)
-{
-	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
-	int local_id;
-
-	if (subflow->request_join && !subflow->local_nonce) {
-		struct mptcp_sock *msk = (struct mptcp_sock *)subflow->conn;
-
-		pr_debug("subflow=%p", sk);
-
-		do {
-			get_random_bytes(&subflow->local_nonce, sizeof(u32));
-		} while (!subflow->local_nonce);
-
-		if (subflow->local_id)
-			goto out;
-
-		local_id = mptcp_pm_get_local_id(msk, (struct sock_common *)sk);
-		if (local_id < 0)
-			return -EINVAL;
-
-		subflow->local_id = local_id;
-	}
-
-out:
-	return subflow->icsk_af_ops->rebuild_header(sk);
-}
-
 static void subflow_req_destructor(struct request_sock *req)
 {
 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
@@ -987,7 +959,9 @@ int __mptcp_subflow_connect(struct sock *sk, int ifindex,
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	struct mptcp_subflow_context *subflow;
 	struct sockaddr_storage addr;
+	int local_id = loc->id;
 	struct socket *sf;
+	struct sock *ssk;
 	u32 remote_token;
 	int addrlen;
 	int err;
@@ -999,7 +973,20 @@ int __mptcp_subflow_connect(struct sock *sk, int ifindex,
 	if (err)
 		return err;
 
-	subflow = mptcp_subflow_ctx(sf->sk);
+	ssk = sf->sk;
+	subflow = mptcp_subflow_ctx(ssk);
+	do {
+		get_random_bytes(&subflow->local_nonce, sizeof(u32));
+	} while (!subflow->local_nonce);
+
+	if (!local_id) {
+		err = mptcp_pm_get_local_id(msk, (struct sock_common *)ssk);
+		if (err < 0)
+			goto failed;
+
+		local_id = err;
+	}
+
 	subflow->remote_key = msk->remote_key;
 	subflow->local_key = msk->local_key;
 	subflow->token = msk->token;
@@ -1010,15 +997,16 @@ int __mptcp_subflow_connect(struct sock *sk, int ifindex,
 	if (loc->family == AF_INET6)
 		addrlen = sizeof(struct sockaddr_in6);
 #endif
-	sf->sk->sk_bound_dev_if = ifindex;
+	ssk->sk_bound_dev_if = ifindex;
 	err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen);
 	if (err)
 		goto failed;
 
 	mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
-	pr_debug("msk=%p remote_token=%u", msk, remote_token);
+	pr_debug("msk=%p remote_token=%u local_id=%d", msk, remote_token,
+		 local_id);
 	subflow->remote_token = remote_token;
-	subflow->local_id = loc->id;
+	subflow->local_id = local_id;
 	subflow->request_join = 1;
 	subflow->request_bkup = 1;
 	mptcp_info2sockaddr(remote, &addr);
@@ -1279,7 +1267,6 @@ void __init mptcp_subflow_init(void)
 	subflow_specific.conn_request = subflow_v4_conn_request;
 	subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
 	subflow_specific.sk_rx_dst_set = subflow_finish_connect;
-	subflow_specific.rebuild_header = subflow_rebuild_header;
 
 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
 	subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
@@ -1289,7 +1276,6 @@ void __init mptcp_subflow_init(void)
 	subflow_v6_specific.conn_request = subflow_v6_conn_request;
 	subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
 	subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;
-	subflow_v6_specific.rebuild_header = subflow_rebuild_header;
 
 	subflow_v6m_specific = subflow_v6_specific;
 	subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
Signature · 1 line
2.21.3
#5 lore raw
In reply to Paolo Abeni
Quoted 4 lines · Paolo Abeni

The patch 1 addesses a race on close which may possibly fix issues/36 && issues/29, the 2nd patch is resolve conflicts and the 3rd one is a follow-up cleanup.

Quoted 8 lines

Patch 3 should be inserted just after "mptcp: refactor token container."

v1 -> v2:
- hopefully includes the correct patches

protocol.c | 9 ++++---
subflow.c | 69 ++++++++++++++++++++++---------------------------------------

2 files changed, 30 insertions(+), 48 deletions(-)

Thanks Paolo, these fixes look good. From IRC and
https://github.com/multipath-tcp/mptcp_net-next/issues/36 it looks like #36 is still happening, do you prefer to try more changes in a v3 or squash these in the export branch and work from there?

--
Mat Martineau
Intel

author
#6 lore raw
Quoted 11 lines · Mat Martineau
Quoted 9 lines · Paolo Abeni

The patch 1 addesses a race on close which may possibly fix issues/36 && issues/29, the 2nd patch is resolve conflicts and the 3rd one is a follow-up cleanup.

Patch 3 should be inserted just after "mptcp: refactor token container."

v1 -> v2:
- hopefully includes the correct patches

Quoted 8 lines
Quoted 3 lines

protocol.c | 9 ++++---
subflow.c | 69 ++++++++++++++++++++++---------------------------------------

2 files changed, 30 insertions(+), 48 deletions(-)

Thanks Paolo, these fixes look good. From IRC and
https://github.com/multipath-tcp/mptcp_net-next/issues/36 it looks like #36 is still happening, do you prefer to try more changes in a v3 or squash these in the export branch and work from there?

I don't have anything more in my pocket, so I would opt for squashing this changes as is, thanks!

/P

#7 lore raw

Hi Paolo, Mat, Davide,

Quoted 16 lines · Paolo Abeni
Quoted 15 lines · Mat Martineau
Quoted 12 lines · Paolo Abeni

The patch 1 addesses a race on close which may possibly fix issues/36 && issues/29, the 2nd patch is resolve conflicts and the 3rd one is a follow-up cleanup.

Patch 3 should be inserted just after "mptcp: refactor token container."

v1 -> v2:
- hopefully includes the correct patches

protocol.c | 9 ++++---
subflow.c | 69 ++++++++++++++++++++++---------------------------------------

2 files changed, 30 insertions(+), 48 deletions(-)
Quoted 7 lines
Quoted 4 lines

Thanks Paolo, these fixes look good. From IRC and
https://github.com/multipath-tcp/mptcp_net-next/issues/36 it looks like #36 is still happening, do you prefer to try more changes in a v3 or squash these in the export branch and work from there?

I don't have anything more in my pocket, so I would opt for squashing this changes as is, thanks!

Thank you for the patches and reviews!

- 8f56b23f5662: "squashed" patch 1/3 in "mptcp: refactor token container."
- 7c391f0de905: conflict in t/net-mptcp-improve-fallback-to-TCP
- 358748f8e4d2..89cd01041d51: result

- b04d6cff8748: "squashed" patch 2/3 in "net: mptcp: improve fallback to TCP"
(I removed an extra space in the context to avoid conflicts "} else {")
- 1a420b54c8c3: "Signed-off-by" + "Co-developed-by"
- 89cd01041d51..ab59469e1460: result

The third patch has been added between "mptcp: refactor token container." and "mptcp: move crypto test to KUNIT".
- ab59469e1460..e1337aec1cf5: result

One last thing: I remove the dot at the end of "mptcp: refactor token container."
- d89075f9c5c2: tg:msg: remove dot at the end of the title

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

Cheers,
Matt
--
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net