linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ieee802154: hwsim: fix memory leak in __pskb_copy_fclone
@ 2021-07-13  5:40 Dongliang Mu
  2021-07-13 13:47 ` Alexander Aring
  0 siblings, 1 reply; 3+ messages in thread
From: Dongliang Mu @ 2021-07-13  5:40 UTC (permalink / raw)
  To: Alexander Aring, Stefan Schmidt, David S. Miller, Jakub Kicinski
  Cc: Dongliang Mu, Alexander Aring, linux-wpan, netdev, linux-kernel

hwsim_hw_xmit fails to deallocate the newskb copied by pskb_copy. Fix
this by adding kfree_skb after ieee802154_rx_irqsafe.

  [<ffffffff836433fb>] __alloc_skb+0x22b/0x250 net/core/skbuff.c:414
  [<ffffffff8364ad95>] __pskb_copy_fclone+0x75/0x360 net/core/skbuff.c:1609
  [<ffffffff82ae65e3>] __pskb_copy include/linux/skbuff.h:1176 [inline]
  [<ffffffff82ae65e3>] pskb_copy include/linux/skbuff.h:3207 [inline]
  [<ffffffff82ae65e3>] hwsim_hw_xmit+0xd3/0x140 drivers/net/ieee802154/mac802154_hwsim.c:132
  [<ffffffff83ff8f47>] drv_xmit_async net/mac802154/driver-ops.h:16 [inline]
  [<ffffffff83ff8f47>] ieee802154_tx+0xc7/0x190 net/mac802154/tx.c:83
  [<ffffffff83ff9138>] ieee802154_subif_start_xmit+0x58/0x70 net/mac802154/tx.c:132
  [<ffffffff83670b82>] __netdev_start_xmit include/linux/netdevice.h:4944 [inline]
  [<ffffffff83670b82>] netdev_start_xmit include/linux/netdevice.h:4958 [inline]
  [<ffffffff83670b82>] xmit_one net/core/dev.c:3658 [inline]
  [<ffffffff83670b82>] dev_hard_start_xmit+0xe2/0x330 net/core/dev.c:3674
  [<ffffffff83718028>] sch_direct_xmit+0xf8/0x520 net/sched/sch_generic.c:342
  [<ffffffff8367193b>] __dev_xmit_skb net/core/dev.c:3874 [inline]
  [<ffffffff8367193b>] __dev_queue_xmit+0xa3b/0x1360 net/core/dev.c:4241
  [<ffffffff83ff5437>] dgram_sendmsg+0x437/0x570 net/ieee802154/socket.c:682
  [<ffffffff836345b6>] sock_sendmsg_nosec net/socket.c:702 [inline]
  [<ffffffff836345b6>] sock_sendmsg+0x56/0x80 net/socket.c:722

Fixes: f25da51fdc38 ("ieee802154: hwsim: add replacement for fakelb")
Signed-off-by: Dongliang Mu <mudongliangabcd@gmail.com>
---
 drivers/net/ieee802154/mac802154_hwsim.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/net/ieee802154/mac802154_hwsim.c b/drivers/net/ieee802154/mac802154_hwsim.c
index ebc976b7fcc2..d97ed033ac77 100644
--- a/drivers/net/ieee802154/mac802154_hwsim.c
+++ b/drivers/net/ieee802154/mac802154_hwsim.c
@@ -132,9 +132,11 @@ static int hwsim_hw_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
 			struct sk_buff *newskb = pskb_copy(skb, GFP_ATOMIC);
 
 			einfo = rcu_dereference(e->info);
-			if (newskb)
+			if (newskb) {
 				ieee802154_rx_irqsafe(e->endpoint->hw, newskb,
 						      einfo->lqi);
+				kfree_skb(newskb);
+			}
 		}
 	}
 	rcu_read_unlock();
-- 
2.25.1


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

* Re: [PATCH] ieee802154: hwsim: fix memory leak in __pskb_copy_fclone
  2021-07-13  5:40 [PATCH] ieee802154: hwsim: fix memory leak in __pskb_copy_fclone Dongliang Mu
@ 2021-07-13 13:47 ` Alexander Aring
  2021-07-13 14:03   ` Dongliang Mu
  0 siblings, 1 reply; 3+ messages in thread
From: Alexander Aring @ 2021-07-13 13:47 UTC (permalink / raw)
  To: Dongliang Mu
  Cc: Stefan Schmidt, David S. Miller, Jakub Kicinski, Alexander Aring,
	linux-wpan - ML, open list:NETWORKING [GENERAL],
	kernel list

Hi,

On Tue, 13 Jul 2021 at 01:40, Dongliang Mu <mudongliangabcd@gmail.com> wrote:
>
> hwsim_hw_xmit fails to deallocate the newskb copied by pskb_copy. Fix
> this by adding kfree_skb after ieee802154_rx_irqsafe.
>
>   [<ffffffff836433fb>] __alloc_skb+0x22b/0x250 net/core/skbuff.c:414
>   [<ffffffff8364ad95>] __pskb_copy_fclone+0x75/0x360 net/core/skbuff.c:1609
>   [<ffffffff82ae65e3>] __pskb_copy include/linux/skbuff.h:1176 [inline]
>   [<ffffffff82ae65e3>] pskb_copy include/linux/skbuff.h:3207 [inline]
>   [<ffffffff82ae65e3>] hwsim_hw_xmit+0xd3/0x140 drivers/net/ieee802154/mac802154_hwsim.c:132
>   [<ffffffff83ff8f47>] drv_xmit_async net/mac802154/driver-ops.h:16 [inline]
>   [<ffffffff83ff8f47>] ieee802154_tx+0xc7/0x190 net/mac802154/tx.c:83
>   [<ffffffff83ff9138>] ieee802154_subif_start_xmit+0x58/0x70 net/mac802154/tx.c:132
>   [<ffffffff83670b82>] __netdev_start_xmit include/linux/netdevice.h:4944 [inline]
>   [<ffffffff83670b82>] netdev_start_xmit include/linux/netdevice.h:4958 [inline]
>   [<ffffffff83670b82>] xmit_one net/core/dev.c:3658 [inline]
>   [<ffffffff83670b82>] dev_hard_start_xmit+0xe2/0x330 net/core/dev.c:3674
>   [<ffffffff83718028>] sch_direct_xmit+0xf8/0x520 net/sched/sch_generic.c:342
>   [<ffffffff8367193b>] __dev_xmit_skb net/core/dev.c:3874 [inline]
>   [<ffffffff8367193b>] __dev_queue_xmit+0xa3b/0x1360 net/core/dev.c:4241
>   [<ffffffff83ff5437>] dgram_sendmsg+0x437/0x570 net/ieee802154/socket.c:682
>   [<ffffffff836345b6>] sock_sendmsg_nosec net/socket.c:702 [inline]
>   [<ffffffff836345b6>] sock_sendmsg+0x56/0x80 net/socket.c:722
>
> Fixes: f25da51fdc38 ("ieee802154: hwsim: add replacement for fakelb")
> Signed-off-by: Dongliang Mu <mudongliangabcd@gmail.com>

sorry, I don't get the fix. Is this a memory leak? I remember there
was something reported by syzkaller [0] but I wasn't able yet to get
into it. Is it what you referring to?
__pskb_copy_fclone() shows "The returned buffer has a reference count
of 1" and ieee802154_rx_irqsafe() will queue the skb for a tasklet.
With your patch it will be immediately freed and a use after free will
occur. I believe there is something wrong in the error path of
802.15.4 frame parsing and that's why we sometimes have a leaks there.

I need to test this patch, but I don't get how this patch is supposed
to fix the issue.

- Alex

[0] https://groups.google.com/g/syzkaller-bugs/c/EoIvZbk3Zfo/m/AlKUiErlAwAJ

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

* Re: [PATCH] ieee802154: hwsim: fix memory leak in __pskb_copy_fclone
  2021-07-13 13:47 ` Alexander Aring
@ 2021-07-13 14:03   ` Dongliang Mu
  0 siblings, 0 replies; 3+ messages in thread
From: Dongliang Mu @ 2021-07-13 14:03 UTC (permalink / raw)
  To: Alexander Aring
  Cc: Stefan Schmidt, David S. Miller, Jakub Kicinski, Alexander Aring,
	linux-wpan - ML, open list:NETWORKING [GENERAL],
	kernel list

On Tue, Jul 13, 2021 at 9:47 PM Alexander Aring <alex.aring@gmail.com> wrote:
>
> Hi,
>
> On Tue, 13 Jul 2021 at 01:40, Dongliang Mu <mudongliangabcd@gmail.com> wrote:
> >
> > hwsim_hw_xmit fails to deallocate the newskb copied by pskb_copy. Fix
> > this by adding kfree_skb after ieee802154_rx_irqsafe.
> >
> >   [<ffffffff836433fb>] __alloc_skb+0x22b/0x250 net/core/skbuff.c:414
> >   [<ffffffff8364ad95>] __pskb_copy_fclone+0x75/0x360 net/core/skbuff.c:1609
> >   [<ffffffff82ae65e3>] __pskb_copy include/linux/skbuff.h:1176 [inline]
> >   [<ffffffff82ae65e3>] pskb_copy include/linux/skbuff.h:3207 [inline]
> >   [<ffffffff82ae65e3>] hwsim_hw_xmit+0xd3/0x140 drivers/net/ieee802154/mac802154_hwsim.c:132
> >   [<ffffffff83ff8f47>] drv_xmit_async net/mac802154/driver-ops.h:16 [inline]
> >   [<ffffffff83ff8f47>] ieee802154_tx+0xc7/0x190 net/mac802154/tx.c:83
> >   [<ffffffff83ff9138>] ieee802154_subif_start_xmit+0x58/0x70 net/mac802154/tx.c:132
> >   [<ffffffff83670b82>] __netdev_start_xmit include/linux/netdevice.h:4944 [inline]
> >   [<ffffffff83670b82>] netdev_start_xmit include/linux/netdevice.h:4958 [inline]
> >   [<ffffffff83670b82>] xmit_one net/core/dev.c:3658 [inline]
> >   [<ffffffff83670b82>] dev_hard_start_xmit+0xe2/0x330 net/core/dev.c:3674
> >   [<ffffffff83718028>] sch_direct_xmit+0xf8/0x520 net/sched/sch_generic.c:342
> >   [<ffffffff8367193b>] __dev_xmit_skb net/core/dev.c:3874 [inline]
> >   [<ffffffff8367193b>] __dev_queue_xmit+0xa3b/0x1360 net/core/dev.c:4241
> >   [<ffffffff83ff5437>] dgram_sendmsg+0x437/0x570 net/ieee802154/socket.c:682
> >   [<ffffffff836345b6>] sock_sendmsg_nosec net/socket.c:702 [inline]
> >   [<ffffffff836345b6>] sock_sendmsg+0x56/0x80 net/socket.c:722
> >
> > Fixes: f25da51fdc38 ("ieee802154: hwsim: add replacement for fakelb")
> > Signed-off-by: Dongliang Mu <mudongliangabcd@gmail.com>
>
> sorry, I don't get the fix. Is this a memory leak? I remember there
> was something reported by syzkaller [0] but I wasn't able yet to get
> into it. Is it what you referring to?

Yes, you're right.

I get this memory leak many times in my local syzkaller instance but
do not recognize there is already a bug report in the syzbot
dashboard.

> __pskb_copy_fclone() shows "The returned buffer has a reference count
> of 1" and ieee802154_rx_irqsafe() will queue the skb for a tasklet.
> With your patch it will be immediately freed and a use after free will
> occur.

Thanks for your feedback. I am sorry about this fix since I did not
observe UAF in my testing.

I will keep learning more materials about socket in Linux kernel. :)

> I believe there is something wrong in the error path of
> 802.15.4 frame parsing and that's why we sometimes have a leaks there.

Should be yes, it occurs many times in my local syzkaller instance.

>
> I need to test this patch, but I don't get how this patch is supposed
> to fix the issue.

This patch should be incorrect. Please directly focus on bug reports
on the syzbot dashboard. If possible, please cc me your final patch
about this bug. I can learn something from this bug.

Best regards
Dongliang Mu

>
> - Alex
>
> [0] https://groups.google.com/g/syzkaller-bugs/c/EoIvZbk3Zfo/m/AlKUiErlAwAJ

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

end of thread, other threads:[~2021-07-13 14:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-13  5:40 [PATCH] ieee802154: hwsim: fix memory leak in __pskb_copy_fclone Dongliang Mu
2021-07-13 13:47 ` Alexander Aring
2021-07-13 14:03   ` Dongliang Mu

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).