f0275396d5d3e74efa52c4681b13fd91b65ba682
[openwrt/staging/mans0n.git] /
1 From 13696b0d3219c3ca9ff4ce6a580c53fab6284312 Mon Sep 17 00:00:00 2001
2 From: Eric Dumazet <edumazet@google.com>
3 Date: Tue, 4 Feb 2020 22:17:25 +0100
4 Subject: [PATCH 082/124] wireguard: allowedips: fix use-after-free in
5 root_remove_peer_lists
6
7 commit 9981159fc3b677b357f84e069a11de5a5ec8a2a8 upstream.
8
9 In the unlikely case a new node could not be allocated, we need to
10 remove @newnode from @peer->allowedips_list before freeing it.
11
12 syzbot reported:
13
14 BUG: KASAN: use-after-free in __list_del_entry_valid+0xdc/0xf5 lib/list_debug.c:54
15 Read of size 8 at addr ffff88809881a538 by task syz-executor.4/30133
16
17 CPU: 0 PID: 30133 Comm: syz-executor.4 Not tainted 5.5.0-syzkaller #0
18 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
19 Call Trace:
20 __dump_stack lib/dump_stack.c:77 [inline]
21 dump_stack+0x197/0x210 lib/dump_stack.c:118
22 print_address_description.constprop.0.cold+0xd4/0x30b mm/kasan/report.c:374
23 __kasan_report.cold+0x1b/0x32 mm/kasan/report.c:506
24 kasan_report+0x12/0x20 mm/kasan/common.c:639
25 __asan_report_load8_noabort+0x14/0x20 mm/kasan/generic_report.c:135
26 __list_del_entry_valid+0xdc/0xf5 lib/list_debug.c:54
27 __list_del_entry include/linux/list.h:132 [inline]
28 list_del include/linux/list.h:146 [inline]
29 root_remove_peer_lists+0x24f/0x4b0 drivers/net/wireguard/allowedips.c:65
30 wg_allowedips_free+0x232/0x390 drivers/net/wireguard/allowedips.c:300
31 wg_peer_remove_all+0xd5/0x620 drivers/net/wireguard/peer.c:187
32 wg_set_device+0xd01/0x1350 drivers/net/wireguard/netlink.c:542
33 genl_family_rcv_msg_doit net/netlink/genetlink.c:672 [inline]
34 genl_family_rcv_msg net/netlink/genetlink.c:717 [inline]
35 genl_rcv_msg+0x67d/0xea0 net/netlink/genetlink.c:734
36 netlink_rcv_skb+0x177/0x450 net/netlink/af_netlink.c:2477
37 genl_rcv+0x29/0x40 net/netlink/genetlink.c:745
38 netlink_unicast_kernel net/netlink/af_netlink.c:1302 [inline]
39 netlink_unicast+0x59e/0x7e0 net/netlink/af_netlink.c:1328
40 netlink_sendmsg+0x91c/0xea0 net/netlink/af_netlink.c:1917
41 sock_sendmsg_nosec net/socket.c:652 [inline]
42 sock_sendmsg+0xd7/0x130 net/socket.c:672
43 ____sys_sendmsg+0x753/0x880 net/socket.c:2343
44 ___sys_sendmsg+0x100/0x170 net/socket.c:2397
45 __sys_sendmsg+0x105/0x1d0 net/socket.c:2430
46 __do_sys_sendmsg net/socket.c:2439 [inline]
47 __se_sys_sendmsg net/socket.c:2437 [inline]
48 __x64_sys_sendmsg+0x78/0xb0 net/socket.c:2437
49 do_syscall_64+0xfa/0x790 arch/x86/entry/common.c:294
50 entry_SYSCALL_64_after_hwframe+0x49/0xbe
51 RIP: 0033:0x45b399
52 Code: ad b6 fb ff c3 66 2e 0f 1f 84 00 00 00 00 00 66 90 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 0f 83 7b b6 fb ff c3 66 2e 0f 1f 84 00 00 00 00
53 RSP: 002b:00007f99a9bcdc78 EFLAGS: 00000246 ORIG_RAX: 000000000000002e
54 RAX: ffffffffffffffda RBX: 00007f99a9bce6d4 RCX: 000000000045b399
55 RDX: 0000000000000000 RSI: 0000000020001340 RDI: 0000000000000003
56 RBP: 000000000075bf20 R08: 0000000000000000 R09: 0000000000000000
57 R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000004
58 R13: 00000000000009ba R14: 00000000004cb2b8 R15: 0000000000000009
59
60 Allocated by task 30103:
61 save_stack+0x23/0x90 mm/kasan/common.c:72
62 set_track mm/kasan/common.c:80 [inline]
63 __kasan_kmalloc mm/kasan/common.c:513 [inline]
64 __kasan_kmalloc.constprop.0+0xcf/0xe0 mm/kasan/common.c:486
65 kasan_kmalloc+0x9/0x10 mm/kasan/common.c:527
66 kmem_cache_alloc_trace+0x158/0x790 mm/slab.c:3551
67 kmalloc include/linux/slab.h:556 [inline]
68 kzalloc include/linux/slab.h:670 [inline]
69 add+0x70a/0x1970 drivers/net/wireguard/allowedips.c:236
70 wg_allowedips_insert_v4+0xf6/0x160 drivers/net/wireguard/allowedips.c:320
71 set_allowedip drivers/net/wireguard/netlink.c:343 [inline]
72 set_peer+0xfb9/0x1150 drivers/net/wireguard/netlink.c:468
73 wg_set_device+0xbd4/0x1350 drivers/net/wireguard/netlink.c:591
74 genl_family_rcv_msg_doit net/netlink/genetlink.c:672 [inline]
75 genl_family_rcv_msg net/netlink/genetlink.c:717 [inline]
76 genl_rcv_msg+0x67d/0xea0 net/netlink/genetlink.c:734
77 netlink_rcv_skb+0x177/0x450 net/netlink/af_netlink.c:2477
78 genl_rcv+0x29/0x40 net/netlink/genetlink.c:745
79 netlink_unicast_kernel net/netlink/af_netlink.c:1302 [inline]
80 netlink_unicast+0x59e/0x7e0 net/netlink/af_netlink.c:1328
81 netlink_sendmsg+0x91c/0xea0 net/netlink/af_netlink.c:1917
82 sock_sendmsg_nosec net/socket.c:652 [inline]
83 sock_sendmsg+0xd7/0x130 net/socket.c:672
84 ____sys_sendmsg+0x753/0x880 net/socket.c:2343
85 ___sys_sendmsg+0x100/0x170 net/socket.c:2397
86 __sys_sendmsg+0x105/0x1d0 net/socket.c:2430
87 __do_sys_sendmsg net/socket.c:2439 [inline]
88 __se_sys_sendmsg net/socket.c:2437 [inline]
89 __x64_sys_sendmsg+0x78/0xb0 net/socket.c:2437
90 do_syscall_64+0xfa/0x790 arch/x86/entry/common.c:294
91 entry_SYSCALL_64_after_hwframe+0x49/0xbe
92
93 Freed by task 30103:
94 save_stack+0x23/0x90 mm/kasan/common.c:72
95 set_track mm/kasan/common.c:80 [inline]
96 kasan_set_free_info mm/kasan/common.c:335 [inline]
97 __kasan_slab_free+0x102/0x150 mm/kasan/common.c:474
98 kasan_slab_free+0xe/0x10 mm/kasan/common.c:483
99 __cache_free mm/slab.c:3426 [inline]
100 kfree+0x10a/0x2c0 mm/slab.c:3757
101 add+0x12d2/0x1970 drivers/net/wireguard/allowedips.c:266
102 wg_allowedips_insert_v4+0xf6/0x160 drivers/net/wireguard/allowedips.c:320
103 set_allowedip drivers/net/wireguard/netlink.c:343 [inline]
104 set_peer+0xfb9/0x1150 drivers/net/wireguard/netlink.c:468
105 wg_set_device+0xbd4/0x1350 drivers/net/wireguard/netlink.c:591
106 genl_family_rcv_msg_doit net/netlink/genetlink.c:672 [inline]
107 genl_family_rcv_msg net/netlink/genetlink.c:717 [inline]
108 genl_rcv_msg+0x67d/0xea0 net/netlink/genetlink.c:734
109 netlink_rcv_skb+0x177/0x450 net/netlink/af_netlink.c:2477
110 genl_rcv+0x29/0x40 net/netlink/genetlink.c:745
111 netlink_unicast_kernel net/netlink/af_netlink.c:1302 [inline]
112 netlink_unicast+0x59e/0x7e0 net/netlink/af_netlink.c:1328
113 netlink_sendmsg+0x91c/0xea0 net/netlink/af_netlink.c:1917
114 sock_sendmsg_nosec net/socket.c:652 [inline]
115 sock_sendmsg+0xd7/0x130 net/socket.c:672
116 ____sys_sendmsg+0x753/0x880 net/socket.c:2343
117 ___sys_sendmsg+0x100/0x170 net/socket.c:2397
118 __sys_sendmsg+0x105/0x1d0 net/socket.c:2430
119 __do_sys_sendmsg net/socket.c:2439 [inline]
120 __se_sys_sendmsg net/socket.c:2437 [inline]
121 __x64_sys_sendmsg+0x78/0xb0 net/socket.c:2437
122 do_syscall_64+0xfa/0x790 arch/x86/entry/common.c:294
123 entry_SYSCALL_64_after_hwframe+0x49/0xbe
124
125 The buggy address belongs to the object at ffff88809881a500
126 which belongs to the cache kmalloc-64 of size 64
127 The buggy address is located 56 bytes inside of
128 64-byte region [ffff88809881a500, ffff88809881a540)
129 The buggy address belongs to the page:
130 page:ffffea0002620680 refcount:1 mapcount:0 mapping:ffff8880aa400380 index:0x0
131 raw: 00fffe0000000200 ffffea000250b748 ffffea000254bac8 ffff8880aa400380
132 raw: 0000000000000000 ffff88809881a000 0000000100000020 0000000000000000
133 page dumped because: kasan: bad access detected
134
135 Memory state around the buggy address:
136 ffff88809881a400: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
137 ffff88809881a480: 00 00 00 00 00 fc fc fc fc fc fc fc fc fc fc fc
138 >ffff88809881a500: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
139 ^
140 ffff88809881a580: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
141 ffff88809881a600: 00 00 00 00 00 00 fc fc fc fc fc fc fc fc fc fc
142
143 Fixes: e7096c131e51 ("net: WireGuard secure network tunnel")
144 Signed-off-by: Eric Dumazet <edumazet@google.com>
145 Reported-by: syzbot <syzkaller@googlegroups.com>
146 Cc: Jason A. Donenfeld <Jason@zx2c4.com>
147 Cc: wireguard@lists.zx2c4.com
148 Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
149 Signed-off-by: David S. Miller <davem@davemloft.net>
150 Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
151 ---
152 drivers/net/wireguard/allowedips.c | 1 +
153 1 file changed, 1 insertion(+)
154
155 --- a/drivers/net/wireguard/allowedips.c
156 +++ b/drivers/net/wireguard/allowedips.c
157 @@ -263,6 +263,7 @@ static int add(struct allowedips_node __
158 } else {
159 node = kzalloc(sizeof(*node), GFP_KERNEL);
160 if (unlikely(!node)) {
161 + list_del(&newnode->peer_list);
162 kfree(newnode);
163 return -ENOMEM;
164 }