Skip to contentSkip to conversation

net: rds: fix memory leak in rds_recvmsg

PATCH Superseded Pavel Skripkin 4 participants 7 messages Jun 7, 2021 – Jun 8, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: rds: fix memory leak in rds_recvmsg

Syzbot reported memory leak in rds. The problem was in unputted refcount in case of error.

int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
int msg_flags)
{
...

	if (!rds_next_incoming(rs, &inc)) {
		...
	}

After this "if" inc refcount incremented and

	if (rds_cmsg_recv(inc, msg, rs)) {
		ret = -EFAULT;
		goto out;
	}
...
out:
	return ret;
}

in case of rds_cmsg_recv() fail the refcount won't be decremented. And it's easy to see from ftrace log, that rds_inc_addref() don't have rds_inc_put() pair in rds_recvmsg() after rds_cmsg_recv()

 1)               |  rds_recvmsg() {
 1)   3.721 us    |    rds_inc_addref();
 1)   3.853 us    |    rds_message_inc_copy_to_user();
 1) + 10.395 us   |    rds_cmsg_recv();
 1) + 34.260 us   |  }
Fixes bdbe6fbc6a2f ("RDS: recv.c")
Reported-and-tested-by [email protected]
---
 net/rds/recv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/rds/recv.c +2 -1
index 4db109fb6ec2..3fa16c339bfe 100644
@@ -714,7 +714,7 @@ int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
 
 		if (rds_cmsg_recv(inc, msg, rs)) {
 			ret = -EFAULT;
-			goto out;
+			goto out_put;
 		}
 		rds_recvmsg_zcookie(rs, msg);
 
@@ -740,6 +740,7 @@ int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
 		break;
 	}
 
+out_put:
 	if (inc)
 		rds_inc_put(inc);
 
Signature · 1 line
2.31.1
#1 lore raw
Quoted 38 lines · Pavel Skripkin

Syzbot reported memory leak in rds. The problem was in unputted refcount in case of error.

int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
int msg_flags)
{
...

	if (!rds_next_incoming(rs, &inc)) {
		...
	}

After this "if" inc refcount incremented and

	if (rds_cmsg_recv(inc, msg, rs)) {
		ret = -EFAULT;
		goto out;
	}
...
out:
	return ret;
}

in case of rds_cmsg_recv() fail the refcount won't be decremented. And it's easy to see from ftrace log, that rds_inc_addref() don't have rds_inc_put() pair in rds_recvmsg() after rds_cmsg_recv()

1)               |  rds_recvmsg() {
1)   3.721 us    |    rds_inc_addref();
1)   3.853 us    |    rds_message_inc_copy_to_user();
1) + 10.395 us   |    rds_cmsg_recv();
1) + 34.260 us   |  }
Quoted 3 lines
Fixes bdbe6fbc6a2f ("RDS: recv.c")
Reported-and-tested-by [email protected]

Thank for your commit and analyses. One small nit below.

Quoted 9 lines

---
net/rds/recv.c | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)
net/rds/recv.c +0 -0
index 4db109fb6ec2..3fa16c339bfe 100644

net/rds/recv.c int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, · line 717

Quoted 5 lines
net/rds/recv.c +1 -1
@@ -714,7 +714,7 @@ int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
 
 		if (rds_cmsg_recv(inc, msg, rs)) {
 			ret = -EFAULT;
-			goto out;
+			goto out_put;

Would a simple "break;" do it here and no need for the next hunk?

Thxs, Håkon

Quoted 14 lines
net/rds/recv.c +1 -0
 		}
 		rds_recvmsg_zcookie(rs, msg);
 
@@ -740,6 +740,7 @@ int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
 		break;
 	}
 
+out_put:
 	if (inc)
 		rds_inc_put(inc);
 
Signature · 1 line
2.31.1
#2 lore raw
Quoted 65 lines · On Tue, 8 Jun 2021 07:11:27 +0000 Haakon Bugge <[email protected]> wrote:
Quoted 39 lines

On 7 Jun 2021, at 21:41, Pavel Skripkin <[email protected]> wrote:

Syzbot reported memory leak in rds. The problem was in unputted refcount in case of error.

int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, int msg_flags)
{
...

	if (!rds_next_incoming(rs, &inc)) {
		...
	}

After this "if" inc refcount incremented and

	if (rds_cmsg_recv(inc, msg, rs)) {
		ret = -EFAULT;
		goto out;
	}
...
out:
	return ret;
}

in case of rds_cmsg_recv() fail the refcount won't be decremented. And it's easy to see from ftrace log, that rds_inc_addref() don't have rds_inc_put() pair in rds_recvmsg() after rds_cmsg_recv()

1)               |  rds_recvmsg() {
1)   3.721 us    |    rds_inc_addref();
1)   3.853 us    |    rds_message_inc_copy_to_user();
1) + 10.395 us   |    rds_cmsg_recv();
1) + 34.260 us   |  }
Quoted 4 lines
Fixes bdbe6fbc6a2f ("RDS: recv.c")

Reported-and-tested-by:
[email protected]

Thank for your commit and analyses. One small nit below.

Quoted 9 lines

---
net/rds/recv.c | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)
net/rds/recv.c +0 -0
index 4db109fb6ec2..3fa16c339bfe 100644

net/rds/recv.c int rds_recvmsg(struct socket *sock, struct · line 718

Quoted 6 lines
net/rds/recv.c +1 -1
@@ -714,7 +714,7 @@ int rds_recvmsg(struct socket *sock, struct
 msghdr *msg, size_t size,
 
 		if (rds_cmsg_recv(inc, msg, rs)) {
 			ret = -EFAULT;
-			goto out;
+			goto out_put;

Would a simple "break;" do it here and no need for the next hunk?

Quoted 2 lines

Thxs, Håkon

Sure! I'll send v2 soon. Thank you for feedback :)

With regards,
Pavel Skripkin

#3 lore raw

PATCH v2 1/1 net: rds: fix memory leak in rds_recvmsg

In reply to Haakon Bugge

Syzbot reported memory leak in rds. The problem was in unputted refcount in case of error.

int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
int msg_flags)
{
...

	if (!rds_next_incoming(rs, &inc)) {
		...
	}

After this "if" inc refcount incremented and

	if (rds_cmsg_recv(inc, msg, rs)) {
		ret = -EFAULT;
		goto out;
	}
...
out:
	return ret;
}

in case of rds_cmsg_recv() fail the refcount won't be decremented. And it's easy to see from ftrace log, that rds_inc_addref() don't have rds_inc_put() pair in rds_recvmsg() after rds_cmsg_recv()

 1)               |  rds_recvmsg() {
 1)   3.721 us    |    rds_inc_addref();
 1)   3.853 us    |    rds_message_inc_copy_to_user();
 1) + 10.395 us   |    rds_cmsg_recv();
 1) + 34.260 us   |  }
Fixes bdbe6fbc6a2f ("RDS: recv.c")
Reported-and-tested-by [email protected]

---

Changes in v2:
Changed goto to break.

---
 net/rds/recv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/rds/recv.c +1 -1
index 4db109fb6ec2..5b426dc3634d 100644
@@ -714,7 +714,7 @@ int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
 
 		if (rds_cmsg_recv(inc, msg, rs)) {
 			ret = -EFAULT;
-			goto out;
+			break;
 		}
 		rds_recvmsg_zcookie(rs, msg);
 
Signature · 1 line
2.31.1
Reviewed-by on v2 1/1
#4 lore raw
Quoted 38 lines · Pavel Skripkin

Syzbot reported memory leak in rds. The problem was in unputted refcount in case of error.

int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
int msg_flags)
{
...

	if (!rds_next_incoming(rs, &inc)) {
		...
	}

After this "if" inc refcount incremented and

	if (rds_cmsg_recv(inc, msg, rs)) {
		ret = -EFAULT;
		goto out;
	}
...
out:
	return ret;
}

in case of rds_cmsg_recv() fail the refcount won't be decremented. And it's easy to see from ftrace log, that rds_inc_addref() don't have rds_inc_put() pair in rds_recvmsg() after rds_cmsg_recv()

1)               |  rds_recvmsg() {
1)   3.721 us    |    rds_inc_addref();
1)   3.853 us    |    rds_message_inc_copy_to_user();
1) + 10.395 us   |    rds_cmsg_recv();
1) + 34.260 us   |  }
Quoted 3 lines
Fixes bdbe6fbc6a2f ("RDS: recv.c")
Reported-and-tested-by [email protected]

Thanks for fixing this, Håkon

Quoted 25 lines

---

Changes in v2:
Changed goto to break.

---
net/rds/recv.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
net/rds/recv.c +1 -1
index 4db109fb6ec2..5b426dc3634d 100644
@@ -714,7 +714,7 @@ int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
 
 		if (rds_cmsg_recv(inc, msg, rs)) {
 			ret = -EFAULT;
-			goto out;
+			break;
 		}
 		rds_recvmsg_zcookie(rs, msg);
 
Signature · 1 line
2.31.1
Acked-by on v2 1/1
#5 lore raw
In reply to Pavel Skripkin
Quoted 35 lines · Pavel Skripkin

Syzbot reported memory leak in rds. The problem was in unputted refcount in case of error.

int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
int msg_flags)
{
...

	if (!rds_next_incoming(rs, &inc)) {
		...
	}

After this "if" inc refcount incremented and

	if (rds_cmsg_recv(inc, msg, rs)) {
		ret = -EFAULT;
		goto out;
	}
...
out:
	return ret;
}

in case of rds_cmsg_recv() fail the refcount won't be decremented. And it's easy to see from ftrace log, that rds_inc_addref() don't have rds_inc_put() pair in rds_recvmsg() after rds_cmsg_recv()

1)               |  rds_recvmsg() {
1)   3.721 us    |    rds_inc_addref();
1)   3.853 us    |    rds_message_inc_copy_to_user();
1) + 10.395 us   |    rds_cmsg_recv();
1) + 34.260 us   |  }
Quoted 8 lines
Fixes bdbe6fbc6a2f ("RDS: recv.c")
Reported-and-tested-by [email protected]

---

Changes in v2:
Changed goto to break.

Looks fine by me. Thanks for the fix.

#6 lore raw
In reply to Pavel Skripkin
This patch was applied to netdev/net.git (refs/heads/master):

Hello:

This patch was applied to netdev/net.git (refs/heads/master):

Quoted 9 lines · On Tue, 8 Jun 2021 11:06:41 +0300 you wrote:

Syzbot reported memory leak in rds. The problem
was in unputted refcount in case of error.

int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
int msg_flags)
{
...

[...]

Here is the summary with links:
- [v2] net: rds: fix memory leak in rds_recvmsg

    https://git.kernel.org/netdev/net/c/49bfcbfd989a

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html