Skip to contentSkip to conversation

ovpn: fix peer refcount leak in TCP error paths

PATCH Applied Pavitra Jha 3 participants 3 messages May 23 – May 28

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

PATCH v3 1/1 ovpn: fix peer refcount leak in TCP error paths

When either the TCP RX or TX error path calls ovpn_peer_hold() followed by schedule_work(&peer->tcp.defer_del_work), and the work item is already pending from the other path, schedule_work() returns false and the work runs only once. Since ovpn_tcp_peer_del_work() calls ovpn_peer_put() exactly once, the extra reference taken by the losing path is never dropped, leaking the peer object.

The race window:

CPU0 (strparser/RX error): CPU1 (tcp_tx_work/TX error):
ovpn_peer_hold() <- refcnt+1 ovpn_peer_hold() <- refcnt+2
schedule_work() <- queued schedule_work() <- NO-OP
(work already pending)
ovpn_tcp_peer_del_work runs:

    ovpn_peer_del()
    ovpn_peer_put()  <- refcnt+1
                                   <- peer never freed

Fix by checking the return value of schedule_work() in both paths and calling ovpn_peer_put() to drop the extra reference if the work was already pending. ovpn_peer_hold() is kept unconditional in the TX path as it cannot fail at that point.

Fixes a6a5e87b3ee4 ("ovpn: avoid sleep in atomic context in TCP RX error path")

---
Changes since v2:
- Include RX path fix in the diff (was missing from v2)
- Link: https://lore.kernel.org/netdev/[email protected]/

Changes since v1:
- TX path: keep ovpn_peer_hold() unconditional per Antonio Quartulli's review; only check schedule_work() return value
- Link: https://lore.kernel.org/netdev/[email protected]/

---
 drivers/net/ovpn/tcp.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/ovpn/tcp.c +4 -2
index 5499c1572..2c7d830e7 100644
@@ -151,7 +151,8 @@ static void ovpn_tcp_rcv(struct strparser *strp, struct sk_buff *skb)
 	/* take reference for deferred peer deletion. should never fail */
 	if (WARN_ON(!ovpn_peer_hold(peer)))
 		goto err_nopeer;
-	schedule_work(&peer->tcp.defer_del_work);
+	if (!schedule_work(&peer->tcp.defer_del_work))
+		ovpn_peer_put(peer);
 	dev_dstats_rx_dropped(peer->ovpn->dev);
 err_nopeer:
 	kfree_skb(skb);
@@ -283,7 +284,8 @@ static void ovpn_tcp_send_sock(struct ovpn_peer *peer, struct sock *sk)
 			 * stream therefore we abort the connection
 			 */
 			ovpn_peer_hold(peer);
-			schedule_work(&peer->tcp.defer_del_work);
+			if (!schedule_work(&peer->tcp.defer_del_work))
+				ovpn_peer_put(peer);
 
 			/* we bail out immediately and keep tx_in_progress set
 			 * to true. This way we prevent more TX attempts
Signature · 1 line
2.53.0
reviewer Reviewed-by on v3 1/1
#1 lore raw
Quoted 31 lines · Pavitra Jha

When either the TCP RX or TX error path calls ovpn_peer_hold() followed by schedule_work(&peer->tcp.defer_del_work), and the work item is already pending from the other path, schedule_work() returns false and the work runs only once. Since ovpn_tcp_peer_del_work() calls ovpn_peer_put() exactly once, the extra reference taken by the losing path is never dropped, leaking the peer object.

The race window:

CPU0 (strparser/RX error): CPU1 (tcp_tx_work/TX error):
ovpn_peer_hold() <- refcnt+1 ovpn_peer_hold() <- refcnt+2
schedule_work() <- queued schedule_work() <- NO-OP
(work already pending)
ovpn_tcp_peer_del_work runs:

    ovpn_peer_del()
    ovpn_peer_put()  <- refcnt+1
                                   <- peer never freed

Fix by checking the return value of schedule_work() in both paths and calling ovpn_peer_put() to drop the extra reference if the work was already pending. ovpn_peer_hold() is kept unconditional in the TX path as it cannot fail at that point.

Fixes a6a5e87b3ee4 ("ovpn: avoid sleep in atomic context in TCP RX error path")

---
Changes since v2:
- Include RX path fix in the diff (was missing from v2)
- Link: https://lore.kernel.org/netdev/[email protected]/

Quoted 7 lines

Changes since v1:
- TX path: keep ovpn_peer_hold() unconditional per Antonio Quartulli's review; only check schedule_work() return value
- Link: https://lore.kernel.org/netdev/[email protected]/

---
 drivers/net/ovpn/tcp.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

This looks correct to me:

Signature · 1 line
Sabrina
maintainer Applied
#2 lore raw
In reply to Pavitra Jha
Quoted 8 lines · Pavitra Jha

When either the TCP RX or TX error path calls ovpn_peer_hold() followed by schedule_work(&peer->tcp.defer_del_work), and the work item is already pending from the other path, schedule_work() returns false and the work runs only once. Since ovpn_tcp_peer_del_work() calls ovpn_peer_put() exactly once, the extra reference taken by the losing path is never dropped, leaking the peer object.

[...]

Applied, thanks!

[1/1] ovpn: fix peer refcount leak in TCP error paths

      commit: f9e5e5f464d7a7c5b71a48e06d616f57e05a66fd

Best regards,

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