Skip to contentSkip to conversation

ovpn: hold peer before scheduling keepalive work

PATCH Applied Shuvam Pandey 3 participants 3 messages May 23 – May 28

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ovpn: hold peer before scheduling keepalive work

ovpn_peer_keepalive_send() passes its peer reference to ovpn_xmit_special(), which ultimately drops it. The keepalive scheduler currently queues the work first and takes the reference only after schedule_work() reports that the work was queued.

Once schedule_work() queues the item, another CPU may run the worker before the caller gets to ovpn_peer_hold(). In that case the worker can consume a reference that was not acquired for it, corrupting the peer lifetime accounting.

Take the peer reference before queueing the work and drop it again when the work was already pending.

Fixes 3ecfd9349f40 ("ovpn: implement keepalive mechanism")
---
 drivers/net/ovpn/peer.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/ovpn/peer.c +4 -2
index a09d61296..4e6cd2b69 100644
@@ -1285,8 +1285,10 @@ static time64_t ovpn_peer_keepalive_work_single(struct ovpn_peer *peer,
 		netdev_dbg(peer->ovpn->dev,
 			   "sending keepalive to peer %u\n",
 			   peer->id);
-		if (schedule_work(&peer->keepalive_work))
-			ovpn_peer_hold(peer);
+		if (WARN_ON(!ovpn_peer_hold(peer)))
+			return 0;
+		if (!schedule_work(&peer->keepalive_work))
+			ovpn_peer_put(peer);
 	}
 
 	if (next_run1 < next_run2)
Signature · 1 line
2.50.1
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · Shuvam Pandey

ovpn_peer_keepalive_send() passes its peer reference to ovpn_xmit_special(), which ultimately drops it. The keepalive scheduler currently queues the work first and takes the reference only after schedule_work() reports that the work was queued.

Once schedule_work() queues the item, another CPU may run the worker before the caller gets to ovpn_peer_hold(). In that case the worker can consume a reference that was not acquired for it, corrupting the peer lifetime accounting.

Take the peer reference before queueing the work and drop it again when the work was already pending.

Quoted 6 lines
Fixes 3ecfd9349f40 ("ovpn: implement keepalive mechanism")
---
 drivers/net/ovpn/peer.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Signature · 1 line
Sabrina
maintainer Applied
#2 lore raw
In reply to Shuvam Pandey
Quoted 11 lines · Shuvam Pandey

ovpn_peer_keepalive_send() passes its peer reference to ovpn_xmit_special(), which ultimately drops it. The keepalive scheduler currently queues the work first and takes the reference only after schedule_work() reports that the work was queued.

Once schedule_work() queues the item, another CPU may run the worker before the caller gets to ovpn_peer_hold(). In that case the worker can consume a reference that was not acquired for it, corrupting the peer lifetime accounting.

[...]

Applied, thanks!

[1/1] ovpn: hold peer before scheduling keepalive work

      commit: c0951d651254b7d7f9a15d3ef149e8d2c9ff6ab6

Best regards,

Signature · 1 line
Antonio Quartulli <[email protected]>