linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] net: qrtr: fix memory leak in qrtr_local_enqueue
@ 2021-07-22 16:16 Pavel Skripkin
  2021-07-23 12:27 ` Manivannan Sadhasivam
  0 siblings, 1 reply; 6+ messages in thread
From: Pavel Skripkin @ 2021-07-22 16:16 UTC (permalink / raw)
  To: mani, davem, kuba, bjorn.andersson, courtney.cavin
  Cc: linux-arm-msm, netdev, linux-kernel, Pavel Skripkin,
	syzbot+35a511c72ea7356cdcf3

Syzbot reported memory leak in qrtr. The problem was in unputted
struct sock. qrtr_local_enqueue() function calls qrtr_port_lookup()
which takes sock reference if port was found. Then there is the following
check:

if (!ipc || &ipc->sk == skb->sk) {
	...
	return -ENODEV;
}

Since we should drop the reference before returning from this function and
ipc can be non-NULL inside this if, we should add qrtr_port_put() inside
this if.

Fixes: bdabad3e363d ("net: Add Qualcomm IPC router")
Reported-and-tested-by: syzbot+35a511c72ea7356cdcf3@syzkaller.appspotmail.com
Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>
---
 net/qrtr/qrtr.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
index e6f4a6202f82..d5ce428d0b25 100644
--- a/net/qrtr/qrtr.c
+++ b/net/qrtr/qrtr.c
@@ -839,6 +839,8 @@ static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,
 
 	ipc = qrtr_port_lookup(to->sq_port);
 	if (!ipc || &ipc->sk == skb->sk) { /* do not send to self */
+		if (ipc)
+			qrtr_port_put(ipc);
 		kfree_skb(skb);
 		return -ENODEV;
 	}
-- 
2.32.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] net: qrtr: fix memory leak in qrtr_local_enqueue
  2021-07-22 16:16 [PATCH] net: qrtr: fix memory leak in qrtr_local_enqueue Pavel Skripkin
@ 2021-07-23 12:27 ` Manivannan Sadhasivam
  2021-07-23 15:08   ` Pavel Skripkin
  2021-07-23 15:31   ` [PATCH v2] net: qrtr: fix memory leaks Pavel Skripkin
  0 siblings, 2 replies; 6+ messages in thread
From: Manivannan Sadhasivam @ 2021-07-23 12:27 UTC (permalink / raw)
  To: Pavel Skripkin
  Cc: davem, kuba, bjorn.andersson, courtney.cavin, linux-arm-msm,
	netdev, linux-kernel, syzbot+35a511c72ea7356cdcf3

On Thu, Jul 22, 2021 at 07:16:25PM +0300, Pavel Skripkin wrote:
> Syzbot reported memory leak in qrtr. The problem was in unputted
> struct sock. qrtr_local_enqueue() function calls qrtr_port_lookup()
> which takes sock reference if port was found. Then there is the following
> check:
> 
> if (!ipc || &ipc->sk == skb->sk) {
> 	...
> 	return -ENODEV;
> }
> 
> Since we should drop the reference before returning from this function and
> ipc can be non-NULL inside this if, we should add qrtr_port_put() inside
> this if.
> 
> Fixes: bdabad3e363d ("net: Add Qualcomm IPC router")
> Reported-and-tested-by: syzbot+35a511c72ea7356cdcf3@syzkaller.appspotmail.com
> Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>

Reviewed-by: Manivannan Sadhasivam <mani@kernel.org>

It'd be good if this patch can be extended to fix one more corner case here:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/qrtr/qrtr.c#n522

Thanks,
Mani

> ---
>  net/qrtr/qrtr.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
> index e6f4a6202f82..d5ce428d0b25 100644
> --- a/net/qrtr/qrtr.c
> +++ b/net/qrtr/qrtr.c
> @@ -839,6 +839,8 @@ static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,
>  
>  	ipc = qrtr_port_lookup(to->sq_port);
>  	if (!ipc || &ipc->sk == skb->sk) { /* do not send to self */
> +		if (ipc)
> +			qrtr_port_put(ipc);
>  		kfree_skb(skb);
>  		return -ENODEV;
>  	}
> -- 
> 2.32.0
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] net: qrtr: fix memory leak in qrtr_local_enqueue
  2021-07-23 12:27 ` Manivannan Sadhasivam
@ 2021-07-23 15:08   ` Pavel Skripkin
  2021-07-23 15:31   ` [PATCH v2] net: qrtr: fix memory leaks Pavel Skripkin
  1 sibling, 0 replies; 6+ messages in thread
From: Pavel Skripkin @ 2021-07-23 15:08 UTC (permalink / raw)
  To: Manivannan Sadhasivam
  Cc: davem, kuba, bjorn.andersson, courtney.cavin, linux-arm-msm,
	netdev, linux-kernel, syzbot+35a511c72ea7356cdcf3

On Fri, 23 Jul 2021 17:57:53 +0530
Manivannan Sadhasivam <mani@kernel.org> wrote:

> On Thu, Jul 22, 2021 at 07:16:25PM +0300, Pavel Skripkin wrote:
> > Syzbot reported memory leak in qrtr. The problem was in unputted
> > struct sock. qrtr_local_enqueue() function calls qrtr_port_lookup()
> > which takes sock reference if port was found. Then there is the
> > following check:
> > 
> > if (!ipc || &ipc->sk == skb->sk) {
> > 	...
> > 	return -ENODEV;
> > }
> > 
> > Since we should drop the reference before returning from this
> > function and ipc can be non-NULL inside this if, we should add
> > qrtr_port_put() inside this if.
> > 
> > Fixes: bdabad3e363d ("net: Add Qualcomm IPC router")
> > Reported-and-tested-by:
> > syzbot+35a511c72ea7356cdcf3@syzkaller.appspotmail.com
> > Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>
> 
> Reviewed-by: Manivannan Sadhasivam <mani@kernel.org>
> 
> It'd be good if this patch can be extended to fix one more corner
> case here:
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/qrtr/qrtr.c#n522
> 
> Thanks,
> Mani

Hi, Manivannan!

I will fix leak there too in v2, thank you! 



With regards,
Pavel Skripkin

> 
> > ---
> >  net/qrtr/qrtr.c | 2 ++
> >  1 file changed, 2 insertions(+)
> > 
> > diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
> > index e6f4a6202f82..d5ce428d0b25 100644
> > --- a/net/qrtr/qrtr.c
> > +++ b/net/qrtr/qrtr.c
> > @@ -839,6 +839,8 @@ static int qrtr_local_enqueue(struct qrtr_node
> > *node, struct sk_buff *skb, 
> >  	ipc = qrtr_port_lookup(to->sq_port);
> >  	if (!ipc || &ipc->sk == skb->sk) { /* do not send to self
> > */
> > +		if (ipc)
> > +			qrtr_port_put(ipc);
> >  		kfree_skb(skb);
> >  		return -ENODEV;
> >  	}
> > -- 
> > 2.32.0
> > 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2] net: qrtr: fix memory leaks
  2021-07-23 12:27 ` Manivannan Sadhasivam
  2021-07-23 15:08   ` Pavel Skripkin
@ 2021-07-23 15:31   ` Pavel Skripkin
  2021-07-23 15:43     ` Manivannan Sadhasivam
  2021-07-23 16:50     ` patchwork-bot+netdevbpf
  1 sibling, 2 replies; 6+ messages in thread
From: Pavel Skripkin @ 2021-07-23 15:31 UTC (permalink / raw)
  To: mani, davem, kuba, loic.poulain, bjorn.andersson, xiyou.wangcong,
	edumazet
  Cc: netdev, linux-kernel, Pavel Skripkin, syzbot+35a511c72ea7356cdcf3

Syzbot reported memory leak in qrtr. The problem was in unputted
struct sock. qrtr_local_enqueue() function calls qrtr_port_lookup()
which takes sock reference if port was found. Then there is the following
check:

if (!ipc || &ipc->sk == skb->sk) {
	...
	return -ENODEV;
}

Since we should drop the reference before returning from this function and
ipc can be non-NULL inside this if, we should add qrtr_port_put() inside
this if.

The similar corner case is in qrtr_endpoint_post() as Manivannan
reported. In case of sock_queue_rcv_skb() failure we need to put
port reference to avoid leaking struct sock pointer.

Fixes: e04df98adf7d ("net: qrtr: Remove receive worker")
Fixes: bdabad3e363d ("net: Add Qualcomm IPC router")
Reported-and-tested-by: syzbot+35a511c72ea7356cdcf3@syzkaller.appspotmail.com
Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>
---

Changes in v2:
	Added missing qrtr_port_put() in qrtr_endpoint_post() as Manivannan
	reported.

---
 net/qrtr/qrtr.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
index b34358282f37..a8b2c9b21a8d 100644
--- a/net/qrtr/qrtr.c
+++ b/net/qrtr/qrtr.c
@@ -514,8 +514,10 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
 		if (!ipc)
 			goto err;
 
-		if (sock_queue_rcv_skb(&ipc->sk, skb))
+		if (sock_queue_rcv_skb(&ipc->sk, skb)) {
+			qrtr_port_put(ipc);
 			goto err;
+		}
 
 		qrtr_port_put(ipc);
 	}
@@ -850,6 +852,8 @@ static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,
 
 	ipc = qrtr_port_lookup(to->sq_port);
 	if (!ipc || &ipc->sk == skb->sk) { /* do not send to self */
+		if (ipc)
+			qrtr_port_put(ipc);
 		kfree_skb(skb);
 		return -ENODEV;
 	}
-- 
2.32.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] net: qrtr: fix memory leaks
  2021-07-23 15:31   ` [PATCH v2] net: qrtr: fix memory leaks Pavel Skripkin
@ 2021-07-23 15:43     ` Manivannan Sadhasivam
  2021-07-23 16:50     ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 6+ messages in thread
From: Manivannan Sadhasivam @ 2021-07-23 15:43 UTC (permalink / raw)
  To: Pavel Skripkin
  Cc: davem, kuba, loic.poulain, bjorn.andersson, xiyou.wangcong,
	edumazet, netdev, linux-kernel, syzbot+35a511c72ea7356cdcf3

On Fri, Jul 23, 2021 at 06:31:32PM +0300, Pavel Skripkin wrote:
> Syzbot reported memory leak in qrtr. The problem was in unputted
> struct sock. qrtr_local_enqueue() function calls qrtr_port_lookup()
> which takes sock reference if port was found. Then there is the following
> check:
> 
> if (!ipc || &ipc->sk == skb->sk) {
> 	...
> 	return -ENODEV;
> }
> 
> Since we should drop the reference before returning from this function and
> ipc can be non-NULL inside this if, we should add qrtr_port_put() inside
> this if.
> 
> The similar corner case is in qrtr_endpoint_post() as Manivannan
> reported. In case of sock_queue_rcv_skb() failure we need to put
> port reference to avoid leaking struct sock pointer.
> 
> Fixes: e04df98adf7d ("net: qrtr: Remove receive worker")
> Fixes: bdabad3e363d ("net: Add Qualcomm IPC router")
> Reported-and-tested-by: syzbot+35a511c72ea7356cdcf3@syzkaller.appspotmail.com
> Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>

Reviewed-by: Manivannan Sadhasivam <mani@kernel.org>

Thanks,
Mani

> ---
> 
> Changes in v2:
> 	Added missing qrtr_port_put() in qrtr_endpoint_post() as Manivannan
> 	reported.
> 
> ---
>  net/qrtr/qrtr.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
> index b34358282f37..a8b2c9b21a8d 100644
> --- a/net/qrtr/qrtr.c
> +++ b/net/qrtr/qrtr.c
> @@ -514,8 +514,10 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
>  		if (!ipc)
>  			goto err;
>  
> -		if (sock_queue_rcv_skb(&ipc->sk, skb))
> +		if (sock_queue_rcv_skb(&ipc->sk, skb)) {
> +			qrtr_port_put(ipc);
>  			goto err;
> +		}
>  
>  		qrtr_port_put(ipc);
>  	}
> @@ -850,6 +852,8 @@ static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,
>  
>  	ipc = qrtr_port_lookup(to->sq_port);
>  	if (!ipc || &ipc->sk == skb->sk) { /* do not send to self */
> +		if (ipc)
> +			qrtr_port_put(ipc);
>  		kfree_skb(skb);
>  		return -ENODEV;
>  	}
> -- 
> 2.32.0
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] net: qrtr: fix memory leaks
  2021-07-23 15:31   ` [PATCH v2] net: qrtr: fix memory leaks Pavel Skripkin
  2021-07-23 15:43     ` Manivannan Sadhasivam
@ 2021-07-23 16:50     ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 6+ messages in thread
From: patchwork-bot+netdevbpf @ 2021-07-23 16:50 UTC (permalink / raw)
  To: Pavel Skripkin
  Cc: mani, davem, kuba, loic.poulain, bjorn.andersson, xiyou.wangcong,
	edumazet, netdev, linux-kernel, syzbot+35a511c72ea7356cdcf3

Hello:

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

On Fri, 23 Jul 2021 18:31:32 +0300 you wrote:
> Syzbot reported memory leak in qrtr. The problem was in unputted
> struct sock. qrtr_local_enqueue() function calls qrtr_port_lookup()
> which takes sock reference if port was found. Then there is the following
> check:
> 
> if (!ipc || &ipc->sk == skb->sk) {
> 	...
> 	return -ENODEV;
> }
> 
> [...]

Here is the summary with links:
  - [v2] net: qrtr: fix memory leaks
    https://git.kernel.org/netdev/net/c/52f3456a96c0

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



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-07-23 16:50 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-22 16:16 [PATCH] net: qrtr: fix memory leak in qrtr_local_enqueue Pavel Skripkin
2021-07-23 12:27 ` Manivannan Sadhasivam
2021-07-23 15:08   ` Pavel Skripkin
2021-07-23 15:31   ` [PATCH v2] net: qrtr: fix memory leaks Pavel Skripkin
2021-07-23 15:43     ` Manivannan Sadhasivam
2021-07-23 16:50     ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).