From patchwork Thu Mar 14 22:49:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jason A. Donenfeld" X-Patchwork-Id: 13592790 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 399A81094E for ; Thu, 14 Mar 2024 22:49:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710456581; cv=none; b=pKUHF+rUzAUYdGDSLdyzKHdsDFR5pR7SGqYOZJq6Hv8rDvvm3hHjbfCaYWpcv33RiMDxEZ+CtuZ1VXPx4VfSqRj8vjErRGGDLpl0GPyuuCAp9rKaz9mZvNiOiasxN03H2GdQxF7balmj+ng4BUxF16ltxPwZvHErM7+rKwPkgvw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710456581; c=relaxed/simple; bh=4cPSvM5uhrh9A6FezSV8Th9Mkp3LWijY0FMJCDh9o7U=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XIezy4/g0AkqCHoIkG/srjk3+Ldx0BzLHrMYCJ9EhvOMokw74DHrxtSTsVRcUe09OX8Sm3BbldVyYXrQNti/TgbH+jc/NMqIffCWoeiAcSPsf9Dh/W2lc5Dg+StdNejSGZxJtXNHEQotHcTLdYENnVzQ3xVKo1kazFHUGNy5XVA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b=go62pmEw; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="go62pmEw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 32A9DC433F1; Thu, 14 Mar 2024 22:49:40 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="go62pmEw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1710456579; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nvW9jCszkqb/TYUY2+QAYTE3N92e1fnoYLVzNL5XRoY=; b=go62pmEwqg3KWfJ9l75acEaDfx+P3ofYB5QZAVN7ArQsb+misE8NwGuJ86Hq6QhmMZ0I0t E3bKE6WiWvqJ6oy0OpsvfXwZqyoUwB51nOFsUCyaxCP3oQ4xxkoo5xkyL4NeyuYaqP0rGX ZKkvwVfWAwjCGu/lClyBIhEZCGofBRE= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 36c4cb08 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 14 Mar 2024 22:49:39 +0000 (UTC) From: "Jason A. Donenfeld" To: netdev@vger.kernel.org, davem@davemloft.net, kuba@kernel.org Subject: [PATCH net 4/6] wireguard: netlink: check for dangling peer via is_dead instead of empty list Date: Thu, 14 Mar 2024 16:49:09 -0600 Message-ID: <20240314224911.6653-5-Jason@zx2c4.com> In-Reply-To: <20240314224911.6653-1-Jason@zx2c4.com> References: <20240314224911.6653-1-Jason@zx2c4.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org 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: 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 Cc: stable@vger.kernel.org Fixes: e7096c131e51 ("net: WireGuard secure network tunnel") Reported-by: Lillian Berry Signed-off-by: Jason A. Donenfeld --- drivers/net/wireguard/netlink.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/net/wireguard/netlink.c b/drivers/net/wireguard/netlink.c index e220d761b1f2..c17aee454fa3 100644 --- a/drivers/net/wireguard/netlink.c +++ b/drivers/net/wireguard/netlink.c @@ -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)) {