]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - queue-4.9/mdio_bus-fix-use-after-free-on-device_register-fails.patch
Linux 4.14.107
[thirdparty/kernel/stable-queue.git] / queue-4.9 / mdio_bus-fix-use-after-free-on-device_register-fails.patch
1 From foo@baz Fri Mar 15 21:00:09 PDT 2019
2 From: YueHaibing <yuehaibing@huawei.com>
3 Date: Thu, 21 Feb 2019 22:42:01 +0800
4 Subject: mdio_bus: Fix use-after-free on device_register fails
5
6 From: YueHaibing <yuehaibing@huawei.com>
7
8 [ Upstream commit 6ff7b060535e87c2ae14dd8548512abfdda528fb ]
9
10 KASAN has found use-after-free in fixed_mdio_bus_init,
11 commit 0c692d07842a ("drivers/net/phy/mdio_bus.c: call
12 put_device on device_register() failure") call put_device()
13 while device_register() fails,give up the last reference
14 to the device and allow mdiobus_release to be executed
15 ,kfreeing the bus. However in most drives, mdiobus_free
16 be called to free the bus while mdiobus_register fails.
17 use-after-free occurs when access bus again, this patch
18 revert it to let mdiobus_free free the bus.
19
20 KASAN report details as below:
21
22 BUG: KASAN: use-after-free in mdiobus_free+0x85/0x90 drivers/net/phy/mdio_bus.c:482
23 Read of size 4 at addr ffff8881dc824d78 by task syz-executor.0/3524
24
25 CPU: 1 PID: 3524 Comm: syz-executor.0 Not tainted 5.0.0-rc7+ #45
26 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1ubuntu1 04/01/2014
27 Call Trace:
28 __dump_stack lib/dump_stack.c:77 [inline]
29 dump_stack+0xfa/0x1ce lib/dump_stack.c:113
30 print_address_description+0x65/0x270 mm/kasan/report.c:187
31 kasan_report+0x149/0x18d mm/kasan/report.c:317
32 mdiobus_free+0x85/0x90 drivers/net/phy/mdio_bus.c:482
33 fixed_mdio_bus_init+0x283/0x1000 [fixed_phy]
34 ? 0xffffffffc0e40000
35 ? 0xffffffffc0e40000
36 ? 0xffffffffc0e40000
37 do_one_initcall+0xfa/0x5ca init/main.c:887
38 do_init_module+0x204/0x5f6 kernel/module.c:3460
39 load_module+0x66b2/0x8570 kernel/module.c:3808
40 __do_sys_finit_module+0x238/0x2a0 kernel/module.c:3902
41 do_syscall_64+0x147/0x600 arch/x86/entry/common.c:290
42 entry_SYSCALL_64_after_hwframe+0x49/0xbe
43 RIP: 0033:0x462e99
44 Code: f7 d8 64 89 02 b8 ff ff ff ff c3 66 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 bc ff ff ff f7 d8 64 89 01 48
45 RSP: 002b:00007f6215c19c58 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
46 RAX: ffffffffffffffda RBX: 000000000073bf00 RCX: 0000000000462e99
47 RDX: 0000000000000000 RSI: 0000000020000080 RDI: 0000000000000003
48 RBP: 00007f6215c19c70 R08: 0000000000000000 R09: 0000000000000000
49 R10: 0000000000000000 R11: 0000000000000246 R12: 00007f6215c1a6bc
50 R13: 00000000004bcefb R14: 00000000006f7030 R15: 0000000000000004
51
52 Allocated by task 3524:
53 set_track mm/kasan/common.c:85 [inline]
54 __kasan_kmalloc.constprop.3+0xa0/0xd0 mm/kasan/common.c:496
55 kmalloc include/linux/slab.h:545 [inline]
56 kzalloc include/linux/slab.h:740 [inline]
57 mdiobus_alloc_size+0x54/0x1b0 drivers/net/phy/mdio_bus.c:143
58 fixed_mdio_bus_init+0x163/0x1000 [fixed_phy]
59 do_one_initcall+0xfa/0x5ca init/main.c:887
60 do_init_module+0x204/0x5f6 kernel/module.c:3460
61 load_module+0x66b2/0x8570 kernel/module.c:3808
62 __do_sys_finit_module+0x238/0x2a0 kernel/module.c:3902
63 do_syscall_64+0x147/0x600 arch/x86/entry/common.c:290
64 entry_SYSCALL_64_after_hwframe+0x49/0xbe
65
66 Freed by task 3524:
67 set_track mm/kasan/common.c:85 [inline]
68 __kasan_slab_free+0x130/0x180 mm/kasan/common.c:458
69 slab_free_hook mm/slub.c:1409 [inline]
70 slab_free_freelist_hook mm/slub.c:1436 [inline]
71 slab_free mm/slub.c:2986 [inline]
72 kfree+0xe1/0x270 mm/slub.c:3938
73 device_release+0x78/0x200 drivers/base/core.c:919
74 kobject_cleanup lib/kobject.c:662 [inline]
75 kobject_release lib/kobject.c:691 [inline]
76 kref_put include/linux/kref.h:67 [inline]
77 kobject_put+0x146/0x240 lib/kobject.c:708
78 put_device+0x1c/0x30 drivers/base/core.c:2060
79 __mdiobus_register+0x483/0x560 drivers/net/phy/mdio_bus.c:382
80 fixed_mdio_bus_init+0x26b/0x1000 [fixed_phy]
81 do_one_initcall+0xfa/0x5ca init/main.c:887
82 do_init_module+0x204/0x5f6 kernel/module.c:3460
83 load_module+0x66b2/0x8570 kernel/module.c:3808
84 __do_sys_finit_module+0x238/0x2a0 kernel/module.c:3902
85 do_syscall_64+0x147/0x600 arch/x86/entry/common.c:290
86 entry_SYSCALL_64_after_hwframe+0x49/0xbe
87
88 The buggy address belongs to the object at ffff8881dc824c80
89 which belongs to the cache kmalloc-2k of size 2048
90 The buggy address is located 248 bytes inside of
91 2048-byte region [ffff8881dc824c80, ffff8881dc825480)
92 The buggy address belongs to the page:
93 page:ffffea0007720800 count:1 mapcount:0 mapping:ffff8881f6c02800 index:0x0 compound_mapcount: 0
94 flags: 0x2fffc0000010200(slab|head)
95 raw: 02fffc0000010200 0000000000000000 0000000500000001 ffff8881f6c02800
96 raw: 0000000000000000 00000000800f000f 00000001ffffffff 0000000000000000
97 page dumped because: kasan: bad access detected
98
99 Memory state around the buggy address:
100 ffff8881dc824c00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
101 ffff8881dc824c80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
102 >ffff8881dc824d00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
103 ^
104 ffff8881dc824d80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
105 ffff8881dc824e00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
106
107 Fixes: 0c692d07842a ("drivers/net/phy/mdio_bus.c: call put_device on device_register() failure")
108 Signed-off-by: YueHaibing <yuehaibing@huawei.com>
109 Reviewed-by: Andrew Lunn <andrew@lunn.ch>
110 Signed-off-by: David S. Miller <davem@davemloft.net>
111 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
112 ---
113 drivers/net/phy/mdio_bus.c | 1 -
114 1 file changed, 1 deletion(-)
115
116 --- a/drivers/net/phy/mdio_bus.c
117 +++ b/drivers/net/phy/mdio_bus.c
118 @@ -319,7 +319,6 @@ int __mdiobus_register(struct mii_bus *b
119 err = device_register(&bus->dev);
120 if (err) {
121 pr_err("mii_bus %s failed to register\n", bus->id);
122 - put_device(&bus->dev);
123 return -EINVAL;
124 }
125