[Openvpn-devel,net,2/3] ovpn: release netdev reference from the peer RCU callback

Message ID 79158eb33d6442ca84b03593596407cee8aaec0c.1795703343.git.ralf@mandelbit.com
State New
Headers
Series ovpn: fix issues reported by Sashiko |

Commit Message

Ralf Lici Nov. 26, 2026, 2:45 p.m. UTC
  ovpn_peer_release queues ovpn_peer_release_rcu, which frees the peer, and
only then calls netdev_put, which still dereferences peer->ovpn and
peer->dev_tracker.

When the last reference is dropped from preemptible process context,
for instance in a netlink handler, the task may be preempted right after
call_rcu. If a grace period elapses before it runs again, netdev_put
operates on freed memory, and with CONFIG_NET_DEV_REFCNT_TRACKER
ref_tracker_free also writes through the stale tracker pointer.

Drop the netdev reference from the RCU callback instead, right before
freeing the peer, so that the peer is not accessed once it has been
queued for freeing. This also keeps the netdev, and therefore the
module, pinned until the callback has run, as ovpn_cleanup expects when
it waits for it with rcu_barrier. Simply calling netdev_put before
call_rcu would let unregistration and module exit complete before the
callback is even queued.

Fixes: 80747caef33d ("ovpn: introduce the ovpn_peer object")
Link: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260809212142.2249027-1-antonio%40openvpn.net?part=2
Signed-off-by: Ralf Lici <ralf@mandelbit.com>
---
 drivers/net/ovpn/peer.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
  

Patch

diff --git a/drivers/net/ovpn/peer.c b/drivers/net/ovpn/peer.c
index 07f3fd9cbed6..7c3c33a6068b 100644
--- a/drivers/net/ovpn/peer.c
+++ b/drivers/net/ovpn/peer.c
@@ -352,6 +352,10 @@  static void ovpn_peer_release_rcu(struct rcu_head *head)
 	 * perform it in the RCU callback, when all contexts are done
 	 */
 	dst_cache_destroy(&peer->dst_cache);
+	/* drop the netdev reference only now, so that unregistration cannot
+	 * complete before this callback has run
+	 */
+	netdev_put(peer->ovpn->dev, &peer->dev_tracker);
 	kfree(peer);
 }
 
@@ -366,7 +370,6 @@  static void ovpn_peer_release(struct ovpn_peer *peer)
 	ovpn_bind_reset(peer, NULL);
 	spin_unlock_bh(&peer->lock);
 	call_rcu(&peer->rcu, ovpn_peer_release_rcu);
-	netdev_put(peer->ovpn->dev, &peer->dev_tracker);
 }
 
 /**