[syzbot] KASAN: use-after-free Read in em28xx_close_extension

42 views
Skip to first unread message

syzbot

unread,
Jul 14, 2021, 10:27:19 AM7/14/21
to hverkui...@xs4all.nl, igormt...@gmail.com, linux-...@vger.kernel.org, linux...@vger.kernel.org, mchehab...@kernel.org, mch...@kernel.org, syzkall...@googlegroups.com
Hello,

syzbot found the following issue on:

HEAD commit: 3dbdb38e Merge branch 'for-5.14' of git://git.kernel.org/p..
git tree: upstream
console output: https://syzkaller.appspot.com/x/log.txt?x=158cf1e2300000
kernel config: https://syzkaller.appspot.com/x/.config?x=a1fcf15a09815757
dashboard link: https://syzkaller.appspot.com/bug?extid=005037419ebdf14e1d87
syz repro: https://syzkaller.appspot.com/x/repro.syz?x=169746b0300000
C reproducer: https://syzkaller.appspot.com/x/repro.c?x=157e89c4300000

The issue was bisected to:

commit ac5688637144644f06ed1f3c6d4dd8bb7db96020
Author: Igor Matheus Andrade Torrente <igormt...@gmail.com>
Date: Tue May 4 18:32:49 2021 +0000

media: em28xx: Fix possible memory leak of em28xx struct

bisection log: https://syzkaller.appspot.com/x/bisect.txt?x=1742cffc300000
final oops: https://syzkaller.appspot.com/x/report.txt?x=14c2cffc300000
console output: https://syzkaller.appspot.com/x/log.txt?x=10c2cffc300000

IMPORTANT: if you fix the issue, please add the following tag to the commit:
Reported-by: syzbot+005037...@syzkaller.appspotmail.com
Fixes: ac5688637144 ("media: em28xx: Fix possible memory leak of em28xx struct")

usb 1-1: bad CDC descriptors
usb 1-1: USB disconnect, device number 2
em28xx 1-1:64.138: Disconnecting em28xx
em28xx 1-1:64.138: Closing input extension
em28xx 1-1:64.138: Freeing device
==================================================================
BUG: KASAN: use-after-free in __list_del_entry_valid+0xcc/0xf0 lib/list_debug.c:42
Read of size 8 at addr ffff888021aa8258 by task kworker/1:2/3456

CPU: 1 PID: 3456 Comm: kworker/1:2 Not tainted 5.13.0-syzkaller #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
Workqueue: usb_hub_wq hub_event
Call Trace:
__dump_stack lib/dump_stack.c:79 [inline]
dump_stack_lvl+0xcd/0x134 lib/dump_stack.c:96
print_address_description.constprop.0.cold+0x6c/0x309 mm/kasan/report.c:233
__kasan_report mm/kasan/report.c:419 [inline]
kasan_report.cold+0x83/0xdf mm/kasan/report.c:436
__list_del_entry_valid+0xcc/0xf0 lib/list_debug.c:42
__list_del_entry include/linux/list.h:132 [inline]
list_del include/linux/list.h:146 [inline]
em28xx_close_extension+0x10b/0x2a0 drivers/media/usb/em28xx/em28xx-core.c:1146
em28xx_usb_disconnect.cold+0x14b/0x237 drivers/media/usb/em28xx/em28xx-cards.c:4140
usb_unbind_interface+0x1d8/0x8d0 drivers/usb/core/driver.c:458
__device_release_driver+0x3bd/0x6f0 drivers/base/dd.c:1181
device_release_driver_internal drivers/base/dd.c:1212 [inline]
device_release_driver+0x26/0x40 drivers/base/dd.c:1235
bus_remove_device+0x2eb/0x5a0 drivers/base/bus.c:533
device_del+0x502/0xd40 drivers/base/core.c:3512
usb_disable_device+0x35b/0x7b0 drivers/usb/core/message.c:1413
usb_disconnect.cold+0x27a/0x78e drivers/usb/core/hub.c:2221
hub_port_connect drivers/usb/core/hub.c:5129 [inline]
hub_port_connect_change drivers/usb/core/hub.c:5418 [inline]
port_event drivers/usb/core/hub.c:5564 [inline]
hub_event+0x1c9c/0x4330 drivers/usb/core/hub.c:5646
process_one_work+0x98d/0x1630 kernel/workqueue.c:2276
process_scheduled_works kernel/workqueue.c:2338 [inline]
worker_thread+0x85c/0x11f0 kernel/workqueue.c:2424
kthread+0x3e5/0x4d0 kernel/kthread.c:319
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:295

The buggy address belongs to the page:
page:ffffea000086aa00 refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x21aa8
flags: 0xfff00000000000(node=0|zone=1|lastcpupid=0x7ff)
raw: 00fff00000000000 ffffea0000ac0d08 ffff8880b9d3b288 0000000000000000
raw: 0000000000000000 0000000000000000 00000000ffffffff 0000000000000000
page dumped because: kasan: bad access detected
page_owner tracks the page as freed
page last allocated via order 2, migratetype Unmovable, gfp_mask 0x40dc0(GFP_KERNEL|__GFP_COMP|__GFP_ZERO), pid 3456, ts 70583523123, free_ts 71218691485
prep_new_page mm/page_alloc.c:2445 [inline]
get_page_from_freelist+0xa72/0x2f80 mm/page_alloc.c:4178
__alloc_pages+0x1b2/0x500 mm/page_alloc.c:5386
alloc_pages+0x18c/0x2a0 mm/mempolicy.c:2272
kmalloc_order+0x34/0xf0 mm/slab_common.c:945
kmalloc_order_trace+0x14/0x120 mm/slab_common.c:961
kmalloc_large include/linux/slab.h:520 [inline]
kmalloc include/linux/slab.h:584 [inline]
kzalloc include/linux/slab.h:721 [inline]
em28xx_usb_probe+0x1f7/0xd00 drivers/media/usb/em28xx/em28xx-cards.c:3855
usb_probe_interface+0x315/0x7f0 drivers/usb/core/driver.c:396
really_probe+0x291/0xf60 drivers/base/dd.c:576
driver_probe_device+0x298/0x410 drivers/base/dd.c:763
__device_attach_driver+0x203/0x2c0 drivers/base/dd.c:870
bus_for_each_drv+0x15f/0x1e0 drivers/base/bus.c:431
__device_attach+0x228/0x4a0 drivers/base/dd.c:938
bus_probe_device+0x1e4/0x290 drivers/base/bus.c:491
device_add+0xbe0/0x2100 drivers/base/core.c:3324
usb_set_configuration+0x113f/0x1910 drivers/usb/core/message.c:2164
usb_generic_driver_probe+0xba/0x100 drivers/usb/core/generic.c:238
page last free stack trace:
reset_page_owner include/linux/page_owner.h:24 [inline]
free_pages_prepare mm/page_alloc.c:1355 [inline]
free_pcp_prepare+0x2c5/0x780 mm/page_alloc.c:1406
free_unref_page_prepare mm/page_alloc.c:3341 [inline]
free_unref_page+0x19/0x690 mm/page_alloc.c:3420
__refcount_sub_and_test include/linux/refcount.h:282 [inline]
__refcount_dec_and_test include/linux/refcount.h:315 [inline]
refcount_dec_and_test include/linux/refcount.h:333 [inline]
kref_put.constprop.0.isra.0+0x3d/0x7e include/linux/kref.h:64
em28xx_ir_fini.cold+0x7c/0x120 drivers/media/usb/em28xx/em28xx-input.c:875
em28xx_close_extension+0xc9/0x2a0 drivers/media/usb/em28xx/em28xx-core.c:1143
em28xx_usb_disconnect.cold+0x14b/0x237 drivers/media/usb/em28xx/em28xx-cards.c:4140
usb_unbind_interface+0x1d8/0x8d0 drivers/usb/core/driver.c:458
__device_release_driver+0x3bd/0x6f0 drivers/base/dd.c:1181
device_release_driver_internal drivers/base/dd.c:1212 [inline]
device_release_driver+0x26/0x40 drivers/base/dd.c:1235
bus_remove_device+0x2eb/0x5a0 drivers/base/bus.c:533
device_del+0x502/0xd40 drivers/base/core.c:3512
usb_disable_device+0x35b/0x7b0 drivers/usb/core/message.c:1413
usb_disconnect.cold+0x27a/0x78e drivers/usb/core/hub.c:2221
hub_port_connect drivers/usb/core/hub.c:5129 [inline]
hub_port_connect_change drivers/usb/core/hub.c:5418 [inline]
port_event drivers/usb/core/hub.c:5564 [inline]
hub_event+0x1c9c/0x4330 drivers/usb/core/hub.c:5646
process_one_work+0x98d/0x1630 kernel/workqueue.c:2276
process_scheduled_works kernel/workqueue.c:2338 [inline]
worker_thread+0x85c/0x11f0 kernel/workqueue.c:2424

Memory state around the buggy address:
ffff888021aa8100: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ffff888021aa8180: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
>ffff888021aa8200: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
^
ffff888021aa8280: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ffff888021aa8300: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
==================================================================


---
This report is generated by a bot. It may contain errors.
See https://goo.gl/tpsmEJ for more information about syzbot.
syzbot engineers can be reached at syzk...@googlegroups.com.

syzbot will keep track of this issue. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.
For information about bisection process see: https://goo.gl/tpsmEJ#bisection
syzbot can test patches for this issue, for details see:
https://goo.gl/tpsmEJ#testing-patches

syzbot

unread,
Jul 15, 2021, 6:25:09 AM7/15/21
to mudongl...@gmail.com, syzkall...@googlegroups.com
Hello,

syzbot has tested the proposed patch and the reproducer did not trigger any issue:

Reported-and-tested-by: syzbot+005037...@syzkaller.appspotmail.com

Tested on:

commit: 8096acd7 Merge tag 'net-5.14-rc2' of git://git.kernel...
git tree: upstream
kernel config: https://syzkaller.appspot.com/x/.config?x=fd3d21e4b63c61eb
dashboard link: https://syzkaller.appspot.com/bug?extid=005037419ebdf14e1d87
compiler:
patch: https://syzkaller.appspot.com/x/patch.diff?x=13733254300000

Note: testing is done by a robot and is best-effort only.

Hillf Danton

unread,
Jul 27, 2021, 6:02:14 AM7/27/21
to syzbot, igormt...@gmail.com, Hillf Danton, linux-...@vger.kernel.org, linux...@vger.kernel.org, syzkall...@googlegroups.com
On Wed, 14 Jul 2021 07:27:17 -0700
Along the probe path,

em28xx_usb_probe
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
retval = em28xx_init_dev(dev, udev, intf, nr);
em28xx_init_extension(dev);
em28xx_ir_init(struct em28xx *dev)
kref_get(&dev->ref);

kref_init(&dev->ref);

kref leak occurs because kref_init falls behind kref_get, and it is fixed
by initializing kref as early as it should be.

The same applies to dev->dev_next.

+++ x/drivers/media/usb/em28xx/em28xx-cards.c
@@ -3854,6 +3854,8 @@ static int em28xx_usb_probe(struct usb_i
goto err;
}

+ kref_init(&dev->ref);
+
/* compute alternate max packet sizes */
dev->alt_max_pkt_size_isoc = kcalloc(intf->num_altsetting,
sizeof(dev->alt_max_pkt_size_isoc[0]),
@@ -4029,6 +4031,7 @@ static int em28xx_usb_probe(struct usb_i
dev->dev_next->ifnum = ifnum;
dev->dev_next->model = id->driver_info;

+ kref_init(&dev->dev_next->ref);
mutex_init(&dev->dev_next->lock);
retval = em28xx_init_dev(dev->dev_next, udev, intf,
dev->dev_next->devno);
@@ -4075,12 +4078,8 @@ static int em28xx_usb_probe(struct usb_i
em28xx_write_reg(dev, 0x0b, 0x82);
mdelay(100);
}
-
- kref_init(&dev->dev_next->ref);
}

- kref_init(&dev->ref);
-
request_modules(dev);

/*

Dan Carpenter

unread,
Jul 27, 2021, 10:15:18 AM7/27/21
to Hillf Danton, syzbot, igormt...@gmail.com, linux-...@vger.kernel.org, linux...@vger.kernel.org, syzkall...@googlegroups.com
On Tue, Jul 27, 2021 at 06:01:51PM +0800, Hillf Danton wrote:
> Along the probe path,
>
> em28xx_usb_probe
> dev = kzalloc(sizeof(*dev), GFP_KERNEL);
> retval = em28xx_init_dev(dev, udev, intf, nr);
> em28xx_init_extension(dev);
> em28xx_ir_init(struct em28xx *dev)
> kref_get(&dev->ref);
>
> kref_init(&dev->ref);

Good detective work.

I've created a Smatch check to try find these. It uses the fact that
Smatch creates a bunch of fake assignments to set all the struct members
of "dev" to zero. Then it uses the modification hook to find the
kref_init(). Those are sort of new uses for those hooks so that's quite
fun.

I'll test it out overnight and see how it works.

drivers/media/usb/em28xx/em28xx-cards.c:4086 em28xx_usb_probe() warn: kref has already been modifed (see line 3979)

regards,
dan carpenter

#include "smatch.h"
#include "smatch_slist.h"
#include "smatch_extra.h"

static int my_id;

STATE(fresh);

static int get_line(struct sm_state *sm)
{
struct sm_state *tmp;
int line = 0;

FOR_EACH_PTR(sm->possible, tmp) {
if (tmp->state == &undefined &&
tmp->line > line)
line = tmp->line;
} END_FOR_EACH_PTR(tmp);

if (!line)
return sm->line;
return line;
}

static void match_modify(struct sm_state *sm, struct expression *mod_expr)
{
if (sm->state != &fresh &&
mod_expr &&
mod_expr->type == EXPR_CALL &&
sym_name_is("kref_init", mod_expr->fn))
sm_warning("kref has already been modifed (see line %d)", get_line(sm));

set_state(my_id, sm->name, sm->sym, &undefined);
}

static bool is_alloc(struct expression *expr)
{
static struct expression *ignore, *alloc_expr;
struct expression *right;

if (!expr || expr->type != EXPR_ASSIGNMENT || expr->op != '=')
return false;
if (expr == ignore)
return false;
if (expr == alloc_expr)
return true;
right = strip_expr(expr->right);
if (right->type == EXPR_CALL &&
(sym_name_is("kzalloc", right->fn) ||
sym_name_is("kmalloc", right->fn))) {
alloc_expr = expr;
return true;
}
ignore = expr;
return false;
}

static void match_assign(struct expression *expr)
{
char *name;

if (!is_alloc(get_faked_expression()))
return;
name = expr_to_str(expr->left);
if (name && strstr(name, "refcount.refs.counter"))
set_state_expr(my_id, expr->left, &fresh);
free_string(name);
}

void check_kref_init_too_late(int id)
{
my_id = id;

add_hook(&match_assign, ASSIGNMENT_HOOK_AFTER);
add_modification_hook(my_id, &match_modify);
}

Dongliang Mu

unread,
Jul 27, 2021, 11:13:41 AM7/27/21
to Dan Carpenter, Hillf Danton, syzbot, Igor Matheus Andrade Torrente, linux-kernel, linux...@vger.kernel.org, syzkaller-bugs
On Tue, Jul 27, 2021 at 10:15 PM Dan Carpenter <dan.ca...@oracle.com> wrote:
>
> On Tue, Jul 27, 2021 at 06:01:51PM +0800, Hillf Danton wrote:
> > Along the probe path,
> >
> > em28xx_usb_probe
> > dev = kzalloc(sizeof(*dev), GFP_KERNEL);
> > retval = em28xx_init_dev(dev, udev, intf, nr);
> > em28xx_init_extension(dev);
> > em28xx_ir_init(struct em28xx *dev)
> > kref_get(&dev->ref);
> >
> > kref_init(&dev->ref);
>

Hi Dan,

I have developed a patch [1] to fix this crash. Would you like to help
me double-check if it correctly fixes the underlying bug?

[1] [PATCH v2] [media] em28xx-input: fix refcount bug in
em28xx_usb_disconnect https://lkml.org/lkml/2021/7/19/263
> --
> You received this message because you are subscribed to the Google Groups "syzkaller-bugs" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to syzkaller-bug...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/syzkaller-bugs/20210727141455.GM1931%40kadam.

Dan Carpenter

unread,
Jul 27, 2021, 11:33:52 AM7/27/21
to Dongliang Mu, Hillf Danton, syzbot, Igor Matheus Andrade Torrente, linux-kernel, linux...@vger.kernel.org, syzkaller-bugs
On Tue, Jul 27, 2021 at 11:13:13PM +0800, Dongliang Mu wrote:
> On Tue, Jul 27, 2021 at 10:15 PM Dan Carpenter <dan.ca...@oracle.com> wrote:
> >
> > On Tue, Jul 27, 2021 at 06:01:51PM +0800, Hillf Danton wrote:
> > > Along the probe path,
> > >
> > > em28xx_usb_probe
> > > dev = kzalloc(sizeof(*dev), GFP_KERNEL);
> > > retval = em28xx_init_dev(dev, udev, intf, nr);
> > > em28xx_init_extension(dev);
> > > em28xx_ir_init(struct em28xx *dev)
> > > kref_get(&dev->ref);
> > >
> > > kref_init(&dev->ref);
> >
>
> Hi Dan,
>
> I have developed a patch [1] to fix this crash. Would you like to help
> me double-check if it correctly fixes the underlying bug?
>
> [1] [PATCH v2] [media] em28xx-input: fix refcount bug in
> em28xx_usb_disconnect https://lkml.org/lkml/2021/7/19/263

No one likes to review patches which aren't sent over email... :/
LKML.org doesn't give the message-id so I can't use b4 to download the
email.

That patch doesn't make any sense. We call kref_get() at the start of
the function so we need to call kref_put() at the end. The fact that
em28xx_ir_fini() calls kref_put() doesn't matter. We will only call
fini if the init() succeeds (hopefully). The em28xx_ir_init() error
handling is Ad Hoc Style but I don't see anything outright buggy.

I think Hillf's approach is correct.

regards,
dan carpenter

Dan Carpenter

unread,
Jul 28, 2021, 9:39:37 AM7/28/21
to Hillf Danton, syzbot, igormt...@gmail.com, linux-...@vger.kernel.org, linux...@vger.kernel.org, syzkall...@googlegroups.com
On Tue, Jul 27, 2021 at 05:14:55PM +0300, Dan Carpenter wrote:
> On Tue, Jul 27, 2021 at 06:01:51PM +0800, Hillf Danton wrote:
> > Along the probe path,
> >
> > em28xx_usb_probe
> > dev = kzalloc(sizeof(*dev), GFP_KERNEL);
> > retval = em28xx_init_dev(dev, udev, intf, nr);
> > em28xx_init_extension(dev);
> > em28xx_ir_init(struct em28xx *dev)
> > kref_get(&dev->ref);
> >
> > kref_init(&dev->ref);
>
> Good detective work.
>
> I've created a Smatch check to try find these. It uses the fact that
> Smatch creates a bunch of fake assignments to set all the struct members
> of "dev" to zero. Then it uses the modification hook to find the
> kref_init(). Those are sort of new uses for those hooks so that's quite
> fun.
>
> I'll test it out overnight and see how it works.

My Smatch check didn't find any other bugs, but it only had 3 false
positives so I'll keep running it nightly on new code.

regards,
dan carpenter

syzbot

unread,
Oct 1, 2021, 4:55:08 AM10/1/21
to dan.ca...@oracle.com, hda...@sina.com, hverkui...@xs4all.nl, igormt...@gmail.com, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linux...@vger.kernel.org, mch...@kernel.org, mudongl...@gmail.com, stephen....@oracle.com, syzkall...@googlegroups.com, vi...@zeniv.linux.org.uk
syzbot suspects this issue was fixed by commit:

commit 0766ec82e5fb26fc5dc6d592bc61865608bdc651
Author: Stephen Brennan <stephen....@oracle.com>
Date: Wed Sep 1 17:51:41 2021 +0000

namei: Fix use after free in kern_path_locked

bisection log: https://syzkaller.appspot.com/x/bisect.txt?x=17bf2a03300000
start commit: fa54d366a6e4 Merge tag 'acpi-5.14-rc7' of git://git.kernel..
git tree: upstream
kernel config: https://syzkaller.appspot.com/x/.config?x=96f0602203250753
dashboard link: https://syzkaller.appspot.com/bug?extid=005037419ebdf14e1d87
syz repro: https://syzkaller.appspot.com/x/repro.syz?x=14c086c5300000
C reproducer: https://syzkaller.appspot.com/x/repro.c?x=12950bee300000

If the result looks correct, please mark the issue as fixed by replying with:

#syz fix: namei: Fix use after free in kern_path_locked

syzbot

unread,
Sep 30, 2022, 8:26:27 AM9/30/22
to syzkall...@googlegroups.com
Auto-closing this bug as obsolete.
No recent activity, existing reproducers are no longer triggering the issue.
Reply all
Reply to author
Forward
0 new messages