From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0BE8723A984 for ; Tue, 12 May 2026 04:20:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778559650; cv=none; b=uU0er73dwaMLXhDD+EWl26QBsurOmPMlr3JJ4O2Zj/LHEvqiepvYY5+dwON3mWDTQsuKOKZh6WB/C9+Sv0ybMBDZiPCED2dKVDaM2CS1sXX2m1Uk6WL7YwHzw/7cVPr6Y1r/JINjnrYfC1n6zy/QTKBCCKgfWgWzKCT+rYnrSrQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778559650; c=relaxed/simple; bh=eaO/GAO2q9KnLpudsDf6ByiH6uufTvW/8bDXbw/Lot4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bOIl6pHJhEZA39RCi1unMjwS9sZRlZeKkZjW/RhyIE3XbuLapj57vBsPUB1OlrTw6sQJzrBt2jRyUVvspDkFCRc8H/nBUVoNbmwL00QRWFqPi0BQ2ZfRo0+RV4cdbqiZ2JWgAb5SJy6YMKAwpMx0/1Qn2TGjg1vitnICGzhCV9g= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jknoj79U; arc=none smtp.client-ip=209.85.128.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jknoj79U" Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-48d102471a4so49394895e9.2 for ; Mon, 11 May 2026 21:20:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778559647; x=1779164447; darn=vger.kernel.org; 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; bh=KrTxPrLZSrX9/AE26HqoZYG+0GxCFkz4Bhu3XTJcKCM=; b=jknoj79UDYd4IZAHjm5zToPiEa1hoP+cMULohiFLl6UG/+efO4dh/h3pH+KB7UYRRK MGpHuKa3cta/klU91PB/m1HEaBCCX/9y/OGoKxvzM4lOk9QJspXePGDej4VRC4vWszjB fOSLB1lV8YwZDMengKUXGUY2UbPSV0zzNdNJhlbcrZmsaYrpj0K3ITxRJIEvuQVuplmz gWNwJvvClhvO4mJfS8bo7LHBmT2aH6oyHn6KuywIf8I/xQWHQJ1DKHfjaxb8pJrxZvgI P0l/j6xTYnyJ+uc7ejepen/aiHLzqn/p4qulXMmv/ALEvg8fhlks6Uo9mqnn3356KCaa g8tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778559647; x=1779164447; 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; bh=KrTxPrLZSrX9/AE26HqoZYG+0GxCFkz4Bhu3XTJcKCM=; b=n1pH+UZx/7GFoQYUMm4Jfxy4qD56pivRnVBulDFmbd/vmPieeXFwhe+ajrU4GftxSI 8r37muIwuu10ydKxwFrIiv+tupMfm2Sm/lG8nv7Dy1jWwgV/VCdkODA/YYmiFiKVajN9 8UoBYTdsXahBnMeRrfLOxunkKLCkH7n0MiXp4MPiSkRgCtuTUUq4D4IbyOtAWePaeCmY aBPvbWiwT1KqB8n7aDgpWH7/HeIVE1z6Dotd4m+Q3YiKkXS48V0brpnDv1UPOUk/PYH2 tEi3RgRjGQhOxlLWAhhU+pgrPkrk1e5oaweJsNX5aEzRKy8p4NCzyL0hWHOUxdgAuJom MTaA== X-Forwarded-Encrypted: i=1; AFNElJ+cW6Xq8shVmnyvSqvDOUE7tp/L6Vz9J8OzSWzAbamiuqLVHUvGqLYe6C8vf5iFKQpfHAE6YNjwF94mf2Y=@vger.kernel.org X-Gm-Message-State: AOJu0Yy+Xjh8IElRiWBuYV8fNPaKhYXBqmaRFZD5QbAkxGU7bQzxNtOX LaRdv+fd14s5Y0LDk3t2Zo+wkJTf6zMRPoVQuO2vKQOWupSyJSoPYbnS X-Gm-Gg: Acq92OF7du3XYRdWp4u7Wtj95cijimW4/fCsZhP/nyrAMaFfoqcHXakQ6x4v8TZn4tp 9CYHhb8MDo8C2hSk5YjJ18jZH1O4jrTdjo2xAqYEHYUcg0zCecg+64PuzowIqs2T/3LStToS+nq N2tOf/Hho7iejDwBuEvqguBf05VzqDRJ4XUI0nbU63oi3Tv1rZr6h4t15uzqn5QsvOyNwAubx4p LHny91rlAha0GkYVk5UEkdd7sJYw48L+ijgwSNV0wrI9eD6pSVsItqVMdvoQyHmXMgGSF6Uzla6 B5GpN4eCZxh7VeTAViD/6vy4xJ4s+x2sCyYCO4Y9tOzjRdinV6pt8Sb4J0QXWrIa0zHNbBZissZ 4uL5+NA0dk41IijcdbxNjcRyXZry+0Asu/FGoGOAwm/0p+2fCEBDMcsxu4JmON/KXHBGGtAtB72 xFu6rB33rSQb3Dzr9h2s4papLbr9Byf9g27FDi9yCQUfaewYYFlgXXHOYazpbT85xm+D8P0kTyg N3D8Pkluk4= X-Received: by 2002:a05:600c:524e:b0:489:1d74:56d with SMTP id 5b1f17b1804b1-48e676c14b9mr262868345e9.29.1778559647181; Mon, 11 May 2026 21:20:47 -0700 (PDT) Received: from dohko.chello.ie (188-141-5-72.dynamic.upc.ie. [188.141.5.72]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48e908d2c77sm16802215e9.12.2026.05.11.21.20.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 May 2026 21:20:46 -0700 (PDT) From: David Carlier To: netdev@vger.kernel.org Cc: David Carlier , Antonio Quartulli , Sabrina Dubroca , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , linux-kernel@vger.kernel.org Subject: [PATCH net 2/2] ovpn: respect peer refcount in CMD_NEW_PEER error path Date: Tue, 12 May 2026 05:19:13 +0100 Message-ID: <20260512042036.19870-3-devnexen@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260512042036.19870-1-devnexen@gmail.com> References: <20260512042036.19870-1-devnexen@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit ovpn_nl_peer_new_doit()'s error path calls ovpn_peer_release() directly rather than ovpn_peer_put(), bypassing the kref. The accompanying comment ("peer was not yet hashed, thus it is not used in any context") holds for UDP but not for TCP. For UDP, the ovpn_socket union uses the .ovpn arm and never points back at a peer; UDP encap_recv looks up peers via the not-yet-populated hashtables, so the new peer is unreachable until ovpn_peer_add() publishes it. For TCP, ovpn_socket_new() sets ovpn_sock->peer and ovpn_tcp_socket_attach() publishes ovpn_sock via rcu_assign_sk_user_data(). >From that moment until ovpn_socket_release() detaches in the error path, the TCP fd is fully wired: userspace recvmsg / sendmsg / close / poll on the fd, as well as the strparser-driven ovpn_tcp_rcv() path, can reach the peer through sk_user_data -> ovpn_sock->peer and bump its refcount via ovpn_peer_hold(). ovpn_tcp_socket_wait_finish() (called inside ovpn_socket_release()) drains strparser and the tx work, but does not synchronize with userspace syscall callers that already hold a peer reference. If ovpn_nl_peer_modify() or ovpn_peer_add() returns an error while such a caller is in flight - notably an ovpn_tcp_recvmsg() blocked in __skb_recv_datagram() on peer->tcp.user_queue - the direct ovpn_peer_release() destroys the peer while the caller still holds the reference, and the eventual ovpn_peer_put() from that caller operates on freed memory. Replace the direct destructor call with ovpn_peer_put() so the kref correctly defers destruction until the last reference is dropped. In the common case where no concurrent user is present, behaviour is unchanged: the kref hits zero immediately and ovpn_peer_release_kref() runs the same destructor. Fixes: 11851cbd60ea ("ovpn: implement TCP transport") Assisted-by: Claude:claude-opus-4-7 Signed-off-by: David Carlier --- drivers/net/ovpn/netlink.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/net/ovpn/netlink.c b/drivers/net/ovpn/netlink.c index 291e2e5bb450..4c66c1ec497e 100644 --- a/drivers/net/ovpn/netlink.c +++ b/drivers/net/ovpn/netlink.c @@ -462,10 +462,12 @@ int ovpn_nl_peer_new_doit(struct sk_buff *skb, struct genl_info *info) sock_release: ovpn_socket_release(peer); peer_release: - /* release right away because peer was not yet hashed, thus it is not - * used in any context + /* For UDP, the peer is unreachable until added to the hashtables, so + * dropping the initial reference is enough. For TCP, the peer may be + * concurrently reachable via sk_user_data->peer until + * ovpn_socket_release() detaches; rely on the refcount. */ - ovpn_peer_release(peer); + ovpn_peer_put(peer); return ret; } -- 2.53.0