Skip to contentSkip to conversation

wireguard fixes for 6.9-rc1

PATCH Applied Jason A. Donenfeld 3 participants 9 messages Mar 14, 2024 – Mar 19, 2024

Subsystems (5)
lore
Share & tools
lore

Jason A. Donenfeld

author maintainer
#0 lore raw

cover wireguard fixes for 6.9-rc1

Hey netdev,

This series has four WireGuard fixes:

1) Annotate a data race that KCSAN found by using READ_ONCE/WRITE_ONCE, which has been causing syzkaller noise.

2) Use the generic netdev tstats allocation and stats getters instead of doing this within the driver.

3) Explicitly check a flag variable instead of an empty list in the netlink code, to prevent a UaF situation when paging through GET results during a remove-all SET operation.

4) Set a flag in the RISC-V CI config so the selftests continue to boot.

Please apply these!

Thanks,
Jason

Breno Leitao (2):
wireguard: device: leverage core stats allocator
wireguard: device: remove generic .ndo_get_stats64

Jason A. Donenfeld (3):
wireguard: netlink: check for dangling peer via is_dead instead of
empty list
wireguard: netlink: access device through ctx instead of peer
wireguard: selftests: set RISCV_ISA_FALLBACK on riscv{32,64}

Nikita Zhandarovich (1):
wireguard: receive: annotate data-race around
receiving_counter.counter

 drivers/net/wireguard/device.c                        | 11 ++---------
 drivers/net/wireguard/netlink.c                       | 10 +++++-----
 drivers/net/wireguard/receive.c                       |  6 +++---
 .../selftests/wireguard/qemu/arch/riscv32.config      |  1 +
 .../selftests/wireguard/qemu/arch/riscv64.config      |  1 +
 5 files changed, 12 insertions(+), 17 deletions(-)
Signature · 1 line
2.44.0
author maintainer
#1 lore raw

PATCH 1/6 wireguard: receive: annotate data-race around receiving_counter.counter

From: Nikita Zhandarovich <[email protected]>

Syzkaller with KCSAN identified a data-race issue when accessing keypair->receiving_counter.counter. Use READ_ONCE() and WRITE_ONCE() annotations to mark the data race as intentional.

    BUG: KCSAN: data-race in wg_packet_decrypt_worker / wg_packet_rx_poll
    write to 0xffff888107765888 of 8 bytes by interrupt on cpu 0:
     counter_validate drivers/net/wireguard/receive.c:321 [inline]
     wg_packet_rx_poll+0x3ac/0xf00 drivers/net/wireguard/receive.c:461
     __napi_poll+0x60/0x3b0 net/core/dev.c:6536
     napi_poll net/core/dev.c:6605 [inline]
     net_rx_action+0x32b/0x750 net/core/dev.c:6738
     __do_softirq+0xc4/0x279 kernel/softirq.c:553
     do_softirq+0x5e/0x90 kernel/softirq.c:454
     __local_bh_enable_ip+0x64/0x70 kernel/softirq.c:381
     __raw_spin_unlock_bh include/linux/spinlock_api_smp.h:167 [inline]
     _raw_spin_unlock_bh+0x36/0x40 kernel/locking/spinlock.c:210
     spin_unlock_bh include/linux/spinlock.h:396 [inline]
     ptr_ring_consume_bh include/linux/ptr_ring.h:367 [inline]
     wg_packet_decrypt_worker+0x6c5/0x700 drivers/net/wireguard/receive.c:499
     process_one_work kernel/workqueue.c:2633 [inline]
     ...

    read to 0xffff888107765888 of 8 bytes by task 3196 on cpu 1:
     decrypt_packet drivers/net/wireguard/receive.c:252 [inline]
     wg_packet_decrypt_worker+0x220/0x700 drivers/net/wireguard/receive.c:501
     process_one_work kernel/workqueue.c:2633 [inline]
     process_scheduled_works+0x5b8/0xa30 kernel/workqueue.c:2706
     worker_thread+0x525/0x730 kernel/workqueue.c:2787
     ...
Fixes a9e90d9931f3 ("wireguard: noise: separate receive counter from send counter")
Reported-by [email protected]
---
 drivers/net/wireguard/receive.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/wireguard/receive.c +3 -3
index df275b4fccb6..eb8851113654 100644
@@ -251,7 +251,7 @@ static bool decrypt_packet(struct sk_buff *skb, struct noise_keypair *keypair)
 
 	if (unlikely(!READ_ONCE(keypair->receiving.is_valid) ||
 		  wg_birthdate_has_expired(keypair->receiving.birthdate, REJECT_AFTER_TIME) ||
-		  keypair->receiving_counter.counter >= REJECT_AFTER_MESSAGES)) {
+		  READ_ONCE(keypair->receiving_counter.counter) >= REJECT_AFTER_MESSAGES)) {
 		WRITE_ONCE(keypair->receiving.is_valid, false);
 		return false;
 	}
@@ -318,7 +318,7 @@ static bool counter_validate(struct noise_replay_counter *counter, u64 their_cou
 		for (i = 1; i <= top; ++i)
 			counter->backtrack[(i + index_current) &
 				((COUNTER_BITS_TOTAL / BITS_PER_LONG) - 1)] = 0;
-		counter->counter = their_counter;
+		WRITE_ONCE(counter->counter, their_counter);
 	}
 
 	index &= (COUNTER_BITS_TOTAL / BITS_PER_LONG) - 1;
@@ -463,7 +463,7 @@ int wg_packet_rx_poll(struct napi_struct *napi, int budget)
 			net_dbg_ratelimited("%s: Packet has invalid nonce %llu (max %llu)\n",
 					    peer->device->dev->name,
 					    PACKET_CB(skb)->nonce,
-					    keypair->receiving_counter.counter);
+					    READ_ONCE(keypair->receiving_counter.counter));
 			goto next;
 		}
 
Signature · 1 line
2.44.0
author maintainer
#2 lore raw

PATCH 2/6 wireguard: device: leverage core stats allocator

In reply to Jason A. Donenfeld

From: Breno Leitao <[email protected]>

With commit 34d21de99cea9 ("net: Move {l,t,d}stats allocation to core and convert veth & vrf"), stats allocation could be done on net core instead of in this driver.

With this new approach, the driver doesn't have to bother with error handling (allocation failure checking, making sure free happens in the right spot, etc). This is core responsibility now.

Remove the allocation in this driver and leverage the network core allocation instead.

---
 drivers/net/wireguard/device.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
drivers/net/wireguard/device.c +2 -8
index deb9636b0ecf..6aa071469e1c 100644
@@ -262,7 +262,6 @@ static void wg_destruct(struct net_device *dev)
 	rcu_barrier(); /* Wait for all the peers to be actually freed. */
 	wg_ratelimiter_uninit();
 	memzero_explicit(&wg->static_identity, sizeof(wg->static_identity));
-	free_percpu(dev->tstats);
 	kvfree(wg->index_hashtable);
 	kvfree(wg->peer_hashtable);
 	mutex_unlock(&wg->device_update_lock);
@@ -297,6 +296,7 @@ static void wg_setup(struct net_device *dev)
 	dev->hw_enc_features |= WG_NETDEV_FEATURES;
 	dev->mtu = ETH_DATA_LEN - overhead;
 	dev->max_mtu = round_down(INT_MAX, MESSAGE_PADDING_MULTIPLE) - overhead;
+	dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
 
 	SET_NETDEV_DEVTYPE(dev, &device_type);
 
@@ -331,14 +331,10 @@ static int wg_newlink(struct net *src_net, struct net_device *dev,
 	if (!wg->index_hashtable)
 		goto err_free_peer_hashtable;
 
-	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
-	if (!dev->tstats)
-		goto err_free_index_hashtable;
-
 	wg->handshake_receive_wq = alloc_workqueue("wg-kex-%s",
 			WQ_CPU_INTENSIVE | WQ_FREEZABLE, 0, dev->name);
 	if (!wg->handshake_receive_wq)
-		goto err_free_tstats;
+		goto err_free_index_hashtable;
 
 	wg->handshake_send_wq = alloc_workqueue("wg-kex-%s",
 			WQ_UNBOUND | WQ_FREEZABLE, 0, dev->name);
@@ -397,8 +393,6 @@ static int wg_newlink(struct net *src_net, struct net_device *dev,
 	destroy_workqueue(wg->handshake_send_wq);
 err_destroy_handshake_receive:
 	destroy_workqueue(wg->handshake_receive_wq);
-err_free_tstats:
-	free_percpu(dev->tstats);
 err_free_index_hashtable:
 	kvfree(wg->index_hashtable);
 err_free_peer_hashtable:
Signature · 1 line
2.44.0
author maintainer
#3 lore raw

PATCH 3/6 wireguard: device: remove generic .ndo_get_stats64

In reply to Jason A. Donenfeld

From: Breno Leitao <[email protected]>

Commit 3e2f544dd8a33 ("net: get stats64 if device if driver is configured") moved the callback to dev_get_tstats64() to net core, so, unless the driver is doing some custom stats collection, it does not need to set .ndo_get_stats64.

Since this driver is now relying in NETDEV_PCPU_STAT_TSTATS, then, it doesn't need to set the dev_get_tstats64() generic .ndo_get_stats64 function pointer.

---
 drivers/net/wireguard/device.c | 1 -
 1 file changed, 1 deletion(-)
drivers/net/wireguard/device.c +0 -1
index 6aa071469e1c..3feb36ee5bfb 100644
@@ -237,7 +237,6 @@ static const struct net_device_ops netdev_ops = {
 	.ndo_open		= wg_open,
 	.ndo_stop		= wg_stop,
 	.ndo_start_xmit		= wg_xmit,
-	.ndo_get_stats64	= dev_get_tstats64
 };
 
 static void wg_destruct(struct net_device *dev)
Signature · 1 line
2.44.0
author maintainer
#4 lore raw

PATCH 4/6 wireguard: netlink: check for dangling peer via is_dead instead of empty list

In reply to Jason A. Donenfeld

If all peers are removed via wg_peer_remove_all(), rather than setting peer_list to empty, the peer is added to a temporary list with a head on the stack of wg_peer_remove_all(). If a netlink dump is resumed and the cursored peer is one that has been removed via wg_peer_remove_all(), it will iterate from that peer and then attempt to dump freed peers.

Fix this by instead checking peer->is_dead, which was explictly created for this purpose. Also move up the device_update_lock lockdep assertion, since reading is_dead relies on that.

It can be reproduced by a small script like:

echo "Setting config..."
ip link add dev wg0 type wireguard
wg setconf wg0 /big-config
(
while true; do
echo "Showing config..."
wg showconf wg0 > /dev/null
done
) &
sleep 4
wg setconf wg0 <(printf "[Peer]\nPublicKey=$(wg genkey)\n")

Resulting in:

    BUG: KASAN: slab-use-after-free in __lock_acquire+0x182a/0x1b20
    Read of size 8 at addr ffff88811956ec70 by task wg/59
    CPU: 2 PID: 59 Comm: wg Not tainted 6.8.0-rc2-debug+ #5
    Call Trace:
     <TASK>
     dump_stack_lvl+0x47/0x70
     print_address_description.constprop.0+0x2c/0x380
     print_report+0xab/0x250
     kasan_report+0xba/0xf0
     __lock_acquire+0x182a/0x1b20
     lock_acquire+0x191/0x4b0
     down_read+0x80/0x440
     get_peer+0x140/0xcb0
     wg_get_device_dump+0x471/0x1130
Fixes e7096c131e51 ("net: WireGuard secure network tunnel")
---
 drivers/net/wireguard/netlink.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/wireguard/netlink.c +3 -3
index e220d761b1f2..c17aee454fa3 100644
@@ -255,17 +255,17 @@ static int wg_get_device_dump(struct sk_buff *skb, struct netlink_callback *cb)
 	if (!peers_nest)
 		goto out;
 	ret = 0;
-	/* If the last cursor was removed via list_del_init in peer_remove, then
+	lockdep_assert_held(&wg->device_update_lock);
+	/* If the last cursor was removed in peer_remove or peer_remove_all, then
 	 * we just treat this the same as there being no more peers left. The
 	 * reason is that seq_nr should indicate to userspace that this isn't a
 	 * coherent dump anyway, so they'll try again.
 	 */
 	if (list_empty(&wg->peer_list) ||
-	    (ctx->next_peer && list_empty(&ctx->next_peer->peer_list))) {
+	    (ctx->next_peer && ctx->next_peer->is_dead)) {
 		nla_nest_cancel(skb, peers_nest);
 		goto out;
 	}
-	lockdep_assert_held(&wg->device_update_lock);
 	peer = list_prepare_entry(ctx->next_peer, &wg->peer_list, peer_list);
 	list_for_each_entry_continue(peer, &wg->peer_list, peer_list) {
 		if (get_peer(peer, skb, ctx)) {
Signature · 1 line
2.44.0
author maintainer
#5 lore raw

PATCH 5/6 wireguard: netlink: access device through ctx instead of peer

In reply to Jason A. Donenfeld

The previous commit fixed a bug that led to a NULL peer->device being dereferenced. It's actually easier and faster performance-wise to instead get the device from ctx->wg. This semantically makes more sense too, since ctx->wg->peer_allowedips.seq is compared with ctx->allowedips_seq, basing them both in ctx. This also acts as a defence in depth provision against freed peers.

Fixes e7096c131e51 ("net: WireGuard secure network tunnel")
---
 drivers/net/wireguard/netlink.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/wireguard/netlink.c +2 -2
index c17aee454fa3..f7055180ba4a 100644
@@ -164,8 +164,8 @@ get_peer(struct wg_peer *peer, struct sk_buff *skb, struct dump_ctx *ctx)
 	if (!allowedips_node)
 		goto no_allowedips;
 	if (!ctx->allowedips_seq)
-		ctx->allowedips_seq = peer->device->peer_allowedips.seq;
-	else if (ctx->allowedips_seq != peer->device->peer_allowedips.seq)
+		ctx->allowedips_seq = ctx->wg->peer_allowedips.seq;
+	else if (ctx->allowedips_seq != ctx->wg->peer_allowedips.seq)
 		goto no_allowedips;
 
 	allowedips_nest = nla_nest_start(skb, WGPEER_A_ALLOWEDIPS);
Signature · 1 line
2.44.0
author maintainer
#6 lore raw

PATCH 6/6 wireguard: selftests: set RISCV_ISA_FALLBACK on riscv{32,64}

In reply to Jason A. Donenfeld

This option is needed to continue booting with QEMU. Recent changes that made this optional meant that it gets unset in the test harness, and so WireGuard CI has been broken. Fix this by simply setting this option.

Fixes 496ea826d1e1 ("RISC-V: provide Kconfig & commandline options to control parsing "riscv,isa"")
---
 tools/testing/selftests/wireguard/qemu/arch/riscv32.config | 1 +
 tools/testing/selftests/wireguard/qemu/arch/riscv64.config | 1 +
 2 files changed, 2 insertions(+)
tools/testing/selftests/wireguard/qemu/arch/riscv32.config +1 -0
index 2fc36efb166d..a7f8e8a95625 100644
@@ -3,6 +3,7 @@ CONFIG_ARCH_RV32I=y
 CONFIG_MMU=y
 CONFIG_FPU=y
 CONFIG_SOC_VIRT=y
+CONFIG_RISCV_ISA_FALLBACK=y
 CONFIG_SERIAL_8250=y
 CONFIG_SERIAL_8250_CONSOLE=y
 CONFIG_SERIAL_OF_PLATFORM=y
tools/testing/selftests/wireguard/qemu/arch/riscv64.config +1 -0
index dc266f3b1915..daeb3e5e0965 100644
@@ -2,6 +2,7 @@ CONFIG_ARCH_RV64I=y
 CONFIG_MMU=y
 CONFIG_FPU=y
 CONFIG_SOC_VIRT=y
+CONFIG_RISCV_ISA_FALLBACK=y
 CONFIG_SERIAL_8250=y
 CONFIG_SERIAL_8250_CONSOLE=y
 CONFIG_SERIAL_OF_PLATFORM=y
Signature · 1 line
2.44.0
Reviewed-by on the v1 series
#7 lore raw
In reply to Jason A. Donenfeld
Quoted 32 lines · Thu, Mar 14, 2024 at 11:49:05PM CET, [email protected] wrote:

Hey netdev,

This series has four WireGuard fixes:

1) Annotate a data race that KCSAN found by using READ_ONCE/WRITE_ONCE, which has been causing syzkaller noise.

2) Use the generic netdev tstats allocation and stats getters instead of doing this within the driver.

3) Explicitly check a flag variable instead of an empty list in the netlink code, to prevent a UaF situation when paging through GET results during a remove-all SET operation.

4) Set a flag in the RISC-V CI config so the selftests continue to boot.

Please apply these!

Thanks,
Jason

Breno Leitao (2):
wireguard: device: leverage core stats allocator
wireguard: device: remove generic .ndo_get_stats64

Jason A. Donenfeld (3):
wireguard: netlink: check for dangling peer via is_dead instead of
empty list
wireguard: netlink: access device through ctx instead of peer
wireguard: selftests: set RISCV_ISA_FALLBACK on riscv{32,64}

Quoted 3 lines

Nikita Zhandarovich (1):
wireguard: receive: annotate data-race around
receiving_counter.counter

Looks fine to me.

set-

Quoted 12 lines
drivers/net/wireguard/device.c                        | 11 ++---------
drivers/net/wireguard/netlink.c                       | 10 +++++-----
drivers/net/wireguard/receive.c                       |  6 +++---
.../selftests/wireguard/qemu/arch/riscv32.config      |  1 +
.../selftests/wireguard/qemu/arch/riscv64.config      |  1 +
5 files changed, 12 insertions(+), 17 deletions(-)
Signature · 1 line
2.44.0
#8 lore raw
In reply to Jason A. Donenfeld
This series was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:

Quoted 8 lines · On Thu, 14 Mar 2024 16:49:05 -0600 you wrote:

Hey netdev,

This series has four WireGuard fixes:

1) Annotate a data race that KCSAN found by using READ_ONCE/WRITE_ONCE, which has been causing syzkaller noise.

[...]

Here is the summary with links:
- [net,1/6] wireguard: receive: annotate data-race around receiving_counter.counter

    https://git.kernel.org/netdev/net/c/bba045dc4d99
  - [net,2/6] wireguard: device: leverage core stats allocator
    https://git.kernel.org/netdev/net/c/db2952dfbdf1
  - [net,3/6] wireguard: device: remove generic .ndo_get_stats64
    https://git.kernel.org/netdev/net/c/df9bbb5e776a
  - [net,4/6] wireguard: netlink: check for dangling peer via is_dead instead of empty list
    https://git.kernel.org/netdev/net/c/55b6c7386738
  - [net,5/6] wireguard: netlink: access device through ctx instead of peer
    https://git.kernel.org/netdev/net/c/71cbd32e3db8
  - [net,6/6] wireguard: selftests: set RISCV_ISA_FALLBACK on riscv{32,64}
    https://git.kernel.org/netdev/net/c/e995f5dd9a9c

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html