diff mbox series

net: kcm: fix memory leak in kcm_sendmsg

Message ID 20210602192640.13597-1-paskripkin@gmail.com (mailing list archive)
State Accepted
Commit c47cc304990a2813995b1a92bbc11d0bb9a19ea9
Delegated to: Netdev Maintainers
Headers show
Series net: kcm: fix memory leak in kcm_sendmsg | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Guessed tree name to be net-next
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cc_maintainers warning 3 maintainers not CCed: unixbhaskar@gmail.com zhengyongjun3@huawei.com jonathan.lemon@gmail.com
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 11 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link

Commit Message

Pavel Skripkin June 2, 2021, 7:26 p.m. UTC
Syzbot reported memory leak in kcm_sendmsg()[1].
The problem was in non-freed frag_list in case of error.

In the while loop:

	if (head == skb)
		skb_shinfo(head)->frag_list = tskb;
	else
		skb->next = tskb;

frag_list filled with skbs, but nothing was freeing them.

backtrace:
  [<0000000094c02615>] __alloc_skb+0x5e/0x250 net/core/skbuff.c:198
  [<00000000e5386cbd>] alloc_skb include/linux/skbuff.h:1083 [inline]
  [<00000000e5386cbd>] kcm_sendmsg+0x3b6/0xa50 net/kcm/kcmsock.c:967 [1]
  [<00000000f1613a8a>] sock_sendmsg_nosec net/socket.c:652 [inline]
  [<00000000f1613a8a>] sock_sendmsg+0x4c/0x60 net/socket.c:672

Reported-and-tested-by: syzbot+b039f5699bd82e1fb011@syzkaller.appspotmail.com
Fixes: ab7ac4eb9832 ("kcm: Kernel Connection Multiplexor module")
Cc: stable@vger.kernel.org
Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>
---
 net/kcm/kcmsock.c | 5 +++++
 1 file changed, 5 insertions(+)

Comments

patchwork-bot+netdevbpf@kernel.org June 3, 2021, 9:20 p.m. UTC | #1
Hello:

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

On Wed,  2 Jun 2021 22:26:40 +0300 you wrote:
> Syzbot reported memory leak in kcm_sendmsg()[1].
> The problem was in non-freed frag_list in case of error.
> 
> In the while loop:
> 
> 	if (head == skb)
> 		skb_shinfo(head)->frag_list = tskb;
> 	else
> 		skb->next = tskb;
> 
> [...]

Here is the summary with links:
  - net: kcm: fix memory leak in kcm_sendmsg
    https://git.kernel.org/netdev/net/c/c47cc304990a

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Cong Wang June 3, 2021, 10:32 p.m. UTC | #2
On Wed, Jun 2, 2021 at 12:29 PM Pavel Skripkin <paskripkin@gmail.com> wrote:
>
> Syzbot reported memory leak in kcm_sendmsg()[1].
> The problem was in non-freed frag_list in case of error.
>
> In the while loop:
>
>         if (head == skb)
>                 skb_shinfo(head)->frag_list = tskb;
>         else
>                 skb->next = tskb;
>
> frag_list filled with skbs, but nothing was freeing them.

What do you mean by "nothing was freeing them"?

I am sure kfree_skb() will free those in frag_list:

 654 static void skb_release_data(struct sk_buff *skb)
 655 {
 656         struct skb_shared_info *shinfo = skb_shinfo(skb);
 657         int i;
...
 669         if (shinfo->frag_list)
 670                 kfree_skb_list(shinfo->frag_list);


>
> backtrace:
>   [<0000000094c02615>] __alloc_skb+0x5e/0x250 net/core/skbuff.c:198
>   [<00000000e5386cbd>] alloc_skb include/linux/skbuff.h:1083 [inline]
>   [<00000000e5386cbd>] kcm_sendmsg+0x3b6/0xa50 net/kcm/kcmsock.c:967 [1]
>   [<00000000f1613a8a>] sock_sendmsg_nosec net/socket.c:652 [inline]
>   [<00000000f1613a8a>] sock_sendmsg+0x4c/0x60 net/socket.c:672
>
> Reported-and-tested-by: syzbot+b039f5699bd82e1fb011@syzkaller.appspotmail.com
> Fixes: ab7ac4eb9832 ("kcm: Kernel Connection Multiplexor module")
> Cc: stable@vger.kernel.org
> Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>
> ---
>  net/kcm/kcmsock.c | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c
> index 6201965bd822..1c572c8daced 100644
> --- a/net/kcm/kcmsock.c
> +++ b/net/kcm/kcmsock.c
> @@ -1066,6 +1066,11 @@ static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
>                 goto partial_message;
>         }
>
> +       if (skb_has_frag_list(head)) {
> +               kfree_skb_list(skb_shinfo(head)->frag_list);
> +               skb_shinfo(head)->frag_list = NULL;
> +       }
> +
>         if (head != kcm->seq_skb)
>                 kfree_skb(head);

This exact kfree_skb() should free those in frag_list. If the above
if condition does not meet for some reason, then fix that condition?

Thanks.
Pavel Skripkin June 4, 2021, 11:38 a.m. UTC | #3
On Thu, 3 Jun 2021 15:32:03 -0700
Cong Wang <xiyou.wangcong@gmail.com> wrote:

> On Wed, Jun 2, 2021 at 12:29 PM Pavel Skripkin <paskripkin@gmail.com>
> wrote:
> >
> > Syzbot reported memory leak in kcm_sendmsg()[1].
> > The problem was in non-freed frag_list in case of error.
> >
> > In the while loop:
> >
> >         if (head == skb)
> >                 skb_shinfo(head)->frag_list = tskb;
> >         else
> >                 skb->next = tskb;
> >
> > frag_list filled with skbs, but nothing was freeing them.
> 
> What do you mean by "nothing was freeing them"?
> 
> I am sure kfree_skb() will free those in frag_list:
> 
>  654 static void skb_release_data(struct sk_buff *skb)
>  655 {
>  656         struct skb_shared_info *shinfo = skb_shinfo(skb);
>  657         int i;
> ...
>  669         if (shinfo->frag_list)
>  670                 kfree_skb_list(shinfo->frag_list);
> 
> 

Indeed. I didn't know about that. Im sorry.

> >
> > backtrace:
> >   [<0000000094c02615>] __alloc_skb+0x5e/0x250 net/core/skbuff.c:198
> >   [<00000000e5386cbd>] alloc_skb include/linux/skbuff.h:1083
> > [inline] [<00000000e5386cbd>] kcm_sendmsg+0x3b6/0xa50
> > net/kcm/kcmsock.c:967 [1] [<00000000f1613a8a>] sock_sendmsg_nosec
> > net/socket.c:652 [inline] [<00000000f1613a8a>]
> > sock_sendmsg+0x4c/0x60 net/socket.c:672
> >
> > Reported-and-tested-by:
> > syzbot+b039f5699bd82e1fb011@syzkaller.appspotmail.com Fixes:
> > ab7ac4eb9832 ("kcm: Kernel Connection Multiplexor module") Cc:
> > stable@vger.kernel.org Signed-off-by: Pavel Skripkin
> > <paskripkin@gmail.com> ---
> >  net/kcm/kcmsock.c | 5 +++++
> >  1 file changed, 5 insertions(+)
> >
> > diff --git a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c
> > index 6201965bd822..1c572c8daced 100644
> > --- a/net/kcm/kcmsock.c
> > +++ b/net/kcm/kcmsock.c
> > @@ -1066,6 +1066,11 @@ static int kcm_sendmsg(struct socket *sock,
> > struct msghdr *msg, size_t len) goto partial_message;
> >         }
> >
> > +       if (skb_has_frag_list(head)) {
> > +               kfree_skb_list(skb_shinfo(head)->frag_list);
> > +               skb_shinfo(head)->frag_list = NULL;
> > +       }
> > +
> >         if (head != kcm->seq_skb)
> >                 kfree_skb(head);
> 
> This exact kfree_skb() should free those in frag_list. If the above
> if condition does not meet for some reason, then fix that condition?
> 
> Thanks.

I will debug this today later. I think, this commit should be reverted,
because it's broken. Or I can send next patch on top of this. What do
you think of that, David? 


With regards,
Pavel Skripkin
diff mbox series

Patch

diff --git a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c
index 6201965bd822..1c572c8daced 100644
--- a/net/kcm/kcmsock.c
+++ b/net/kcm/kcmsock.c
@@ -1066,6 +1066,11 @@  static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
 		goto partial_message;
 	}
 
+	if (skb_has_frag_list(head)) {
+		kfree_skb_list(skb_shinfo(head)->frag_list);
+		skb_shinfo(head)->frag_list = NULL;
+	}
+
 	if (head != kcm->seq_skb)
 		kfree_skb(head);