Skip to contentSkip to conversation

mptcp: plug mptcp_accept memory leaks

PATCH In review Florian Westphal 2 participants 5 messages May 16, 2019 – May 17, 2019

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author
#0 lore raw

v3 cover mptcp: plug mptcp_accept memory leaks

v3: rebase & fix erroneous conditional in patch #3.

v2 cover letter:
v2:
- rename helper in patch 2
- add back crucial mptcp_init_sock() after clone in patch 3. This was lost during rebase and broke the tests but i forgot to refresh this patch when i sent v1.

This series resolves the memory leaks that occur when running mptcp_connect.sh.

I don't mind if they get squashed.
I've decided to split this into 3 distinct changes to make it a bit clearer what is going on.

Second patch is not a bug fix, i can resend and drop it if the consensus is to continue using the sk->sk_socket back reference.

Third patch is a bit large due to the change in how the new mptcp socket is created.

See the commit messages for more details.
After these changes kmemleak doesn't report any leaks when the test script has completed.

author
#1 lore raw

PATCH v3 1/3 mptcp: release mptcp socket in mptcp_close

kmemleak reports several stray objects.
This fixes following splats:

unreferenced object 0xffff88810e27f240 (size 2016):

    comm "mptcp_connect", pid 2187, jiffies 4295218907 (age 1371.080s)
    hex dump (first 32 bytes):
      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
      02 00 01 41 00 00 00 00 00 00 00 00 00 00 00 00  ...A............
    backtrace:
      [<00000000298ae054>] sk_alloc+0x4c/0x600
      [<000000001cd7d411>] inet_create+0x1f4/0x6a0
      [<00000000ac4d1289>] __sock_create+0x1b4/0x3c0
      [<00000000cf2663ed>] __sys_socket+0xb6/0x170
      [<00000000b8efb3a9>] __x64_sys_socket+0x3d/0x50
      [<000000006e83b432>] do_syscall_64+0x72/0x270
      [<0000000007655965>] entry_SYSCALL_64_after_hwframe+0x49/0xbe
      [<0000000073c2278e>] 0xffffffffffffffff

This is because mptcp_close forgot to put the last reference.

We also need to set the state to CLOSED and orphan the socket, else the put will trigger warning about 'alive' inet socket being torn down.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c | 5 +++++
 1 file changed, 5 insertions(+)
net/mptcp/protocol.c +5 -0
index e80b4a50a208..ad8b1dcd525e 100644
@@ -592,6 +592,8 @@ static void mptcp_close(struct sock *sk, long timeout)
 	struct socket *ssk = NULL;
 	struct list_head list;
 
+	inet_sk_state_store(sk, TCP_CLOSE);
+
 	spin_lock_bh(&msk->conn_list_lock);
 	if (msk->subflow) {
 		ssk = msk->subflow;
@@ -615,6 +617,9 @@ static void mptcp_close(struct sock *sk, long timeout)
 		pr_debug("conn_list->subflow=%p", subflow);
 		sock_release(sock_sk(subflow)->sk_socket);
 	};
+
+	sock_orphan(sk);
+	sock_put(sk);
 }
 
 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
Signature · 1 line
2.21.0
author
#2 lore raw

PATCH v3 2/3 mptcp: store tcp socket, not tcp sock in subflow ctx

In reply to Florian Westphal

Instead of using sk->sk_socket to get back the socket reference, store the 'socket' pointer that is coming from kernel_accept().

This doesn't fix a bug, but it appears cleaner to me.

v3: rebase
v2: rename helper (Peter)

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c | 23 ++++++++++++-----------
 net/mptcp/protocol.h |  6 +++---
 net/mptcp/subflow.c  | 10 +++++++---
 3 files changed, 22 insertions(+), 17 deletions(-)
net/mptcp/protocol.c +12 -11
index ad8b1dcd525e..ca8a0d799a11 100644
@@ -27,7 +27,7 @@ static inline bool before64(__u64 seq1, __u64 seq2)
 
 static bool mptcp_subflow_hold(struct subflow_context *subflow)
 {
-	struct sock *sk = sock_sk(subflow);
+	struct sock *sk = mptcp_subflow_tcp_socket(subflow)->sk;
 
 	return refcount_inc_not_zero(&sk->sk_refcnt);
 }
@@ -40,7 +40,7 @@ static struct sock *mptcp_subflow_get_ref(const struct mptcp_sock *msk)
 	mptcp_for_each_subflow(msk, subflow) {
 		if (mptcp_subflow_hold(subflow)) {
 			rcu_read_unlock();
-			return sock_sk(subflow);
+			return mptcp_subflow_tcp_socket(subflow)->sk;
 		}
 	}
 
@@ -236,9 +236,9 @@ static u64 expand_seq(u64 old_seq, u16 old_data_len, u64 seq)
 
 static u64 get_map_offset(struct subflow_context *subflow)
 {
-	return tcp_sk(sock_sk(subflow))->copied_seq -
-			  subflow->ssn_offset -
-			  subflow->map_subflow_seq;
+	return tcp_sk(mptcp_subflow_tcp_socket(subflow)->sk)->copied_seq -
+		      subflow->ssn_offset -
+		      subflow->map_subflow_seq;
 }
 
 static u64 get_mapped_dsn(struct subflow_context *subflow)
@@ -615,8 +615,8 @@ static void mptcp_close(struct sock *sk, long timeout)
 
 	list_for_each_entry_safe(subflow, tmp, &list, node) {
 		pr_debug("conn_list->subflow=%p", subflow);
-		sock_release(sock_sk(subflow)->sk_socket);
-	};
+		sock_release(mptcp_subflow_tcp_socket(subflow));
+	}
 
 	sock_orphan(sk);
 	sock_put(sk);
@@ -670,6 +670,7 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 		subflow->map_subflow_seq = 1;
 		subflow->rel_write_seq = 1;
 		subflow->conn = new_mptcp_sock->sk;
+		subflow->tcp_sock = new_sock;
 	} else {
 		msk->subflow = new_sock;
 	}
@@ -948,8 +949,8 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,
 		if (!tmp)
 			break;
 
-		ret |= tcp_poll(file, sock_sk(tmp)->sk_socket, wait);
-		sock_put(sock_sk(tmp));
+		ret |= tcp_poll(file, mptcp_subflow_tcp_socket(tmp), wait);
+		sock_put(mptcp_subflow_tcp_socket(tmp)->sk);
 	}
 
 	return ret;
@@ -978,9 +979,9 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	lock_sock(sock->sk);
 	rcu_read_lock();
 	list_for_each_entry_rcu(subflow, &msk->conn_list, node) {
-		rcu_read_unlock();
 		pr_debug("conn_list->subflow=%p", subflow);
-		ret = kernel_sock_shutdown(sock_sk(subflow)->sk_socket, how);
+		rcu_read_unlock();
+		ret = kernel_sock_shutdown(mptcp_subflow_tcp_socket(subflow), how);
 		rcu_read_lock();
 	}
 	rcu_read_unlock();
net/mptcp/protocol.h +3 -3
index de1131c543d0..3dfe15f42058 100644
@@ -90,7 +90,7 @@ struct subflow_context {
 		map_valid : 1,
 		use_checksum : 1;
 
-	struct  sock *sk;       /* underlying tcp_sock */
+	struct  socket *tcp_sock;/* underlying tcp_sock */
 	struct  sock *conn;     /* parent mptcp_sock */
 	void	(*tcp_sk_data_ready)(struct sock *sk);
 };
@@ -101,9 +101,9 @@ static inline struct subflow_context *subflow_ctx(const struct sock *sk)
 	return (struct subflow_context *)icsk->icsk_ulp_data;
 }
 
-static inline struct sock *sock_sk(const struct subflow_context *subflow)
+static inline struct socket *mptcp_subflow_tcp_socket(const struct subflow_context *subflow)
 {
-	return subflow->sk;
+	return subflow->tcp_sock;
 }
 
 int subflow_init(void);
net/mptcp/subflow.c +7 -3
index d2ee65a98e30..bc8e1a0658ec 100644
@@ -174,6 +174,7 @@ static void subflow_data_ready(struct sock *sk)
 }
 
 static struct subflow_context *subflow_create_ctx(struct sock *sk,
+						  struct socket *sock,
 						  gfp_t priority)
 {
 	struct inet_connection_sock *icsk = inet_csk(sk);
@@ -187,7 +188,8 @@ static struct subflow_context *subflow_create_ctx(struct sock *sk,
 
 	pr_debug("subflow=%p", ctx);
 
-	ctx->sk = sk;
+	/* might be NULL */
+	ctx->tcp_sock = sock;
 
 	return ctx;
 }
@@ -199,7 +201,7 @@ static int subflow_ulp_init(struct sock *sk)
 	struct subflow_context *ctx;
 	int err = 0;
 
-	ctx = subflow_create_ctx(sk, GFP_KERNEL);
+	ctx = subflow_create_ctx(sk, sk->sk_socket, GFP_KERNEL);
 	if (!ctx) {
 		err = -ENOMEM;
 		goto out;
@@ -234,7 +236,9 @@ static void subflow_ulp_clone(const struct request_sock *req,
 	struct subflow_context *new_ctx;
 
 	old_ctx = inet_csk(newsk)->icsk_ulp_data;
-	new_ctx = subflow_create_ctx(newsk, priority);
+
+	/* newsk->sk_socket is NULL at this point */
+	new_ctx = subflow_create_ctx(newsk, NULL, priority);
 
 	if (!new_ctx)
 		return;
Signature · 1 line
2.21.0
author
#3 lore raw

PATCH v3 3/3 mptcp: accept: don't leak mptcp socket structure

In reply to Florian Westphal

accept() is supposed to prepare and return a 'struct sock'.

The caller holds a new inode/socket, and will associate the returned sock with it.

mptcp_accept however will allocate it via 'struct socket', then returns socket->sk.
This then leaks the outer socket struct inode returned by sock_create():

unreferenced object 0xffff88810512e8c0 (size 936):
comm "mptcp_connect", [..]
backtrace:

    [<00000000872561ba>] alloc_inode+0x35/0xe0
    [<00000000646e04ed>] new_inode_pseudo+0x12/0x80
    [<00000000e2e77036>] sock_alloc+0x26/0x100
    [<00000000870a8688>] __sock_create+0x8f/0x3c0
    [<000000000558b3fa>] mptcp_accept+0x140/0x530
    [<0000000044718d60>] inet_accept+0xac/0x470
    [<00000000da8f3979>] mptcp_stream_accept+0x62/0xa0
    [<00000000c9010499>] __sys_accept4+0x228/0x3c0

To fix this, make several (unfortunately, intrusive) changes:

1. Instead of allocating a new mptcp socket, clone the mptcp listen socket socket->sk.

This gives us a mptcp sock without the inode container. We return this sock struct to the caller, the caller will then complete creation of the mptcp socket.

2. For the 'compat' (old tcp, not mp capable) case, return the tcp socket directly and release the socket coming from kernel_accept().

We can use mptcp_getname() to override the socket->ops to tcp. This will make mptcp_accept work like tcp accept, and mptcp won't be involved anymore for the particular socket.

v3: fix sk_clone_lock check (Mat)
v2: add mptcp_init_sock() that got lost during last rebase.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c | 65 +++++++++++++++++++++++++++++++-------------
 1 file changed, 46 insertions(+), 19 deletions(-)
net/mptcp/protocol.c +46 -19
index ca8a0d799a11..9a98c8e0c996 100644
@@ -627,9 +627,9 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	struct socket *listener = msk->subflow;
-	struct socket *new_sock;
-	struct socket *new_mptcp_sock;
 	struct subflow_context *subflow;
+	struct socket *new_sock;
+	struct sock *newsk;
 
 	pr_debug("msk=%p, listener=%p", msk, subflow_ctx(listener->sk));
 	*err = kernel_accept(listener, &new_sock, flags);
@@ -639,28 +639,34 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 	subflow = subflow_ctx(new_sock->sk);
 	pr_debug("new subflow=%p", subflow);
 
-	*err = sock_create(PF_INET, SOCK_STREAM, IPPROTO_MPTCP, &new_mptcp_sock);
-	if (*err < 0) {
-		kernel_sock_shutdown(new_sock, SHUT_RDWR);
-		sock_release(new_sock);
-		return NULL;
-	}
-
-	msk = mptcp_sk(new_mptcp_sock->sk);
 	pr_debug("new msk=%p", msk);
 
 	if (subflow->mp_capable) {
+		struct sock *new_mptcp_sock;
 		u64 ack_seq;
 
+		local_bh_disable();
+		new_mptcp_sock = sk_clone_lock(sk, GFP_ATOMIC);
+		if (!new_mptcp_sock) {
+			*err = -ENOBUFS;
+			local_bh_enable();
+			kernel_sock_shutdown(new_sock, SHUT_RDWR);
+			sock_release(new_sock);
+			return NULL;
+		}
+
+		mptcp_init_sock(new_mptcp_sock);
+
+		msk = mptcp_sk(new_mptcp_sock);
 		msk->remote_key = subflow->remote_key;
 		msk->local_key = subflow->local_key;
 		msk->token = subflow->token;
 		pr_debug("token=%u", msk->token);
-		token_update_accept(new_sock->sk, new_mptcp_sock->sk);
-		spin_lock_bh(&msk->conn_list_lock);
+		token_update_accept(new_sock->sk, new_mptcp_sock);
+		spin_lock(&msk->conn_list_lock);
 		list_add_rcu(&subflow->node, &msk->conn_list);
 		msk->subflow = NULL;
-		spin_unlock_bh(&msk->conn_list_lock);
+		spin_unlock(&msk->conn_list_lock);
 
 		crypto_key_sha1(msk->remote_key, NULL, &ack_seq);
 		msk->write_seq = subflow->idsn + 1;
@@ -669,14 +675,20 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 		subflow->map_seq = ack_seq;
 		subflow->map_subflow_seq = 1;
 		subflow->rel_write_seq = 1;
-		subflow->conn = new_mptcp_sock->sk;
 		subflow->tcp_sock = new_sock;
+		newsk = new_mptcp_sock;
+		subflow->conn = new_mptcp_sock;
+		bh_unlock_sock(new_mptcp_sock);
+		local_bh_enable();
+		inet_sk_state_store(newsk, TCP_ESTABLISHED);
 	} else {
-		msk->subflow = new_sock;
+		newsk = new_sock->sk;
+		tcp_sk(newsk)->is_mptcp = 0;
+		new_sock->sk = NULL;
+		sock_release(new_sock);
 	}
-	inet_sk_state_store(new_mptcp_sock->sk, TCP_ESTABLISHED);
 
-	return new_mptcp_sock->sk;
+	return newsk;
 }
 
 static void mptcp_destroy(struct sock *sk)
@@ -866,6 +878,18 @@ static int mptcp_getname(struct socket *sock, struct sockaddr *uaddr,
 
 	pr_debug("msk=%p", msk);
 
+	if (sock->sk->sk_prot == &tcp_prot) {
+		/* we are being invoked from __sys_accept4, after
+		 * mptcp_accept() has just accepted a non-mp-capable
+		 * flow: sk is a tcp_sk, not an mptcp one.
+		 *
+		 * Hand the socket over to tcp so all further socket ops
+		 * bypass mptcp.
+		 */
+		sock->ops = &inet_stream_ops;
+		return inet_getname(sock, uaddr, peer);
+	}
+
 	if (msk->subflow) {
 		pr_debug("subflow=%p", msk->subflow->sk);
 		return inet_getname(msk->subflow, uaddr, peer);
@@ -979,9 +1003,12 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	lock_sock(sock->sk);
 	rcu_read_lock();
 	list_for_each_entry_rcu(subflow, &msk->conn_list, node) {
-		pr_debug("conn_list->subflow=%p", subflow);
+		struct socket *tcp_socket;
+
+		tcp_socket = mptcp_subflow_tcp_socket(subflow);
 		rcu_read_unlock();
-		ret = kernel_sock_shutdown(mptcp_subflow_tcp_socket(subflow), how);
+		pr_debug("conn_list->subflow=%p", subflow);
+		ret = kernel_sock_shutdown(tcp_socket, how);
 		rcu_read_lock();
 	}
 	rcu_read_unlock();
Signature · 1 line
2.21.0
#4 lore raw
In reply to Florian Westphal

Hi Florian,

Thank you for the patches and the update.
And thanks Peter and Mat for the reviews!

Quoted 9 lines · Florian Westphal

v3: rebase & fix erroneous conditional in patch #3.

v2 cover letter:
v2:
- rename helper in patch 2
- add back crucial mptcp_init_sock() after clone in patch 3. This was lost during rebase and broke the tests but i forgot to refresh this patch when i sent v1.

Quoted 6 lines

This series resolves the memory leaks that occur when running mptcp_connect.sh.

I don't mind if they get squashed.
I've decided to split this into 3 distinct changes to make it a bit clearer what is going on.

Thank you for this!

Because the 1st one was fixing a bug introduced in the patch series and the 2nd one change the architecture, I have squashed them in different commits. For the 3rd one, I guess it made more sense to create a specific commit to explain the decision that you took.

Here are the details of the squashes if someone wants to see them:

Patch 1: mptcp: release mptcp socket in mptcp_close:

	2bb08839b911 → squash in "mptcp: Add MPTCP socket stubs"
	419b1ecc8760 → sign-off
	af1f253ddd49 → conflict 1
	5a85931b0875 → conflict 2
	14035afa54ce..e58d56c3bdb0 → result patch 1

Patch 2: mptcp: store tcp socket, not tcp sock in subflow ctx
278ebcdf1fcd → squash in "mptcp: Associate MPTCP context with TCP socket" 1cf900c3d339 → sign-off
e5fabdff04da → conflict 1
0867ab696817 → conflict 2
3f9772cce9fb → squash in "mptcp: Create SUBFLOW socket for incoming
connections"

	fdf93ec159c6 → sign-off
	67fb09ba1e9f → conflict 3
	adb0b1e24c8e → conflict 4
	9127b8ba2843 + 5ca554150c60 → squash in "mptcp: Make connection_list a
real list of subflows" (use the new function instead of the old one +
remove unneeded semi-colon)
	b2f24974d1b2 → conflict 5
	5f525e95f57f + 907383369eab → fix-up 1
	e58d56c3bdb0..be0a3d56b3bb → result patch 2

Patch 3: mptcp: accept: don't leak mptcp socket structure
a7bc3b13590e → added at the end (without the updates notes, I hope you
don't mind)

	be0a3d56b3bb..d90f77dd19c4 → result patch 3

Cheers,
Matt

Quoted 17 lines

Second patch is not a bug fix, i can resend and drop it if the consensus is to continue using the sk->sk_socket back reference.

Third patch is a bit large due to the change in how the new mptcp socket is created.

See the commit messages for more details.
After these changes kmemleak doesn't report any leaks when the test script has completed.

_______________________________________________
mptcp mailing list
mptcp(a)lists.01.org
https://lists.01.org/mailman/listinfo/mptcp

Signature · 5 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium