From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists.zx2c4.com (lists.zx2c4.com [165.227.139.114]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 59B2C606A6BB for ; Fri, 27 Nov 2026 10:22:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=O0g+j49d Received: by lists.zx2c4.com (OpenSMTPD) with ESMTP id fb3eec4b; Fri, 27 Nov 2026 10:22:52 +0000 (UTC) Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [2a00:1450:4864:20::52b]) by lists.zx2c4.com (OpenSMTPD) with ESMTPS id 0328c5a5 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Fri, 27 Nov 2026 10:22:47 +0000 (UTC) Received: by mail-ed1-x52b.google.com with SMTP id 4fb4d7f45d1cf-6b587538a37so11952949a12.2 for ; Fri, 27 Nov 2026 02:22:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795774967; x=1796379767; darn=lists.zx2c4.com; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=I8jZKLvyVFU/3fMfQszx3S+AK4kpCDpBdmM0K1CgiX8=; b=O0g+j49duAWWiYhC/V9384FUVtCbAwCc9Mo6no9QU3NjKC32S/bIJDq8+/1pHXsDsZ NoPpx3z6fqiWbteNe5zhBGKrhX14b6FUTstE1RIM+Y8D+AkMUF9O6jObJEOLMzSy+X0c 9YZHXaWTNgrWTHVijH4aZ/9gRNBE1YTfxLlWiHzmYghcq9/+oN6CfdiSu6M4kY97WcGs soZUg5GUVYuplzpRudi9NQEJMz3b68SqviK5uSqtGiC/uv/otUqd5IWaxt5S1mzoEmE2 mdumKmcqHQTPDxcJv2crsfDfq0Q+f+Brhq0ljPCCO3xVqKvEfURygFIjf//ApKn6ubF8 J3dw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795774967; x=1796379767; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=I8jZKLvyVFU/3fMfQszx3S+AK4kpCDpBdmM0K1CgiX8=; b=gEa1gAX7+vGyJHFpiYzUFJvDhxolRL+cWB3K9I+pF5Ce9SRs+evuTc7CHzh7bMKNeT 61IFnckMmSK+xFTZAUfj5NFcAXNobMWz8vjkmhzNgW9gX9qnTaYSptbjju1CwTtlmvHr HlJmkMmuuXSNCiIXnsI/q03gYsiXjd7Peq9NjHVLQCCEi2s7dI0YgiqshVCnipMoBPHC 5pvh2TZITXY7k4XJjcUjcu9H4ruGi03PjSWOAnE/essX8lMfGk0Usqmcipxp5VRp9HRB 8Zz0+fAz2qyfughxUhDAO5HtrNwRNASUDWv8owpwNT02xrSWDtppIzSu+wzZXTp71xu5 JPKg== X-Gm-Message-State: APuU4Z/qk0/ymVC+MCPtr5LOXE1449XQ87r/UjwGWh6+cPOdiCc+LHhr b8GxUWumrjkOXPGnf1K6M2T1cUuF5ZurzayEVvwUpFyKiU1OSBTOG6f2 X-Gm-Gg: AZFdTZchLMyu/UJXcFK6jPNKmw8d6RS9SEuZ1MWcAEuThqIojvlVGxpIuSJjT5GJNkF BkepSe8EpWY+6xTQ9XYfWt35NaPj2rCOv5uJ/wqRmJSs4OKhwth6r4hJFpphv86RFdLEqYuffYY kb5xQ+vgAKzjAvFxP3NmpJdSLO68kajwcMixqquQkBKmV5dLJ6Hvk6D3eYYHH2gxj57wkDORK9u i9lY4wTJkOX7djePBGRwxBzQ+juw2b5WFNqAhIVfuCYgoCyMBYPY9rtixYBitv9OcKm5QAVFb+l sy6wfvTDpA9W7TPzpWC1mTrqaYRxTSJ8OXDrYE6utdcUjekbR0juoEg+wQlVifuC8cYwzPd64+P s/GPOYuGXYZb2bAGwLJ5qFtcWMnvGx/3LLiMhGaek7o874cVR1j1h1I+TqjPDXxYUjga8kY28UB ZbYWCnJExrkYZTHU6z+gD/ssEuCp7GtB5c3RJODzfvCxLBH+GU/rO1JfQuFtoHdu0Mzja25e6+i NMbz2Jwrq/I3Ji2lCIoe+G6duhVyfnpRC6N+2a8EWu0PekpoMyy0TY85NycUiUn8mIRRKyujTR/ hPFpuRgy/PCUR0h262cQ7D9rm234wmo8NJwQIxqP2+vMR/UW X-Received: by 2002:a05:6402:1d4c:b0:6b9:c43d:893 with SMTP id 4fb4d7f45d1cf-6ba37ee46b3mr18117759a12.35.1795774966892; Fri, 27 Nov 2026 02:22:46 -0800 (PST) Received: from Air-pasichndev ([188.163.8.107]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-6ba370cbb80sm10886407a12.9.2026.11.27.02.22.46 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 27 Nov 2026 02:22:46 -0800 (PST) From: Andrii Pasichnyk To: "Jason A . Donenfeld" Cc: wireguard@lists.zx2c4.com, netdev@vger.kernel.org, Andrew Lunn , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Shuah Khan , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net v3 1/2] wireguard: peer: free packets left on the receive queue on removal Date: Fri, 27 Nov 2026 12:22:42 +0200 Message-ID: <20261127102243.49405-2-apasichnik9@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261127102243.49405-1-apasichnik9@gmail.com> References: <20261127102243.49405-1-apasichnik9@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: wireguard@lists.zx2c4.com X-Mailman-Version: 2.1.30rc1 Precedence: list List-Id: Development discussion of WireGuard List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "WireGuard" X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 peer_remove_after_dead() flushes the crypt workqueue and then disables the peer's NAPI. Once a disable is pending, __napi_poll() completes the instance after the poll returns even if it used its whole budget, so a peer removed while more than one budget of decrypted packets waits on rx_queue keeps the rest there. Each entry holds a keypair and a peer reference, so the peer is never released; the WARN_ON in rcu_release() that checks for leftovers is never reached either. Free what is left on rx_queue once the NAPI is gone and nothing reads the queue any more. tx_queue needs no such step: transmit_packet_work drains it without a budget, and its entries keep the peer alive until then. Hit on a single-CPU MIPS router (Realtek RTL8196E) in everyday use as a WireGuard gateway, then reproduced with the WireGuard selftest VM (x86 KVM): remove a peer while a UDP flood keeps its receive queue busy, re-add it, bring the session back up, repeat, then run the selftest's created/destroyed object check. With 1 vCPU the unpatched kernel leaks a peer and its keypair in 25-40% of such removals ("wg0: Peer 11: merely created"); with this patch, 0 in 210. The next patch adds the loop to netns.sh. Fixes: e7096c131e51 ("net: WireGuard secure network tunnel") Signed-off-by: Andrii Pasichnyk --- drivers/net/wireguard/peer.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/drivers/net/wireguard/peer.c b/drivers/net/wireguard/peer.c index 1cb502a93..6bafe5724 100644 --- a/drivers/net/wireguard/peer.c +++ b/drivers/net/wireguard/peer.c @@ -91,6 +91,19 @@ static void peer_make_dead(struct wg_peer *peer) /* The caller must now synchronize_net() for this to take effect. */ } +/* Each receive queue entry holds a keypair and a peer reference. */ +static void peer_purge_rx_queue(struct wg_peer *peer) +{ + struct sk_buff *skb; + + while ((skb = wg_prev_queue_peek(&peer->rx_queue)) != NULL) { + wg_prev_queue_drop_peeked(&peer->rx_queue); + wg_noise_keypair_put(PACKET_CB(skb)->keypair, false); + wg_peer_put(peer); + dev_kfree_skb(skb); + } +} + static void peer_remove_after_dead(struct wg_peer *peer) { WARN_ON(!peer->is_dead); @@ -108,8 +121,9 @@ static void peer_remove_after_dead(struct wg_peer *peer) /* The transition between packet encryption/decryption queues isn't * guarded by is_dead, but each reference's life is strictly bounded by * two generations: once for parallel crypto and once for serial - * ingestion, so we can simply flush twice, and be sure that we no - * longer have references inside these queues. + * ingestion, so we can simply flush twice, and be sure that no crypto + * worker still holds a packet of this peer. The per-peer queues may + * still hold packets after that; see below. */ /* a) For encrypt/decrypt. */ @@ -122,6 +136,13 @@ static void peer_remove_after_dead(struct wg_peer *peer) * here from process context. */ netif_napi_del(&peer->napi); + /* A NAPI that is being disabled completes after the poll in progress, + * even if that poll used its whole budget, so rx_queue may still hold + * packets, and nothing reads it any more. tx_queue is left alone: a + * transmit_packet_work queued during the second flush may still be + * draining it, and its entries keep the peer alive until it has. + */ + peer_purge_rx_queue(peer); /* Ensure any workstructs we own (like transmit_handshake_work or * clear_peer_work) no longer are in use. -- 2.43.0